001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing.tree;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Graphics;
027: import java.awt.Rectangle;
028:
029: import javax.swing.Icon;
030: import javax.swing.JLabel;
031: import javax.swing.JTree;
032: import javax.swing.SwingConstants;
033: import javax.swing.UIManager;
034:
035: import org.apache.harmony.x.swing.Utilities;
036:
037: public class DefaultTreeCellRenderer extends JLabel implements
038: TreeCellRenderer {
039: protected boolean selected;
040: protected boolean hasFocus;
041: protected transient Icon closedIcon;
042: protected transient Icon leafIcon;
043: protected transient Icon openIcon;
044: protected Color textSelectionColor;
045: protected Color textNonSelectionColor;
046: protected Color backgroundSelectionColor;
047: protected Color backgroundNonSelectionColor;
048: protected Color borderSelectionColor;
049:
050: private Font font;
051: private Color textDisabledColor;
052: private boolean drawsFocusBorderAroundIcon;
053:
054: public DefaultTreeCellRenderer() {
055: setHorizontalAlignment(SwingConstants.LEFT);
056: closedIcon = getDefaultClosedIcon();
057: leafIcon = getDefaultLeafIcon();
058: openIcon = getDefaultOpenIcon();
059: textSelectionColor = UIManager
060: .getColor("Tree.selectionForeground");
061: textNonSelectionColor = UIManager
062: .getColor("Tree.textForeground");
063: backgroundSelectionColor = UIManager
064: .getColor("Tree.selectionBackground");
065: backgroundNonSelectionColor = UIManager
066: .getColor("Tree.textBackground");
067: borderSelectionColor = UIManager
068: .getColor("Tree.selectionBorderColor");
069: textDisabledColor = UIManager
070: .getColor("Label.disabledForeground");
071: drawsFocusBorderAroundIcon = UIManager
072: .getBoolean("Tree.drawsFocusBorderAroundIcon");
073: }
074:
075: public Icon getDefaultOpenIcon() {
076: return UIManager.getIcon("Tree.openIcon");
077: }
078:
079: public Icon getDefaultClosedIcon() {
080: return UIManager.getIcon("Tree.closedIcon");
081: }
082:
083: public Icon getDefaultLeafIcon() {
084: return UIManager.getIcon("Tree.leafIcon");
085: }
086:
087: public void setOpenIcon(final Icon icon) {
088: openIcon = icon;
089: }
090:
091: public Icon getOpenIcon() {
092: return openIcon;
093: }
094:
095: public void setClosedIcon(final Icon icon) {
096: closedIcon = icon;
097: }
098:
099: public Icon getClosedIcon() {
100: return closedIcon;
101: }
102:
103: public void setLeafIcon(final Icon icon) {
104: leafIcon = icon;
105: }
106:
107: public Icon getLeafIcon() {
108: return leafIcon;
109: }
110:
111: public void setTextSelectionColor(final Color color) {
112: textSelectionColor = color;
113: }
114:
115: public Color getTextSelectionColor() {
116: return textSelectionColor;
117: }
118:
119: public void setTextNonSelectionColor(final Color color) {
120: textNonSelectionColor = color;
121: }
122:
123: public Color getTextNonSelectionColor() {
124: return textNonSelectionColor;
125: }
126:
127: public void setBackgroundSelectionColor(final Color color) {
128: backgroundSelectionColor = color;
129: }
130:
131: public Color getBackgroundSelectionColor() {
132: return backgroundSelectionColor;
133: }
134:
135: public void setBackgroundNonSelectionColor(final Color color) {
136: backgroundNonSelectionColor = color;
137: }
138:
139: public Color getBackgroundNonSelectionColor() {
140: return backgroundNonSelectionColor;
141: }
142:
143: public void setBorderSelectionColor(final Color color) {
144: borderSelectionColor = color;
145: }
146:
147: public Color getBorderSelectionColor() {
148: return borderSelectionColor;
149: }
150:
151: public void setFont(final Font font) {
152: if (!Utilities.isUIResource(font)) {
153: this .font = font;
154: }
155: }
156:
157: public Font getFont() {
158: return font;
159: }
160:
161: public void setBackground(final Color color) {
162: if (!Utilities.isUIResource(color)) {
163: super .setBackground(color);
164: }
165: }
166:
167: public Component getTreeCellRendererComponent(final JTree tree,
168: final Object value, final boolean selected,
169: final boolean expanded, final boolean leaf, final int row,
170: final boolean hasFocus) {
171: this .selected = selected;
172: this .hasFocus = hasFocus;
173:
174: setText(tree.convertValueToText(value, selected, expanded,
175: leaf, row, hasFocus));
176: if (font == null) {
177: font = tree.getFont();
178: }
179: setBackground(selected ? getBackgroundSelectionColor()
180: : getBackgroundNonSelectionColor());
181: if (tree.isEnabled()) {
182: setForeground(selected ? getTextSelectionColor()
183: : getTextNonSelectionColor());
184: } else {
185: setForeground(textDisabledColor);
186: }
187:
188: if (leaf) {
189: setIcon(getLeafIcon());
190: } else if (expanded) {
191: setIcon(getOpenIcon());
192: } else {
193: setIcon(getClosedIcon());
194: }
195:
196: return this ;
197: }
198:
199: public void paint(final Graphics g) {
200: Color oldColor = g.getColor();
201:
202: if (selected) {
203: g.setColor(getBackgroundSelectionColor());
204: } else {
205: g.setColor(getBackgroundNonSelectionColor());
206: }
207: int textOffest = getTextOffset();
208: int x = getComponentOrientation().isLeftToRight() ? textOffest
209: : 0;
210: g.fillRect(x, 0, getWidth() - textOffest, getHeight());
211:
212: super .paint(g);
213:
214: if (hasFocus) {
215: g.setColor(getBorderSelectionColor());
216: if (drawsFocusBorderAroundIcon) {
217: g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
218: } else {
219: g.drawRect(x, 0, getWidth() - textOffest - 1,
220: getHeight() - 1);
221: }
222: }
223:
224: g.setColor(oldColor);
225: }
226:
227: public Dimension getPreferredSize() {
228: Dimension basePrefSize = super .getPreferredSize();
229: return new Dimension(basePrefSize.width + 2,
230: basePrefSize.height);
231: }
232:
233: public void validate() {
234: }
235:
236: public void invalidate() {
237: }
238:
239: public void revalidate() {
240: }
241:
242: public void repaint(final long tm, final int x, final int y,
243: final int width, final int height) {
244: }
245:
246: public void repaint(final Rectangle r) {
247: }
248:
249: public void repaint() {
250: }
251:
252: protected void firePropertyChange(final String propertyName,
253: final Object oldValue, final Object newValue) {
254: }
255:
256: public void firePropertyChange(final String propertyName,
257: final byte oldValue, final byte newValue) {
258: }
259:
260: public void firePropertyChange(final String propertyName,
261: final char oldValue, final char newValue) {
262: }
263:
264: public void firePropertyChange(final String propertyName,
265: final short oldValue, final short newValue) {
266: }
267:
268: public void firePropertyChange(final String propertyName,
269: final int oldValue, final int newValue) {
270: }
271:
272: public void firePropertyChange(final String propertyName,
273: final long oldValue, final long newValue) {
274: }
275:
276: public void firePropertyChange(final String propertyName,
277: final float oldValue, final float newValue) {
278: }
279:
280: public void firePropertyChange(final String propertyName,
281: final double oldValue, final double newValue) {
282: }
283:
284: public void firePropertyChange(final String propertyName,
285: final boolean oldValue, final boolean newValue) {
286: }
287:
288: private int getTextOffset() {
289: return getIcon() != null ? getIcon().getIconWidth() + 2 : 0;
290: }
291: }
|