001/**
002 *  '$Author: welker $'
003 *  '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $'
004 *  '$Revision: 24234 $'
005 *
006 *  For Details:
007 *  http://www.kepler-project.org
008 *
009 *  Copyright (c) 2009-2010 The Regents of the
010 *  University of California. All rights reserved. Permission is hereby granted,
011 *  without written agreement and without license or royalty fees, to use, copy,
012 *  modify, and distribute this software and its documentation for any purpose,
013 *  provided that the above copyright notice and the following two paragraphs
014 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
015 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
016 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
017 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
018 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
019 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
020 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
021 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 *  ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package org.kepler.workflowrunmanager.gui;
027
028import java.util.ArrayList;
029import java.util.Iterator;
030import java.util.Vector;
031
032import javax.swing.table.DefaultTableColumnModel;
033import javax.swing.table.TableColumn;
034
035/**
036 * ColumnGroup, GroupableTableColumnModel, GroupableTableHeader,
037 * GroupableTableHeaderUI and GroupableTableCellRenderer taken from a post by
038 * Steve Webb (16/09/04). He extended code originally posted by Nobuo Tamemasa. Many thanks
039 * to both authors. I've made some changes, including making the headers editable.
040 * 
041 */
042
043/**
044 * Class which extends the functionality of DefaultColumnTableModel to also
045 * provide capabilities to group columns. This can be used for instance to aid
046 * in the layout of groupable table headers.
047 */
048public class GroupableTableColumnModel extends DefaultTableColumnModel {
049
050        protected ArrayList<ColumnGroup> allColumnGroups = new ArrayList<ColumnGroup>();
051
052        /**
053         * Hold the list of ColumnGroups which define what group each normal column
054         * is within, if any.
055         */
056        protected ArrayList<ColumnGroup> columnGroups = new ArrayList<ColumnGroup>();
057
058        public void addColumnGroup(ColumnGroup columnGroup) {
059                if (!allColumnGroups.contains(columnGroup)) {
060                        allColumnGroups.add(columnGroup);
061                }
062                if (!columnGroups.contains(columnGroup)) {
063                        columnGroups.add(columnGroup);
064                }
065        }
066
067        /**
068         * Add a new columnGroup.
069         * 
070         * @param columnGroup
071         *            new ColumnGroup
072         */
073        public void addColumnGroup(int colIndex, ColumnGroup columnGroup) {
074                if (!allColumnGroups.contains(columnGroup)) {
075                        allColumnGroups.add(colIndex, columnGroup);
076                }
077                if (!columnGroups.contains(columnGroup)) {
078                        columnGroups.add(colIndex, columnGroup);
079                        addColumn(columnGroup);
080                        moveColumn(columnGroups.size() - 1, colIndex);
081                }
082        }
083
084        /**
085         * Remove a columnGroup.
086         * 
087         * @param colIndex
088         * @param columnGroup
089         */
090        public void removeColumnGroup(int colIndex, ColumnGroup columnGroup) {
091
092                columnGroups.remove(columnGroup);
093                // this.removeColumn(columnGroup);
094                // this.removeColumn(this.getColumn(colIndex));
095                super.removeColumn(this.getColumn(colIndex));
096        }
097
098        /**
099         * Provides an Iterator to iterate over the ColumnGroup list.
100         * @return columnGroups.itererator
101         */
102        public Iterator<ColumnGroup> columnGroupIterator() {
103                return columnGroups.iterator();
104        }
105
106        /**
107         * Returns a ColumnGroup specified by an index.
108         * 
109         * @param index
110         *            index of ColumnGroup
111         * @return ColumnGroup
112         */
113        public ColumnGroup getColumnGroup(int index) {
114                /*
115                 * if (index >= 0 && index < allColumnGroups.size()) { return
116                 * (ColumnGroup) allColumnGroups.get(index); }
117                 * System.out.println("getColumnGroup - couldnt find columnGroup at index:"
118                 * +index +" returning null"); return null;
119                 */
120                if (index >= 0 && index < columnGroups.size()) {
121                        return (ColumnGroup) columnGroups.get(index);
122                }
123                System.out
124                                .println("getColumnGroup - couldnt find columnGroup at index:"
125                                                + index + " returning null");
126                return null;
127        }
128
129        /**
130         * Provides an iterator for accessing the ColumnGroups associated with a
131         * column.
132         * 
133         * @param tableColumn
134         * @return ColumnGroup iterator
135         */
136        public Iterator<ColumnGroup> getColumnGroups(TableColumn tableColumn) {
137                if (allColumnGroups.isEmpty()) {
138                        return null;
139                }
140                Iterator<ColumnGroup> iter = allColumnGroups.iterator();
141                while (iter.hasNext()) {
142                        ColumnGroup cGroup = (ColumnGroup) iter.next();
143                        Vector<ColumnGroup> v_ret = (Vector<ColumnGroup>) cGroup
144                                        .getColumnGroups(tableColumn, new Vector());
145                        if (v_ret != null) {
146                                return v_ret.iterator();
147                        }
148                }
149                return null;
150        }
151
152        // FIXME this doesn't work yet.
153        public void setColumnGroupVisible(int colIndex, ColumnGroup columnGroup,
154                        boolean visible) {
155
156                if (!visible) {
157                        columnGroup.setVisible(visible);
158                        removeColumnGroup(colIndex, columnGroup);
159                        removeColumn(columnGroup);
160                        // super.removeColumn(super.getColumn(colIndex));
161                } else {
162                        columnGroup.setVisible(visible);
163                        addColumnGroup(colIndex, columnGroup);
164                        addColumn(columnGroup);
165                        moveColumn(allColumnGroups.size() - 1, colIndex);
166                }
167        }
168
169}