01: /*
02: * AbstractTreeCellRenderer.java
03: *
04: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21:
22: package org.underworldlabs.swing.tree;
23:
24: import java.awt.Component;
25: import java.awt.Graphics;
26: import javax.swing.Icon;
27: import javax.swing.JTree;
28: import javax.swing.tree.DefaultTreeCellRenderer;
29:
30: /* ----------------------------------------------------------
31: * CVS NOTE: Changes to the CVS repository prior to the
32: * release of version 3.0.0beta1 has meant a
33: * resetting of CVS revision numbers.
34: * ----------------------------------------------------------
35: */
36:
37: /**
38: * Abstract tree cell renderer with custom paint method.
39: *
40: * @author Takis Diakoumis
41: * @version $Revision: 1.4 $
42: * @date $Date: 2006/05/14 06:56:07 $
43: */
44: public abstract class AbstractTreeCellRenderer extends
45: DefaultTreeCellRenderer {
46:
47: /**
48: * Sets the value of the current tree cell to value. If
49: * selected is true, the cell will be drawn as if selected.
50: * If expanded is true the node is currently expanded and if
51: * leaf is true the node represets a leaf and if hasFocus
52: * is true the node currently has focus. tree is the JTree
53: * the receiver is being configured for. Returns the Component
54: * that the renderer uses to draw the value.
55: *
56: * @return the Component that the renderer uses to draw the value
57: */
58: public abstract Component getTreeCellRendererComponent(JTree tree,
59: Object value, boolean isSelected, boolean isExpanded,
60: boolean isLeaf, int row, boolean hasFocus);
61:
62: public void paintComponent(Graphics g) {
63:
64: if (selected) {
65: int imageOffset = getLabelX();
66:
67: // paint the background
68: if (backgroundSelectionColor != null) {
69: g.setColor(backgroundSelectionColor);
70: g.fillRect(imageOffset, 0,
71: getWidth() - 1 - imageOffset, getHeight());
72: }
73:
74: // paint the border
75: if (borderSelectionColor != null) {
76: g.setColor(borderSelectionColor);
77: g.drawRect(imageOffset, 0,
78: getWidth() - 1 - imageOffset, getHeight() - 1);
79: }
80: }
81:
82: super .paintComponent(g);
83: }
84:
85: protected int getLabelX() {
86: Icon currentI = getIcon();
87: if (currentI != null && getText() != null) {
88: return currentI.getIconWidth()
89: + Math.max(0, getIconTextGap() - 1);
90: }
91: return 0;
92: }
93:
94: }
|