1 package com.genimen.djeneric.tools.scriptengine.test; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.awt.Font ; 6 import java.awt.Graphics ; 7 8 import javax.swing.Icon ; 9 import javax.swing.ImageIcon ; 10 import javax.swing.JLabel ; 11 import javax.swing.JTree ; 12 import javax.swing.tree.DefaultMutableTreeNode ; 13 import javax.swing.tree.DefaultTreeModel ; 14 import javax.swing.tree.TreeCellRenderer ; 15 16 import com.genimen.djeneric.tools.scriptengine.core.Node; 17 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode; 18 19 public class ParseTreeNode extends DefaultMutableTreeNode 20 { 21 private static final long serialVersionUID = 1L; 22 protected JTree _tree; 23 protected SimpleNode _node; 24 25 public ParseTreeNode(JTree tree, SimpleNode node) 26 { 27 _tree = tree; 28 _node = node; 29 } 30 31 public ParseTreeNode() 32 { 33 _tree = null; 34 _node = null; 35 } 36 37 public void setTree(JTree tree) 38 { 39 _tree = tree; 40 } 41 42 public String toString() 43 { 44 if (_node == null) return ""; 45 return _node.toString(); 46 } 47 48 public DefaultTreeModel getModel() 49 { 50 return (DefaultTreeModel ) _tree.getModel(); 51 } 52 53 public void insertAsChild(ParseTreeNode node) 54 { 55 getModel().insertNodeInto(node, this, getChildCount()); 56 } 57 58 public Node getNode() 59 { 60 return _node; 61 } 62 } 63 64 class ParseTreeCellRenderer extends JLabel implements TreeCellRenderer 65 { 66 private static final long serialVersionUID = 1L; 67 static protected Font _defaultFont; 68 static protected final Color _selectedBackgroundColor = Color.yellow; static ImageIcon _icon = null; 70 71 static 72 { 73 _defaultFont = new Font ("SansSerif", 0, 12); 74 } 75 76 77 protected boolean _selected; 78 79 85 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 86 boolean leaf, int row, boolean hasFocus) 87 { 88 if (value == null) return this; 89 String stringValue = value.toString(); 90 setText(stringValue); 91 setToolTipText(stringValue); 92 93 if (_icon == null) _icon = DjScriptParser.getImageIcon("node.gif"); 94 setIcon(_icon); 95 96 97 _selected = selected; 98 99 return this; 100 } 101 102 107 public void paint(Graphics g) 108 { 109 Color bColor; 110 Icon currentI = getIcon(); 111 112 if (_selected) 113 { 114 bColor = _selectedBackgroundColor; 115 } 116 else if (getParent() != null) 117 { bColor = getParent().getBackground(); 119 } 120 else 121 { 122 bColor = getBackground(); 123 } 124 g.setColor(bColor); 125 126 if (currentI != null && getText() != null) 127 { 128 int offset = (currentI.getIconWidth() + getIconTextGap()); 129 g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); 130 } 131 else 132 { 133 g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 134 } 135 super.paint(g); 136 } 137 } 138 | Popular Tags |