001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.tree;
014:
015: import org.wings.SComponent;
016: import org.wings.SConstants;
017: import org.wings.SIcon;
018: import org.wings.SLabel;
019: import org.wings.STree;
020: import org.wings.util.SStringBuilder;
021: import org.wings.resource.ResourceManager;
022:
023: /**
024: * @author <a href="mailto:engels@mercatis.de">Holger Engels</a>
025: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
026: */
027: public class SDefaultTreeCellRenderer extends SLabel implements
028: STreeCellRenderer {
029: private SStringBuilder nameBuffer = new SStringBuilder();
030:
031: // Icons
032: /**
033: * Icon used to show non-leaf nodes that aren't expanded.
034: */
035: transient protected SIcon closedIcon;
036:
037: /**
038: * Icon used to show leaf nodes.
039: */
040: transient protected SIcon leafIcon;
041:
042: /**
043: * Icon used to show non-leaf nodes that are expanded.
044: */
045: transient protected SIcon openIcon;
046:
047: /**
048: * Create a SDefaultTreeCellRenderer with default properties.
049: */
050: public SDefaultTreeCellRenderer() {
051: setHorizontalAlignment(SConstants.LEFT);
052: setLeafIcon(getDefaultLeafIcon());
053: setClosedIcon(getDefaultClosedIcon());
054: setOpenIcon(getDefaultOpenIcon());
055: }
056:
057: /**
058: * Returns the default icon, for the current laf, that is used to
059: * represent non-leaf nodes that are expanded.
060: */
061: public SIcon getDefaultOpenIcon() {
062: return (SIcon) ResourceManager.getObject("TreeCG.openIcon",
063: SIcon.class);
064: }
065:
066: /**
067: * Returns the default icon, for the current laf, that is used to
068: * represent non-leaf nodes that are not expanded.
069: */
070: public SIcon getDefaultClosedIcon() {
071: return (SIcon) ResourceManager.getObject("TreeCG.closedIcon",
072: SIcon.class);
073: }
074:
075: /**
076: * Returns the default icon, for the current laf, that is used to
077: * represent leaf nodes.
078: */
079: public SIcon getDefaultLeafIcon() {
080: return (SIcon) ResourceManager.getObject("TreeCG.leafIcon",
081: SIcon.class);
082: }
083:
084: /**
085: * Sets the icon used to represent non-leaf nodes that are expanded.
086: */
087: public void setOpenIcon(SIcon newIcon) {
088: openIcon = newIcon;
089: }
090:
091: /**
092: * Returns the icon used to represent non-leaf nodes that are expanded.
093: */
094: public SIcon getOpenIcon() {
095: return openIcon;
096: }
097:
098: /**
099: * Sets the icon used to represent non-leaf nodes that are not expanded.
100: */
101: public void setClosedIcon(SIcon newIcon) {
102: closedIcon = newIcon;
103: }
104:
105: /**
106: * Returns the icon used to represent non-leaf nodes that are not
107: * expanded.
108: */
109: public SIcon getClosedIcon() {
110: return closedIcon;
111: }
112:
113: /**
114: * Sets the icon used to represent leaf nodes.
115: */
116: public void setLeafIcon(SIcon newIcon) {
117: leafIcon = newIcon;
118: }
119:
120: /**
121: * Returns the icon used to represent leaf nodes.
122: */
123: public SIcon getLeafIcon() {
124: return leafIcon;
125: }
126:
127: /**
128: * Style to use for the foreground for selected nodes.
129: */
130: protected String selectionStyle = null;
131:
132: /**
133: * Style to use for the foreground for non-selected nodes.
134: */
135: protected String nonSelectionStyle = null;
136:
137: public SComponent getTreeCellRendererComponent(STree tree,
138: Object value, boolean selected, boolean expanded,
139: boolean leaf, int row, boolean hasFocus) {
140: setNameRaw(name(tree, row));
141: String string = value != null ? value.toString() : null;
142:
143: if (value == null || string == null || string.length() == 0) {
144: setText(null);
145: } else {
146: setText(string);
147: setToolTipText(string);
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 (getStyle() == null)
171: setStyle(tree.getStyle());
172: if (getDynamicStyles() == null)
173: setDynamicStyles(tree.getDynamicStyles());
174:
175: return this ;
176: }
177:
178: protected String name(SComponent component, int row) {
179: nameBuffer.setLength(0);
180: nameBuffer.append(component.getName()).append("_").append(row);
181: return nameBuffer.toString();
182: }
183:
184: /**
185: * Sets the style the cell is drawn with when the cell is selected.
186: */
187: public void setSelectionStyle(String newStyle) {
188: selectionStyle = newStyle;
189: }
190:
191: /**
192: * Returns the style the cell is drawn with when the cell is selected.
193: */
194: public String getSelectionStyle() {
195: return selectionStyle;
196: }
197:
198: /**
199: * Sets the style the cell is drawn with when the cell isn't selected.
200: */
201: public void setNonSelectionStyle(String newStyle) {
202: nonSelectionStyle = newStyle;
203: }
204:
205: /**
206: * Returns the style the cell is drawn with when the cell isn't selected.
207: */
208: public String getNonSelectionStyle() {
209: return nonSelectionStyle;
210: }
211:
212: }
|