1 84 85 package swingwtx.swing; 86 87 import org.eclipse.swt.widgets.*; 88 import org.eclipse.swt.events.*; 89 import org.eclipse.swt.*; 90 91 import swingwtx.swing.event.*; 92 import swingwtx.swing.tree.*; 93 94 import java.util.*; 95 96 public class JTree extends swingwtx.swing.JComponent implements TreeModelListener, CellEditorListener { 97 98 protected Tree ppeer = null; 99 protected TreeModel model = null; 100 protected TreeNode root = null; 101 protected TreeCellRenderer cellRenderer = new DefaultTreeCellRenderer(); 102 protected TreeCellEditor cellEditor = new DefaultCellEditor(new JTextField()); 103 protected boolean isEditable = false; 104 protected boolean isRootVisible = true; 105 protected TreePath anchorSelectionPath = null; 106 107 protected org.eclipse.swt.custom.TreeEditor swtEditor = null; 108 protected org.eclipse.swt.widgets.TreeItem editingTreeItem = null; 109 protected DefaultMutableTreeNode editingNode = null; 110 protected swingwt.awt.Component editingComponent = null; 111 112 113 private Object retval = null; 114 115 protected Vector treeWillExpansionListeners = new Vector(); 116 protected Vector treeExpansionListeners = new Vector(); 117 protected Vector treeSelectionListeners = new Vector(); 118 119 public JTree() { cellEditor.addCellEditorListener(this); } 120 public JTree(TreeNode root) { this.root = root; cellEditor.addCellEditorListener(this); } 121 public JTree(TreeModel model) { this.model = model; root = (TreeNode) model.getRoot(); cellEditor.addCellEditorListener(this); } 122 123 public void addTreeExpansionListener(TreeExpansionListener l) { 124 treeExpansionListeners.add(l); 125 } 126 public void removeTreeExpansionListener(TreeExpansionListener l) { 127 treeExpansionListeners.remove(l); 128 } 129 public void addWillTreeExpansionListener(TreeWillExpandListener l) { 130 treeWillExpansionListeners.add(l); 131 } 132 public void removeTreeWillExpansionListener(TreeWillExpandListener l) { 133 treeWillExpansionListeners.remove(l); 134 } 135 public void addTreeSelectionListener(TreeSelectionListener l) { 136 treeSelectionListeners.add(l); 137 } 138 public void removeTreeSelectionListener(TreeSelectionListener l) { 139 treeSelectionListeners.remove(l); 140 } 141 142 public void setModel(TreeModel model){ 143 this.model = model; 144 root = (TreeNode) model.getRoot(); 145 model.addTreeModelListener(this); 146 drawTree(); 147 } 148 149 public TreeModel getModel(){ 150 return model; 151 } 152 153 public boolean isEditable() { return isEditable; } 154 public void setEditable(boolean b) { isEditable = b; } 155 public void setCellEditor(TreeCellEditor cell) { cellEditor = cell; } 156 public TreeCellEditor getCellEditor() { return cellEditor; } 157 158 163 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 164 descendantHasPeer = true; 165 ppeer = new Tree(parent.getComposite(), SWT.BORDER ); 166 167 swtEditor = new org.eclipse.swt.custom.TreeEditor(ppeer); 169 170 peer = ppeer; 172 this.parent = parent; 173 174 drawTree(); 175 176 registerTreeEvents(); 177 178 } 179 180 184 protected void registerTreeEvents() { 185 186 ppeer.addSelectionListener(new SelectionListener() { 187 public void widgetSelected(SelectionEvent e) { 188 processTreeSelection(e); 189 processTreeEdit((TreeNode) e.item.getData(), (org.eclipse.swt.widgets.TreeItem) e.item); 190 } 191 public void widgetDefaultSelected(SelectionEvent e) { 192 } 193 }); 194 195 ppeer.addTreeListener(new TreeListener() { 196 public void treeCollapsed(TreeEvent e) { 197 processTreeExpansion(e, true); 198 } 199 public void treeExpanded(TreeEvent e) { 200 processTreeExpansion(e, false); 201 } 202 }); 203 204 } 205 206 210 protected void processTreeEdit(TreeNode node, org.eclipse.swt.widgets.TreeItem item) { 211 212 if (!isEditable) return; 214 215 if (!(node instanceof DefaultMutableTreeNode)) return; 217 218 DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) node; 219 editingTreeItem = item; 220 editingNode = dmt; 221 222 editingComponent = cellEditor.getTreeCellEditorComponent(this, dmt.getUserObject(), true, true, dmt.isLeaf(), 1); 224 225 if (swtEditor != null) { 227 if (swtEditor.getEditor() != null) 228 swtEditor.getEditor().dispose(); 229 swtEditor.dispose(); 230 swtEditor = null; 231 } 232 233 try { 238 editingComponent.setSwingWTParent(new TreeEditorContainer(this)); 239 editingComponent.registerEvents(); 240 } 241 catch (Exception e) { 242 e.printStackTrace(); 243 } 244 245 swtEditor = new org.eclipse.swt.custom.TreeEditor(ppeer); 247 swtEditor.horizontalAlignment = SWT.LEFT; 248 swtEditor.grabHorizontal = true; 249 swtEditor.minimumWidth = 50; 250 swtEditor.setEditor(editingComponent.getPeer(), item); 251 252 if (cellEditor instanceof DefaultCellEditor) { 254 ((DefaultCellEditor) cellEditor).getHandler().setValue(dmt.getUserObject()); 255 } 256 257 editingComponent.grabFocus(); 259 } 260 261 264 public void editingStopped(ChangeEvent e) { 265 Object value = cellEditor.getCellEditorValue(); 267 268 editingNode.setUserObject(value); 270 271 if (editingNode instanceof DefaultMutableTreeNode) { 273 274 DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) editingNode; 275 276 org.eclipse.swt.widgets.TreeItem item = dmt.peer; 278 279 JLabel cr = (JLabel) cellRenderer.getTreeCellRendererComponent(this, 281 dmt, 282 false, dmt.isLeaf(), 283 dmt.isLeaf(), 0, 284 true); 285 286 item.setText(cr.getText()); 288 289 if (cr.getIcon() != null) 291 item.setImage(SwingWTUtils.getSWTImageFromSwingIcon(this, cr.getIcon())); 292 293 if (cr.getBackground() != null) 295 if (cr.getBackground().getSWTColor() != null) 296 item.setBackground(cr.getBackground().getSWTColor()); 297 if (cr.getForeground() != null) 298 if (cr.getForeground().getSWTColor() != null) 299 item.setForeground(cr.getForeground().getSWTColor()); 300 301 if (cr.hasSetFont()) 303 item.setFont(cr.getFont().getSWTFont()); 304 } 305 else 306 System.out.println("FIXME: Node does not descend from DefaultMutableTreeNode"); 307 } 308 309 public void fireTreeExpanded(TreePath path) { 310 TreeExpansionEvent event = new TreeExpansionEvent(this, path); 311 Iterator i = treeExpansionListeners.iterator(); 312 while(i.hasNext()) { 313 ((TreeExpansionListener)i.next()).treeExpanded(event); 314 } 315 } 316 317 public TreePath getAnchorSelectionPath() { 318 return anchorSelectionPath; 319 } 320 public void setAnchorSelectionPath(TreePath path) { 321 this.anchorSelectionPath = path; 322 } 323 324 protected void processTreeSelection(SelectionEvent e) { 325 if (treeSelectionListeners.size() == 0) return; 326 Iterator i = treeSelectionListeners.iterator(); 327 TreeSelectionEvent ev = new TreeSelectionEvent(this, getTreePath((TreeNode) e.item.getData()), false, getTreePath((TreeNode) e.item.getData()), getTreePath((TreeNode) e.item.getData())); 330 while (i.hasNext()) { 331 TreeSelectionListener l = (TreeSelectionListener) i.next(); 332 l.valueChanged(ev); 333 } 334 } 335 336 protected void processTreeExpansion(TreeEvent e, boolean collapse) { 337 try { 338 fireTreeWillExpandOrCollapse(e,collapse); 339 } 340 catch (ExpandVetoException ex) { 341 e.doit = false; 342 return; 343 } 344 345 renderNode((org.eclipse.swt.widgets.TreeItem) e.item); 347 348 if (treeExpansionListeners.size() == 0) return; 349 Iterator i = treeExpansionListeners.iterator(); 350 TreeExpansionEvent ev = new TreeExpansionEvent(e.item.getData(), getTreePath((TreeNode) e.item.getData())); 351 while (i.hasNext()) { 352 TreeExpansionListener l = (TreeExpansionListener) i.next(); 353 if (collapse) 354 l.treeCollapsed(ev); 355 else 356 l.treeExpanded(ev); 357 } 358 } 359 360 368 public void fireTreeWillExpandOrCollapse(TreeEvent e, boolean collapse) throws ExpandVetoException { 369 if (treeWillExpansionListeners.size() != 0) { 370 Iterator itrWillExpand = treeWillExpansionListeners.iterator(); 371 TreeExpansionEvent ev = new TreeExpansionEvent(e.item.getData(), getTreePath((TreeNode) e.item.getData())); 372 while (itrWillExpand.hasNext()) { 373 TreeWillExpandListener l = (TreeWillExpandListener) itrWillExpand.next(); 374 if (collapse) { 375 l.treeWillCollapse(ev); 376 } 377 else { 378 l.treeWillExpand(ev); 379 } 380 } 381 } 382 } 383 384 385 387 public TreePath getTreePath(TreeNode node) { 388 Vector path = new Vector(); 389 path.add(node); 390 while (node.getParent() != null) { 391 node = node.getParent(); 392 path.add(node); 393 } 394 Object [] retPath = path.toArray(); 395 path.removeAllElements(); 396 path = null; 397 return new TreePath(retPath); 398 } 399 400 public void addSelectionPaths(TreePath[] path) {} 401 402 public TreePath getSelectionPath() { 403 404 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return null; 405 406 SwingUtilities.invokeSync( new Runnable () { 407 public void run() { 408 TreeItem[] selpath = ppeer.getSelection(); 409 if (selpath.length == 0) { retval = null; return; } 410 411 Object [] nodes = new Object [selpath.length]; 412 for (int i = 0; i < selpath.length; i++) { 413 nodes[i] = selpath[i].getData(); 414 } 415 retval = new TreePath(nodes); 416 } 417 }); 418 419 return (TreePath) retval; 420 } 421 422 public TreePath[] getSelectionPaths() { 423 return new TreePath[] { getSelectionPath() }; 424 } 425 426 public void addSelectionPath(TreePath t) { 427 setSelectionPath(t); 428 } 429 430 public void removeSelectionPath(TreePath t) { 431 SwingUtilities.invokeSync(new Runnable () { 432 public void run() { 433 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 434 ppeer.deselectAll(); 435 } 436 }); 437 } 438 439 public void removeSelectionPaths(TreePath t[]) { 440 removeSelectionPath(null); 441 } 442 443 444 public void setScrollsOnExpand(boolean b) { 445 } 446 447 448 public void setShowsRootHandles(boolean b) { 449 } 450 451 452 public void setSelectionModel(TreeSelectionModel model) { 453 } 454 455 456 public TreeSelectionModel getSelectionModel() { 457 return new DefaultTreeSelectionModel(); 458 } 459 460 461 public void scrollPathToVisible(final TreePath t) { 462 SwingUtilities.invokeSync(new Runnable () { 463 public void run() { 464 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 465 DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) t.getLastPathComponent(); 466 ppeer.showItem(dmt.peer); 467 } 468 } 469 }); 470 } 471 472 public void setSelectionPaths(TreePath[] t) { 473 setSelectionPath(t[0]); 474 } 475 476 public void setSelectionPath(TreePath t) { 477 478 if (t == null) return; 479 if (t.getPathCount() == 0) return; 480 481 final Object [] nodes = t.getPath(); 482 483 SwingUtilities.invokeSync( new Runnable () { 484 public void run() { 485 TreeItem[] ti = new TreeItem[nodes.length]; 486 for (int i = 0; i < nodes.length; i++) { 487 ti[i] = ((DefaultMutableTreeNode) nodes[i]).peer; 488 } 489 ppeer.setSelection(ti); 490 } 491 }); 492 493 } 494 495 public TreePath getPathForLocation(int x, int y) { 496 return getClosestPathForLocation(x, y); 497 } 498 499 public TreePath getClosestPathForLocation(final int x, final int y) { 500 final TreePath[] path = new TreePath[1]; 501 SwingUtilities.invokeSync(new Runnable () { 502 public void run() { 503 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 504 TreeItem item = ppeer.getItem(new org.eclipse.swt.graphics.Point(x, y)); 505 if (item!=null) 506 path[0] = new TreePath((TreeNode) item.getData()); 507 else 508 path[0] = null; 509 } 510 else 511 path[0] = null; 512 } 513 }); 514 return path[0]; 515 } 516 517 525 public TreePath getPathForRow(final int row) { 526 final TreePath[] path = new TreePath[1]; 527 SwingUtilities.invokeSync(new Runnable () { 528 public void run() { 529 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 530 TreeItem item = ppeer.getItems()[row]; 531 TreeNode node = (TreeNode) item.getData(); 532 path[0] = new TreePath(node); 533 } 534 else 535 path[0] = null; 536 } 537 }); 538 return path[0]; 539 } 540 541 public int getRowCount() { 542 final int[] rowCount = new int[1]; 543 SwingUtilities.invokeSync(new Runnable () { 544 public void run() { 545 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 546 rowCount[0] = ppeer.getItemHeight(); 547 else 548 rowCount[0] = 0; 549 } 550 }); 551 return rowCount[0]; 552 } 553 554 558 public void expandRow(int row) { 559 expandPath(getPathForRow(row)); 560 } 561 562 566 public void collapseRow(int row) { 567 collapsePath(getPathForRow(row)); 568 } 569 570 public void expandPath(TreePath parent) { 571 if (parent == null) 572 return; 573 Object element = parent.getLastPathComponent(); 574 if (parent.getParentPath()!=null && parent.getLastPathComponent()!=null){ 575 expandPath(parent.getParentPath()); 576 } 577 if (element == null) 578 return; 579 580 fireTreeExpanded(parent); 581 582 if (element instanceof DefaultMutableTreeNode) { 583 DefaultMutableTreeNode node = (DefaultMutableTreeNode) element; 584 TreeItem peeritem = node.peer; 585 if (peeritem != null) 586 if (!peeritem.isDisposed()) { 587 if (node.isLeaf()) { 588 if (!peeritem.getExpanded()) { 589 peeritem.setExpanded(true); 590 renderNode(peeritem); 591 } 592 } 593 } 594 } 595 596 } 597 598 public void collapsePath(TreePath parent) { 599 if (parent == null) 600 return; 601 Object element = parent.getLastPathComponent(); 602 if (parent.getParentPath()!=null && parent.getLastPathComponent()!=null){ 603 collapsePath(parent.getParentPath()); 604 } 605 if (element == null) 606 return; 607 608 if (element instanceof DefaultMutableTreeNode) { 609 DefaultMutableTreeNode node = (DefaultMutableTreeNode) element; 610 TreeItem peeritem = node.peer; 611 if (peeritem != null) 612 if (!peeritem.isDisposed()) { 613 if (node.getChildCount() != 0) { 614 if (peeritem.getExpanded()) { 615 peeritem.setExpanded(false); 616 renderNode(peeritem); 617 } 618 } 619 } 620 } 621 } 622 623 626 public void drawTree() { 627 628 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return; 630 631 SwingUtilities.invokeSync(new Runnable () { 632 public void run() { 633 634 ppeer.removeAll(); 636 637 if (root != null) { 640 if(isRootVisible){ 641 addNodes(ppeer, null, root); 642 } 643 else { 644 if (root.getChildCount() > 0) { 645 for (Enumeration e = root.children(); e.hasMoreElements();) { 646 addNodes(ppeer, null, (TreeNode)e.nextElement()); 647 } 648 } 649 } 650 } 651 } 652 }); 653 } 654 655 public Object getLastSelectedPathComponent() { 656 TreePath t = getSelectionPath(); 657 return t != null ? t.getLastPathComponent() : null; 658 } 659 660 664 private void addNodes(Tree tree, TreeItem treeItem, TreeNode swingNode) { 665 666 TreeItem item = null; 668 if (tree != null) { 669 item = new TreeItem(tree, SWT.BORDER); 670 item.setExpanded(true); 672 } 673 else 674 item = new TreeItem(treeItem, SWT.BORDER); 675 676 if (swingNode instanceof DefaultMutableTreeNode) 681 ((DefaultMutableTreeNode)swingNode).peer = item; 683 else 684 item.setText("FIXME: Node does not descend from DefaultMutableTreeNode"); 685 686 swingwt.awt.Component renderer = 688 cellRenderer.getTreeCellRendererComponent( 689 this, 690 swingNode, 691 false, !(tree == null), swingNode.isLeaf(), 0, true); 697 renderNode(item, renderer); 699 700 item.setData(swingNode); 703 704 if (swingNode.getChildCount() > 0) { 706 for (Enumeration e = swingNode.children() ; e.hasMoreElements() ;) { 707 addNodes(null, item, (TreeNode) e.nextElement()); 708 } 709 } 710 } 711 712 713 719 private void renderNode(org.eclipse.swt.widgets.TreeItem item) { 720 721 TreeNode swingNode = (TreeNode) item.getData(); 722 723 if (swingNode instanceof DefaultMutableTreeNode) 728 ((DefaultMutableTreeNode)swingNode).peer = item; 730 else 731 item.setText("FIXME: Node does not descend from DefaultMutableTreeNode"); 732 733 swingwt.awt.Component renderer = 735 cellRenderer.getTreeCellRendererComponent( 736 this, 737 swingNode, 738 false, !item.getExpanded(), swingNode.isLeaf(), 0, true); 744 renderNode(item, renderer); 745 746 } 747 748 753 private void renderNode(org.eclipse.swt.widgets.TreeItem item, swingwt.awt.Component renderer) { 754 755 if (renderer instanceof JLabel) { 758 759 JLabel cr = (JLabel) renderer; 760 761 item.setText(cr.getText()); 763 764 if (cr.getIcon() != null) 766 item.setImage(SwingWTUtils.getSWTImageFromSwingIcon(this, cr.getIcon())); 767 768 if (cr.getBackground() != null) 770 if (cr.getBackground().getSWTColor() != null) 771 item.setBackground(cr.getBackground().getSWTColor()); 772 if (cr.getForeground() != null) 773 if (cr.getForeground().getSWTColor() != null) 774 item.setForeground(cr.getForeground().getSWTColor()); 775 776 if (cr.hasSetFont()) 778 item.setFont(cr.getFont().getSWTFont()); 779 780 } 781 else 782 { 783 item.setText("CellRenderer not JLabel: " + renderer.getClass().getName()); 784 } 785 } 786 787 public void setCellRenderer(TreeCellRenderer renderer) { cellRenderer = renderer; } 788 public TreeCellRenderer getCellRenderer() { return cellRenderer; } 789 790 791 public void treeNodesChanged(TreeModelEvent e) { drawTree(); } 792 public void treeNodesInserted(TreeModelEvent e) { drawTree(); } 793 public void treeNodesRemoved(TreeModelEvent e) { drawTree(); } 794 public void treeStructureChanged(TreeModelEvent e) { drawTree(); } 795 796 800 public void setRootVisible(boolean rootVisible){ 801 isRootVisible = rootVisible; 802 } 803 804 808 public boolean isRootVisible(){ 809 return isRootVisible; 810 } 811 812 public void editingCanceled(ChangeEvent e) { 813 } 814 815 819 private class TreeEditorContainer extends swingwt.awt.Container { 820 public TreeEditorContainer(JTree parent) { 821 this.peer = parent.getPeer(); 822 this.composite = (org.eclipse.swt.widgets.Composite) parent.getPeer(); 823 } 824 } 825 826 827 } 828 | Popular Tags |