Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicTableHeaderUI.java
blobebc702ad39329023875a514d56c4c57e0e096557
1 /* BasicTableHeaderUI.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import java.awt.Component;
42 import java.awt.Dimension;
43 import java.awt.Graphics;
44 import java.awt.Rectangle;
45 import java.awt.event.MouseEvent;
47 import javax.swing.CellRendererPane;
48 import javax.swing.JComponent;
49 import javax.swing.UIDefaults;
50 import javax.swing.UIManager;
51 import javax.swing.border.Border;
52 import javax.swing.event.MouseInputListener;
53 import javax.swing.plaf.ComponentUI;
54 import javax.swing.plaf.TableHeaderUI;
55 import javax.swing.table.JTableHeader;
56 import javax.swing.table.TableCellRenderer;
57 import javax.swing.table.TableColumn;
58 import javax.swing.table.TableColumnModel;
60 public class BasicTableHeaderUI
61 extends TableHeaderUI
64 public static ComponentUI createUI(JComponent h)
66 return new BasicTableHeaderUI();
69 protected JTableHeader header;
70 protected MouseInputListener mouseInputListener;
71 protected CellRendererPane rendererPane;
72 protected Border cellBorder;
74 class MouseInputHandler
75 implements MouseInputListener
77 public void mouseClicked(MouseEvent e) {}
78 public void mouseDragged(MouseEvent e) {}
79 public void mouseEntered(MouseEvent e) {}
80 public void mouseExited(MouseEvent e) {}
81 public void mouseMoved(MouseEvent e) {}
82 public void mousePressed(MouseEvent e) {}
83 public void mouseReleased(MouseEvent e) {}
86 protected MouseInputListener createMouseInputListener()
88 return new MouseInputHandler();
91 public BasicTableHeaderUI()
93 mouseInputListener = createMouseInputListener();
96 protected void installDefaults()
98 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
99 header.setBackground(defaults.getColor("TableHeader.background"));
100 header.setForeground(defaults.getColor("TableHeader.foreground"));
101 header.setFont(defaults.getFont("TableHeader.font"));
102 cellBorder = defaults.getBorder("TableHeader.cellBorder");
105 protected void installKeyboardActions()
109 protected void installListeners()
111 header.addMouseListener(mouseInputListener);
114 public void installUI(JComponent c)
116 header = (JTableHeader) c;
117 installDefaults();
118 installKeyboardActions();
119 installListeners();
122 protected void uninstallDefaults()
124 header.setBackground(null);
125 header.setForeground(null);
126 header.setFont(null);
129 protected void uninstallKeyboardActions()
133 protected void uninstallListeners()
135 header.removeMouseListener(mouseInputListener);
138 public void uninstallUI(JComponent c)
140 uninstallListeners();
141 uninstallKeyboardActions();
142 uninstallDefaults();
145 public void paint(Graphics gfx, JComponent c)
147 TableColumnModel cmod = header.getColumnModel();
148 int ncols = cmod.getColumnCount();
149 if (ncols == 0)
150 return;
152 Rectangle clip = gfx.getClipBounds();
153 TableCellRenderer defaultRend = header.getDefaultRenderer();
155 for (int i = 0; i < ncols; ++i)
157 Rectangle bounds = header.getHeaderRect(i);
158 if (bounds.intersects(clip))
160 TableColumn col = cmod.getColumn(i);
161 TableCellRenderer rend = col.getHeaderRenderer();
162 if (rend == null)
163 rend = defaultRend;
164 Object val = col.getHeaderValue();
165 Component comp = rend.getTableCellRendererComponent(header.getTable(),
166 val,
167 false, // isSelected
168 false, // isFocused
169 -1, i);
170 comp.setFont(header.getFont());
171 comp.setBackground(header.getBackground());
172 comp.setForeground(header.getForeground());
173 if (comp instanceof JComponent)
174 ((JComponent)comp).setBorder(cellBorder);
175 gfx.translate(bounds.x, bounds.y);
176 comp.setSize(bounds.width, bounds.height);
177 comp.setLocation(0,0);
178 comp.paint(gfx);
179 gfx.translate(-bounds.x, -bounds.y);
185 public Dimension getPreferredSize(JComponent c)
187 TableColumnModel cmod = header.getColumnModel();
188 TableCellRenderer defaultRend = header.getDefaultRenderer();
189 int ncols = cmod.getColumnCount();
190 Dimension ret = new Dimension(0,0);
191 int spacing = 0;
193 if (header.getTable() != null
194 && header.getTable().getIntercellSpacing() != null)
195 spacing = header.getTable().getIntercellSpacing().width;
197 for (int i = 0; i < ncols; ++i)
199 TableColumn col = cmod.getColumn(i);
200 TableCellRenderer rend = col.getHeaderRenderer();
201 if (rend == null)
202 rend = defaultRend;
203 Object val = col.getHeaderValue();
204 Component comp = rend.getTableCellRendererComponent(header.getTable(),
205 val,
206 false, // isSelected
207 false, // isFocused
208 -1, i);
209 comp.setFont(header.getFont());
210 comp.setBackground(header.getBackground());
211 comp.setForeground(header.getForeground());
212 if (comp instanceof JComponent)
213 ((JComponent)comp).setBorder(cellBorder);
215 Dimension d = comp.getPreferredSize();
216 ret.width += spacing;
217 ret.height = Math.max(d.height, ret.height);
219 ret.width = cmod.getTotalColumnWidth();
220 return ret;