KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > tree > SDefaultTreeCellRenderer


1 /*
2  * $Id: SDefaultTreeCellRenderer.java,v 1.5 2005/05/26 13:18:16 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.tree;
15
16 import org.wings.SComponent;
17 import org.wings.SConstants;
18 import org.wings.SIcon;
19 import org.wings.SLabel;
20 import org.wings.STree;
21
22 /**
23  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
24  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
25  * @version $Revision: 1.5 $
26  */

27 public class SDefaultTreeCellRenderer
28         extends SLabel
29         implements STreeCellRenderer {
30     // Icons
31
/**
32      * Icon used to show non-leaf nodes that aren't expanded.
33      */

34     transient protected SIcon closedIcon;
35
36     /**
37      * Icon used to show leaf nodes.
38      */

39     transient protected SIcon leafIcon;
40
41     /**
42      * Icon used to show non-leaf nodes that are expanded.
43      */

44     transient protected SIcon openIcon;
45
46     /**
47      * Create a SDefaultTreeCellRenderer with default properties.
48      */

49     public SDefaultTreeCellRenderer() {
50         setHorizontalAlignment(SConstants.LEFT);
51         setLeafIcon(getDefaultLeafIcon());
52         setClosedIcon(getDefaultClosedIcon());
53         setOpenIcon(getDefaultOpenIcon());
54     }
55
56     /**
57      * Returns the default icon, for the current laf, that is used to
58      * represent non-leaf nodes that are expanded.
59      */

60     public SIcon getDefaultOpenIcon() {
61         return getSession().getCGManager().getIcon("TreeCG.openIcon");
62     }
63
64     /**
65      * Returns the default icon, for the current laf, that is used to
66      * represent non-leaf nodes that are not expanded.
67      */

68     public SIcon getDefaultClosedIcon() {
69         return getSession().getCGManager().getIcon("TreeCG.closedIcon");
70     }
71
72     /**
73      * Returns the default icon, for the current laf, that is used to
74      * represent leaf nodes.
75      */

76     public SIcon getDefaultLeafIcon() {
77         return getSession().getCGManager().getIcon("TreeCG.leafIcon");
78     }
79
80     /**
81      * Sets the icon used to represent non-leaf nodes that are expanded.
82      */

83     public void setOpenIcon(SIcon newIcon) {
84         openIcon = newIcon;
85     }
86
87     /**
88      * Returns the icon used to represent non-leaf nodes that are expanded.
89      */

90     public SIcon getOpenIcon() {
91         return openIcon;
92     }
93
94     /**
95      * Sets the icon used to represent non-leaf nodes that are not expanded.
96      */

97     public void setClosedIcon(SIcon newIcon) {
98         closedIcon = newIcon;
99     }
100
101     /**
102      * Returns the icon used to represent non-leaf nodes that are not
103      * expanded.
104      */

105     public SIcon getClosedIcon() {
106         return closedIcon;
107     }
108
109     /**
110      * Sets the icon used to represent leaf nodes.
111      */

112     public void setLeafIcon(SIcon newIcon) {
113         leafIcon = newIcon;
114     }
115
116     /**
117      * Returns the icon used to represent leaf nodes.
118      */

119     public SIcon getLeafIcon() {
120         return leafIcon;
121     }
122
123     /**
124      * Style to use for the foreground for selected nodes.
125      */

126     protected String JavaDoc selectionStyle = null;
127
128     /**
129      * Style to use for the foreground for non-selected nodes.
130      */

131     protected String JavaDoc nonSelectionStyle = null;
132
133     public SComponent getTreeCellRendererComponent(STree tree,
134                                                    Object JavaDoc value,
135                                                    boolean selected,
136                                                    boolean expanded,
137                                                    boolean leaf,
138                                                    int row,
139                                                    boolean hasFocus) {
140
141         if (value == null || value.toString() == null ||
142                 value.toString().length() == 0) {
143
144             setText("&nbsp;");
145         } else {
146             setText(value.toString());
147             setToolTipText(value.toString());
148         }
149
150         if (!tree.isEnabled()) {
151             setEnabled(false);
152             if (leaf) {
153                 setDisabledIcon(getLeafIcon());
154             } else if (expanded) {
155                 setDisabledIcon(getOpenIcon());
156             } else {
157                 setDisabledIcon(getClosedIcon());
158             }
159         } else {
160             setEnabled(true);
161             if (leaf) {
162                 setIcon(getLeafIcon());
163             } else if (expanded) {
164                 setIcon(getOpenIcon());
165             } else {
166                 setIcon(getClosedIcon());
167             }
168         }
169
170         if (style == null)
171             setStyle(tree.getStyle());
172         if (dynamicStyles == null)
173             setDynamicStyles(tree.getDynamicStyles());
174
175         return this;
176     }
177
178     /**
179      * Sets the style the cell is drawn with when the cell is selected.
180      */

181     public void setSelectionStyle(String JavaDoc newStyle) {
182         selectionStyle = newStyle;
183     }
184
185     /**
186      * Returns the style the cell is drawn with when the cell is selected.
187      */

188     public String JavaDoc getSelectionStyle() {
189         return selectionStyle;
190     }
191
192     /**
193      * Sets the style the cell is drawn with when the cell isn't selected.
194      */

195     public void setNonSelectionStyle(String JavaDoc newStyle) {
196         nonSelectionStyle = newStyle;
197     }
198
199     /**
200      * Returns the style the cell is drawn with when the cell isn't selected.
201      */

202     public String JavaDoc getNonSelectionStyle() {
203         return nonSelectionStyle;
204     }
205
206 }
207
208
209
Popular Tags