1 7 package org.ejtools.adwt; 8 9 import java.awt.Component ; 10 11 import javax.swing.Icon ; 12 import javax.swing.JTree ; 13 import javax.swing.tree.DefaultTreeCellRenderer ; 14 import javax.swing.tree.TreePath ; 15 16 import org.ejtools.adwt.util.DefaultObjectRenderer; 17 import org.ejtools.adwt.util.ObjectIndexer; 18 import org.ejtools.adwt.util.ObjectRenderer; 19 import org.ejtools.adwt.util.ObjectSearcher; 20 import org.ejtools.adwt.util.SearchableTreeModel; 21 22 28 public class BeanContextTree extends JTree implements ObjectSearcher 29 { 30 31 protected ObjectRenderer renderer = new DefaultObjectRenderer(); 32 33 34 39 public BeanContextTree(SearchableTreeModel newModel) 40 { 41 super(newModel); 42 43 this.setShowsRootHandles(true); 44 this.setExpandsSelectedPaths(true); 45 this.putClientProperty("JTree.lineStyle", "Angled"); 46 47 this.setCellRenderer( 48 new DefaultTreeCellRenderer () 49 { 50 public Component getTreeCellRendererComponent(JTree jtree, Object obj, boolean flag, boolean flag1, boolean flag2, int i, boolean flag3) 51 { 52 try 53 { 54 Icon leafIcon = null; 55 Icon openIcon = null; 56 Icon closedIcon = null; 57 58 Icon newIcon = BeanContextTree.this.renderer.getIcon(obj); 59 60 if (newIcon != null) 61 { 62 leafIcon = this.getLeafIcon(); 63 openIcon = this.getOpenIcon(); 64 closedIcon = this.getClosedIcon(); 65 this.setLeafIcon(newIcon); 66 this.setOpenIcon(newIcon); 67 this.setClosedIcon(newIcon); 68 } 69 70 super.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, flag3); 71 72 if (newIcon != null) 73 { 74 this.setLeafIcon(leafIcon); 75 this.setOpenIcon(openIcon); 76 this.setClosedIcon(closedIcon); 77 } 78 79 return this; 80 } 81 catch (Exception e) 82 { 83 return super.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, flag3); 84 } 85 } 86 }); 87 } 88 89 90 96 public Object find(Object o) 97 { 98 Object r = ((SearchableTreeModel) this.getModel()).find(o); 99 if (r != null) 100 { 101 Object [] paths = (Object []) r; 102 TreePath path = new TreePath (paths); 103 this.setSelectionPath(path); 104 } 105 return null; 106 } 107 108 109 110 public void selectRoot() 111 { 112 TreePath path = new TreePath (this.getModel().getRoot()); 113 this.setSelectionPath(path); 114 } 115 116 117 122 public void setIndexer(ObjectIndexer indexer) 123 { 124 ((SearchableTreeModel) this.getModel()).setIndexer(indexer); 125 } 126 127 128 133 public void setRenderer(ObjectRenderer renderer) 134 { 135 this.renderer = renderer; 136 } 137 } 138 | Popular Tags |