Skip to content

Commit 7dfa34d

Browse files
committed
Context hook working for update reference right click on package explorer....
1 parent 60e8187 commit 7dfa34d

File tree

6 files changed

+196
-30
lines changed

6 files changed

+196
-30
lines changed

src/ServiceStackEclipse/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui,
99
org.eclipse.jdt.core;bundle-version="3.10.0",
1010
org.eclipse.ui.ide;bundle-version="3.10.2",
1111
org.eclipse.osgi;bundle-version="3.10.2",
12-
org.eclipse.core.runtime;bundle-version="3.10.0"
12+
org.eclipse.core.runtime;bundle-version="3.10.0",
13+
org.eclipse.core.expressions;bundle-version="3.4.600"
1314
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
1415
Import-Package: org.eclipse.core.runtime,
1516
org.eclipse.jdt.core,

src/ServiceStackEclipse/plugin.xml

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
name="Update ServiceStack Reference">
2626
</command>
2727
</extension>
28-
<extension
29-
point="org.eclipse.ui.handlers">
30-
<handler
31-
class="net.servicestack.eclipse.handlers.SampleHandler"
32-
commandId="net.servicestack.eclipse.commands.sampleCommand">
33-
</handler>
34-
</extension>
3528
<extension
3629
point="org.eclipse.ui.bindings">
3730
<key
@@ -50,24 +43,41 @@
5043
icon="icons/logo-16.png"
5144
id="net.servicestack.eclipse.menus.sampleCommand"
5245
mnemonic="M">
53-
<visibleWhen
54-
checkEnabled="true">
55-
<with
56-
variable="net.servicestack.eclipse.sourceprovider.active">
57-
<equals
58-
value="ENABLED">
59-
</equals>
60-
</with>
61-
</visibleWhen>
46+
<enabledWhen>
47+
<with
48+
variable="selection">
49+
<test
50+
forcePluginActivation="true"
51+
property="net.servicestack.eclipse.hasNonEmptyTextSelection"
52+
value="true">
53+
</test>
54+
</with>
55+
</enabledWhen>
6256
</command>
6357
</menuContribution>
6458
</extension>
59+
<extension point="org.eclipse.ui.handlers">
60+
<handler
61+
class="net.servicestack.eclipse.handlers.SampleHandler"
62+
commandId="net.servicestack.eclipse.commands.sampleCommand">
63+
<enabledWhen>
64+
<with
65+
variable="selection">
66+
<test
67+
forcePluginActivation="true"
68+
property="net.servicestack.eclipse.hasNonEmptyTextSelection"
69+
value="true">
70+
</test>
71+
</with>
72+
</enabledWhen>
73+
</handler>
74+
</extension>
6575
<extension
6676
point="org.eclipse.ui.services">
6777
<sourceProvider
6878
provider="net.servicestack.eclipse.handlers.UpdateCommandState">
6979
<variable
70-
name="net.servicestack.eclipse.handlers.active"
80+
name="net.servicestack.eclipse.handlers.updatecommandstate.update"
7181
priorityLevel="workbench">
7282
</variable>
7383
</sourceProvider>
@@ -79,4 +89,13 @@
7989
class="net.servicestack.eclipse.startup.PluginStartup">
8090
</startup>
8191
</extension>
92+
<extension point="org.eclipse.core.expressions.propertyTesters">
93+
<propertyTester
94+
class="net.servicestack.eclipse.testers.SelectionTester"
95+
id="net.servicestack.eclipse.testers.selectionTester"
96+
namespace="net.servicestack.eclipse"
97+
properties="hasNonEmptyTextSelection"
98+
type="java.lang.Object">
99+
</propertyTester>
100+
</extension>>
82101
</plugin>

src/ServiceStackEclipse/src/net/servicestack/eclipse/handlers/SampleHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
3030
.getActiveWorkbenchWindow(event).getService(ISourceProviderService.class);
3131
// now get my service
3232
UpdateCommandState commandStateService = (UpdateCommandState) sourceProviderService
33-
.getSourceProvider(UpdateCommandState.MY_STATE);
34-
commandStateService.enabled = false;
33+
.getSourceProvider(UpdateCommandState.SHOW_UPDATE_CONTEXT_STATE);
34+
commandStateService.setUpdateEnabled();
3535
return null;
3636
}
3737

src/ServiceStackEclipse/src/net/servicestack/eclipse/handlers/UpdateCommandState.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import java.util.Map;
66

77
import org.eclipse.ui.AbstractSourceProvider;
8+
import org.eclipse.ui.ISources;
89

910
public class UpdateCommandState extends AbstractSourceProvider {
10-
public final static String MY_STATE = "net.servicestack.eclipse.handlers.active";
11-
public final static String ENABLED = "ENABLED";
12-
public final static String DISENABLED = "DISENABLED";
13-
public boolean enabled = true;
11+
public final static String ID = "net.servicestack.eclipse.handlers.updatecommandstate";
12+
public final static String SHOW_UPDATE_CONTEXT_STATE = "net.servicestack.eclipse.handlers.updatecommandstate.update";
13+
private boolean enabled = true;
1414

1515
@Override
1616
public void dispose() {
@@ -20,15 +20,26 @@ public void dispose() {
2020

2121
@Override
2222
public Map getCurrentState() {
23-
Map map = new HashMap(1);
24-
String value = enabled ? ENABLED : DISENABLED;
25-
map.put(MY_STATE, value);
26-
return map;
23+
HashMap<String, Object> hashMap = new HashMap<String, Object>();
24+
hashMap.put(ID, ID);
25+
String value = enabled ? "true" : "false";
26+
hashMap.put(SHOW_UPDATE_CONTEXT_STATE, value);
27+
return hashMap;
2728
}
2829

2930
@Override
3031
public String[] getProvidedSourceNames() {
31-
return new String[] { MY_STATE };
32+
return new String[] { SHOW_UPDATE_CONTEXT_STATE };
3233
}
34+
35+
public void setUpdateEnabled() {
36+
enabled = true;
37+
fireSourceChanged(ISources.WORKBENCH, SHOW_UPDATE_CONTEXT_STATE, "true");
38+
}
39+
40+
public void setUpdateDisabled() {
41+
enabled = false;
42+
fireSourceChanged(ISources.WORKBENCH, SHOW_UPDATE_CONTEXT_STATE, "false");
43+
}
3344

3445
}

src/ServiceStackEclipse/src/net/servicestack/eclipse/startup/PluginStartup.java

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
package net.servicestack.eclipse.startup;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
8+
import net.servicestack.eclipse.handlers.UpdateCommandState;
9+
10+
import org.eclipse.core.resources.IFile;
11+
import org.eclipse.core.resources.IFolder;
12+
import org.eclipse.core.resources.IProject;
13+
import org.eclipse.core.resources.IResource;
14+
import org.eclipse.core.runtime.CoreException;
15+
import org.eclipse.core.runtime.IAdaptable;
16+
import org.eclipse.core.runtime.IPath;
17+
import org.eclipse.core.runtime.Path;
18+
import org.eclipse.core.runtime.jobs.ISchedulingRule;
19+
import org.eclipse.jdt.core.ICompilationUnit;
20+
import org.eclipse.jdt.core.IImportDeclaration;
21+
import org.eclipse.jdt.core.IPackageFragment;
22+
import org.eclipse.jface.action.MenuManager;
323
import org.eclipse.jface.viewers.ISelection;
24+
import org.eclipse.jface.viewers.IStructuredSelection;
25+
import org.eclipse.jface.viewers.ITreeSelection;
26+
import org.eclipse.jface.viewers.TreePath;
27+
import org.eclipse.jface.viewers.TreeSelection;
28+
import org.eclipse.swt.widgets.Menu;
429
import org.eclipse.ui.ISelectionListener;
530
import org.eclipse.ui.ISelectionService;
631
import org.eclipse.ui.IStartup;
@@ -9,6 +34,8 @@
934
import org.eclipse.ui.IWorkbenchPart;
1035
import org.eclipse.ui.IWorkbenchWindow;
1136
import org.eclipse.ui.PlatformUI;
37+
import org.eclipse.ui.handlers.HandlerUtil;
38+
import org.eclipse.ui.services.ISourceProviderService;
1239

1340
public class PluginStartup implements IStartup, ISelectionListener {
1441

@@ -63,7 +90,93 @@ private void removeSelectionListener(IWorkbenchWindow window) {
6390

6491
@Override
6592
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
93+
94+
IWorkbench workbench = PlatformUI.getWorkbench();
95+
ISourceProviderService sourceProviderService =
96+
(ISourceProviderService) workbench.getService(ISourceProviderService.class);
97+
// now get my service
98+
UpdateCommandState commandStateService = (UpdateCommandState) sourceProviderService
99+
.getSourceProvider(UpdateCommandState.SHOW_UPDATE_CONTEXT_STATE);
100+
66101
// TODO handle selection changes
67-
System.out.println("selection changed");
102+
if(!(selection instanceof ITreeSelection)) {
103+
commandStateService.setUpdateDisabled();
104+
return;
105+
}
106+
107+
TreeSelection treeSelection = (TreeSelection)selection;
108+
if(!(treeSelection.getFirstElement() instanceof ICompilationUnit)) {
109+
commandStateService.setUpdateDisabled();
110+
return;
111+
}
112+
113+
ICompilationUnit compilationUnit = (ICompilationUnit)treeSelection.getFirstElement();
114+
IResource resource = extractSelection(selection);
115+
IProject project = resource.getProject();
116+
IImportDeclaration importDeclaration = compilationUnit.getImport("net.servicestack.client");
117+
if(importDeclaration == null) {
118+
commandStateService.setUpdateDisabled();
119+
return;
120+
}
121+
122+
IFile file = project.getFile(stripFirstDirectoryFromPath(compilationUnit.getPath()));
123+
InputStream contents = null;
124+
try {
125+
contents = file.getContents();
126+
InputStreamReader is = new InputStreamReader(contents);
127+
BufferedReader br = new BufferedReader(is);
128+
String read;
129+
try {
130+
for(int i = 0; i < 10; i++) {
131+
String line = br.readLine();
132+
if(line.startsWith("/* Options:")) {
133+
134+
commandStateService.setUpdateEnabled();
135+
return;
136+
}
137+
}
138+
} catch (IOException e) {
139+
// TODO Auto-generated catch block
140+
e.printStackTrace();
141+
}
142+
143+
} catch (CoreException e) {
144+
// TODO Auto-generated catch block
145+
e.printStackTrace();
146+
}
147+
if(contents == null) {
148+
commandStateService.setUpdateDisabled();
149+
return;
150+
}
151+
commandStateService.setUpdateDisabled();
152+
System.out.println("File selected");
68153
}
154+
155+
IResource extractSelection(ISelection sel) {
156+
if (!(sel instanceof IStructuredSelection))
157+
return null;
158+
IStructuredSelection ss = (IStructuredSelection) sel;
159+
Object element = ss.getFirstElement();
160+
if (element instanceof IResource)
161+
return (IResource) element;
162+
if (!(element instanceof IAdaptable))
163+
return null;
164+
IAdaptable adaptable = (IAdaptable) element;
165+
Object adapter = adaptable.getAdapter(IResource.class);
166+
return (IResource) adapter;
167+
}
168+
169+
private IPath stripFirstDirectoryFromPath(IPath path) {
170+
String constructedPath = "";
171+
// Skip first segment of project path as we want path relative to
172+
// project.
173+
for (int i = 1; i < path.segmentCount(); i++) {
174+
constructedPath += "/" + path.segment(i);
175+
}
176+
if (constructedPath.equals("")) {
177+
constructedPath = "/";
178+
}
179+
Path result = new Path(constructedPath);
180+
return result;
181+
}
69182
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.servicestack.eclipse.testers;
2+
3+
import org.eclipse.core.expressions.PropertyTester;
4+
import org.eclipse.ui.IWorkbenchPart;
5+
import org.eclipse.ui.PlatformUI;
6+
7+
public class SelectionTester extends PropertyTester {
8+
9+
@Override
10+
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
11+
if ("hasNonEmptyTextSelection".equals(property)) {
12+
try {
13+
IWorkbenchPart activePart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
14+
String foo = activePart.toString();
15+
} catch (Exception e) {
16+
// Do nothing. Will throw an NPE when the application is closed as there is no longer an active part.
17+
}
18+
}
19+
return true;
20+
}
21+
22+
}

0 commit comments

Comments
 (0)