Skip to content

Commit da161b1

Browse files
author
Chris
committed
Moving breadcrumb logic to EnableNavigationAction, added toggle state for Flower icon
1 parent 84a64a7 commit da161b1

6 files changed

Lines changed: 111 additions & 81 deletions

File tree

dataTool/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
toolbarPath="TempGroup"
2222
tooltip="Highlights flow of data within your code"/>
2323
</actionSet>
24+
<command id="org.eclipse.example.command.toggle"
25+
name="Toggle Me">
26+
<state class="org.eclipse.jface.commands.ToggleState"
27+
id="org.eclipse.example.command.toggleState:true" />
28+
</command>
2429
</extension>
2530
<extension
2631
point="org.eclipse.ui.commands">

dataTool/src/dataTool/AnnotationManager.java

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public class AnnotationManager implements ISelectionChangedListener {
4646
private SourceViewer sourceViewer;
4747
private ProgramNavigationPainter painter;
4848
private static boolean isActive = false;
49+
private boolean isEnabled;
4950
private IBreadcrumb upBreadcrumb;
5051
private IBreadcrumb downBreadcrumb;
51-
private ShowDataInBreadcrumbAction crumbs;
5252

5353
// the visitor for the editor
5454
private Visitor visitor;
@@ -62,6 +62,7 @@ public class AnnotationManager implements ISelectionChangedListener {
6262
* @param anEditor
6363
*/
6464
public AnnotationManager(AbstractDecoratedTextEditor anEditor) {
65+
isEnabled = true;
6566
parseCU(anEditor);
6667
sourceViewer = EclipseHacks.getSourceViewer(anEditor);
6768
painter = new ProgramNavigationPainter(sourceViewer);
@@ -72,74 +73,74 @@ public AnnotationManager(AbstractDecoratedTextEditor anEditor) {
7273

7374
public void selectionChanged(ITextSelection selection) {
7475
painter.removeAllAnnotations();
75-
try {
76-
DataNode one = getNode(selection.getOffset());
77-
Finder finder = Finder.getInstance();
78-
if(one != null) {
79-
addAnnotation(one);
80-
currentSearch = one.getKey();
81-
linkAnnotation.setDataNode(one);
82-
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
83-
IEditorPart activeEditor = activePage.getActiveEditor();
84-
JavaEditor j = (JavaEditor) activeEditor;
85-
upBreadcrumb = j.getBreadcrumb();
86-
downBreadcrumb = j.getBreadcrumb2();
87-
if(!isActive) {
88-
isActive = true;
89-
upBreadcrumb.setText(null);
90-
downBreadcrumb.setText(null);
91-
crumbs = new ShowDataInBreadcrumbAction(j, activePage);
92-
crumbs.run();
93-
}
94-
DataCallHierarchy call = new DataCallHierarchy();
95-
Set<IMethod> searchUp = null;
96-
Set<IMethod> searchDown = null;
97-
if((finder.upSearch(one) != null || finder.downSearch(one) != null || one.getInvocationMethod() != null) && currentSearch != null) {
98-
searchUp = call.searchProject(one, Finder.UP);
99-
searchDown = call.searchProject(one, Finder.DOWN);
100-
if(one.isParameterSelected(selection.getOffset())) {
101-
linkAnnotation.searchResultsDown = searchDown;
102-
linkAnnotation.searchResultsUp = searchUp;
103-
addLinkAnnotation(one);
76+
if(isEnabled) {
77+
try {
78+
DataNode one = getNode(selection.getOffset());
79+
Finder finder = Finder.getInstance();
80+
if(one != null) {
81+
addAnnotation(one);
82+
currentSearch = one.getKey();
83+
linkAnnotation.setDataNode(one);
84+
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
85+
IEditorPart activeEditor = activePage.getActiveEditor();
86+
JavaEditor j = (JavaEditor) activeEditor;
87+
upBreadcrumb = j.getBreadcrumb();
88+
downBreadcrumb = j.getBreadcrumb2();
89+
if(!isActive) {
90+
isActive = true;
91+
upBreadcrumb.setText(null);
92+
downBreadcrumb.setText(null);
10493
}
105-
}
106-
107-
//Adds all occurrences of data node off screen
108-
ArrayList<Object> textUp = new ArrayList<Object>();
109-
ArrayList<Object> textDown = new ArrayList<Object>();
110-
for(DataNode dn: finder.getOccurrences(new Position(one.getStartPosition(), one.getLength()))) {
111-
addLinkAnnotation(dn);
112-
int[] offScreen = new int[3];
113-
int line = sourceViewer.widgetLineOfWidgetOffset(dn.getStartPosition())+1;
114-
offScreen[0] = line;
115-
offScreen[1] = dn.getStartPosition();
116-
offScreen[2] = dn.getLength();
117-
if(dn.getStartPosition() < sourceViewer.getTopIndexStartOffset()) {
118-
textUp.add(offScreen);
94+
DataCallHierarchy call = new DataCallHierarchy();
95+
Set<IMethod> searchUp = null;
96+
Set<IMethod> searchDown = null;
97+
if((finder.upSearch(one) != null || finder.downSearch(one) != null || one.getInvocationMethod() != null) && currentSearch != null) {
98+
searchUp = call.searchProject(one, Finder.UP);
99+
searchDown = call.searchProject(one, Finder.DOWN);
100+
if(one.isParameterSelected(selection.getOffset())) {
101+
linkAnnotation.searchResultsDown = searchDown;
102+
linkAnnotation.searchResultsUp = searchUp;
103+
addLinkAnnotation(one);
104+
}
119105
}
120-
else if(dn.getStartPosition() > sourceViewer.getBottomIndexEndOffset()) {
121-
textDown.add(offScreen);
106+
107+
//Adds all occurrences of data node off screen
108+
ArrayList<Object> textUp = new ArrayList<Object>();
109+
ArrayList<Object> textDown = new ArrayList<Object>();
110+
for(DataNode dn: finder.getOccurrences(new Position(one.getStartPosition(), one.getLength()))) {
111+
addLinkAnnotation(dn);
112+
int[] offScreen = new int[3];
113+
int line = sourceViewer.widgetLineOfWidgetOffset(dn.getStartPosition())+1;
114+
offScreen[0] = line;
115+
offScreen[1] = dn.getStartPosition();
116+
offScreen[2] = dn.getLength();
117+
if(dn.getStartPosition() < sourceViewer.getTopIndexStartOffset()) {
118+
textUp.add(offScreen);
119+
}
120+
else if(dn.getStartPosition() > sourceViewer.getBottomIndexEndOffset()) {
121+
textDown.add(offScreen);
122+
}
122123
}
124+
if(searchUp != null) {
125+
textUp.addAll(searchUp);
126+
((EditorBreadcrumb)upBreadcrumb).setSearchMethod(call.getCurrentMethod(one.getStartPosition()));
127+
((EditorBreadcrumb)upBreadcrumb).setSearchIndex(one.getParameterIndex());
128+
linkAnnotation.setSearchMethod(call.getCurrentMethod(one.getStartPosition()));
129+
}
130+
if(searchDown != null) {
131+
textDown.addAll(searchDown);
132+
((EditorBreadcrumb)downBreadcrumb).setSearchIndex(one.getParameterIndex());
133+
}
134+
upBreadcrumb.setText(textUp);
135+
downBreadcrumb.setText(textDown);
123136
}
124-
if(searchUp != null) {
125-
textUp.addAll(searchUp);
126-
((EditorBreadcrumb)upBreadcrumb).setSearchMethod(call.getCurrentMethod(one.getStartPosition()));
127-
((EditorBreadcrumb)upBreadcrumb).setSearchIndex(one.getParameterIndex());
128-
linkAnnotation.setSearchMethod(call.getCurrentMethod(one.getStartPosition()));
129-
}
130-
if(searchDown != null) {
131-
textDown.addAll(searchDown);
132-
((EditorBreadcrumb)downBreadcrumb).setSearchIndex(one.getParameterIndex());
137+
else {
138+
removeAnnotations();
133139
}
134-
upBreadcrumb.setText(textUp);
135-
downBreadcrumb.setText(textDown);
136-
}
137-
else {
140+
} catch (Exception e) {
141+
Activator.logError(e);
138142
removeAnnotations();
139143
}
140-
} catch (Exception e) {
141-
Activator.logError(e);
142-
removeAnnotations();
143144
}
144145
}
145146

@@ -239,25 +240,31 @@ public void removeAnnotations() {
239240
try {
240241
if (highlightAnnotation != null) {
241242
painter.removeAllAnnotations();
243+
painter.dispose();
242244
}
243245
} catch (Exception ignore) {
246+
System.out.println("ignore");
244247
}
245248
}
246249

250+
/**
251+
* Keeps track of if tool is enabled or disabled.
252+
*/
253+
public void setEnabled(boolean flag){
254+
isEnabled = flag;
255+
}
256+
247257
public void dispose() {
248258
painter.dispose();
249259
currentSearch = null;
250260
}
251261

252262
public void deactivate() {
253263
isActive = false;
254-
crumbs.stop();
255-
upBreadcrumb.dispose();
256-
downBreadcrumb.dispose();
264+
isEnabled = false;
257265
removeAnnotations();
258-
dispose();
259266
}
260-
267+
261268
public void selectionChanged(SelectionChangedEvent event) {
262269
selectionChanged((ITextSelection) event.getSelection());
263270
}

dataTool/src/dataTool/EnableNavigationAction.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import java.util.HashSet;
33
import java.util.Set;
44

5+
import org.eclipse.core.commands.Command;
6+
import org.eclipse.core.commands.State;
57
import org.eclipse.core.resources.ResourcesPlugin;
68
import org.eclipse.core.runtime.IAdaptable;
79
import org.eclipse.jdt.core.JavaModelException;
@@ -24,6 +26,7 @@
2426
import org.eclipse.ui.IWorkbenchWindow;
2527
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
2628
import org.eclipse.ui.PlatformUI;
29+
import org.eclipse.ui.commands.ICommandService;
2730
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
2831

2932
import dataTool.ui.ShowDataInBreadcrumbAction;
@@ -46,8 +49,9 @@ public class EnableNavigationAction implements IWorkbenchWindowActionDelegate {
4649

4750
//whether the listener is currently enabled
4851
private boolean isEnabled = false;
49-
5052
private String previous = "";
53+
private String toggleId = "org.eclipse.example.command.toggleState";
54+
private ShowDataInBreadcrumbAction crumbs;
5155

5256
public void dispose() {
5357
if(annotationManager!=null)
@@ -70,6 +74,7 @@ private void enable(IEditorPart activeEditor) {
7074
*/
7175
private void disable() {
7276
if(annotationManager!=null)
77+
annotationManager.setEnabled(false);
7378
annotationManager.deactivate();
7479
}
7580

@@ -123,24 +128,37 @@ public void partOpened(IWorkbenchPart arg0) {
123128
public void run(IAction action) {
124129
//JavaCore.cre
125130
try {
126-
if(!isEnabled) {
131+
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
132+
Command command = service.getCommand("org.eclipse.example.command.toggle");
133+
State state = command.getState("org.eclipse.example.command.toggleState");
134+
if (state == null) {
135+
state = new State();
136+
state.setValue(true);
137+
state.setId("org.eclipse.example.command.toggleState");
138+
command.addState("org.eclipse.example.command.toggleState", state);
139+
}
140+
else {
141+
state.setValue(!(Boolean) state.getValue());
142+
}
143+
if((boolean) state.getValue()) {
144+
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
145+
IEditorPart activeEditor = activePage.getActiveEditor();
146+
JavaEditor j = (JavaEditor) activeEditor;
147+
crumbs = new ShowDataInBreadcrumbAction(j, activePage);
148+
crumbs.run();
127149
enable(page.getActiveEditor());
128150
isEnabled = true;
129151
}
130152
else {
153+
crumbs.stop();
131154
isEnabled = false;
132155
disable();
133-
Activator.getDefault().shutdown();
134156
}
135157
} catch (Exception e) {
136158
Activator.logError(e);
137159
}
138160
}
139161

140-
public boolean isEnabled() {
141-
return isEnabled;
142-
}
143-
144162
public void selectionChanged(IAction action, ISelection selection) {
145163
//do nothing
146164
}
@@ -160,6 +178,7 @@ public void reset(IWorkbenchPage workbench) throws JavaModelException {
160178
}
161179
JavaEditor editor = (JavaEditor)newPage.getActiveEditor();
162180
init(newPage.getWorkbenchWindow());
163-
run(null);
181+
enable(page.getActiveEditor());
182+
isEnabled = true;
164183
}
165184
}

dataTool/src/dataTool/annotations/LinkAnnotation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void mouseUp(MouseEvent arg0) {
105105
else if (linkNode.getDeclarationMethod() == null) {
106106
up = true;
107107
}
108-
else if (click - linkNode.getDeclarationMethod().getName().getStartPosition() >
109-
click - linkNode.getInvocationMethod().getName().getStartPosition()) {
108+
else if (Math.abs(click - linkNode.getDeclarationMethod().getName().getStartPosition()) >
109+
Math.abs(click - linkNode.getInvocationMethod().getName().getStartPosition())) {
110110
up = false;
111111
}
112112
else {

dataTool/src/dataTool/annotations/ProgramNavigationPainter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public void paintControl(PaintEvent e) {
230230
// Highlight all instances in class
231231
for (DataNode node : finder.getOccurrences(p)) {
232232
ann.draw(e.gc, viewer.getTextWidget(), node.getStartPosition(), node.getLength());
233+
System.out.println(node.getInvocationMethod());
233234
if(node.getInvocationMethod() != null) {
234235
linkAnnotation.setDataNode(node);
235236
linkAnnotation.draw(e.gc, viewer.getTextWidget(),

dataTool/src/dataTool/ui/ShowDataInBreadcrumbAction.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ public void run() {
7373
public void stop() {
7474
IBreadcrumb breadcrumb = fEditor.getBreadcrumb();
7575
IBreadcrumb breadcrumb2 = fEditor.getBreadcrumb2();
76-
if (breadcrumb != null && breadcrumb2 != null) {
77-
breadcrumb.dispose();
78-
breadcrumb2.dispose();
79-
}
8076
IPreferenceStore store = JavaPlugin.getDefault().getPreferenceStore();
8177
store.setValue(getPreferenceKey(), false);
78+
breadcrumb.dispose();
79+
breadcrumb2.dispose();
8280
}
8381

8482
/**

0 commit comments

Comments
 (0)