| 1 26 27 package org.objectweb.util.browser.core.common; 28 29 30 import org.objectweb.util.browser.core.naming.DefaultEntry; 31 import org.objectweb.util.browser.core.naming.DefaultName; 32 import org.objectweb.util.browser.core.naming.InitialContextContainer; 33 import org.objectweb.util.browser.core.naming.InitialContextContainerFactory; 34 import org.objectweb.util.browser.core.naming.InitialEntry; 35 import org.objectweb.util.browser.api.Context; 36 import org.objectweb.util.browser.api.DropAction; 37 import org.objectweb.util.browser.api.Entry; 38 import org.objectweb.util.browser.api.Table; 39 import org.objectweb.util.browser.api.Tree; 40 import org.objectweb.util.browser.api.TreeView; 41 import org.objectweb.util.browser.api.Panel; 42 import org.objectweb.util.browser.api.Wrapper; 43 import org.objectweb.util.browser.api.Info; 44 import org.objectweb.util.browser.core.api.BrowserProperty; 45 import org.objectweb.util.browser.core.api.ContextContainer; 46 import org.objectweb.util.browser.core.api.ContextContainerFactory; 47 import org.objectweb.util.browser.core.api.ContextProperty; 48 import org.objectweb.util.browser.core.api.ExtendedEntry; 49 import org.objectweb.util.browser.core.api.Role; 50 import org.objectweb.util.browser.core.api.RoleManagement; 51 import org.objectweb.util.browser.core.api.StatusBar; 52 import org.objectweb.util.browser.core.api.ViewPanel; 53 import org.objectweb.util.browser.core.api.TreeConfiguration; 54 import org.objectweb.util.browser.core.dnd.DefaultDropTreeView; 55 import org.objectweb.util.browser.core.dnd.EntryViewTransferable; 56 import org.objectweb.util.browser.core.panel.WhitePanel; 57 import org.objectweb.util.browser.core.xmlparser.ContextXMLParser; 58 import org.objectweb.util.browser.core.xmlparser.XMLParser; 59 import org.objectweb.util.browser.core.xmlparser.BrowserXMLParser; 60 61 import org.objectweb.util.browser.core.popup.DefaultAction; 62 63 64 import java.util.Arrays ; 65 import java.util.Comparator ; 66 import java.util.Enumeration ; 67 import java.util.HashMap ; 68 import java.util.Iterator ; 69 import java.util.List ; 70 import java.util.Map ; 71 import java.util.Vector ; 72 import java.io.IOException ; 73 74 75 import java.awt.datatransfer.DataFlavor ; 76 import java.awt.datatransfer.Transferable ; 77 import java.awt.datatransfer.UnsupportedFlavorException ; 78 import java.awt.dnd.Autoscroll ; 79 import java.awt.dnd.DnDConstants ; 80 import java.awt.dnd.DragGestureEvent ; 81 import java.awt.dnd.DragGestureListener ; 82 import java.awt.dnd.DragSource ; 83 import java.awt.dnd.DragSourceContext ; 84 import java.awt.dnd.DragSourceDragEvent ; 85 import java.awt.dnd.DragSourceDropEvent ; 86 import java.awt.dnd.DragSourceEvent ; 87 import java.awt.dnd.DragSourceListener ; 88 import java.awt.dnd.DropTarget ; 89 import java.awt.dnd.DropTargetDragEvent ; 90 import java.awt.dnd.DropTargetDropEvent ; 91 import java.awt.dnd.DropTargetEvent ; 92 import java.awt.dnd.DropTargetListener ; 93 import java.awt.dnd.InvalidDnDOperationException ; 94 95 96 import javax.swing.AbstractAction ; 97 import javax.swing.ButtonGroup ; 98 import javax.swing.JCheckBoxMenuItem ; 99 import javax.swing.JMenu ; 100 import javax.swing.JMenuBar ; 101 import javax.swing.JMenuItem ; 102 import javax.swing.JOptionPane ; 103 import javax.swing.JRadioButtonMenuItem ; 104 import javax.swing.JToolBar ; 105 import javax.swing.JTree ; 106 import javax.swing.JPopupMenu ; 107 import javax.swing.Icon ; 108 import javax.swing.event.TreeWillExpandListener ; 109 import javax.swing.event.TreeSelectionListener ; 110 import javax.swing.event.TreeSelectionEvent ; 111 import javax.swing.event.TreeExpansionEvent ; 112 import javax.swing.event.TreeExpansionListener ; 113 114 import java.awt.Dimension ; 115 import java.awt.Insets ; 116 import java.awt.Point ; 117 import java.awt.Component ; 118 import java.awt.Rectangle ; 119 import java.awt.event.ActionEvent ; 120 import java.awt.event.MouseAdapter ; 121 import java.awt.event.MouseEvent ; 122 123 124 import javax.swing.tree.DefaultMutableTreeNode ; 125 import javax.swing.tree.DefaultTreeModel ; 126 import javax.swing.tree.DefaultTreeSelectionModel ; 127 import javax.swing.tree.DefaultTreeCellRenderer ; 128 import javax.swing.tree.TreeNode ; 129 import javax.swing.tree.TreePath ; 130 import javax.swing.tree.TreeSelectionModel ; 131 132 140 public class DynamicTree 141 extends JTree 142 implements TreeConfiguration, Tree, Autoscroll , RoleManagement { 143 144 150 151 protected AdminCustomization custom; 152 153 154 protected ViewPanel viewPanel_; 155 156 157 protected DefaultMutableTreeNode rootNode; 158 159 160 protected ContextContainer graphicalInitialContext_; 161 162 163 protected Vector initialContext_; 164 165 166 protected DefaultTreeModel treeModel; 167 168 169 protected DefaultTreeSelectionModel selectionModel; 170 171 172 protected DefaultTreeCellRenderer treeCellRenderer; 173 174 175 protected Map nodeState_; 176 177 178 protected String selectedNode_; 179 180 181 protected TreePath selectedTreePath_; 182 183 184 protected Panel blankPanel_; 185 186 187 protected ContextProperty contextProperty_; 188 189 190 protected XMLParser browserProperty_; 191 192 193 protected TreeView currentTreeView_; 194 195 196 protected boolean popupEnabled_; 197 198 199 protected boolean dndEnabled_; 200 201 202 protected boolean multipleRolesEnabled_; 203 204 205 protected StatusBar statusBar_; 206 207 208 protected JMenuBar menuBar_; 209 210 211 protected JToolBar toolBar_ = null; 212 213 214 protected int nbElementsToRemove_ = 0; 215 216 217 protected JMenu actionMenu_, roleMenu_; 218 219 220 protected JPopupMenu currentJPopupMenu_ = null; 221 222 223 protected Vector alreadyInInitialContext_ = null; 224 225 227 228 protected DragSource dragSource_; 229 230 231 protected DragGestureListener dragGestureListener_; 232 233 234 protected DragSourceListener dragSourceListener_; 235 236 237 protected int dragAction_ = DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK; 238 239 240 protected int acceptableActions_ = DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK; 241 242 243 protected DropTarget dropTarget_; 244 245 246 protected DropTargetListener dropTargetListener_; 247 248 254 257 private void buildDnD(){ 258 dragSource_ = DragSource.getDefaultDragSource(); 260 dragSourceListener_ = new TreeDragSourceListener(); 261 dragGestureListener_ = new TreeDragGestureListener(); 262 dragSource_.createDefaultDragGestureRecognizer(this, dragAction_, dragGestureListener_); 263 264 dropTargetListener_ = new TreeDropTargetListener(); 266 dropTarget_ = new DropTarget (this, acceptableActions_, dropTargetListener_, true); 267 } 268 269 274 private void build(ContextContainer initialContext, boolean initAll) { 275 graphicalInitialContext_ = initialContext; 276 if(initialContext instanceof InitialContextContainer) 277 ((InitialContextContainer)initialContext).setDynamicTree(this); 278 279 rootNode = new DefaultMutableTreeNode (new DefaultEntry(graphicalInitialContext_,new DefaultName("Root"),null)); 280 treeModel = new MyTreeModel(rootNode); 281 setModel(treeModel); 282 283 if (initAll) { 284 nodeState_ = new HashMap (); 285 initialContext_ = new Vector (); 286 alreadyInInitialContext_ = new Vector (); 287 selectedNode_ = ""; 288 blankPanel_ = new WhitePanel(); 289 selectionModel = new DefaultTreeSelectionModel (); 290 selectionModel.setSelectionMode( 291 TreeSelectionModel.SINGLE_TREE_SELECTION); 292 setSelectionModel(selectionModel); 293 addMouseListener(new MyMouseAdapter()); 294 addTreeWillExpandListener(new MyTreeWillExpandListener()); 295 addTreeExpansionListener(new MyTreeExpansionListener()); 296 addTreeSelectionListener(new MyTreeSelectionListener()); 297 setCellRenderer(new MyTreeCellRenderer()); 298 popupEnabled_ = true; 299 dndEnabled_ = true; 300 multipleRolesEnabled_ = false; 301 buildDnD(); 302 setToolTipText(""); 303 } 304 305 setShowsRootHandles(true); 306 collapseRow(0); 307 expandRow(0); 308 309 setRootVisible(false); 310 } 311 312 315 public DynamicTree() { 316 ContextContainerFactory ccf = new InitialContextContainerFactory(null); 317 build(ccf.create(), true); 318 } 319 320 324 public DynamicTree(ContextContainer initialContext) { 325 build(initialContext, true); 326 } 327 328 334 340 protected DefaultMutableTreeNode addObject( 341 DefaultMutableTreeNode parent, 342 ExtendedEntry child) { 343 if (custom != null) { 344 Context context = custom.getContext(child.getValue()); 345 if (context != null) { 346 child.setOWValue(context); 347 child.setWrappedObject(((Wrapper) context).getWrapped()); 348 } 349 } 350 DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (child); 351 if (parent == null) { 352 parent = rootNode; 353 } 354 treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); 355 356 return childNode; 357 } 358 359 366 protected String pathToString(DefaultMutableTreeNode node) { 367 StringBuffer sf = new StringBuffer (); 368 Object [] o = node.getUserObjectPath(); 369 for (int i = 0; i < o.length; i++) { 370 sf.append(((Entry) o[i]).getName().toString()); 371 if (i != o.length - 1) 372 sf.append("/"); 373 } 374 return sf.toString(); 375 } 376 377 382 protected Entry objectToEntry(Object o) { 383 if (o != null) { 384 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) o; 385 return (Entry) node.getUserObject(); 386 } 387 return null; 388 } 389 390 394 protected TreePath getPath(Point p){ 395 if(p!=null){ 396 return getPathForLocation((int) p.getX(), (int) p.getY()); 397 } 398 return null; 399 } 400 401 405 protected void showMenu(Point p) { 406 TreePath path = getPath(p); 407 if (path != null) { 408 selectionModel.setSelectionPath(path); 409 Entry entry = objectToEntry(getLastSelectedPathComponent()); 410 if (custom != null) { 411 JPopupMenu menu = custom.getMenu((ExtendedEntry) entry); 412 if (menu != null) { 413 menu.show((Component ) this,(int) (p.getX()),(int) (p.getY())); 414 } 415 } 416 } 417 } 418 419 424 protected TreePath getPathFromInitialContext(String id) { 425 if (id != null) { 426 Enumeration children = rootNode.children(); 427 while (children.hasMoreElements()) { 428 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) children.nextElement(); 429 Entry entry = (Entry) childNode.getUserObject(); 430 if (entry.getName().toString().equals(id)) 431 return new TreePath (childNode.getPath()); 432 } 433 } 434 return null; 435 } 436 437 440 protected void createGraphicalInitialContext() { 441 graphicalInitialContext_.clear(); 443 if(custom!=null){ 444 Entry[] entries = custom.getRoot(); 446 for (int i = 0 ; i < entries.length ; i++){ 447 InitialEntry initialEntry = null; 448 try{ 449 initialEntry = (InitialEntry)entries[i]; 450 } catch (ClassCastException e) { 451 initialEntry = new InitialEntry(entries[i].getName().toString(), entries[i].getValue(), 0); 452 } 453 addToInitialContext(initialEntry.getName().toString(), initialEntry.getValue(), initialEntry.getLevel()); 454 } 455 } 456 InitialEntry entryToAdd = null; 458 for(int i=0 ; i<initialContext_.size() ; i++) { 459 entryToAdd = (InitialEntry)initialContext_.get(i); 460 addToInitialContext(entryToAdd.getName().toString(), entryToAdd.getValue(), entryToAdd.getLevel()); 461 } 462 463 } 464 465 471 protected void openTree(TreePath root, int level) { 472 if (level > 0) { 473 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) root.getLastPathComponent(); 474 Enumeration children = node.children(); 475 while (children.hasMoreElements()) { 476 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode ) children.nextElement(); 477 TreePath p = new TreePath (childNode.getPath()); 478 expandPath(p); 479 openTree(p, level - 1); 480 } 481 } 485 } 486 487 490 protected void refreshPanel(){ 491 if(viewPanel_!=null){ 492 Panel currentPanel = viewPanel_.getViewPanel(); 494 if(currentPanel!=null && currentTreeView_!=null) 495 currentPanel.unselected(currentTreeView_); 496 Entry entry = null; 497 Object object = getLastSelectedPathComponent(); 498 String newSelectedNode = null; 499 if (object != null) { 500 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 501 if (!node.equals(rootNode)) { 502 newSelectedNode = pathToString(node); 503 } 504 entry = (Entry) node.getUserObject(); 505 } 506 if (newSelectedNode==null || newSelectedNode.equals("")) { 507 viewPanel_.setViewPanel(blankPanel_); 508 } else if (selectedNode_ != null && newSelectedNode != null && !selectedNode_.equals(newSelectedNode)) { 509 selectedNode_ = newSelectedNode; 511 Panel panel = null; 513 Table table = null; 514 if (entry != null && custom != null) { 515 516 panel = custom.getPanel((ExtendedEntry) entry); 517 if (panel != null) { 518 currentTreeView_ = new DefaultTreeView(this); 519 panel.selected(currentTreeView_); 520 viewPanel_.setViewPanel(panel); 521 } 522 523 550 } 551 if (entry == null || custom == null || panel == null) { 552 viewPanel_.setViewPanel(blankPanel_); 553 } 554 } 555 } 556 } 557 558 561 protected void refreshStatusBar(){ 562 if(statusBar_!=null){ 563 Entry entry = null; 564 Object object = getLastSelectedPathComponent(); 565 if (object != null) { 566 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 567 entry = (Entry) node.getUserObject(); 568 } 569 if (entry != null && custom != null) { 570 Info info = custom.getInfo((ExtendedEntry) entry); 571 if (info != null) { 572 statusBar_.setText(info.getInfo(new DefaultTreeView(this))); 573 } else { 574 statusBar_.setText(""); 575 } 576 } else { 577 statusBar_.setText(""); 578 } 579 } 580 } 581 582 585 protected void refreshActionMenu(){ 586 if(menuBar_!=null && actionMenu_ != null){ 587 Entry entry = null; 588 Object object = getLastSelectedPathComponent(); 589 if (object != null) { 590 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 591 entry = (Entry) node.getUserObject(); 592 } 593 if (entry != null && custom != null) { 594 currentJPopupMenu_ = custom.getMenu((ExtendedEntry) entry); 595 actionMenu_.removeAll(); 596 Component [] componentList = currentJPopupMenu_.getComponents(); 597 if(componentList.length>0){ 598 actionMenu_.setEnabled(true); 599 for(int i=0; i<componentList.length;i++){ 600 if(JPopupMenu.Separator .class.isAssignableFrom(componentList[i].getClass())) 601 actionMenu_.addSeparator(); 602 else 603 actionMenu_.add((JMenuItem )componentList[i]); 604 } 605 } else { 606 actionMenu_.setEnabled(false); 607 } 608 actionMenu_.revalidate(); 609 } 610 } 611 } 612 613 protected void refreshToolBar(){ 614 if(toolBar_!=null){ 615 if(nbElementsToRemove_>0){ 617 int nbComp = toolBar_.getComponentCount(); 618 for(int i = nbComp - 1 ; i >= nbComp - nbElementsToRemove_ ; i--) 619 toolBar_.remove(i); 620 nbElementsToRemove_ = 0; 621 } 622 623 Entry entry = null; 625 Object object = getLastSelectedPathComponent(); 626 if (object != null) { 627 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) object; 628 entry = (Entry) node.getUserObject(); 629 } 630 if (entry != null && custom != null) { 631 currentJPopupMenu_ = custom.getMenu((ExtendedEntry) entry); 632 Component [] componentList = currentJPopupMenu_.getComponents(); 633 if(componentList.length>0){ 634 boolean isFirst = true; 635 boolean iconDisplayed = false; 636 for(int i=0; i<componentList.length;i++){ 637 if(!JPopupMenu.Separator .class.isAssignableFrom(componentList[i].getClass())){ 638 if(((DefaultAction)((JMenuItem )componentList[i]).getAction()).isUserIcon()){ 639 if(isFirst){ 640 isFirst = false; 641 if(toolBar_.getComponentCount()>0){ 642 toolBar_.addSeparator(); 643 nbElementsToRemove_++; 644 } 645 } 646 toolBar_.add(((JMenuItem )componentList[i]).getAction()); 647 iconDisplayed = true; 648 nbElementsToRemove_++; 649 } 650 } else if(iconDisplayed){ 651 toolBar_.addSeparator(); 652 iconDisplayed = false; 653 nbElementsToRemove_++; 654 } 655 } 656 } 657 } 658 if(toolBar_.getComponentCount()==0){ 659 toolBar_.setVisible(false); 660 } else{ 661 toolBar_.setVisible(false); 662 toolBar_.setVisible(true); 663 } 664 } 665 } 666 667 672 protected boolean hasChild(Context context) { 673 if(context!=null){ 674 Entry[] entries = context.getEntries(); 675 return (entries.length > 0); 676 } 677 return false; 678 } 679 680 |