001: package tide.syntaxtree;
002:
003: import javaparser.*;
004: import tide.editor.UIConstants;
005: import java.io.*;
006: import java.util.*;
007: import java.awt.*;
008: import java.awt.event.*;
009: import javax.swing.*;
010: import javax.swing.tree.*;
011: import javax.swing.event.*;
012: import javax.swing.border.*;
013:
014: /** Shared for all syntax trees.
015: */
016: public class SyntaxTreeRenderer extends DefaultTreeCellRenderer {
017: final SyntaxTreeIcon icon = new SyntaxTreeIcon();
018:
019: // an utility to "preselect" tree nodes.
020: // Used in search
021: public final Set<TreeNode> preselectedNodes = new HashSet<TreeNode>();
022:
023: // TODO.
024: //public TreeNode nodeContainingCursor = null;
025:
026: public SyntaxTreeRenderer() {
027: super ();
028: this .setIcon(icon);
029: }
030:
031: /*
032: public void updateUI()
033: {
034: super.updateUI();
035: // JDK bug ??
036: // not working well on theme change, the events come, but are not reflected well to parent
037: // 1) the DefaultTreeCellRenderer is not really in the tree, only his paint is called
038: // 2) on updateUI, DefaultTreeCellRenderer seems to ignore the new values
039: // (only set in the constructor, from UIManager ...
040: }*/
041:
042: @Override
043: public Component getTreeCellRendererComponent(JTree tree,
044: Object value, boolean selected, boolean expanded,
045: boolean leaf, int row, boolean hasFocus) {
046: // call super
047: JLabel comp = (JLabel) super .getTreeCellRendererComponent(tree,
048: value, selected, expanded, leaf, row, hasFocus);
049: comp.setIcon(icon);
050: // if( value instanceof SourceFile)
051: ParserTreeNode ptn = (ParserTreeNode) value;
052: icon.text = "";
053: icon.isMain = false;
054: comp.setText(ptn.toString());
055:
056: if (ptn instanceof ClassNode) {
057: ClassNode cn = (ClassNode) ptn;
058: icon.text = cn.classOrInterface;
059: icon.letterColor = UIConstants.blue;
060: icon.width = 55;
061: }
062: /* else if(ptn instanceof MainModifierNode)
063: {
064: MainModifierNode mmn = (MainModifierNode) ptn;
065: icon.text = ptn.toString();
066: icon.letterColor = mmn.iconColor;
067:
068: icon.width = 95;
069: comp.setText("");
070: }*/
071: else if (ptn instanceof MethodNode) {
072: MethodNode met = (MethodNode) ptn;
073: icon.text = "M" + met.modifiersShort;
074: icon.letterColor = Color.black;
075: icon.isMain = met.isPublicStaticMainMethod();
076:
077: /*if(met.isPublicStaticMainMethod())
078: {
079: icon.letterColor = UIConstants.green;
080: }*/
081:
082: icon.width = 30;
083: } else if (ptn instanceof FieldNode) {
084: icon.text = "F" + ((FieldNode) ptn).modifiersShort;
085: icon.letterColor = Color.black;
086: icon.width = 30;
087: } else if (ptn instanceof ConstructorNode) {
088: icon.text = "C" + ((ConstructorNode) ptn).modifiersShort;
089: icon.letterColor = Color.black;
090: icon.width = 30;
091: } else if (ptn instanceof EnumNode) {
092: icon.text = "enum";
093: icon.letterColor = UIConstants.blue;
094: icon.width = 55;
095: } else if (ptn instanceof AnnotationDeclNode) {
096: icon.text = "annotation";
097: icon.letterColor = UIConstants.blue;
098: icon.width = 75;
099: } else if (ptn instanceof WarningNode) {
100: MainNode mn = (WarningNode) ptn;
101: icon.text = mn.iconName;
102: icon.letterColor = mn.iconColor;
103: icon.width = 25;
104: } else if (ptn instanceof ErrorNode) {
105: MainNode mn = (ErrorNode) ptn;
106: icon.text = "";
107: icon.letterColor = mn.iconColor;
108: icon.width = 1;
109: }
110:
111: else if (ptn instanceof MainNode) {
112: MainNode mn = (MainNode) ptn;
113: icon.text = mn.iconName;
114: icon.letterColor = mn.iconColor;
115: icon.width = 75;
116: } else if (ptn instanceof AnnotationMemberNode) {
117: icon.text = "M";
118: icon.letterColor = Color.black;
119: icon.width = 20;
120: } else {
121: comp.setIcon(null);
122: icon.text = "";
123: }
124:
125: if (preselectedNodes.contains(ptn)) {
126:
127: comp.setBorder(UIManager.getBorder("Tree.editorBorder"));
128: } else {
129: comp.setBorder(null);
130: }
131:
132: if (ptn instanceof NodeWithMod) {
133: NodeWithMod nm = (NodeWithMod) ptn;
134: if (nm.isPublic()) {
135: icon.letterColor = UIConstants.green;
136: } else if (nm.isPrivate()) {
137: icon.letterColor = UIConstants.red;
138: } else if (nm.isProtected()) {
139: icon.letterColor = UIConstants.brown;
140: } else {
141: // default= package scope
142: icon.letterColor = Color.black;
143: }
144:
145: }
146:
147: return comp;
148: }
149:
150: }
|