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.4KB

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