001: package Schmortopf.FileComponents.View;
002:
003: import javax.swing.*;
004: import javax.swing.tree.*;
005: import java.awt.*;
006:
007: import Schmortopf.FileComponents.Model.*;
008: import Language.Language;
009:
010: import Schmortopf.Utility.themes.*; // These themes must be used.
011:
012: public class FileComponentsTreeCellRenderer extends
013: DefaultTreeCellRenderer {
014:
015: // Keys for specific icons :
016:
017: public static final byte UseTokenLeafIcon = (byte) 0; // leafs from JavaCC parser
018: public static final byte UseImportLeafIcon = (byte) 1;
019: public static final byte UseExtendsLeafIcon = (byte) 2;
020: public static final byte UseImplementsLeafIcon = (byte) 3;
021: public static final byte UseFieldLeafIcon = (byte) 4;
022: public static final byte UseConstructorLeafIcon = (byte) 5;
023: public static final byte UseMethodLeafIcon = (byte) 6;
024: public static final byte UseInitializerLeafIcon = (byte) 7;
025: public static final byte UseClassLeafIcon = (byte) 8;
026: public static final byte UseInterfaceLeafIcon = (byte) 9;
027: public static final byte UseSuperClassLeafIcon = (byte) 10;
028: public static final byte UseParserLeafIcon = (byte) 11;
029:
030: public static final byte UseClassNodeIcon = (byte) 100;
031: public static final byte UseInterfaceNodeIcon = (byte) 101;
032: public static final byte UsePackageNodeIcon = (byte) 102;
033: public static final byte UseImportNodeIcon = (byte) 103;
034:
035: public static final byte UsePublicNodeIcon = (byte) 104;
036: public static final byte UsePrivateNodeIcon = (byte) 105;
037: public static final byte UseProtectedNodeIcon = (byte) 106;
038: public static final byte UsePackageScopeNodeIcon = (byte) 107;
039:
040: private final Icon classLeafIcon;
041: private final Icon interfaceLeafIcon;
042: private final Icon tokenLeafIcon;
043: private final Icon importLeafIcon;
044: private final Icon extendsLeafIcon;
045: private final Icon constructorLeafIcon;
046: private final Icon methodLeafIcon;
047: private final Icon initializerLeafIcon;
048: private final Icon fieldLeafIcon;
049: private final Icon implements LeafIcon;
050: private final Icon super ClassLeafIcon;
051: private final Icon parserLeafIcon;
052:
053: private final Icon packageNodeIcon;
054: private final Icon importNodeIcon;
055: private final Icon classNodeIcon;
056: private final Icon interfaceNodeIcon;
057: private final Icon publicNodeIcon;
058: private final Icon privateNodeIcon;
059: private final Icon protectedNodeIcon;
060: private final Icon packageScopeNodeIcon;
061:
062: private final Icon defaultNodeIcon;
063:
064: private final Icon rootNodeIcon;
065:
066: public FileComponentsTreeCellRenderer() {
067: super ();
068: classLeafIcon = new SelfDrawingIcon("C",
069: SelfDrawingIcon.ColorBlack);
070: interfaceLeafIcon = new SelfDrawingIcon("I",
071: SelfDrawingIcon.ColorBlue);
072: tokenLeafIcon = new SelfDrawingIcon("T",
073: SelfDrawingIcon.ColorBlack);
074: importLeafIcon = new SelfDrawingIcon("I",
075: SelfDrawingIcon.ColorBlue);
076: extendsLeafIcon = new SelfDrawingIcon("E",
077: SelfDrawingIcon.ColorBlue);
078: constructorLeafIcon = new SelfDrawingIcon("C",
079: SelfDrawingIcon.ColorRed);
080: methodLeafIcon = new SelfDrawingIcon("M",
081: SelfDrawingIcon.ColorGreen);
082: initializerLeafIcon = new SelfDrawingIcon("In",
083: SelfDrawingIcon.ColorGreen);
084: fieldLeafIcon = new SelfDrawingIcon("A",
085: SelfDrawingIcon.ColorBlue);
086: implements LeafIcon = new SelfDrawingIcon(Language
087: .Translate("implements"), SelfDrawingIcon.ColorBlue);
088: super ClassLeafIcon = new SelfDrawingIcon(Language
089: .Translate("extends"), SelfDrawingIcon.ColorBlue);
090: parserLeafIcon = new SelfDrawingIcon(Language
091: .Translate("Parser"), SelfDrawingIcon.ColorBlue);
092:
093: packageNodeIcon = new SelfDrawingIcon(Language
094: .Translate("Package"), SelfDrawingIcon.ColorBlue);
095: importNodeIcon = new SelfDrawingIcon(Language
096: .Translate("Import"), SelfDrawingIcon.ColorBlue);
097: classNodeIcon = new SelfDrawingIcon(
098: Language.Translate("Class"), SelfDrawingIcon.ColorRed);
099: interfaceNodeIcon = new SelfDrawingIcon(Language
100: .Translate("Interface"), SelfDrawingIcon.ColorBlue);
101: publicNodeIcon = new SelfDrawingIcon(Language
102: .Translate("Public"), SelfDrawingIcon.ColorGreen);
103: privateNodeIcon = new SelfDrawingIcon(Language
104: .Translate("Private"), SelfDrawingIcon.ColorRed);
105: protectedNodeIcon = new SelfDrawingIcon(Language
106: .Translate("Protected"), SelfDrawingIcon.ColorRed);
107: packageScopeNodeIcon = new SelfDrawingIcon(Language
108: .Translate("Package Scope"), SelfDrawingIcon.ColorRed);
109: defaultNodeIcon = new SelfDrawingIcon(" ",
110: SelfDrawingIcon.ColorBlue);
111: rootNodeIcon = new SelfDrawingIcon(Language
112: .Translate("Components"), SelfDrawingIcon.ColorBlue);
113: }
114:
115: /**
116: * Configures the renderer based on the passed in components.
117: * The value is set from messaging the tree with
118: * <code>convertValueToText</code>, which ultimately invokes
119: * <code>toString</code> on <code>value</code>.
120: * The foreground color is set based on the selection and the icon
121: * is set based on on leaf and expanded.
122: */
123: public Component getTreeCellRendererComponent(JTree tree,
124: Object value, boolean sel, boolean expanded, boolean leaf,
125: int row, boolean hasFocus) {
126: final String stringValue = tree.convertValueToText(value, sel,
127: expanded, leaf, row, hasFocus);
128: this .hasFocus = hasFocus;
129: setText(stringValue);
130: this .selected = sel;
131: if (this .selected)
132: setForeground(getTextSelectionColor());
133: else
134: setForeground(getTextNonSelectionColor());
135:
136: Icon leafIcon = getLeafIcon();
137: Icon nodeOpenIcon = defaultNodeIcon;
138: Icon nodeClosedIcon = defaultNodeIcon;
139: // Some FileComponentsTreeUserObject's have specific node or leaf icons :
140: if (value instanceof DefaultMutableTreeNode) {
141: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
142: if (node.getUserObject() instanceof TokenTreeUserObject) {
143: TokenTreeUserObject userValue = (TokenTreeUserObject) node
144: .getUserObject();
145:
146: // check types :
147: final int iconType = userValue.getIconType();
148: switch (iconType) {
149: // leafs :
150: case UseTokenLeafIcon:
151: leafIcon = tokenLeafIcon;
152: break;
153: case UseImportLeafIcon:
154: leafIcon = importLeafIcon;
155: break;
156: case UseExtendsLeafIcon:
157: leafIcon = extendsLeafIcon;
158: break;
159: case UseImplementsLeafIcon:
160: leafIcon = implements LeafIcon;
161: break;
162: case UseFieldLeafIcon:
163: leafIcon = fieldLeafIcon;
164: break;
165: case UseConstructorLeafIcon:
166: leafIcon = constructorLeafIcon;
167: break;
168: case UseMethodLeafIcon:
169: leafIcon = methodLeafIcon;
170: break;
171: case UseInitializerLeafIcon:
172: leafIcon = initializerLeafIcon;
173: break;
174: case UseClassLeafIcon:
175: leafIcon = classLeafIcon;
176: break;
177: case UseInterfaceLeafIcon:
178: leafIcon = interfaceLeafIcon;
179: break;
180: case UseSuperClassLeafIcon:
181: leafIcon = super ClassLeafIcon;
182: break;
183: case UseParserLeafIcon:
184: leafIcon = parserLeafIcon;
185: break;
186: // nodes :
187: case UsePackageNodeIcon:
188: nodeOpenIcon = packageNodeIcon;
189: nodeClosedIcon = packageNodeIcon;
190: break;
191: case UseImportNodeIcon:
192: nodeOpenIcon = importNodeIcon;
193: nodeClosedIcon = importNodeIcon;
194: break;
195: case UseClassNodeIcon:
196: nodeOpenIcon = classNodeIcon;
197: nodeClosedIcon = classNodeIcon;
198: break;
199: case UseInterfaceNodeIcon:
200: nodeOpenIcon = interfaceNodeIcon;
201: nodeClosedIcon = interfaceNodeIcon;
202: break;
203: case UsePublicNodeIcon:
204: nodeOpenIcon = publicNodeIcon;
205: nodeClosedIcon = publicNodeIcon;
206: break;
207: case UsePrivateNodeIcon:
208: nodeOpenIcon = privateNodeIcon;
209: nodeClosedIcon = privateNodeIcon;
210: break;
211: case UseProtectedNodeIcon:
212: nodeOpenIcon = protectedNodeIcon;
213: nodeClosedIcon = protectedNodeIcon;
214: break;
215: case UsePackageScopeNodeIcon:
216: nodeOpenIcon = packageScopeNodeIcon;
217: nodeClosedIcon = packageScopeNodeIcon;
218: break;
219: }
220: // else use defaultUI icon(s)
221: } else {
222: // else it's the root node :
223: nodeOpenIcon = rootNodeIcon;
224: nodeClosedIcon = rootNodeIcon;
225: }
226: }
227:
228: // There needs to be a way to specify disabled icons.
229: if (!tree.isEnabled()) {
230: setEnabled(false);
231: if (leaf) {
232: setDisabledIcon(leafIcon);
233: } else if (expanded) {
234: setDisabledIcon(nodeOpenIcon);
235: } else {
236: setDisabledIcon(nodeClosedIcon);
237: }
238: } else {
239: setEnabled(true);
240: if (leaf) {
241: setIcon(leafIcon);
242: } else if (expanded) {
243: setIcon(nodeOpenIcon);
244: } else {
245: setIcon(nodeClosedIcon);
246: }
247: }
248: setComponentOrientation(tree.getComponentOrientation());
249: // Set the selected state of the current icon :
250: final Icon currentIcon = this .getIcon();
251: if (currentIcon instanceof SelfDrawingIcon) {
252: final SelfDrawingIcon selfDrawingIcon = (SelfDrawingIcon) currentIcon;
253: selfDrawingIcon.setSelected(this .selected);
254: selfDrawingIcon.setSelectionColor(this
255: .getBackgroundSelectionColor());
256: }
257: return this ;
258: } // getTreeCellRendererComponent
259:
260: public void updateSpecialUI() {
261: setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
262: setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
263: setOpenIcon(UIManager.getIcon("Tree.openIcon"));
264: setTextSelectionColor(UIManager
265: .getColor("Tree.selectionForeground"));
266: setTextNonSelectionColor(UIManager
267: .getColor("Tree.textForeground"));
268: setBackgroundSelectionColor(UIManager
269: .getColor("Tree.selectionBackground"));
270: setBackgroundNonSelectionColor(UIManager
271: .getColor("Tree.textBackground"));
272: setBorderSelectionColor(UIManager
273: .getColor("Tree.selectionBorderColor"));
274: }
275:
276: } // FileComponentsTreeCellRenderer
|