1 22 package org.jboss.console.navtree; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.MouseAdapter ; 27 import java.awt.event.MouseEvent ; 28 import java.util.HashMap ; 29 30 import javax.swing.JComponent ; 31 import javax.swing.JMenuItem ; 32 import javax.swing.JPopupMenu ; 33 import javax.swing.JTree ; 34 import javax.swing.ToolTipManager ; 35 import javax.swing.event.TreeSelectionListener ; 36 import javax.swing.tree.TreeModel ; 37 import javax.swing.tree.TreePath ; 38 import javax.swing.tree.TreeSelectionModel ; 39 40 import org.jboss.console.manager.interfaces.SimpleTreeNodeMenuEntry; 41 import org.jboss.console.manager.interfaces.TreeAction; 42 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry; 43 import org.jboss.console.manager.interfaces.impl.SeparatorTreeNodeMenuEntry; 44 45 58 59 public class AdminTreeBrowser 60 { 61 TreeContext ctx = null; 62 63 ConsoleTreeModel tm = null; 64 TreeCellRenderer cellRenderer = null; 65 TreeSelectionListener selectionListener = null; 66 OpenNodeAccounter openNodeAccounter = null; 67 68 String webHost = null; 69 70 public static final String RIGHT_FRAME_NAME = "right"; 71 72 73 public AdminTreeBrowser (TreeContext ctx) throws Exception 74 { 75 this.ctx = ctx; 76 77 tm = new ConsoleTreeModel (ctx); 78 cellRenderer = new TreeCellRenderer (ctx); 79 81 initComponents (); 82 83 openNodeAccounter = new OpenNodeAccounter(getTree()); 84 85 getTree().addMouseListener (new PopupMenuMgr()); 87 getTree().getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); 89 ToolTipManager.sharedInstance().registerComponent(getTree()); 90 } 91 92 public void refreshTree (boolean force) 93 { 94 try 95 { 96 this.tm.refreshTree (force); 97 } 98 catch (Exception displayed) 99 { 100 displayed.printStackTrace(); 101 } 102 } 103 104 105 110 private void initComponents() { 112 tree = new javax.swing.JTree (); 113 114 tree.setCellRenderer(getCellRenderer()); 115 tree.setModel(getTreeModel()); 116 tree.setAutoscrolls(true); 117 118 } 120 121 private javax.swing.JTree tree; 123 125 public TreeCellRenderer getCellRenderer () 126 { 127 return this.cellRenderer; 128 } 129 130 public TreeModel getTreeModel () 131 { 132 return this.tm; 133 } 134 135 public TreeSelectionListener getSelectionListener () 136 { 137 return this.selectionListener; 138 } 139 140 public JTree getTree () 141 { 142 return this.tree; 143 } 144 145 171 172 174 public class OpenNodeAccounter 175 implements javax.swing.event.TreeExpansionListener , javax.swing.event.TreeModelListener 176 { 177 protected java.util.TreeSet openNodes = new java.util.TreeSet (); 178 179 public OpenNodeAccounter (JTree tree) 180 { 181 tree.getModel ().addTreeModelListener (this); 182 } 183 184 public void treeExpanded(javax.swing.event.TreeExpansionEvent event) 185 { 186 openNodes.add (((NodeWrapper)(event.getSource ())).getPath ()); 187 } 190 191 public void treeCollapsed(javax.swing.event.TreeExpansionEvent event) 192 { 193 openNodes.remove (((NodeWrapper)(event.getSource ())).getPath ()); 194 } 196 197 public void treeNodesChanged (javax.swing.event.TreeModelEvent e) 198 { 199 207 217 218 } 219 220 public void recursivelyOpen (NodeWrapper son) 221 { 222 } 223 224 public void treeNodesInserted (javax.swing.event.TreeModelEvent e){} 225 public void treeNodesRemoved (javax.swing.event.TreeModelEvent e){} 226 public void treeStructureChanged (javax.swing.event.TreeModelEvent e) { } 227 228 } 229 230 232 public class PopupMenuMgr extends MouseAdapter 233 { 234 HashMap menus = new HashMap (); 235 236 public PopupMenuMgr (){} 237 238 public void mousePressed( MouseEvent e ) 239 { 240 mouseReleased(e); 241 } 242 243 public void mouseReleased( MouseEvent e ) 244 { 245 TreePath loc = getTree().getPathForLocation(e.getX(), e.getY()); 246 if (loc == null) return; 249 getTree().setSelectionPath (loc); 250 251 if ( e.isPopupTrigger()) 252 { 253 Object node = getTree ().getLastSelectedPathComponent (); 254 if (node == null) return; 255 256 JPopupMenu popup = null; 257 if (menus.containsKey (node)) 258 { 259 popup = (JPopupMenu )menus.get (node); 260 } 261 else if (node instanceof NodeWrapper) 262 { 263 NodeWrapper who = (NodeWrapper)node; 264 265 TreeNodeMenuEntry[] entries = who.getMenuEntries (); 266 267 if (entries != null && entries.length > 0) 268 { 269 popup = new JPopupMenu (); 270 popup.setOpaque(true); 271 popup.setLightWeightPopupEnabled(true); 272 menus.put (node, popup); 273 274 for (int i=0; i<entries.length; i++) 277 { 278 if (entries[i] instanceof SeparatorTreeNodeMenuEntry) 279 { 280 popup.addSeparator (); 281 } 282 else if (entries[i] instanceof SimpleTreeNodeMenuEntry) 283 { 284 final SimpleTreeNodeMenuEntry txt = (SimpleTreeNodeMenuEntry)entries[i]; 285 JMenuItem mi = new JMenuItem (txt.getText ()); 286 mi.addActionListener ( new ActionListener () 287 { 288 public void actionPerformed(ActionEvent e) 289 { 290 ctx.doPopupMenuAction (txt); 291 } 292 } 293 ); 294 popup.add(mi); 295 } 296 } 297 } 298 299 } 300 301 if (popup != null) 302 popup.show( (JComponent )e.getSource(), e.getX(), e.getY() ); 303 } 304 else 305 { 306 Object node = getTree ().getLastSelectedPathComponent (); 307 308 if (node != null && node instanceof NodeWrapper) 309 { 310 NodeWrapper who = (NodeWrapper)node; 311 TreeAction act = who.getAssociatedAction (); 312 ctx.doAdminTreeAction (act); 313 } 314 315 } 316 } 317 } 318 } 319 | Popular Tags |