KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > StructureViewManager


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;
27
28 import java.util.*;
29 import org.aspectj.ajde.*;
30 import org.aspectj.ajde.ui.internal.*;
31 import org.aspectj.asm.*;
32 import org.aspectj.asm.associations.*;
33
34 /**
35  * @author Mik Kersten
36  */

37 public class StructureViewManager {
38
39     private TreeStructureViewBuilder treeViewBuilder;
40     private String JavaDoc buildConfigFilePath = null;
41
42     private NavigationHistoryModel historyModel = new NavigationHistoryModel();
43     private ArrayList structureViews = new ArrayList();
44     private FileStructureView defaultFileView = null;
45     
46     private static final StructureViewProperties DEFAULT_VIEW_PROPERTIES;
47     private static final List AVAILABLE_RELATIONS;
48     
49     public final StructureModelListener VIEW_LISTENER = new StructureModelListener() {
50         public void modelUpdated(StructureModel model) {
51             Ajde.getDefault().logEvent("updating structure views: " + structureViews);
52             
53             if (defaultFileView != null) {
54                 defaultFileView.setSourceFile(Ajde.getDefault().getEditorManager().getCurrFile());
55             }
56             
57             for (Iterator it = structureViews.iterator(); it.hasNext(); ) {
58                 treeViewBuilder.buildView((StructureView)it.next(), (StructureModel)model);
59             }
60         }
61     };
62   
63     /**
64      * @param nodeFactory concrete factory for creating view nodes
65      */

66     public StructureViewManager(StructureViewNodeFactory nodeFactory) {
67         treeViewBuilder = new TreeStructureViewBuilder(nodeFactory);
68                 
69         StructureModelManager.INSTANCE.addListener(VIEW_LISTENER);
70     }
71     
72     public void fireNavigateBackAction(StructureView view) {
73         ProgramElementNode backNode = historyModel.navigateBack();
74         
75         if (backNode == null) {
76             Ajde.getDefault().getIdeUIAdapter().displayStatusInformation("No node to navigate back to in history");
77         } else {
78             navigationAction(backNode, false);
79         }
80     }
81   
82     public void fireNavigateForwardAction(StructureView view) {
83         ProgramElementNode forwardNode = historyModel.navigateForward();
84         
85         if (forwardNode == null) {
86             Ajde.getDefault().getIdeUIAdapter().displayStatusInformation("No node to navigate forward to in history");
87         } else {
88             navigationAction(forwardNode, false);
89         }
90     }
91
92     /**
93      * Only navigations of the default view are registered with
94      * the history.
95      */

96     public void fireNavigationAction(String JavaDoc newFilePath, int lineNumber) {
97         StructureNode currNode = Ajde.getDefault().getStructureModelManager().getStructureModel().findNodeForSourceLine(
98             newFilePath,
99             lineNumber);
100         
101         if (currNode instanceof ProgramElementNode) {
102             navigationAction((ProgramElementNode)currNode, true);
103         }
104     }
105         
106     /**
107      * History is recorded for {@link LinkNode} navigations.
108      */

109     public void fireNavigationAction(StructureNode structureNode) {
110         ProgramElementNode node = null;
111         boolean recordHistory = false;
112         if (structureNode instanceof LinkNode) {
113             node = ((LinkNode)structureNode).getProgramElementNode();
114             recordHistory = true;
115         } else if (structureNode instanceof ProgramElementNode) {
116             node = (ProgramElementNode)structureNode;
117         }
118         if (node != null) navigationAction(node, recordHistory);
119     }
120
121     /**
122      * Highlights the given node in all structure views. If the node represents code
123      * and as such is below the granularity visible in the view the parent is highlighted,
124      * along with the corresponding sourceline.
125      */

126     private void navigationAction(ProgramElementNode node, boolean recordHistory) {
127         if (node == null
128             || node == Ajde.getDefault().getStructureModelManager().getStructureModel().NO_STRUCTURE
129             || node.getSourceLocation().getSourceFilePath() == null) {
130             Ajde.getDefault().getIdeUIAdapter().displayStatusInformation("Source not available for node: " + node.getName());
131             return;
132         }
133         Ajde.getDefault().logEvent("navigating to node: " + node + ", recordHistory: " + recordHistory);
134         if (recordHistory) historyModel.navigateToNode(node);
135         if (defaultFileView != null && node.getSourceLocation() != null) {
136             String JavaDoc newFilePath = node.getSourceLocation().getSourceFilePath();
137             if (defaultFileView.getSourceFile() != null
138                 && !defaultFileView.getSourceFile().equals(newFilePath)) {
139                 defaultFileView.setSourceFile(newFilePath);
140                 treeViewBuilder.buildView(defaultFileView, StructureModelManager.INSTANCE.getStructureModel());
141             }
142         }
143            
144         for (Iterator it = structureViews.iterator(); it.hasNext(); ) {
145             StructureView view = (StructureView)it.next();
146             if (!(view instanceof GlobalStructureView) || !recordHistory || defaultFileView == null) {
147                 if (node.getProgramElementKind().equals(ProgramElementNode.Kind.CODE)) {
148                     ProgramElementNode parentNode = (ProgramElementNode)node.getParent();
149                     if (parentNode != null) {
150                         StructureViewNode currNode = view.findCorrespondingViewNode(parentNode);
151                         int lineOffset = node.getSourceLocation().getLineNumber() - parentNode.getSourceLocation().getLineNumber();
152                         if (currNode != null) view.setActiveNode(currNode, lineOffset);
153                     }
154                 } else {
155                     StructureViewNode currNode = view.findCorrespondingViewNode(node);
156                     if (currNode != null) view.setActiveNode(currNode);
157                 }
158             }
159         }
160     }
161     
162     private ProgramElementNode getProgramElementNode(StructureViewNode node) {
163         if (node.getStructureNode() instanceof ProgramElementNode) {
164             return (ProgramElementNode)node.getStructureNode();
165         } else if (node.getStructureNode() instanceof LinkNode) {
166             return ((LinkNode)node.getStructureNode()).getProgramElementNode();
167         } else {
168             return null;
169         }
170     }
171     
172     public void refreshView(StructureView view) {
173         StructureViewNode activeNode = view.getActiveNode();
174         treeViewBuilder.buildView(view, Ajde.getDefault().getStructureModelManager().getStructureModel());
175         view.setActiveNode(activeNode);
176     }
177
178     public StructureViewProperties getDefaultViewProperties() {
179         return DEFAULT_VIEW_PROPERTIES;
180     }
181
182     /**
183      * Returns the list of all available relations.
184      */

185     public List getAvailableRelations() {
186         return AVAILABLE_RELATIONS;
187     }
188     
189     /**
190      * @param properties can not be null
191      */

192     public GlobalStructureView createGlobalView(GlobalViewProperties properties) {
193         GlobalStructureView view = new GlobalStructureView(properties);
194         structureViews.add(view);
195         return view;
196     }
197     
198     /**
199      * @param sourceFilePath full path to corresponding source file
200      * @param properties if null default properties will be used
201      * @return always returns a view intance
202      */

203     public FileStructureView createViewForSourceFile(String JavaDoc sourceFilePath, StructureViewProperties properties) {
204         Ajde.getDefault().logEvent("creating view for file: " + sourceFilePath);
205         if (properties == null) properties = DEFAULT_VIEW_PROPERTIES;
206         FileStructureView view = new FileStructureView(properties);
207         view.setSourceFile(sourceFilePath);
208         treeViewBuilder.buildView(view, StructureModelManager.INSTANCE.getStructureModel());
209         structureViews.add(view);
210         return view;
211     }
212
213     /**
214      * @return true if the view was found and removed, false otherwise
215      */

216     public boolean deleteView(StructureView view) {
217         return structureViews.remove(view);
218     }
219
220     public void setDefaultFileView(FileStructureView defaultFileView) {
221         this.defaultFileView = defaultFileView;
222     }
223
224     static {
225         AVAILABLE_RELATIONS = new ArrayList();
226         AVAILABLE_RELATIONS.add(Advice.METHOD_CALL_SITE_RELATION);
227         AVAILABLE_RELATIONS.add(Advice.METHOD_RELATION);
228         AVAILABLE_RELATIONS.add(Advice.CONSTRUCTOR_CALL_SITE_RELATION);
229         AVAILABLE_RELATIONS.add(Advice.CONSTRUCTOR_RELATION);
230         AVAILABLE_RELATIONS.add(Advice.FIELD_ACCESS_RELATION);
231         AVAILABLE_RELATIONS.add(Advice.INITIALIZER_RELATION);
232         AVAILABLE_RELATIONS.add(Advice.HANDLER_RELATION);
233         AVAILABLE_RELATIONS.add(Advice.INTRODUCTION_RELATION);
234         AVAILABLE_RELATIONS.add(Introduction.INTRODUCES_RELATION);
235         AVAILABLE_RELATIONS.add(Inheritance.IMPLEMENTS_RELATION);
236         AVAILABLE_RELATIONS.add(Inheritance.INHERITS_RELATION);
237         AVAILABLE_RELATIONS.add(Inheritance.INHERITS_MEMBERS_RELATION);
238         AVAILABLE_RELATIONS.add(Reference.USES_POINTCUT_RELATION);
239         AVAILABLE_RELATIONS.add(Reference.IMPORTS_RELATION);
240         
241         DEFAULT_VIEW_PROPERTIES = new StructureViewProperties();
242         DEFAULT_VIEW_PROPERTIES.setRelations(AVAILABLE_RELATIONS);
243     }
244 }
245
246 // this.multiFileViewMode = multiFileViewMode;
247
// if (!multiFileViewMode) {
248
// structureViews.add(DEFAULT_FILE_VIEW);
249
// structureViews.add(DECLARATION_VIEW);
250
// structureViews.add(CROSSCUTTING_VIEW);
251
// structureViews.add(INHERITANCE_VIEW);
252
// }
253

254 // public GlobalStructureView getGlobalStructureView(StructureViewProperties.Hierarchy hierarchy) {
255
// if (hierarchy == StructureViewProperties.Hierarchy.CROSSCUTTING) {
256
// return CROSSCUTTING_VIEW;
257
// } else if (hierarchy == StructureViewProperties.Hierarchy.INHERITANCE) {
258
// return INHERITANCE_VIEW;
259
// } else {
260
// return DECLARATION_VIEW;
261
// }
262
// }
263

264 // public FileStructureView getDefaultFileStructureView() {
265
// return DEFAULT_FILE_VIEW;
266
// }
267

268
269
Popular Tags