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