SyntaxTreeRenderer.java :  » IDE » tIDE » tide » syntaxtree » Java Open Source

Java Open Source » IDE » tIDE 
tIDE » tide » syntaxtree » SyntaxTreeRenderer.java
package tide.syntaxtree;

import javaparser.*;
import tide.editor.UIConstants;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;

/** Shared for all syntax trees.
*/
public class SyntaxTreeRenderer extends DefaultTreeCellRenderer
{
  final SyntaxTreeIcon icon = new SyntaxTreeIcon();

  // an utility to "preselect" tree nodes.
  //  Used in search
  public final Set<TreeNode> preselectedNodes = new HashSet<TreeNode>();
  // TODO.
  //public TreeNode nodeContainingCursor = null;

  public SyntaxTreeRenderer()
  {
     super();
     this.setIcon(icon);
  }

/*
  public void updateUI()
  {
     super.updateUI();
     // JDK bug ??
     //   not working well on theme change, the events come, but are not reflected well to parent
     //   1) the DefaultTreeCellRenderer is not really in the tree, only his paint is called
     //   2) on updateUI, DefaultTreeCellRenderer seems to ignore the new values
     //      (only set in the constructor, from UIManager ...
  }*/

@Override
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
  {
     // call super
     JLabel comp = (JLabel) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
     comp.setIcon(icon);
     //  if( value instanceof SourceFile)
     ParserTreeNode ptn = (ParserTreeNode) value;
     icon.text = "";
     icon.isMain = false;
     comp.setText( ptn.toString() );

     if(ptn instanceof ClassNode)
     {
        ClassNode cn = (ClassNode) ptn;
        icon.text = cn.classOrInterface;
        icon.letterColor = UIConstants.blue;
        icon.width = 55;
     }
    /* else if(ptn instanceof MainModifierNode)
     {
        MainModifierNode mmn = (MainModifierNode) ptn;
        icon.text = ptn.toString();
        icon.letterColor = mmn.iconColor;

        icon.width = 95;
        comp.setText("");
     }*/
     else if(ptn instanceof MethodNode)
     {
        MethodNode met = (MethodNode) ptn;
        icon.text = "M"+met.modifiersShort;
        icon.letterColor = Color.black;
        icon.isMain = met.isPublicStaticMainMethod();

        /*if(met.isPublicStaticMainMethod())
        {
           icon.letterColor = UIConstants.green;
        }*/

        icon.width = 30;
     }
     else if(ptn instanceof FieldNode)
     {
        icon.text = "F"+((FieldNode) ptn).modifiersShort;
        icon.letterColor = Color.black;
        icon.width = 30;
     }
     else if(ptn instanceof ConstructorNode)
     {
        icon.text = "C"+((ConstructorNode) ptn).modifiersShort;
        icon.letterColor = Color.black;
        icon.width = 30;
     }
     else if(ptn instanceof EnumNode)
     {
        icon.text = "enum";
        icon.letterColor = UIConstants.blue;
        icon.width = 55;
     }
     else if(ptn instanceof AnnotationDeclNode)
     {
        icon.text = "annotation";
        icon.letterColor = UIConstants.blue;
        icon.width = 75;
     }
     else if(ptn instanceof WarningNode)
     {
        MainNode mn = (WarningNode) ptn;
        icon.text = mn.iconName;
        icon.letterColor = mn.iconColor;
        icon.width = 25;
     }
     else if(ptn instanceof ErrorNode)
     {
        MainNode mn = (ErrorNode) ptn;
        icon.text = "";
        icon.letterColor = mn.iconColor;
        icon.width = 1;
     }

     else if(ptn instanceof MainNode)
     {
        MainNode mn = (MainNode) ptn;
        icon.text = mn.iconName;
        icon.letterColor = mn.iconColor;
        icon.width = 75;
     }
     else if(ptn instanceof AnnotationMemberNode)
     {
        icon.text = "M";
        icon.letterColor = Color.black;
        icon.width = 20;
     }
     else
     {
        comp.setIcon(null);
        icon.text = "";
     }

     if(preselectedNodes.contains(ptn))
     {

        comp.setBorder(UIManager.getBorder("Tree.editorBorder"));
     }
     else
     {
        comp.setBorder(null);
     }


     if(ptn instanceof NodeWithMod)
     {
        NodeWithMod nm = (NodeWithMod) ptn;
        if(nm.isPublic())
        {
           icon.letterColor = UIConstants.green;
        }
        else if(nm.isPrivate())
        {
           icon.letterColor = UIConstants.red;
        }
        else if(nm.isProtected())
        {
           icon.letterColor = UIConstants.brown;
        }
        else
        {
           // default= package scope
           icon.letterColor = Color.black;
        }

     }



     return comp;
  }



}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.