1 7 8 package org.gjt.jclasslib.browser; 9 10 import org.gjt.jclasslib.browser.config.window.*; 11 import org.gjt.jclasslib.structures.*; 12 13 import javax.swing.*; 14 import javax.swing.event.TreeSelectionEvent ; 15 import javax.swing.event.TreeSelectionListener ; 16 import javax.swing.tree.TreePath ; 17 import java.awt.*; 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 21 27 public class BrowserComponent extends JComponent 28 implements TreeSelectionListener { 29 30 private BrowserHistory history; 31 private BrowserServices services; 32 33 35 private JSplitPane splitPane; 36 private BrowserTreePane treePane; 37 private BrowserDetailPane detailPane; 38 39 44 public BrowserComponent(BrowserServices services) { 45 46 this.services = services; 47 setupComponent(); 48 } 49 50 55 public BrowserTreePane getTreePane() { 56 return treePane; 57 } 58 59 65 public BrowserDetailPane getDetailPane() { 66 return detailPane; 67 } 68 69 74 public BrowserHistory getHistory() { 75 return history; 76 } 77 78 83 public BrowserPath getBrowserPath() { 84 85 TreePath selectionPath = treePane.getTree().getSelectionPath(); 86 if (selectionPath == null || selectionPath.getPathCount() < 3) { 87 return null; 88 } 89 90 BrowserTreeNode categoryNode = (BrowserTreeNode)selectionPath.getPathComponent(2); 91 String category = categoryNode.getType(); 92 if (category.equals(BrowserTreeNode.NODE_NO_CONTENT)) { 93 return null; 94 } 95 96 BrowserPath browserPath = new BrowserPath(); 97 browserPath.addPathComponent(new CategoryHolder(category)); 98 int categoryNodeIndex = categoryNode.getIndex(); 99 if (category.equals(BrowserTreeNode.NODE_CONSTANT_POOL)) { 100 --categoryNodeIndex; 101 } 102 if (category.equals(BrowserTreeNode.NODE_METHOD)) { 103 MethodInfo methodInfo = services.getClassFile().getMethods()[categoryNodeIndex]; 104 addClassMemberPathComponent(methodInfo, browserPath, selectionPath); 105 } else if (category.equals(BrowserTreeNode.NODE_FIELD)) { 106 FieldInfo fieldInfo = services.getClassFile().getFields()[categoryNodeIndex]; 107 addClassMemberPathComponent(fieldInfo, browserPath, selectionPath); 108 } else { 109 browserPath.addPathComponent(new IndexHolder(categoryNodeIndex)); 110 } 111 112 return browserPath; 113 } 114 115 120 public void setBrowserPath(BrowserPath browserPath) { 121 122 if (browserPath == null) { 123 return; 124 } 125 LinkedList pathComponents = browserPath.getPathComponents(); 126 Iterator it = pathComponents.iterator(); 127 if (!it.hasNext()) { 128 return; 129 } 130 CategoryHolder categoryComponent = (CategoryHolder)it.next(); 131 String category = categoryComponent.getCategory(); 132 TreePath path = treePane.getPathForCategory(category); 133 if (path == null) { 134 return; 135 } 136 while (it.hasNext()) { 137 PathComponent pathComponent = (PathComponent)it.next(); 138 int childIndex; 139 if (pathComponent instanceof ReferenceHolder) { 140 ReferenceHolder referenceHolder = (ReferenceHolder)pathComponent; 141 try { 142 if (category.equals(BrowserTreeNode.NODE_METHOD)) { 143 childIndex = services.getClassFile().getMethodIndex(referenceHolder.getName(), referenceHolder.getType()); 144 } else if (category.equals(BrowserTreeNode.NODE_FIELD)) { 145 childIndex = services.getClassFile().getFieldIndex(referenceHolder.getName(), referenceHolder.getType()); 146 } else { 147 break; 148 } 149 } catch (InvalidByteCodeException ex) { 150 break; 151 } 152 } else if (pathComponent instanceof IndexHolder) { 153 childIndex = ((IndexHolder)pathComponent).getIndex(); 154 } else { 155 break; 156 } 157 BrowserTreeNode lastNode = (BrowserTreeNode)path.getLastPathComponent(); 158 if (childIndex >= lastNode.getChildCount()) { 159 break; 160 } 161 path = path.pathByAddingChild(lastNode.getChildAt(childIndex)); 162 } 163 164 JTree tree = treePane.getTree(); 165 tree.expandPath(path); 166 tree.setSelectionPath(path); 167 Object [] pathObjects = path.getPath(); 168 if (pathObjects.length > 2) { 169 TreePath categoryPath = new TreePath (new Object []{pathObjects[0], pathObjects[1], pathObjects[2]}); 170 tree.scrollPathToVisible(categoryPath); 171 } 172 173 } 174 175 178 public void rebuild() { 179 180 181 BrowserPath browserPath = getBrowserPath(); 182 reset(); 183 if (browserPath != null) { 184 setBrowserPath(browserPath); 185 } 186 } 187 188 191 public void reset() { 192 193 JTree tree = treePane.getTree(); 194 tree.removeTreeSelectionListener(this); 195 treePane.rebuild(); 196 history.clear(); 197 tree.addTreeSelectionListener(this); 198 checkSelection(); 199 } 200 201 202 205 public void checkSelection() { 206 207 JTree tree = treePane.getTree(); 208 if (services.getClassFile() == null) { 209 ((CardLayout)detailPane.getLayout()).show(detailPane, BrowserTreeNode.NODE_NO_CONTENT); 210 } else { 211 if (tree.getSelectionPath() == null) { 212 BrowserTreeNode rootNode = (BrowserTreeNode)tree.getModel().getRoot(); 213 tree.setSelectionPath(new TreePath (new Object []{rootNode, rootNode.getFirstChild()})); 214 } 215 } 216 } 217 218 public void valueChanged(TreeSelectionEvent selectionEvent) { 219 220 services.activate(); 221 222 TreePath selectedPath = selectionEvent.getPath(); 223 224 history.updateHistory(selectedPath); 225 showDetailPaneForPath(selectedPath); 226 227 } 228 229 private void addClassMemberPathComponent(ClassMember classMember, BrowserPath browserPath, TreePath selectionPath) { 230 231 try { 232 browserPath.addPathComponent(new ReferenceHolder(classMember.getName(), classMember.getDescriptor())); 233 if (selectionPath.getPathCount() > 3) { 234 for (int i = 3; i < selectionPath.getPathCount(); i++) { 235 BrowserTreeNode attributeNode = (BrowserTreeNode)selectionPath.getPathComponent(i); 236 browserPath.addPathComponent(new IndexHolder(attributeNode.getIndex())); 237 } 238 } 239 } catch (InvalidByteCodeException ex) { 240 } 241 } 242 243 private void showDetailPaneForPath(TreePath path) { 244 245 BrowserTreeNode node = (BrowserTreeNode)path.getLastPathComponent(); 246 String nodeType = node.getType(); 247 detailPane.showPane(nodeType, path); 248 } 249 250 251 private void setupComponent() { 252 253 setLayout(new BorderLayout()); 254 255 detailPane = new BrowserDetailPane(services); 256 257 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 258 buildTreePane(), 259 detailPane); 260 261 add(splitPane, BorderLayout.CENTER); 262 263 } 264 265 private BrowserTreePane buildTreePane() { 266 267 treePane = new BrowserTreePane(services); 268 269 JTree tree = treePane.getTree(); 270 tree.addTreeSelectionListener(this); 271 history = new BrowserHistory(services); 272 273 return treePane; 274 } 275 276 } 277 | Popular Tags |