1   package org.wcb.gui.renderer.jtable;
2   
3   import java.awt.Component;
4   import java.awt.Dimension;
5   import java.util.Iterator;
6   import java.util.Vector;
7   import javax.swing.*;
8   import javax.swing.table.DefaultTableCellRenderer;
9   import javax.swing.table.JTableHeader;
10  import javax.swing.table.TableCellRenderer;
11  import javax.swing.table.TableColumn;
12  
13  /**
14   * <small>
15   * <p/>
16   * Copyright (c)  2006  wbogaardt.
17   * This library is free software; you can redistribute it and/or
18   * modify it under the terms of the GNU Lesser General Public
19   * License as published by the Free Software Foundation; either
20   * version 2.1 of the License, or (at your option) any later version.
21   *
22   * This library is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25   * Lesser General Public License for more details.
26   *
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
30   * <p/>
31   * $File:  $ <br>
32   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 7, 2006 1:52:45 PM $ <br>
33   * </small>
34   *
35   * @author wbogaardt
36   *         Column grouping for table columns view.
37   */
38  public class ColumnGroup {
39      /**
40       * Cell renderer for group header.
41       */
42      protected TableCellRenderer renderer;
43      /**
44       * Holds the TableColumn or ColumnGroup objects contained
45       * within this ColumnGroup instance.
46       */
47      protected Vector v;
48      /**
49       * The ColumnGroup instance name.
50       */
51      protected String text;
52      /**
53       * The margin to use for renderering.
54       */
55      protected int margin=0;
56  
57      /**
58       * Standard ColumnGroup constructor.
59       * @param text Name of the ColumnGroup which will be displayed
60       * when the ColumnGroup is renderered.
61       */
62      public ColumnGroup(String text) {
63          this(null,text);
64      }
65  
66      /**
67       * Standard ColumnGroup constructor.
68       * @param renderer a TableCellRenderer for the group.
69       * @param text Name of the ColumnGroup which will be displayed
70       * when the ColumnGroup is renderered.
71       */
72      public ColumnGroup(TableCellRenderer renderer,String text) {
73          if (renderer == null) {
74              this.renderer = new DefaultTableCellRenderer() {
75                  public Component getTableCellRendererComponent(JTable table, Object value,
76                                                                 boolean isSelected, boolean hasFocus, int row, int column) {
77                      JTableHeader header = table.getTableHeader();
78  
79                      if (header != null) {
80                          setForeground(header.getForeground());
81                          setBackground(header.getBackground());
82                          setFont(header.getFont());
83                      }
84                      setHorizontalAlignment(JLabel.CENTER);
85                      setText((value == null) ? "" : value.toString());
86                      setBorder(UIManager.getBorder("TableHeader.cellBorder"));
87                      return this;
88                  }
89              };
90          } else {
91              this.renderer = renderer;
92          }
93          this.text = text;
94          v = new Vector();
95      }
96  
97  
98      /**
99       * Add a TableColumn or ColumnGroup object to the
100      * ColumnGroup instance.
101      * @param obj TableColumn or ColumnGroup
102      */
103     public void add(Object obj) {
104         if (obj == null) { return; }
105         v.addElement(obj);
106     }
107 
108 
109     /**
110      * Get the ColumnGroup list containing the required table
111      * column.
112      * @param g vector to populate with the ColumnGroup/s
113      * @param c TableColumn
114      * @return Vector containing the ColumnGroup/s
115      */
116     public Vector getColumnGroups(TableColumn c, Vector g) {
117         g.addElement(this);
118         if (v.contains(c)) return g;
119         Iterator iter = v.iterator();
120         while (iter.hasNext()) {
121             Object obj = iter.next();
122             if (obj instanceof ColumnGroup) {
123                 Vector groups =
124                 (Vector)((ColumnGroup)obj).getColumnGroups(c,(Vector)g.clone());
125                 if (groups != null) return groups;
126             }
127         }
128         return null;
129     }
130 
131     /**
132      * Returns the TableCellRenderer for the ColumnGroup.
133      * @return the TableCellRenderer
134      */
135     public TableCellRenderer getHeaderRenderer() {
136         return renderer;
137     }
138 
139     /**
140      * Set the TableCellRenderer for this ColumnGroup.
141      * @param renderer the renderer to use
142      */
143     public void setHeaderRenderer(TableCellRenderer renderer) {
144         if (renderer != null) {
145             this.renderer = renderer;
146         }
147     }
148 
149     /**
150      * Get the ColumnGroup header value.
151      * @return the value.
152      */
153     public Object getHeaderValue() {
154         return text;
155     }
156 
157     /**
158      * Get the dimension of this ColumnGroup.
159      * @param table the table the header is being rendered in
160      * @return the dimension of the ColumnGroup
161      */
162     public Dimension getSize(JTable table) {
163         Component comp = renderer.getTableCellRendererComponent(
164         table, getHeaderValue(), false, false,-1, -1);
165         int height = comp.getPreferredSize().height;
166         int width  = 0;
167         Iterator iter = v.iterator();
168         while (iter.hasNext()) {
169             Object obj = iter.next();
170             if (obj instanceof TableColumn) {
171                 TableColumn aColumn = (TableColumn)obj;
172                 width += aColumn.getWidth();
173             }
174             else {
175                 width += ((ColumnGroup)obj).getSize(table).width;
176             }
177         }
178         return new Dimension(width, height);
179     }
180 
181     /**
182      * Sets the margin that ColumnGroup instance will use and all
183      * held TableColumns and/or ColumnGroups.
184      * @param margin the margin
185      */
186     public void setColumnMargin(int margin) {
187         this.margin = margin;
188         Iterator iter = v.iterator();
189         while (iter.hasNext()) {
190             Object obj = iter.next();
191             if (obj instanceof ColumnGroup) {
192                 ((ColumnGroup)obj).setColumnMargin(margin);
193             }
194         }
195     }
196 }