~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to swingx-core/src/main/java/org/jdesktop/swingx/renderer/TreeCellContext.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: TreeCellContext.java 3424 2009-07-30 10:53:39Z kleopatra $
 
3
 *
 
4
 * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 */
 
21
package org.jdesktop.swingx.renderer;
 
22
 
 
23
import java.awt.Color;
 
24
import java.awt.Component;
 
25
import java.awt.Graphics;
 
26
 
 
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;
 
34
 
 
35
import org.jdesktop.swingx.JXTree;
 
36
 
 
37
/**
 
38
 * Tree specific <code>CellContext</code>. 
 
39
 * 
 
40
 * <ul>
 
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
 
46
 *   generic type?
 
47
 * <li> PENDING: for a JXTree use the icons as returned by the xtree api?
 
48
 * </ul>
 
49
 */
 
50
public class TreeCellContext extends CellContext {
 
51
    /** the icon to use for a leaf node. */
 
52
    protected Icon leafIcon;
 
53
 
 
54
    /** the default icon to use for a closed folder. */
 
55
    protected Icon closedIcon;
 
56
 
 
57
    /** the default icon to use for a open folder. */
 
58
    protected Icon openIcon;
 
59
 
 
60
    /** the border around a focused node. */
 
61
    private Border treeFocusBorder;
 
62
 
 
63
    /**
 
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
 
66
     * with.
 
67
     * 
 
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
 
76
     */
 
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();
 
82
    }
 
83
 
 
84
    private boolean checkDropOnState() {
 
85
        if ((getComponent() == null)) {
 
86
            return false;
 
87
        }
 
88
        JTree.DropLocation dropLocation = getComponent().getDropLocation();
 
89
        if (dropLocation != null
 
90
                && dropLocation.getChildIndex() == -1
 
91
                && getComponent().getRowForPath(dropLocation.getPath()) == row) {
 
92
            return true;
 
93
        }
 
94
        return false;
 
95
    }
 
96
   
 
97
    @Override
 
98
    public JTree getComponent() {
 
99
        return (JTree) super.getComponent();
 
100
    }
 
101
    
 
102
//------------------- accessors for derived state
 
103
    
 
104
    /**
 
105
     * Returns the treePath for the row or null if invalid.
 
106
     * 
 
107
     */
 
108
    public TreePath getTreePath() {
 
109
        if (getComponent() == null) return null;
 
110
        if ((row < 0) || (row >= getComponent().getRowCount())) return null;
 
111
        return getComponent().getPathForRow(row);
 
112
    }
 
113
    /**
 
114
     * {@inheritDoc}
 
115
     * <p>
 
116
     * PENDING: implement to return the tree cell editability!
 
117
     */
 
118
    @Override
 
119
    public boolean isEditable() {
 
120
        return false;
 
121
        // return getComponent() != null ? getComponent().isCellEditable(
 
122
        // getRow(), getColumn()) : false;
 
123
    }
 
124
 
 
125
    /**
 
126
     * {@inheritDoc}
 
127
     */
 
128
    @Override
 
129
    protected Color getSelectionBackground() {
 
130
        Color selection = null;
 
131
        if (isDropOn()) {
 
132
            selection = getDropCellBackground();
 
133
            if (selection != null) return selection;
 
134
        }
 
135
        if (getComponent() instanceof JXTree) {
 
136
            return ((JXTree) getComponent()).getSelectionBackground();
 
137
        }
 
138
        return UIManager.getColor("Tree.selectionBackground");
 
139
    }
 
140
 
 
141
    /**
 
142
     * {@inheritDoc}
 
143
     */
 
144
    @Override
 
145
    protected Color getSelectionForeground() {
 
146
        Color selection = null;
 
147
        if (isDropOn()) {
 
148
            selection = getDropCellForeground();
 
149
            if (selection != null) return selection;
 
150
        }
 
151
        if (getComponent() instanceof JXTree) {
 
152
            return ((JXTree) getComponent()).getSelectionForeground();
 
153
        }
 
154
        return UIManager.getColor("Tree.selectionForeground");
 
155
    }
 
156
 
 
157
    /**
 
158
     * {@inheritDoc}
 
159
     */
 
160
    @Override
 
161
    protected String getUIPrefix() {
 
162
        return "Tree.";
 
163
    }
 
164
 
 
165
    /**
 
166
     * Returns the default icon to use for leaf cell.
 
167
     * 
 
168
     * @return the icon to use for leaf cell.
 
169
     */
 
170
    protected Icon getLeafIcon() {
 
171
        return leafIcon != null ? leafIcon : UIManager
 
172
                .getIcon(getUIKey("leafIcon"));
 
173
    }
 
174
 
 
175
    /**
 
176
     * Returns the default icon to use for open cell.
 
177
     * 
 
178
     * @return the icon to use for open cell.
 
179
     */
 
180
    protected Icon getOpenIcon() {
 
181
        return openIcon != null ? openIcon : UIManager
 
182
                .getIcon(getUIKey("openIcon"));
 
183
    }
 
184
 
 
185
    /**
 
186
     * Returns the default icon to use for closed cell.
 
187
     * 
 
188
     * @return the icon to use for closed cell.
 
189
     */
 
190
    protected Icon getClosedIcon() {
 
191
        return closedIcon != null ? closedIcon : UIManager
 
192
                .getIcon(getUIKey("closedIcon"));
 
193
    }
 
194
 
 
195
    /**
 
196
     * {@inheritDoc}
 
197
     * <p>
 
198
     * 
 
199
     * Overridden to return a default depending for the leaf/open cell state.
 
200
     */
 
201
    @Override
 
202
    public Icon getIcon() {
 
203
        if (isLeaf()) {
 
204
            return getLeafIcon();
 
205
        }
 
206
        if (isExpanded()) {
 
207
            return getOpenIcon();
 
208
        }
 
209
        return getClosedIcon();
 
210
    }
 
211
 
 
212
    @Override
 
213
    protected Border getFocusBorder() {
 
214
        if (treeFocusBorder == null) {
 
215
            treeFocusBorder = new TreeFocusBorder();
 
216
        }
 
217
        return treeFocusBorder;
 
218
    }
 
219
 
 
220
    /**
 
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?
 
224
     */
 
225
    public class TreeFocusBorder extends LineBorder {
 
226
 
 
227
        private Color treeBackground;
 
228
 
 
229
        private Color focusColor;
 
230
 
 
231
        public TreeFocusBorder() {
 
232
            super(Color.BLACK);
 
233
            treeBackground = getBackground();
 
234
            if (treeBackground != null) {
 
235
                focusColor = new Color(~treeBackground.getRGB());
 
236
            }
 
237
        }
 
238
 
 
239
        @Override
 
240
        public void paintBorder(Component c, Graphics g, int x, int y,
 
241
                int width, int height) {
 
242
            Color color = UIManager.getColor("Tree.selectionBorderColor");
 
243
            if (color != null) {
 
244
                lineColor = color;
 
245
            }
 
246
            if (isDashed()) {
 
247
                if (treeBackground != c.getBackground()) {
 
248
                    treeBackground = c.getBackground();
 
249
                    focusColor = new Color(~treeBackground.getRGB());
 
250
                }
 
251
 
 
252
                Color old = g.getColor();
 
253
                g.setColor(focusColor);
 
254
                BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
 
255
                g.setColor(old);
 
256
 
 
257
            } else {
 
258
                super.paintBorder(c, g, x, y, width, height);
 
259
            }
 
260
 
 
261
        }
 
262
 
 
263
        /**
 
264
         * @return a boolean indicating whether the focus border
 
265
         *   should be painted dashed style.
 
266
         */
 
267
        private boolean isDashed() {
 
268
            return Boolean.TRUE.equals(UIManager
 
269
                    .get("Tree.drawDashedFocusIndicator"));
 
270
 
 
271
        }
 
272
 
 
273
        /**
 
274
         * {@inheritDoc}
 
275
         */
 
276
        @Override
 
277
        public boolean isBorderOpaque() {
 
278
            return false;
 
279
        }
 
280
 
 
281
    }
 
282
 
 
283
}
 
 
b'\\ No newline at end of file'