1 4 package com.tc.admin.common; 5 6 import java.awt.Toolkit ; 7 import java.awt.event.MouseEvent ; 8 9 import javax.swing.ActionMap ; 10 import javax.swing.Icon ; 11 import javax.swing.InputMap ; 12 import javax.swing.JPopupMenu ; 13 import javax.swing.KeyStroke ; 14 import javax.swing.event.TreeSelectionEvent ; 15 import javax.swing.tree.DefaultMutableTreeNode ; 16 import javax.swing.tree.DefaultTreeModel ; 17 import javax.swing.tree.TreeCellRenderer ; 18 import javax.swing.tree.TreeNode ; 19 20 public class XTreeNode extends DefaultMutableTreeNode { 21 private TreeCellRenderer m_renderer; 22 private Icon m_icon; 23 private ActionMap m_actionMap; 24 private InputMap m_inputMap; 25 26 protected static final int MENU_SHORTCUT_KEY_MASK = 27 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 28 29 public XTreeNode() { 30 super(); 31 } 32 33 public XTreeNode(Object userObject) { 34 super(userObject); 35 } 36 37 public TreeCellRenderer getRenderer() { 38 return m_renderer; 39 } 40 41 public void setRenderer(TreeCellRenderer renderer) { 42 m_renderer = renderer; 43 } 44 45 public Icon getIcon() { 46 return m_icon; 47 } 48 49 public void setIcon(Icon icon) { 50 m_icon = icon; 51 } 52 53 public JPopupMenu getPopupMenu() { 54 return null; 55 } 56 57 public void tearDownChildren() { 58 if(children != null) { 59 XTreeNode node; 60 61 for(int i = getChildCount()-1; i >= 0; i--) { 62 if((node = (XTreeNode)children.get(i)) != null) { 63 node.tearDown(); 64 node.removeFromParent(); 65 } 66 } 67 } 68 } 69 70 public void tearDown() { 71 tearDownChildren(); 72 setUserObject(null); 73 setIcon(null); 74 } 75 76 public ActionMap getActionMap() { 77 return m_actionMap; 78 } 79 80 public ActionMap ensureActionMap() { 81 if(m_actionMap == null) { 82 m_actionMap = new ActionMap (); 83 } 84 85 return m_actionMap; 86 } 87 88 public void setActionMap(ActionMap actionMap) { 89 m_actionMap = actionMap; 90 } 91 92 public InputMap getInputMap() { 93 return m_inputMap; 94 } 95 96 public InputMap ensureInputMap() { 97 if(m_inputMap == null) { 98 m_inputMap = new InputMap (); 99 } 100 101 return m_inputMap; 102 } 103 104 public void setInputMap(InputMap inputMap) { 105 m_inputMap = inputMap; 106 } 107 108 public void addActionBinding(Object binding, XAbstractAction action) { 109 ensureActionMap().put(binding, action); 110 111 KeyStroke ks = action.getAccelerator(); 112 if(ks != null) { 113 ensureInputMap().put(ks, binding); 114 } 115 } 116 117 public XTreeModel getModel() { 118 TreeNode root = getRoot(); 119 XTreeModel model = null; 120 121 if(root instanceof XRootNode) { 122 model = ((XRootNode)getRoot()).getModel(); 123 } 124 125 return model; 126 } 127 128 public int getIndex() { 129 XTreeNode parentNode = (XTreeNode)getParent(); 130 return (parentNode != null) ? parentNode.getIndex(this) : -1; 131 } 132 133 public void nodeSelected(TreeSelectionEvent e) {} 134 public void nodeClicked(MouseEvent me) {} 135 136 protected void nodeChanged() { 137 DefaultTreeModel model = getModel(); 138 if(model != null) { 139 model.nodeChanged(this); 140 } 141 } 142 143 protected void nodeStructureChanged() { 144 DefaultTreeModel model = getModel(); 145 if(model != null) { 146 model.nodeStructureChanged(this); 147 } 148 } 149 } 150 | Popular Tags |