1 package com.genimen.djeneric.tools.modeler.userperspective; 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.modeler.ModelEditor; 17 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode; 18 19 public class ScriptStructureTreeNode extends DefaultMutableTreeNode 20 { 21 private static final long serialVersionUID = 1L; 22 protected JTree _tree; 23 protected SimpleNode _node; 24 protected String _title = ""; 25 26 public ScriptStructureTreeNode(JTree tree, SimpleNode node) 27 { 28 _tree = tree; 29 _node = node; 30 } 31 32 public ScriptStructureTreeNode() 33 { 34 _tree = null; 35 _node = null; 36 } 37 38 public ScriptStructureTreeNode(String title) 39 { 40 this(); 41 _title = title; 42 } 43 44 public void setTree(JTree tree) 45 { 46 _tree = tree; 47 } 48 49 public String toString() 50 { 51 if (_node == null) return _title; 52 return _node.getNodeTitle(); 53 } 54 55 public DefaultTreeModel getModel() 56 { 57 return (DefaultTreeModel ) _tree.getModel(); 58 } 59 60 public void insertAsChild(ScriptStructureTreeNode node) 61 { 62 getModel().insertNodeInto(node, this, getChildCount()); 63 } 64 65 public SimpleNode getNode() 66 { 67 return _node; 68 } 69 70 public String getTitle() 71 { 72 return _title; 73 } 74 75 public void setTitle(String string) 76 { 77 _title = string; 78 } 79 80 } 81 82 class ParseTreeCellRenderer extends JLabel implements TreeCellRenderer 83 { 84 private static final long serialVersionUID = 1L; 85 static protected Font _defaultFont; 86 static protected final Color _selectedBackgroundColor = Color.yellow; static ImageIcon _icon = null; 88 89 static 90 { 91 _defaultFont = new Font ("SansSerif", 0, 12); 92 } 93 94 95 protected boolean _selected; 96 97 103 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 104 boolean leaf, int row, boolean hasFocus) 105 { 106 if (value == null) return this; 107 String stringValue = value.toString(); 108 setText(stringValue); 109 setToolTipText(stringValue); 110 111 if (_icon == null) _icon = ModelEditor.getImageIcon("node.gif"); 112 setIcon(_icon); 113 114 115 _selected = selected; 116 117 return this; 118 } 119 120 125 public void paint(Graphics g) 126 { 127 Color bColor; 128 Icon currentI = getIcon(); 129 130 if (_selected) 131 { 132 bColor = _selectedBackgroundColor; 133 } 134 else if (getParent() != null) 135 { bColor = getParent().getBackground(); 137 } 138 else 139 { 140 bColor = getBackground(); 141 } 142 g.setColor(bColor); 143 144 if (currentI != null && getText() != null) 145 { 146 int offset = (currentI.getIconWidth() + getIconTextGap()); 147 g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); 148 } 149 else 150 { 151 g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 152 } 153 super.paint(g); 154 } 155 } | Popular Tags |