|
1 | 1 | package dataTool;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.TreeSet; |
3 | 7 |
|
| 8 | +import org.eclipse.jdt.core.IMethod; |
4 | 9 | import org.eclipse.jdt.core.JavaModelException;
|
5 | 10 | import org.eclipse.jdt.core.dom.AST;
|
6 | 11 | import org.eclipse.jdt.core.dom.ASTNode;
|
7 | 12 | import org.eclipse.jdt.core.dom.ASTParser;
|
8 | 13 | import org.eclipse.jdt.core.dom.CompilationUnit;
|
| 14 | +import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 15 | +import org.eclipse.jdt.core.dom.SimpleName; |
| 16 | +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; |
| 17 | +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditorBreadcrumb; |
| 18 | +import org.eclipse.jdt.internal.ui.javaeditor.breadcrumb.EditorBreadcrumb; |
| 19 | +import org.eclipse.jdt.internal.ui.javaeditor.breadcrumb.IBreadcrumb; |
| 20 | +import org.eclipse.jface.action.Action; |
9 | 21 | import org.eclipse.jface.text.ITextSelection;
|
10 | 22 | import org.eclipse.jface.text.Position;
|
11 | 23 | import org.eclipse.jface.text.source.SourceViewer;
|
12 | 24 | import org.eclipse.jface.viewers.ISelectionChangedListener;
|
13 | 25 | import org.eclipse.jface.viewers.SelectionChangedEvent;
|
| 26 | +import org.eclipse.ui.IEditorPart; |
| 27 | +import org.eclipse.ui.IWorkbenchPage; |
| 28 | +import org.eclipse.ui.PlatformUI; |
14 | 29 | import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
|
15 | 30 | import org.eclipse.ui.texteditor.AbstractTextEditor;
|
16 | 31 |
|
| 32 | +import dataTool.annotations.LinkAnnotation; |
17 | 33 | import dataTool.annotations.ProgramNavigationPainter;
|
18 | 34 | import dataTool.annotations.SuggestedSelectionAnnotation;
|
| 35 | +import dataTool.ui.ShowDataInBreadcrumbAction; |
19 | 36 | import edu.pdx.cs.multiview.jdt.util.JDTUtils;
|
20 | 37 | import edu.pdx.cs.multiview.jface.annotation.AnnTransaction;
|
21 | 38 | import edu.pdx.cs.multiview.jface.annotation.AnnotationPainter;
|
| 39 | +import edu.pdx.cs.multiview.jface.annotation.ISelfDrawingAnnotation; |
22 | 40 | import edu.pdx.cs.multiview.util.eclipse.EclipseHacks;
|
23 | 41 |
|
24 | 42 | public class AnnotationManager implements ISelectionChangedListener {
|
25 |
| - |
26 |
| - private SuggestedSelectionAnnotation currentAnnotation = new SuggestedSelectionAnnotation(); |
27 |
| - |
28 |
| - private AnnotationPainter painter; |
29 |
| - |
30 |
| - //the visitor for the editor |
| 43 | + |
| 44 | + private SuggestedSelectionAnnotation highlightAnnotation = new SuggestedSelectionAnnotation(); |
| 45 | + private LinkAnnotation linkAnnotation = new LinkAnnotation(); |
| 46 | + private SourceViewer sourceViewer; |
| 47 | + private ProgramNavigationPainter painter; |
| 48 | + private static boolean isActive = false; |
| 49 | + private IBreadcrumb upBreadcrumb; |
| 50 | + private IBreadcrumb downBreadcrumb; |
| 51 | + |
| 52 | + // the visitor for the editor |
31 | 53 | private Visitor visitor;
|
32 | 54 |
|
| 55 | + public static String currentSearch = null; |
| 56 | + |
33 | 57 | /**
|
34 |
| - * Creates an annotation manager given an editor, containing a |
35 |
| - * compilation unit, assumedly |
| 58 | + * Creates an annotation manager given an editor, containing a compilation |
| 59 | + * unit, assumedly |
36 | 60 | *
|
37 | 61 | * @param anEditor
|
38 | 62 | */
|
39 | 63 | public AnnotationManager(AbstractDecoratedTextEditor anEditor) {
|
40 |
| - |
41 | 64 | parseCU(anEditor);
|
42 |
| - |
43 |
| - SourceViewer sourceViewer = EclipseHacks.getSourceViewer(anEditor); |
| 65 | + sourceViewer = EclipseHacks.getSourceViewer(anEditor); |
44 | 66 | painter = new ProgramNavigationPainter(sourceViewer);
|
45 | 67 | painter.addSelectionChangedListener(this);
|
46 | 68 | sourceViewer.addPainter(painter);
|
47 |
| - |
48 |
| - selectionChanged((ITextSelection)painter.getSelection()); |
| 69 | + selectionChanged((ITextSelection) painter.getSelection()); |
49 | 70 | }
|
50 | 71 |
|
51 | 72 | public void selectionChanged(ITextSelection selection) {
|
52 | 73 | painter.removeAllAnnotations();
|
53 |
| - try { |
54 |
| - |
55 |
| - ASTNode one = getNode(selection.getOffset()); |
56 |
| - |
57 |
| - if(selection.getLength()==0){ |
58 |
| - addAnnotation(one, one); |
59 |
| - return; |
| 74 | + try { |
| 75 | + DataNode one = getNode(selection.getOffset()); |
| 76 | + Finder finder = Finder.getInstance(); |
| 77 | + if(one != null) { |
| 78 | + addAnnotation(one); |
| 79 | + currentSearch = one.getBinding(); |
| 80 | + IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 81 | + IEditorPart activeEditor = activePage.getActiveEditor(); |
| 82 | + JavaEditor j = (JavaEditor) activeEditor; |
| 83 | + upBreadcrumb = j.getBreadcrumb(); |
| 84 | + downBreadcrumb = j.getBreadcrumb2(); |
| 85 | + if(!isActive) { |
| 86 | + isActive = true; |
| 87 | + upBreadcrumb.setText(null); |
| 88 | + downBreadcrumb.setText(null); |
| 89 | + ShowDataInBreadcrumbAction crumbs = new ShowDataInBreadcrumbAction(j, activePage); |
| 90 | + crumbs.run(); |
| 91 | + } |
| 92 | + DataCallHierarchy call = new DataCallHierarchy(); |
| 93 | + Set<IMethod> searchUp = null; |
| 94 | + Set<IMethod> searchDown = null; |
| 95 | + if((finder.upSearch(one) != null || finder.downSearch(one) != null || one.getInvocationMethod() != null) && currentSearch != null) { |
| 96 | + searchUp = call.searchProject(one, Finder.UP); |
| 97 | + searchDown = call.searchProject(one, Finder.DOWN); |
| 98 | + if(one.isParameterSelected(selection.getOffset())) { |
| 99 | + linkAnnotation.searchResultsDown = searchDown; |
| 100 | + linkAnnotation.searchResultsUp = searchUp; |
| 101 | + linkAnnotation.setDataNode(one); |
| 102 | + addLinkAnnotation(one); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + //Adds all occurrences of data node off screen |
| 107 | + ArrayList<Object> textUp = new ArrayList<Object>(); |
| 108 | + ArrayList<Object> textDown = new ArrayList<Object>(); |
| 109 | + for(DataNode dn: finder.getOccurrences(one.getValue(), new Position(one.getStartPosition(), one.getLength()))) { |
| 110 | + int[] offScreen = new int[3]; |
| 111 | + int line = sourceViewer.widgetLineOfWidgetOffset(dn.getStartPosition())+1; |
| 112 | + offScreen[0] = line; |
| 113 | + offScreen[1] = dn.getStartPosition(); |
| 114 | + offScreen[2] = dn.getLength(); |
| 115 | + if(dn.getStartPosition() < sourceViewer.getTopIndexStartOffset()) { |
| 116 | + textUp.add(offScreen); |
| 117 | + } |
| 118 | + else if(dn.getStartPosition() > sourceViewer.getBottomIndexEndOffset()) { |
| 119 | + textDown.add(offScreen); |
| 120 | + } |
| 121 | + } |
| 122 | + if(searchUp != null) { |
| 123 | + textUp.addAll(searchUp); |
| 124 | + ((EditorBreadcrumb)upBreadcrumb).setSearchMethod(call.getCurrentMethod(one.getStartPosition())); |
| 125 | + } |
| 126 | + if(searchDown != null) { |
| 127 | + textDown.addAll(searchDown); |
| 128 | + } |
| 129 | + upBreadcrumb.setText(textUp); |
| 130 | + downBreadcrumb.setText(textDown); |
60 | 131 | }
|
61 |
| - |
62 |
| - ASTNode two = getNode(selection.getOffset()+selection.getLength()); |
63 |
| - |
64 |
| - if(one!=null){ |
65 |
| - if(!areSiblings(one,two)) |
66 |
| - two = meet(one,two); |
67 |
| - |
68 |
| - if(two!=null) |
69 |
| - addAnnotation(one, two); |
70 |
| - |
71 |
| - }else{ |
| 132 | + else { |
72 | 133 | removeAnnotations();
|
73 | 134 | }
|
74 |
| - |
75 | 135 | } catch (Exception e) {
|
76 | 136 | Activator.logError(e);
|
77 | 137 | removeAnnotations();
|
78 |
| - } |
| 138 | + } |
79 | 139 | }
|
| 140 | + |
| 141 | + private void addAnnotation(DataNode node) { |
| 142 | + int start = node.getStartPosition(); |
| 143 | + int end = node.getStartPosition() + node.getLength(); |
80 | 144 |
|
81 |
| - private void addAnnotation(ASTNode one, ASTNode two) { |
82 |
| - int start = one.getStartPosition(); |
83 |
| - int end = two.getStartPosition()+two.getLength(); |
84 |
| - |
85 |
| - if(!isAlreadyAnnotated(start, end)) |
86 |
| - addAnnotationsAt(start, end-start); |
| 145 | + if (!isAlreadyAnnotated(start, end)) |
| 146 | + addAnnotationsAt(start, end - start, true); |
| 147 | + } |
| 148 | + |
| 149 | + private void addLinkAnnotation(DataNode node) { |
| 150 | + SimpleName method; |
| 151 | + if(node.getInvocationMethod() != null) { |
| 152 | + method = node.getInvocationMethod().getName(); |
| 153 | + } |
| 154 | + else { |
| 155 | + method = node.getDeclarationMethod().getName(); |
| 156 | + } |
| 157 | + int start = method.getStartPosition(); |
| 158 | + int end = method.getStartPosition() + method.getLength(); |
| 159 | + if(!isAlreadyAnnotated(start, end)) { |
| 160 | + addAnnotationsAt(start, end - start, false); |
| 161 | + } |
87 | 162 | }
|
88 | 163 |
|
89 | 164 | private boolean areSiblings(ASTNode one, ASTNode two) {
|
90 |
| - |
| 165 | + |
91 | 166 | return one.getParent().equals(two.getParent());
|
92 | 167 | }
|
93 | 168 |
|
94 | 169 | private ASTNode meet(ASTNode from, ASTNode to) {
|
95 | 170 |
|
96 |
| - if(areSiblings(from,to)) |
| 171 | + if (areSiblings(from, to)) |
97 | 172 | return to;
|
98 |
| - |
99 |
| - if(to.getParent()==null) |
| 173 | + |
| 174 | + if (to.getParent() == null) |
100 | 175 | return null;
|
101 |
| - |
102 |
| - return meet(from,to.getParent()); |
| 176 | + |
| 177 | + return meet(from, to.getParent()); |
103 | 178 | }
|
104 | 179 |
|
105 | 180 | private boolean isAlreadyAnnotated(int start, int end) {
|
106 |
| - |
107 |
| - Position headPosition = painter.getPosition(currentAnnotation); |
108 |
| - |
109 |
| - if(headPosition!=null) |
110 |
| - return headPosition.getOffset()==start && |
111 |
| - headPosition.getOffset()+headPosition.getLength()==end; |
112 |
| - |
| 181 | + |
| 182 | + Position headPosition = painter.getPosition(highlightAnnotation); |
| 183 | + |
| 184 | + if (headPosition != null) |
| 185 | + return headPosition.getOffset() == start && headPosition.getOffset() + headPosition.getLength() == end; |
| 186 | + |
113 | 187 | return false;
|
114 | 188 | }
|
115 | 189 |
|
116 |
| - private ASTNode getNode(int position) { |
| 190 | + private DataNode getNode(int position) { |
117 | 191 | return visitor.statementAt(position);
|
118 | 192 | }
|
119 |
| - |
120 |
| - private void parseCU(AbstractTextEditor editor){ |
| 193 | + |
| 194 | + private void parseCU(AbstractTextEditor editor) { |
121 | 195 | try {
|
122 | 196 | visitor = parse(JDTUtils.getCUSource(editor));
|
123 | 197 | } catch (JavaModelException e) {
|
124 | 198 | Activator.logError(e);
|
125 | 199 | }
|
126 | 200 | }
|
127 |
| - |
128 |
| - private static Visitor parse(String source) throws JavaModelException{ |
129 |
| - |
| 201 | + |
| 202 | + private static Visitor parse(String source) throws JavaModelException { |
| 203 | + |
130 | 204 | ASTParser parser = ASTParser.newParser(AST.JLS3);
|
131 | 205 | parser.setSource(source.toCharArray());
|
132 | 206 | CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
|
133 |
| - |
134 |
| - Visitor visitor = new Visitor(source); |
| 207 | + |
| 208 | + Visitor visitor = new Visitor(source); |
135 | 209 | astRoot.accept(visitor);
|
136 |
| - |
| 210 | + |
137 | 211 | return visitor;
|
138 | 212 | }
|
139 | 213 |
|
140 |
| - private void addAnnotationsAt(int statementStart, int length) { |
141 |
| - |
| 214 | + private void addAnnotationsAt(int statementStart, int length, boolean isHighlight) { |
| 215 | + |
142 | 216 | AnnTransaction anns = new AnnTransaction();
|
143 |
| - anns.remove(currentAnnotation); |
144 |
| - anns.add(currentAnnotation, new Position(statementStart,length)); |
145 |
| - |
| 217 | + if(isHighlight) { |
| 218 | + anns.remove(highlightAnnotation); |
| 219 | + anns.add(highlightAnnotation, new Position(statementStart, length)); |
| 220 | + } |
| 221 | + else { |
| 222 | + anns.remove(linkAnnotation); |
| 223 | + anns.add(linkAnnotation, new Position(statementStart, length)); |
| 224 | + } |
146 | 225 | painter.replaceAnnotations(anns);
|
147 | 226 | }
|
148 |
| - |
149 |
| - public void removeAnnotations(){ |
150 |
| - try{ |
151 |
| - if(currentAnnotation!=null){ |
152 |
| - //painter.removeAnnotation(currentAnnotation); |
| 227 | + |
| 228 | + public void removeAnnotations() { |
| 229 | + try { |
| 230 | + if (highlightAnnotation != null) { |
153 | 231 | painter.removeAllAnnotations();
|
154 | 232 | }
|
155 |
| - }catch(Exception ignore){} |
| 233 | + } catch (Exception ignore) { |
| 234 | + } |
156 | 235 | }
|
157 |
| - |
| 236 | + |
158 | 237 | public void dispose() {
|
159 | 238 | painter.dispose();
|
| 239 | + currentSearch = null; |
| 240 | + } |
| 241 | + |
| 242 | + public void deactivate() { |
| 243 | + isActive = false; |
| 244 | + upBreadcrumb.dispose(); |
| 245 | + downBreadcrumb.dispose(); |
160 | 246 | }
|
161 | 247 |
|
162 | 248 | public void selectionChanged(SelectionChangedEvent event) {
|
163 |
| - painter.removeAllAnnotations(); |
164 |
| - selectionChanged((ITextSelection)event.getSelection()); |
| 249 | + selectionChanged((ITextSelection) event.getSelection()); |
165 | 250 | }
|
166 | 251 | }
|
0 commit comments