1 4 package com.tc.admin.common; 5 6 import java.awt.event.KeyEvent ; 7 import java.awt.event.MouseAdapter ; 8 import java.awt.event.MouseEvent ; 9 import java.awt.event.MouseListener ; 10 11 import javax.swing.Action ; 12 import javax.swing.ActionMap ; 13 import javax.swing.InputMap ; 14 import javax.swing.JPopupMenu ; 15 import javax.swing.KeyStroke ; 16 import javax.swing.SwingUtilities ; 17 import javax.swing.tree.TreeModel ; 18 import javax.swing.tree.TreePath ; 19 20 public class XTree extends org.dijon.Tree { 21 protected JPopupMenu m_popupMenu; 22 23 public XTree() { 24 super(); 25 26 setShowsRootHandles(true); 27 setRootVisible(false); 28 setCellRenderer(new XTreeCellRendererDelegate()); 29 30 MouseListener ml = new MouseAdapter () { 31 public void mousePressed(MouseEvent e) { 32 testPopup(e); 33 } 34 35 public void mouseReleased(MouseEvent e) { 36 testPopup(e); 37 } 38 39 public void testPopup(MouseEvent e) { 40 if(e.isPopupTrigger()) { 41 TreePath path = getPathForLocation(e.getX(), e.getY()); 42 XTreeNode node = path != null ? (XTreeNode)path.getLastPathComponent() : null; 43 JPopupMenu menu = node != null ? node.getPopupMenu() : XTree.this.getPopupMenu(); 44 45 if(menu != null) { 46 menu.show(XTree.this, e.getX(), e.getY()); 47 } 48 } 49 } 50 }; 51 addMouseListener(ml); 52 } 53 54 public void setPopupMenu(JPopupMenu popupMenu) { 55 if(popupMenu != null) { 56 add(m_popupMenu = popupMenu); 57 } 58 } 59 60 public JPopupMenu getPopupMenu() { 61 return m_popupMenu; 62 } 63 64 private Action getAction(KeyStroke ks) { 65 Action result = null; 66 XTreeNode node = (XTreeNode)getLastSelectedPathComponent(); 67 68 while(node != null) { 69 ActionMap actionMap = node.getActionMap(); 70 71 if(actionMap != null) { 72 InputMap inputMap = node.getInputMap(); 73 74 if(inputMap != null) { 75 Object binding = inputMap.get(ks); 76 77 if(binding != null) { 78 if((result = actionMap.get(binding)) != null) { 79 return result; 80 } 81 } 82 } 83 } 84 85 node = (XTreeNode)node.getParent(); 86 } 87 88 return null; 89 } 90 91 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 92 int condition, boolean pressed) 93 { 94 Action action = getAction(ks); 95 96 if(action != null) { 97 return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers()); 98 } 99 else { 100 return super.processKeyBinding(ks, e, condition, pressed); 101 } 102 } 103 104 public void setModel(TreeModel model) { 105 super.setModel(model); 106 selectTop(); 107 } 108 109 public void selectTop() { 110 if(getRowCount() > 0) { 111 setSelectionRow(0); 112 expandTop(); 113 } 114 } 115 116 public void expandTop() { 117 expandRow(0); 118 } 119 120 public void expandAll() { 121 XRootNode root = (XRootNode)((XTreeModel)getModel()).getRoot(); 122 XTreeNode node = (XTreeNode)root.getFirstLeaf(); 123 XTreeNode parent; 124 125 while(node != null) { 126 parent = (XTreeNode)node.getParent(); 127 128 if(parent != null) { 129 expandPath(new TreePath (parent.getPath())); 130 } 131 132 node = (XTreeNode)node.getNextLeaf(); 133 } 134 135 revalidate(); 136 repaint(); 137 } 138 } 139 | Popular Tags |