Merge from the pain train
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicTreeUI.java
blobdb74f067ef0bf77d7b65cbdf78a90523c72b964b
1 /* BasicTreeUI.java --
2 Copyright (C) 2002, 2003, 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.Rectangle;
47 import javax.swing.JComponent;
48 import javax.swing.JTree;
49 import javax.swing.UIDefaults;
50 import javax.swing.UIManager;
51 import javax.swing.plaf.ComponentUI;
52 import javax.swing.plaf.TreeUI;
53 import javax.swing.tree.TreeModel;
54 import javax.swing.tree.TreePath;
56 /**
57 * A delegate providing the user interface for <code>JTree</code>
58 * according to the Basic look and feel. The current implementation
59 * of GNU Classpath does really work; it is just a stub that allows
60 * compiling the code.
62 * @see javax.swing.JTree
64 * @author Sascha Brawer (brawer@dandelis.ch)
66 public class BasicTreeUI
67 extends TreeUI
70 /**
71 * Determines the geometric extent of the label that is
72 * drawn for a path.
74 * @param tree the <code>JTree</code> for which this delegate
75 * object provides the user interface.
77 * @param path the path whose label extent is requested.
79 * @return a rectangle enclosing the label, or <code>null</code>
80 * if <code>path</code> contains invalid nodes.
82 public Rectangle getPathBounds(JTree tree, TreePath path)
84 return null; // FIXME: not implemented
88 /**
89 * Creates a <code>TreePath</code> for the specified row.
91 * @param tree the <code>JTree</code> for which this delegate
92 * object provides the user interface.
94 * @param row the index of the row, which should be a number
95 * in the range <code>[0, getRowCount(tree) - 1]</code>.
97 * @return a <code>TreePath</code> for the specified row, or
98 * <code>null</code> if <code>row</code> is outside
99 * the valid range.
101 public TreePath getPathForRow(JTree tree, int row)
103 return null; // FIXME: not implemented
108 * Determines in which row a <code>TreePath</code> is currently
109 * being displayed.
111 * @param tree the <code>JTree</code> for which this delegate
112 * object provides the user interface.
114 * @param path the path for which the caller wants to know
115 * in which row it is being displayed.
117 * @return a number in the range <code>[0, getRowCount(tree)
118 * - 1]</code> if the path is currently on display;
119 * <code>-1</code> if the path is not shown to the
120 * user.
122 public int getRowForPath(JTree tree, TreePath path)
124 return -1; // FIXME: not implemented
129 * Counts how many rows are currently displayed.
131 * @param tree the <code>JTree</code> for which this delegate
132 * object provides the user interface.
134 * @return the number of visible rows.
136 public int getRowCount(JTree tree)
138 return 0; // FIXME: not implemented
143 * Finds the path that is closest to the specified position.
145 * <p><img src="../doc-files/TreeUI-1.png" width="300" height="250"
146 * alt="[A screen shot of a JTree]" />
148 * <p>As shown by the above illustration, the bounds of the
149 * closest path do not necessarily need to contain the passed
150 * location.
152 * @param tree the <code>JTree</code> for which this delegate
153 * object provides the user interface.
155 * @param x the horizontal location, relative to the origin
156 * of <code>tree</code>.
158 * @param y the vertical location, relative to the origin
159 * of <code>tree</code>.
161 * @return the closest path, or <code>null</code> if the
162 * tree is currenlty not displaying any paths at all.
164 public TreePath getClosestPathForLocation(JTree tree,
165 int x, int y)
167 return null; // FIXME: not implemented
172 * Determines whether the user is currently editing a tree cell.
174 * @param tree the <code>JTree</code> for which this delegate
175 * object provides the user interface.
177 * @see #getEditingPath
179 public boolean isEditing(JTree tree)
181 return false; // FIXME: not implemented
186 * Stops editing a tree cell, committing the entered value into the
187 * tree&#x2019;s model. If no editing session is active, or if the
188 * active editor does not agree to stopping, nothing happens. In
189 * some look and feels, this action happens when the user has
190 * pressed the enter key.
192 * @param tree the <code>JTree</code> for which this delegate
193 * object provides the user interface.
195 * @return <code>false</code> if the editing still goes on because
196 * the cell editor has objected to stopping the session;
197 * <code>true</code> if editing has been stopped.
199 public boolean stopEditing(JTree tree)
201 return true; // FIXME: not implemented
205 * Cancels editing a tree cell, discarding any entered value.
206 * If no editing session is active, nothing happens. The cell
207 * editor is not given an opportunity to veto the canceling.
208 * In some look and feels, this action happens when the user has
209 * pressed the escape key.
211 * @param tree the <code>JTree</code> for which this delegate
212 * object provides the user interface.
214 public void cancelEditing(JTree tree)
216 // FIXME: not implemented
221 * Starts a session to edit a tree cell. If the cell editor
222 * rejects editing the cell, it will just be selected.
224 * @param tree the <code>JTree</code> for which this delegate
225 * object provides the user interface.
227 * @param path the cell to edit.
229 public void startEditingAtPath(JTree tree, TreePath path)
231 // FIXME: not implemented
236 * Retrieves the tree cell that is currently being edited.
238 * @return the currently edited path, or <code>null</code>
239 * if no editing session is currently active.
241 public TreePath getEditingPath(JTree tree)
243 return null; // FIXME: not implemented
246 public static ComponentUI createUI(JComponent c)
248 return new BasicTreeUI();
251 int rightChildIndent;
252 int leftChildIndent;
253 int rowHeight;
254 Color hashColor;
256 protected void installDefaults(JTree tree)
258 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
260 tree.setFont(defaults.getFont("Tree.font"));
261 tree.setForeground(defaults.getColor("Tree.foreground"));
262 tree.setBackground(defaults.getColor("Tree.background"));
263 tree.setOpaque(true);
265 hashColor = defaults.getColor("Tree.hash");
266 rightChildIndent = defaults.getInt("Tree.rightChildIndent");
267 leftChildIndent = defaults.getInt("Tree.leftChildIndent");
268 rowHeight = defaults.getInt("Tree.rowHeight");
271 protected void installKeyboardActions()
275 protected void installListeners()
279 public void installUI(JComponent c)
281 installDefaults((JTree) c);
285 protected void uninstallDefaults(JTree tree)
287 tree.setFont(null);
288 tree.setForeground(null);
289 tree.setBackground(null);
291 tree.setCellRenderer(null);
294 public void uninstallUI(JComponent c)
296 uninstallDefaults((JTree) c);
299 public Dimension getPreferredSize(JComponent c)
301 return new Dimension(200,200);
304 protected void paintLeaf(Graphics g, int x, int y, JTree tree, Object leaf)
306 Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree,
307 leaf,
308 false, // selected
309 false, // expanded
310 true, // leaf
311 0, // row
312 false // hasFocus
314 g.translate(x, y);
315 c.paint(g);
316 g.translate(-x, -y);
319 protected void paintNonLeaf(Graphics g, int x, int y, JTree tree, Object nonLeaf)
321 Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree,
322 nonLeaf,
323 false, // selected
324 false, // expanded
325 false, // leaf
326 0, // row
327 false // hasFocus
329 g.translate(x, y);
330 c.paint(g);
331 g.translate(-x, -y);
334 protected int paintRecursive(Graphics g,
335 int indentation,
336 int descent,
337 int childNumber,
338 int depth,
339 JTree tree,
340 TreeModel mod,
341 Object curr)
343 Rectangle clip = g.getClipBounds();
344 if (indentation > clip.x + clip.width + rightChildIndent ||
345 descent > clip.y + clip.height + rowHeight)
346 return descent;
349 int halfHeight = rowHeight / 2;
350 int halfWidth = rightChildIndent / 2;
351 int y0 = descent + halfHeight;
353 if (mod.isLeaf(curr))
355 paintLeaf(g, indentation, descent, tree, curr);
356 descent += rowHeight;
358 else
360 if (depth > 0 || tree.isRootVisible())
362 paintNonLeaf(g, indentation, descent, tree, curr);
363 descent += rowHeight;
364 y0 += halfHeight;
366 int max = mod.getChildCount(curr);
367 for (int i = 0; i < max; ++i)
369 g.setColor(hashColor);
370 g.drawLine(indentation + halfWidth, descent + halfHeight,
371 indentation + rightChildIndent, descent + halfHeight);
372 descent = paintRecursive(g,
373 indentation + rightChildIndent, descent,
374 i, depth+1,
375 tree, mod, mod.getChild(curr, i));
379 int y1 = descent - halfHeight;
380 if (y0 != y1)
382 g.setColor(hashColor);
383 g.drawLine(indentation + halfWidth, y0,
384 indentation + halfWidth, y1);
387 return descent;
390 public void paint(Graphics g, JComponent c)
392 JTree tree = (JTree) c;
393 TreeModel mod = tree.getModel();
394 g.translate(10, 10);
395 paintRecursive(g, 0, 0, 0, 0, tree, mod, mod.getRoot());
396 g.translate(-10, -10);