2
* $Id: TreeCellContext.java 3424 2009-07-30 10:53:39Z kleopatra $
4
* Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
package org.jdesktop.swingx.renderer;
23
import java.awt.Color;
24
import java.awt.Component;
25
import java.awt.Graphics;
27
import javax.swing.Icon;
28
import javax.swing.JTree;
29
import javax.swing.UIManager;
30
import javax.swing.border.Border;
31
import javax.swing.border.LineBorder;
32
import javax.swing.plaf.basic.BasicGraphicsUtils;
33
import javax.swing.tree.TreePath;
35
import org.jdesktop.swingx.JXTree;
38
* Tree specific <code>CellContext</code>.
41
* <li>PENDING: use focus border as returned from list or table instead of
42
* rolling its own? The missing ui-border probably is a consequence of the
43
* border hacking as implemented in core default renderer. SwingX has a
44
* composite default which should use the "normal" border.
45
* <li> PENDING: selection colors couple explicitly to SwingX - should we go JXTree as
47
* <li> PENDING: for a JXTree use the icons as returned by the xtree api?
50
public class TreeCellContext extends CellContext {
51
/** the icon to use for a leaf node. */
52
protected Icon leafIcon;
54
/** the default icon to use for a closed folder. */
55
protected Icon closedIcon;
57
/** the default icon to use for a open folder. */
58
protected Icon openIcon;
60
/** the border around a focused node. */
61
private Border treeFocusBorder;
64
* Sets state of the cell's context. Note that the component might be null
65
* to indicate a cell without a concrete context. All accessors must cope
68
* @param component the component the cell resides on, might be null
69
* @param value the content value of the cell
70
* @param row the cell's row index in view coordinates
71
* @param column the cell's column index in view coordinates
72
* @param selected the cell's selected state
73
* @param focused the cell's focused state
74
* @param expanded the cell's expanded state
75
* @param leaf the cell's leaf state
77
public void installContext(JTree component, Object value, int row, int column,
78
boolean selected, boolean focused, boolean expanded, boolean leaf) {
79
this.component = component;
80
installState(value, row, column, selected, focused, expanded, leaf);
81
this.dropOn = checkDropOnState();
84
private boolean checkDropOnState() {
85
if ((getComponent() == null)) {
88
JTree.DropLocation dropLocation = getComponent().getDropLocation();
89
if (dropLocation != null
90
&& dropLocation.getChildIndex() == -1
91
&& getComponent().getRowForPath(dropLocation.getPath()) == row) {
98
public JTree getComponent() {
99
return (JTree) super.getComponent();
102
//------------------- accessors for derived state
105
* Returns the treePath for the row or null if invalid.
108
public TreePath getTreePath() {
109
if (getComponent() == null) return null;
110
if ((row < 0) || (row >= getComponent().getRowCount())) return null;
111
return getComponent().getPathForRow(row);
116
* PENDING: implement to return the tree cell editability!
119
public boolean isEditable() {
121
// return getComponent() != null ? getComponent().isCellEditable(
122
// getRow(), getColumn()) : false;
129
protected Color getSelectionBackground() {
130
Color selection = null;
132
selection = getDropCellBackground();
133
if (selection != null) return selection;
135
if (getComponent() instanceof JXTree) {
136
return ((JXTree) getComponent()).getSelectionBackground();
138
return UIManager.getColor("Tree.selectionBackground");
145
protected Color getSelectionForeground() {
146
Color selection = null;
148
selection = getDropCellForeground();
149
if (selection != null) return selection;
151
if (getComponent() instanceof JXTree) {
152
return ((JXTree) getComponent()).getSelectionForeground();
154
return UIManager.getColor("Tree.selectionForeground");
161
protected String getUIPrefix() {
166
* Returns the default icon to use for leaf cell.
168
* @return the icon to use for leaf cell.
170
protected Icon getLeafIcon() {
171
return leafIcon != null ? leafIcon : UIManager
172
.getIcon(getUIKey("leafIcon"));
176
* Returns the default icon to use for open cell.
178
* @return the icon to use for open cell.
180
protected Icon getOpenIcon() {
181
return openIcon != null ? openIcon : UIManager
182
.getIcon(getUIKey("openIcon"));
186
* Returns the default icon to use for closed cell.
188
* @return the icon to use for closed cell.
190
protected Icon getClosedIcon() {
191
return closedIcon != null ? closedIcon : UIManager
192
.getIcon(getUIKey("closedIcon"));
199
* Overridden to return a default depending for the leaf/open cell state.
202
public Icon getIcon() {
204
return getLeafIcon();
207
return getOpenIcon();
209
return getClosedIcon();
213
protected Border getFocusBorder() {
214
if (treeFocusBorder == null) {
215
treeFocusBorder = new TreeFocusBorder();
217
return treeFocusBorder;
221
* Border used to draw around the content of the node. <p>
222
* PENDING: isn't that the same as around a list or table cell, but
223
* without a tree-specific key/value pair in UIManager?
225
public class TreeFocusBorder extends LineBorder {
227
private Color treeBackground;
229
private Color focusColor;
231
public TreeFocusBorder() {
233
treeBackground = getBackground();
234
if (treeBackground != null) {
235
focusColor = new Color(~treeBackground.getRGB());
240
public void paintBorder(Component c, Graphics g, int x, int y,
241
int width, int height) {
242
Color color = UIManager.getColor("Tree.selectionBorderColor");
247
if (treeBackground != c.getBackground()) {
248
treeBackground = c.getBackground();
249
focusColor = new Color(~treeBackground.getRGB());
252
Color old = g.getColor();
253
g.setColor(focusColor);
254
BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
258
super.paintBorder(c, g, x, y, width, height);
264
* @return a boolean indicating whether the focus border
265
* should be painted dashed style.
267
private boolean isDashed() {
268
return Boolean.TRUE.equals(UIManager
269
.get("Tree.drawDashedFocusIndicator"));
277
public boolean isBorderOpaque() {
b'\\ No newline at end of file'