KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > swing > StructureTreeManager


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25      
26 package org.aspectj.ajde.ui.swing;
27
28 import java.util.*;
29 import java.awt.Component JavaDoc;
30 import java.awt.Color JavaDoc;
31 import java.awt.Font JavaDoc;
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 /**
44  * @author Mik Kersten
45  */

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     /**
56      * @todo should probably avoid that MouseListener cast
57      */

58     public StructureTreeManager() {
59         structureTree = new StructureTree();
60         structureTree.setModel(NO_STRUCTURE_MODEL);
61         cellRenderer = new SwingTreeViewNodeRenderer();
62         structureTree.setCellRenderer(cellRenderer);
63         //if (fileView) {
64
treeListener = new StructureViewTreeListener(structureTree);
65         //} else {
66
// treeListener = new BrowserViewTreeListener(structureTree);
67
//}
68
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 // public void updateTree(StructureView structureView) {
102
// displayTree(structureView, 10);
103
// }
104
//
105
// public void updateTree(GlobalStructureView structureView) {
106
// displayTree(structureView, depth);
107
// }
108

109     public void updateTree(final StructureView structureView) {
110         if (structureView == null) return;
111         Runnable JavaDoc update = new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc newValue) {
189             DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
190             nodeChanged(node);
191         }
192     }
193 }
194
195 // /**
196
// * @param node if null assume root
197
// */
198
// public void navigationAction(ProgramElementNode node, boolean followedLink, boolean forward) {
199
// if (node == null) {
200
// structureTree.setSelectionRow(0);
201
// structureTree.scrollRowToVisible(0);
202
// } else if (node.getSourceLocation().getSourceFilePath() != null) {
203
// final String fileName = node.getSourceLocation().getSourceFilePath();
204
// final int lineNumber = node.getSourceLocation().getLineNumber();
205
// if (fileName != null && lineNumber > 0) {
206
// Runnable update = new Runnable() {
207
// public void run() {
208
// Ajde.getDefault().getEditorManager().showSourceLine(fileName, lineNumber, true);
209
// }
210
// };
211
//
212
// if (SwingUtilities.isEventDispatchThread()) {
213
// update.run();
214
// } else {
215
// try {
216
// SwingUtilities.invokeAndWait(update);
217
// } catch (Exception ee) {
218
//
219
// }
220
// }
221
// }
222
// if (followedLink) {
223
// highlightNode((SwingTreeViewNode)structureTree.getModel().getRoot(), node);
224
// }
225
// }
226
// }
227

228
229 // if (node instanceof ProgramElementNode) {
230
// ProgramElementNode lexicalNode = (ProgramElementNode)node;
231
// setIcon(getProgramElementNodeIcon(lexicalNode));
232
// } else if (node instanceof RelationNode) {
233
// RelationNode relationNode = (RelationNode)node;
234
//
235
// setIcon(icons.getAssociationSwingIcon(relationNode.getRelation()));
236
// this.setFont(new Font(this.getFont().getName(), Font.ITALIC, this.getFont().getSize()));
237
//
238
// } else if (node instanceof LinkNode) {
239
// LinkNode link = (LinkNode)node;
240
// setIcon(getProgramElementNodeIcon(link.getProgramElementNode()));
241
// } else {
242
// if (node != null && ProgramElementNode.Kind.PACKAGE.equals(node.getKind())) {
243
// setIcon(icons.getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE));
244
// } else if (node != null && ProgramElementNode.Kind.PROJECT.equals(node.getKind())) {
245
// setIcon(icons.getStructureSwingIcon(ProgramElementNode.Kind.PROJECT));
246
// } else if (node != null && ProgramElementNode.Kind.FILE.equals(node.getKind())) {
247
// setIcon(icons.getStructureSwingIcon(ProgramElementNode.Kind.CLASS));
248
// } else {
249
// setIcon(null);
250
// }
251
// }
252

253 // void updateTree(int depth, GlobalViewProperties properties) {
254
// this.hierarchy = properties.getHierarchy();
255
// displayTree(depth, null);
256
// }
257
//
258
// void updateTree(String filePath, int depth, GlobalViewProperties properties) {
259
// this.hierarchy = properties.getHierarchy();
260
// if (filePath == null || filePath.equals("")) {
261
// structureTree.setModel(NO_FILE_SELECTED_MODEL);
262
// } else {
263
// structureTree.setRootFilePath(filePath);
264
// displayTree(depth, filePath);
265
// }
266
// }
267

268 // int accessibility = 0;
269
// if (pNode.getAccessibility().contains(ProgramElementNode.Accessibility.PUBLIC)) {
270
// accessibility = 1;
271
// } else if (pNode.getAccessibility().contains(ProgramElementNode.Accessibility.PROTECTED)) {
272
// accessibility = 2;
273
// } else if (pNode.getAccessibility().contains(ProgramElementNode.Accessibility.PRIVATE)) {
274
// accessibility = 3;
275
// } else if (pNode.getAccessibility().contains(ProgramElementNode.Accessibility.PRIVILEGED)) {
276
// accessibility = 3;
277
// }
278
//
279
// if (pNode == null || pNode.getKind() == null) {
280
// return null;
281
// } else if (ProgramElementNode.Kind.PROJECT.equals(pNode.getKind())) {
282
// return icons.getStructureSwingIcon(ProgramElementNode.Kind.PROJECT);
283
// } else if (ProgramElementNode.Kind.PACKAGE.equals(pNode.getKind())) {
284
// return icons.getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE);
285
// } else if (ProgramElementNode.Kind.FILE.equals(pNode.getKind())) {
286
// return icons.getStructureSwingIcon(ProgramElementNode.Kind.CLASS);
287
// } else if (ProgramElementNode.Kind.CLASS.equals(pNode.getKind())) {
288
// switch (accessibility) {
289
// case 1: return icons.getClassPublicIcon();
290
// case 2: return icons.getClassProtectedIcon();
291
// case 3: return icons.getClassPrivateIcon();
292
// default: return icons.getClassPackageIcon();
293
// }
294
// } else if (ProgramElementNode.Kind.INTERFACE.equals(pNode.getKind())) {
295
// switch (accessibility) {
296
// case 1: return icons.getInterfacePublicIcon();
297
// case 2: return icons.getInterfaceProtectedIcon();
298
// case 3: return icons.getInterfacePrivateIcon();
299
// default: return icons.getInterfacePackageIcon();
300
// }
301
// } else if (ProgramElementNode.Kind.ASPECT.equals(pNode.getKind())) {
302
// switch (accessibility) {
303
// case 1: return icons.getAspectPublicIcon();
304
// case 2: return icons.getAspectProtectedIcon();
305
// case 3: return icons.getAspectPrivateIcon();
306
// case 4: return icons.getAspectPrivilegedIcon();
307
// default: return icons.getAspectPackageIcon();
308
// }
309
// } else if (ProgramElementNode.Kind.METHOD.equals(pNode.getKind())
310
// || ProgramElementNode.Kind.INITIALIZER.equals(pNode.getKind())
311
// || ProgramElementNode.Kind.CONSTRUCTOR.equals(pNode.getKind())) {
312
// switch (accessibility) {
313
// case 1: return icons.getMethodPublicIcon();
314
// case 2: return icons.getMethodProtectedIcon();
315
// case 3: return icons.getMethodPrivateIcon();
316
// default: return icons.getMethodPackageIcon();
317
// }
318
// } else if (ProgramElementNode.Kind.FIELD.equals(pNode.getKind())) {
319
// switch (accessibility) {
320
// case 1: return icons.getFieldPublicIcon();
321
// case 2: return icons.getFieldProtectedIcon();
322
// case 3: return icons.getFieldPrivateIcon();
323
// default: return icons.getFieldPackageIcon();
324
// }
325
// } else if (ProgramElementNode.Kind.INTRODUCTION.equals(pNode.getKind())) {
326
// switch (accessibility) {
327
// case 1: return icons.getIntroductionPublicIcon();
328
// case 2: return icons.getIntroductionProtectedIcon();
329
// case 3: return icons.getIntroductionPrivateIcon();
330
// default: return icons.getIntroductionPackageIcon();
331
// }
332
// } else if (ProgramElementNode.Kind.POINTCUT.equals(pNode.getKind())) {
333
// switch (accessibility) {
334
// case 1: return icons.getJoinpointPublicIcon();
335
// case 2: return icons.getJoinpointProtectedIcon();
336
// case 3: return icons.getJoinpointPrivateIcon();
337
// default: return icons.getJoinpointPackageIcon();
338
// }
339
// } else if (ProgramElementNode.Kind.ADVICE.equals(pNode.getKind())) {
340
// return icons.getAdviceIcon();
341
// } else if (ProgramElementNode.Kind.DECLARE_PARENTS.equals(pNode.getKind())) {
342
// return icons.getDeclareParentsIcon();
343
// } else if (ProgramElementNode.Kind.DECLARE_ERROR.equals(pNode.getKind())) {
344
// return icons.getDeclareErrorIcon();
345
// } else if (ProgramElementNode.Kind.DECLARE_WARNING.equals(pNode.getKind())) {
346
// return icons.getDeclareWarningIcon();
347
// } else if (ProgramElementNode.Kind.DECLARE_SOFT.equals(pNode.getKind())) {
348
// return icons.getDeclareSoftIcon();
349
// } else if (ProgramElementNode.Kind.CODE.equals(pNode.getKind())) {
350
// return icons.getCodeIcon();
351
// } else {
352
// return null;
353
// }
354

355 //
356
// if (relationNode.getKind().equals(org.aspectj.asm.associations.Advice.NAME) ||
357
// relationNode.getKind().equals(org.aspectj.asm.associations.Introduction.NAME)) {
358
// if (relationNode.getRelation().getBackNavigationName().equals(relationNode.getName()) ){
359
// setIcon(icons.getRelationAdviceBackIcon());
360
// } else {
361
// setIcon(icons.getAssociationSwingIcon(relationNode.getRelation()));
362
// setIcon(icons.getRelationAdviceForwardIcon());
363
// }
364
// } else if (relationNode.getKind().equals(org.aspectj.asm.associations.Inheritance.NAME)) {
365
// if (relationNode.getRelation().getBackNavigationName().equals(relationNode.getName()) ){
366
// setIcon(icons.getRelationInheritanceBackIcon());
367
// } else {
368
// setIcon(icons.getRelationInheritanceForwardIcon());
369
// }
370
// } else {
371
// if (relationNode.getRelation().getBackNavigationName().equals(relationNode.getName()) ){
372
// setIcon(icons.getRelationReferenceBackIcon());
373
// } else {
374
// setIcon(icons.getRelationReferenceForwardIcon());
375
// }
376
// }
377

378 // public ProgramElementNode getRootProgramElementNode() {
379
// StructureNode node = (StructureNode)((SwingTreeViewNode)structureTree.getModel().getRoot()).getUserObject();
380
// if (node instanceof ProgramElementNode) {
381
// return (ProgramElementNode)node;
382
// } else {
383
// return null;
384
// }
385
// }
386

387 // /**
388
// * @todo HACK: this is a workaround and can break
389
// */
390
// private static ProgramElementNode mapResult = null;
391
// private ProgramElementNode getNodeForLink(LinkNode node, StructureNode rootNode) {
392
// ProgramElementNode result = null;
393
// if (rootNode instanceof ProgramElementNode &&
394
// ((ProgramElementNode)rootNode).getName().equals(node.getProgramElementNode().getName())) {
395
// mapResult = (ProgramElementNode)rootNode;
396
// } else {
397
// ProgramElementNode linkedNode = node.getProgramElementNode();
398
// for (Iterator it = rootNode.getChildren().iterator(); it.hasNext(); ) {
399
// StructureNode child = (StructureNode)it.next();
400
// getNodeForLink(node, child);
401
// }
402
// }
403
// return mapResult;
404
// }
405

406 // private void sortNodes(List nodes) {
407
// if (sortNodes) {
408
// Collections.sort(nodes, structureNodeComparator);
409
// }
410
// }
411

412 // private class StructureNodeComparator implements Comparator {
413
// public int compare(Object o1, Object o2) {
414
// StructureNode t1 = (StructureNode) ((SwingTreeViewNode) o1).getUserObject();
415
// StructureNode t2 = (StructureNode) ((SwingTreeViewNode) o2).getUserObject();
416
// if (t1 instanceof ProgramElementNode && t2 instanceof ProgramElementNode) {
417
// ProgramElementNode p1 = (ProgramElementNode) t1;
418
// ProgramElementNode p2 = (ProgramElementNode) t2;
419
// return p1.getName().compareTo(p2.getName());
420
// } else {
421
// return 0;
422
// }
423
// }
424
// }
425

426 // private class StructureViewNodeAdapter extends DefaultMutableTreeNode {
427
//
428
// private StructureViewNode nodeInfo = null;
429
//
430
// public StructureViewNodeAdapter(StructureViewNode nodeInfo) {
431
// super(nodeInfo, true);
432
// this.nodeInfo = nodeInfo;
433
// }
434
//
435
// public String toString() {
436
// if (nodeInfo != null) {
437
// return nodeInfo.toString();
438
// } else {
439
// return "";
440
// }
441
// }
442
// }
443

444
445
Popular Tags