1 2 25 26 package org.aspectj.ajde.ui.swing; 27 28 import java.util.*; 29 import java.awt.Component ; 30 import java.awt.Color ; 31 import java.awt.Font ; 32 import java.awt.font.*; 33 import java.awt.event.*; 34 import javax.swing.*; 35 import javax.swing.tree.*; 36 import javax.swing.event.*; 37 import org.aspectj.ajde.Ajde; 38 import org.aspectj.asm.*; 39 import org.aspectj.asm.associations.*; 40 import org.aspectj.asm.views.*; 41 import org.aspectj.ajde.ui.*; 42 43 46 class StructureTreeManager { 47 48 private StructureTree structureTree; 49 private SwingTreeViewNodeRenderer cellRenderer = null; 50 private TreeSelectionListener treeListener = null; 51 52 private final StructureTreeModel NO_STRUCTURE_MODEL 53 = new StructureTreeModel(new SwingTreeViewNode(StructureModel.NO_STRUCTURE, new AbstractIcon(null), new ArrayList())); 54 55 58 public StructureTreeManager() { 59 structureTree = new StructureTree(); 60 structureTree.setModel(NO_STRUCTURE_MODEL); 61 cellRenderer = new SwingTreeViewNodeRenderer(); 62 structureTree.setCellRenderer(cellRenderer); 63 treeListener = new StructureViewTreeListener(structureTree); 65 structureTree.addTreeSelectionListener(treeListener); 69 structureTree.addMouseListener((MouseListener)treeListener); 70 } 71 72 public void highlightNode(ProgramElementNode node) { 73 highlightNode((SwingTreeViewNode)structureTree.getModel().getRoot(), node); 74 } 75 76 public StructureNode getSelectedStructureNode() { 77 return (StructureNode)((SwingTreeViewNode)structureTree.getLastSelectedPathComponent()).getUserObject(); 78 } 79 80 public void scrollToHighlightedNode() { 81 structureTree.scrollPathToVisible(structureTree.getSelectionPath()); 82 } 83 84 private void highlightNode(SwingTreeViewNode parent, ProgramElementNode node) { 85 for (int i = 0; i < parent.getChildCount(); i++) { 86 SwingTreeViewNode currNode = (SwingTreeViewNode)parent.getChildAt(i); 87 StructureNode sNode = (StructureNode)currNode.getUserObject(); 88 if (sNode instanceof ProgramElementNode && 89 ((ProgramElementNode)sNode).equals(node)) { 90 TreePath path = new TreePath(currNode.getPath()); 91 structureTree.setSelectionPath(path); 92 int currRow = structureTree.getRowForPath(path); 93 structureTree.expandRow(currRow); 94 structureTree.scrollRowToVisible(currRow); 95 } else { 96 highlightNode(currNode, node); 97 } 98 } 99 } 100 101 109 public void updateTree(final StructureView structureView) { 110 if (structureView == null) return; 111 Runnable update = new Runnable () { 112 public void run() { 113 structureTree.removeAll(); 114 SwingTreeViewNode currNode; 115 if (structureView.getRootNode() == null) { 116 structureTree.setModel(NO_STRUCTURE_MODEL); 117 } else { 118 structureTree.setModel(new StructureTreeModel((SwingTreeViewNode)structureView.getRootNode())); 119 } 120 121 if (structureView instanceof GlobalStructureView) { 122 GlobalStructureView view = (GlobalStructureView)structureView; 123 if (view.getGlobalViewProperties().getHierarchy() == StructureViewProperties.Hierarchy.DECLARATION) { 124 expandTreeToFiles(); 125 } else { 126 expandTree(15); 127 } 128 } else { 129 expandTree(10); 130 } 131 132 } 133 }; 134 135 if (SwingUtilities.isEventDispatchThread()) { 136 update.run(); 137 } else { 138 try { 139 SwingUtilities.invokeAndWait(update); 140 } catch (Exception e) { 141 Ajde.getDefault().getErrorHandler().handleError("Could not update tree.", e); 142 } 143 } 144 } 145 146 StructureTree getStructureTree() { 147 return structureTree; 148 } 149 150 private void expandTreeToFiles() { 151 for (int i = 0; i < structureTree.getRowCount(); i++) { 152 TreePath path = structureTree.getPathForRow(i); 153 SwingTreeViewNode node = (SwingTreeViewNode)path.getLastPathComponent(); 154 if (node.getUserObject() instanceof ProgramElementNode) { 155 ProgramElementNode pNode = (ProgramElementNode)node.getUserObject(); 156 ProgramElementNode.Kind kind = pNode.getProgramElementKind(); 157 if (kind == ProgramElementNode.Kind.PROJECT 158 || kind == ProgramElementNode.Kind.PACKAGE) { 159 structureTree.expandPath(path); 160 } else { 161 structureTree.collapsePath(path); 162 } 163 } else { 164 structureTree.collapsePath(path); 165 } 166 } 167 structureTree.expandPath(structureTree.getPathForRow(0)); 168 } 169 170 private void expandTree(int depth) { 171 for (int i = 0; i < structureTree.getRowCount(); i++) { 172 TreePath path = structureTree.getPathForRow(i); 173 SwingTreeViewNode node = (SwingTreeViewNode)path.getLastPathComponent(); 174 if (path.getPath().length-1 > depth || node.getUserObject() instanceof RelationNode) { 175 structureTree.collapsePath(path); 176 } else { 177 structureTree.expandPath(path); 178 } 179 } 180 structureTree.expandPath(structureTree.getPathForRow(0)); 181 } 182 183 private class StructureTreeModel extends DefaultTreeModel implements TreeModel { 184 public StructureTreeModel(TreeNode newRoot) { 185 super(newRoot); 186 } 187 188 public void valueForPathChanged(TreePath path, Object newValue) { 189 DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent(); 190 nodeChanged(node); 191 } 192 } 193 } 194 195 228 229 253 268 355 378 387 406 412 426 444 445 | Popular Tags |