Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicTableUI.java
blob9be6d60e918f7529a9a208a688fb157019378d04
1 /* BasicTableUI.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.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.Point;
46 import java.awt.Rectangle;
47 import java.awt.event.FocusEvent;
48 import java.awt.event.FocusListener;
49 import java.awt.event.KeyEvent;
50 import java.awt.event.KeyListener;
51 import java.awt.event.MouseEvent;
53 import javax.swing.CellRendererPane;
54 import javax.swing.JComponent;
55 import javax.swing.JTable;
56 import javax.swing.ListSelectionModel;
57 import javax.swing.UIDefaults;
58 import javax.swing.UIManager;
59 import javax.swing.event.MouseInputListener;
60 import javax.swing.plaf.ComponentUI;
61 import javax.swing.plaf.TableUI;
62 import javax.swing.table.TableCellRenderer;
63 import javax.swing.table.TableColumn;
64 import javax.swing.table.TableColumnModel;
66 public class BasicTableUI
67 extends TableUI
69 public static ComponentUI createUI(JComponent comp)
71 return new BasicTableUI();
74 protected FocusListener focusListener;
75 protected KeyListener keyListener;
76 protected MouseInputListener mouseInputListener;
77 protected CellRendererPane rendererPane;
78 protected JTable table;
80 class FocusHandler implements FocusListener
82 public void focusGained(FocusEvent e)
85 public void focusLost(FocusEvent e)
90 class KeyHandler implements KeyListener
92 public void keyPressed(KeyEvent e)
95 public void keyReleased(KeyEvent e)
98 public void keyTyped(KeyEvent e)
103 class MouseInputHandler implements MouseInputListener
105 Point begin, curr;
107 private void updateSelection()
109 if (table.getRowSelectionAllowed())
111 int lo_row = table.rowAtPoint(begin);
112 int hi_row = table.rowAtPoint(curr);
113 ListSelectionModel rowModel = table.getSelectionModel();
114 if (lo_row != -1 && hi_row != -1)
115 rowModel.setSelectionInterval(lo_row, hi_row);
118 if (table.getColumnSelectionAllowed())
120 int lo_col = table.columnAtPoint(begin);
121 int hi_col = table.columnAtPoint(curr);
122 ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
123 if (lo_col != -1 && hi_col != -1)
124 colModel.setSelectionInterval(lo_col, hi_col);
128 public void mouseClicked(MouseEvent e)
131 public void mouseDragged(MouseEvent e)
133 curr = new Point(e.getX(), e.getY());
134 updateSelection();
136 public void mouseEntered(MouseEvent e)
139 public void mouseExited(MouseEvent e)
142 public void mouseMoved(MouseEvent e)
145 public void mousePressed(MouseEvent e)
147 begin = new Point(e.getX(), e.getY());
148 curr = new Point(e.getX(), e.getY());
149 updateSelection();
151 public void mouseReleased(MouseEvent e)
153 begin = null;
154 curr = null;
158 protected FocusListener createFocusListener()
160 return new FocusHandler();
162 protected KeyListener createKeyListener()
164 return new KeyHandler();
166 protected MouseInputListener createMouseInputListener()
168 return new MouseInputHandler();
171 public Dimension getMaximumSize(JComponent comp)
173 return getPreferredSize(comp);
176 public Dimension getMinimumSize(JComponent comp)
178 return getPreferredSize(comp);
181 public Dimension getPreferredSize(JComponent comp)
183 int width = table.getColumnModel().getTotalColumnWidth();
184 int height = table.getRowCount() * table.getRowHeight();
185 return new Dimension(width, height);
188 protected void installDefaults()
190 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
191 table.setFont(defaults.getFont("Table.font"));
192 table.setGridColor(defaults.getColor("Table.gridColor"));
193 table.setForeground(defaults.getColor("Table.foreground"));
194 table.setBackground(defaults.getColor("Table.background"));
195 table.setSelectionForeground(defaults.getColor("Table.selectionForeground"));
196 table.setSelectionBackground(defaults.getColor("Table.selectionBackground"));
197 table.setOpaque(true);
199 protected void installKeyboardActions()
203 protected void installListeners()
205 table.addFocusListener(focusListener);
206 table.addKeyListener(keyListener);
207 table.addMouseListener(mouseInputListener);
210 protected void uninstallDefaults()
212 table.setFont(null);
213 table.setGridColor(null);
214 table.setForeground(null);
215 table.setBackground(null);
216 table.setSelectionForeground(null);
217 table.setSelectionBackground(null);
220 protected void uninstallKeyboardActions()
224 protected void uninstallListeners()
226 table.removeFocusListener(focusListener);
227 table.removeKeyListener(keyListener);
228 table.removeMouseListener(mouseInputListener);
231 public void installUI(JComponent comp)
233 table = (JTable)comp;
234 focusListener = createFocusListener();
235 keyListener = createKeyListener();
236 mouseInputListener = createMouseInputListener();
237 installDefaults();
238 installKeyboardActions();
239 installListeners();
242 public void uninstallUI(JComponent c)
244 uninstallListeners();
245 uninstallKeyboardActions();
246 uninstallDefaults();
249 public void paint(Graphics gfx, JComponent ignored)
251 int ncols = table.getColumnCount();
252 int nrows = table.getRowCount();
253 if (nrows == 0 || ncols == 0)
254 return;
256 Rectangle clip = gfx.getClipBounds();
257 TableColumnModel cols = table.getColumnModel();
259 int height = table.getRowHeight();
260 int x0 = 0, y0 = 0;
261 int x = x0;
262 int y = y0;
264 Dimension gap = table.getIntercellSpacing();
265 int ymax = clip.y + clip.height;
266 int xmax = clip.x + clip.width;
268 // paint the cell contents
269 for (int c = 0; c < ncols && x < xmax; ++c)
271 y = y0;
272 TableColumn col = cols.getColumn(c);
273 int width = col.getWidth();
274 int modelCol = col.getModelIndex();
276 for (int r = 0; r < nrows && y < ymax; ++r)
278 Rectangle bounds = new Rectangle(x, y, width, height);
279 if (bounds.intersects(clip))
281 TableCellRenderer rend = table.getCellRenderer(r, c);
282 Component comp = table.prepareRenderer(rend, r, c);
283 gfx.translate(x, y);
284 comp.setBounds(new Rectangle(0, 0, width, height));
285 comp.paint(gfx);
286 gfx.translate(-x, -y);
288 y += height;
289 if (gap != null)
290 y += gap.height;
292 x += width;
293 if (gap != null)
294 x += gap.width;
297 // tighten up the x and y max bounds
298 ymax = y;
299 xmax = x;
301 Color grid = table.getGridColor();
303 // paint vertical grid lines
304 if (grid != null && table.getShowVerticalLines())
306 x = x0;
307 Color save = gfx.getColor();
308 gfx.setColor(grid);
309 boolean paintedLine = false;
310 for (int c = 0; c < ncols && x < xmax; ++c)
312 x += cols.getColumn(c).getWidth();;
313 if (gap != null)
314 x += gap.width;
315 gfx.drawLine(x, y0, x, ymax);
316 paintedLine = true;
318 gfx.setColor(save);
321 // paint horizontal grid lines
322 if (grid != null && table.getShowHorizontalLines())
324 y = y0;
325 Color save = gfx.getColor();
326 gfx.setColor(grid);
327 boolean paintedLine = false;
328 for (int r = 0; r < nrows && y < ymax; ++r)
330 y += height;
331 if (gap != null)
332 y += gap.height;
333 gfx.drawLine(x0, y, xmax, y);
334 paintedLine = true;
336 gfx.setColor(save);