View Javadoc

1   /*
2   This file is part of the xframe software package
3   hosted at http://xframe.sourceforge.net
4   
5   Copyright (c) 2003 Kurt Riede.
6   
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21  package net.sf.xframe.swing.table;
22  
23  import java.awt.Point;
24  import java.awt.event.MouseEvent;
25  import java.util.Enumeration;
26  import java.util.Vector;
27  
28  import javax.swing.table.JTableHeader;
29  import javax.swing.table.TableColumn;
30  import javax.swing.table.TableColumnModel;
31  
32  import net.sf.xframe.swing.JXTable;
33  
34  /***
35   *
36   * Table header for headers with column groups.
37   *
38   * @see net.sf.xframe.swing.JXTable
39   *
40   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
41   */
42  public class ColumnGroupHeader extends JTableHeader {
43  
44      /*** List of columns and column groups. */
45      private Vector columns;
46  
47      /***
48       * Constructor.
49       *
50       * @param tableColumnModel the column model
51       */
52      public ColumnGroupHeader(final TableColumnModel tableColumnModel) {
53          super(tableColumnModel);
54          columns = null;
55          setUI(new ColumnGroupHeaderUI());
56      }
57  
58      /***
59       * @see javax.swing.table.JTableHeader#setReorderingAllowed(boolean)
60       */
61      public final void setReorderingAllowed(final boolean flag) {
62          super.setReorderingAllowed(flag);
63      }
64  
65      /***
66       * Adds a column group to the header.
67       *
68       * @param columnGroup the new column group
69       */
70      public final void addColumnGroup(final ColumnGroup columnGroup) {
71          if (columns == null) {
72              columns = new Vector();
73              setReorderingAllowed(false);
74          }
75          columns.addElement(columnGroup);
76      }
77  
78      /***
79       * Returns Enumeration of all hierarchical column groups spaning a given
80       * column.
81       *
82       * @param tableColumn the column
83       * @return Enumeration of all hierarchical column groups
84       */
85      final Enumeration getColumnGroups(final TableColumn tableColumn) {
86          if (columns != null) {
87              for (final Enumeration enumeration = columns.elements(); enumeration.hasMoreElements();) {
88                  final ColumnGroup columngroup = (ColumnGroup) enumeration.nextElement();
89                  final Vector vector = columngroup.getColumnGroups(tableColumn, new Vector());
90                  if (vector != null) {
91                  return vector.elements();
92                  }
93              }
94          }
95          return null;
96      }
97  
98      /***
99       * Allows the renderer's tips to be used if there is text set.
100      * @param e the location of the event identifies the proper renderer and,
101      *            therefore, the proper tip
102      * @return the tool tip for this component
103      */
104     public String getToolTipText(final MouseEvent e) {
105         final int x;
106         final KTable kTable1 = (KTable) getTable();
107         final JXTable jxTable = kTable1.getEnclosingJXTable();
108         if (kTable1.getType() == KTable.TYPE_LOCKED) {
109             x = e.getX();
110         } else {
111             x = e.getX() + jxTable.getLockedTable().getWidth();
112         }
113         final MouseEvent event = new MouseEvent(jxTable.getTableHeader(), e.getID(), e.getWhen(), e.getModifiers(), x, e.getY(),
114                 e.getClickCount(), e.isPopupTrigger(), e.getButton());
115         return jxTable.getTableHeader().getToolTipText(event);
116     }
117 
118     /***
119      * Returns the tooltip location in this component's coordinate system.
120      * If <code>null</code> is returned, Swing will choose a location.
121      * The default implementation returns <code>null</code>.
122      *
123      * @param e  the <code>MouseEvent</code> that caused the
124      *      <code>ToolTipManager</code> to show the tooltip
125      * @return always returns <code>null</code>
126      */
127     public Point getToolTipLocation(final MouseEvent e) {
128         final int x;
129         final KTable kTable1 = (KTable) getTable();
130         final JXTable jxTable = kTable1.getEnclosingJXTable();
131         if (kTable1.getType() == KTable.TYPE_LOCKED) {
132             x = e.getX();
133         } else {
134             x = e.getX() + jxTable.getLockedTable().getWidth();
135         }
136         final MouseEvent event = new MouseEvent(jxTable.getTableHeader(), e.getID(), e.getWhen(), e.getModifiers(), x, e.getY(),
137                 e.getClickCount(), e.isPopupTrigger(), e.getButton());
138         return jxTable.getTableHeader().getToolTipLocation(event);
139     }
140 }