1   package org.wcb.gui.renderer.jtable;
2   
3   /**
4    * <small>
5    * <p/>
6    * Permission is granted to copy, distribute and/or modify this document
7    * under the terms of the GNU Free Documentation License, Version 1.2
8    * or any later version published by the Free Software Foundation;
9    * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
10   * Texts.  A copy of the license is included in the section entitled "GNU
11   * Free Documentation License".
12   * <p/>
13   * $File:  $ <br>
14   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 7, 2006 1:46:36 PM $ <br>
15   * </small>
16   */
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.Vector;
21  import javax.swing.table.DefaultTableColumnModel;
22  import javax.swing.table.TableColumn;
23  
24  // Steve Webb 16/09/04 swebb99_uk@hotmail.com
25  
26  /**
27   * http://www.swebb99.f2s.com/GroupableHeader/
28   * Class which extends the functionality of DefaultColumnTableModel to
29   * also provide capabilities to group columns. This can be used for
30   * instance to aid in the layout of groupable table headers.
31   */
32  
33  public class GroupableTableColumnModel extends DefaultTableColumnModel {
34  
35      /**
36       * Hold the list of ColumnGroups which define what group each normal
37       * column is within, if any.
38       */
39      protected ArrayList columnGroups = new ArrayList();
40  
41  
42      /**
43       * Add a new columngroup.
44       * @param columnGroup new ColumnGroup
45       */
46      public void addColumnGroup(ColumnGroup columnGroup) {
47          columnGroups.add(columnGroup);
48      }
49  
50      /**
51       * Provides an Iterator to iterate over the
52       * ColumnGroup list.
53       * @return Iterator over ColumnGroups
54       */
55      public Iterator columnGroupIterator() {
56          return columnGroups.iterator();
57      }
58  
59      /**
60       * Returns a ColumnGroup specified by an index.
61       * @param index index of ColumnGroup
62       * @return ColumnGroup
63       */
64      public ColumnGroup getColumnGroup(int index) {
65          if(index >= 0 && index < columnGroups.size()) {
66              return (ColumnGroup)columnGroups.get(index);
67          }
68          return null;
69      }
70  
71      /**
72       * Provides and iterator for accessing the ColumnGroups
73       * associated with a column.
74       * @param col Column
75       * @return ColumnGroup iterator
76       */
77      public Iterator getColumnGroups(TableColumn col) {
78          if (columnGroups.isEmpty()) return null;
79          Iterator iter = columnGroups.iterator();
80          while (iter.hasNext()) {
81              ColumnGroup cGroup = (ColumnGroup)iter.next();
82              Vector v_ret = (Vector)cGroup.getColumnGroups(col,new Vector());
83              if (v_ret != null) {
84                  return v_ret.iterator();
85              }
86          }
87          return null;
88      }
89  }
90