You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StructureViewManager.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.ui;
  15. import java.util.ArrayList;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import org.aspectj.ajde.Ajde;
  19. import org.aspectj.ajde.ui.internal.NavigationHistoryModel;
  20. import org.aspectj.ajde.ui.internal.TreeStructureViewBuilder;
  21. import org.aspectj.asm.IHierarchy;
  22. import org.aspectj.asm.IHierarchyListener;
  23. import org.aspectj.asm.IProgramElement;
  24. import org.aspectj.asm.IRelationship;
  25. import org.aspectj.asm.internal.AspectJElementHierarchy;
  26. /**
  27. * @author Mik Kersten
  28. */
  29. public class StructureViewManager {
  30. private final TreeStructureViewBuilder treeViewBuilder;
  31. // private String buildConfigFilePath = null;
  32. private final NavigationHistoryModel historyModel = new NavigationHistoryModel();
  33. private final ArrayList structureViews = new ArrayList();
  34. private FileStructureView defaultFileView = null;
  35. private static final StructureViewProperties DEFAULT_VIEW_PROPERTIES;
  36. private static final List AVAILABLE_RELATIONS;
  37. public final IHierarchyListener VIEW_LISTENER = new IHierarchyListener() {
  38. public void elementsUpdated(IHierarchy model) {
  39. // updating structure views:
  40. for (Object structureView : structureViews) {
  41. treeViewBuilder.buildView((StructureView) structureView, (AspectJElementHierarchy) model);
  42. }
  43. }
  44. };
  45. /**
  46. * @param nodeFactory concrete factory for creating view nodes
  47. */
  48. public StructureViewManager(StructureViewNodeFactory nodeFactory) {
  49. treeViewBuilder = new TreeStructureViewBuilder(nodeFactory);
  50. Ajde.getDefault().getModel().addListener(VIEW_LISTENER);
  51. }
  52. public void fireNavigateBackAction(StructureView view) {
  53. IProgramElement backNode = historyModel.navigateBack();
  54. if (backNode == null) {
  55. Ajde.getDefault().getIdeUIAdapter().displayStatusInformation("No node to navigate back to in history");
  56. } else {
  57. navigationAction(backNode, false);
  58. }
  59. }
  60. public void fireNavigateForwardAction(StructureView view) {
  61. IProgramElement forwardNode = historyModel.navigateForward();
  62. if (forwardNode == null) {
  63. Ajde.getDefault().getIdeUIAdapter().displayStatusInformation("No node to navigate forward to in history");
  64. } else {
  65. navigationAction(forwardNode, false);
  66. }
  67. }
  68. /**
  69. * Only navigations of the default view are registered with the history.
  70. *
  71. * @param newFilePath the canonicalized path to the new file
  72. */
  73. public void fireNavigationAction(String newFilePath, int lineNumber) {
  74. IProgramElement currNode = Ajde.getDefault().getModel().getHierarchy().findElementForSourceLine(newFilePath, lineNumber);
  75. if (currNode != null) {
  76. navigationAction(currNode, true);
  77. }
  78. }
  79. /**
  80. * History is recorded for {@link LinkNode} navigations.
  81. */
  82. public void fireNavigationAction(IProgramElement pe, boolean isLink) {
  83. navigationAction(pe, isLink);
  84. }
  85. /**
  86. * Highlights the given node in all structure views. If the node represents code and as such is below the granularity visible in
  87. * the view the parent is highlighted, along with the corresponding sourceline.
  88. */
  89. private void navigationAction(IProgramElement node, boolean recordHistory) {
  90. if (node == null)
  91. return;
  92. // navigating to node: " + node + ", recordHistory: " + recordHistory
  93. if (recordHistory)
  94. historyModel.navigateToNode(node);
  95. if (defaultFileView != null && node.getSourceLocation() != null) {
  96. String newFilePath = node.getSourceLocation().getSourceFile().getAbsolutePath();
  97. if (defaultFileView.getSourceFile() != null && !defaultFileView.getSourceFile().equals(newFilePath)) {
  98. defaultFileView.setSourceFile(newFilePath);
  99. treeViewBuilder.buildView(defaultFileView, Ajde.getDefault().getModel().getHierarchy());
  100. }
  101. }
  102. for (Object structureView : structureViews) {
  103. StructureView view = (StructureView) structureView;
  104. if (!(view instanceof GlobalStructureView) || !recordHistory || defaultFileView == null) {
  105. if (node.getKind().equals(IProgramElement.Kind.CODE)) {
  106. IProgramElement parentNode = node.getParent();
  107. if (parentNode != null) {
  108. IStructureViewNode currNode = view.findCorrespondingViewNode(parentNode);
  109. int lineOffset = node.getSourceLocation().getLine() - parentNode.getSourceLocation().getLine();
  110. if (currNode != null)
  111. view.setActiveNode(currNode, lineOffset);
  112. }
  113. } else {
  114. IStructureViewNode currNode = view.findCorrespondingViewNode(node);
  115. if (currNode != null)
  116. view.setActiveNode(currNode);
  117. }
  118. }
  119. }
  120. }
  121. public void refreshView(StructureView view) {
  122. IStructureViewNode activeNode = view.getActiveNode();
  123. treeViewBuilder.buildView(view, Ajde.getDefault().getModel().getHierarchy());
  124. view.setActiveNode(activeNode);
  125. }
  126. public StructureViewProperties getDefaultViewProperties() {
  127. return DEFAULT_VIEW_PROPERTIES;
  128. }
  129. /**
  130. * Returns the list of all available relations.
  131. */
  132. public List getAvailableRelations() {
  133. return AVAILABLE_RELATIONS;
  134. }
  135. /**
  136. * @param properties can not be null
  137. */
  138. public GlobalStructureView createGlobalView(GlobalViewProperties properties) {
  139. GlobalStructureView view = new GlobalStructureView(properties);
  140. structureViews.add(view);
  141. return view;
  142. }
  143. /**
  144. * @param sourceFilePath full path to corresponding source file
  145. * @param properties if null default properties will be used
  146. * @return always returns a view intance
  147. */
  148. public FileStructureView createViewForSourceFile(String sourceFilePath, StructureViewProperties properties) {
  149. // creating view for file:
  150. if (properties == null)
  151. properties = DEFAULT_VIEW_PROPERTIES;
  152. FileStructureView view = new FileStructureView(properties);
  153. view.setSourceFile(sourceFilePath);
  154. treeViewBuilder.buildView(view, Ajde.getDefault().getModel().getHierarchy());
  155. structureViews.add(view);
  156. return view;
  157. }
  158. /**
  159. * @return true if the view was found and removed, false otherwise
  160. */
  161. public boolean deleteView(StructureView view) {
  162. return structureViews.remove(view);
  163. }
  164. public void setDefaultFileView(FileStructureView defaultFileView) {
  165. this.defaultFileView = defaultFileView;
  166. }
  167. public FileStructureView getDefaultFileView() {
  168. return defaultFileView;
  169. }
  170. static {
  171. AVAILABLE_RELATIONS = new ArrayList();
  172. AVAILABLE_RELATIONS.add(IRelationship.Kind.ADVICE);
  173. AVAILABLE_RELATIONS.add(IRelationship.Kind.DECLARE);
  174. DEFAULT_VIEW_PROPERTIES = new StructureViewProperties();
  175. DEFAULT_VIEW_PROPERTIES.setRelations(AVAILABLE_RELATIONS);
  176. }
  177. }
  178. // this.multiFileViewMode = multiFileViewMode;
  179. // if (!multiFileViewMode) {
  180. // structureViews.add(DEFAULT_FILE_VIEW);
  181. // structureViews.add(DECLARATION_VIEW);
  182. // structureViews.add(CROSSCUTTING_VIEW);
  183. // structureViews.add(INHERITANCE_VIEW);
  184. // }
  185. // public GlobalStructureView getGlobalStructureView(StructureViewProperties.Hierarchy hierarchy) {
  186. // if (hierarchy == StructureViewProperties.Hierarchy.CROSSCUTTING) {
  187. // return CROSSCUTTING_VIEW;
  188. // } else if (hierarchy == StructureViewProperties.Hierarchy.INHERITANCE) {
  189. // return INHERITANCE_VIEW;
  190. // } else {
  191. // return DECLARATION_VIEW;
  192. // }
  193. // }
  194. // public FileStructureView getDefaultFileStructureView() {
  195. // return DEFAULT_FILE_VIEW;
  196. // }