Skip to content

Commit 8b78085

Browse files
committed
Hide/show of update context menu working.
1 parent 7dfa34d commit 8b78085

File tree

3 files changed

+93
-207
lines changed

3 files changed

+93
-207
lines changed

src/ServiceStackEclipse/plugin.xml

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
icon="icons/logo-16.png"
4444
id="net.servicestack.eclipse.menus.sampleCommand"
4545
mnemonic="M">
46-
<enabledWhen>
46+
<visibleWhen>
4747
<with
4848
variable="selection">
4949
<test
@@ -52,15 +52,15 @@
5252
value="true">
5353
</test>
5454
</with>
55-
</enabledWhen>
55+
</visibleWhen>
5656
</command>
5757
</menuContribution>
5858
</extension>
5959
<extension point="org.eclipse.ui.handlers">
6060
<handler
6161
class="net.servicestack.eclipse.handlers.SampleHandler"
6262
commandId="net.servicestack.eclipse.commands.sampleCommand">
63-
<enabledWhen>
63+
<visibleWhen>
6464
<with
6565
variable="selection">
6666
<test
@@ -69,26 +69,9 @@
6969
value="true">
7070
</test>
7171
</with>
72-
</enabledWhen>
72+
</visibleWhen>
7373
</handler>
7474
</extension>
75-
<extension
76-
point="org.eclipse.ui.services">
77-
<sourceProvider
78-
provider="net.servicestack.eclipse.handlers.UpdateCommandState">
79-
<variable
80-
name="net.servicestack.eclipse.handlers.updatecommandstate.update"
81-
priorityLevel="workbench">
82-
</variable>
83-
</sourceProvider>
84-
</extension>
85-
<extension
86-
id="net.servicetack.eclipse.startup"
87-
point="org.eclipse.ui.startup">
88-
<startup
89-
class="net.servicestack.eclipse.startup.PluginStartup">
90-
</startup>
91-
</extension>
9275
<extension point="org.eclipse.core.expressions.propertyTesters">
9376
<propertyTester
9477
class="net.servicestack.eclipse.testers.SelectionTester"

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

Lines changed: 0 additions & 182 deletions
This file was deleted.
Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
package net.servicestack.eclipse.testers;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
38
import org.eclipse.core.expressions.PropertyTester;
9+
import org.eclipse.core.resources.IFile;
10+
import org.eclipse.core.resources.IProject;
11+
import org.eclipse.core.resources.IResource;
12+
import org.eclipse.core.runtime.CoreException;
13+
import org.eclipse.core.runtime.IAdaptable;
14+
import org.eclipse.core.runtime.IPath;
15+
import org.eclipse.core.runtime.Path;
16+
import org.eclipse.jdt.core.ICompilationUnit;
17+
import org.eclipse.jdt.core.IImportDeclaration;
18+
import org.eclipse.jface.viewers.ISelection;
19+
import org.eclipse.jface.viewers.IStructuredSelection;
20+
import org.eclipse.jface.viewers.ITreeSelection;
21+
import org.eclipse.jface.viewers.TreeSelection;
422
import org.eclipse.ui.IWorkbenchPart;
523
import org.eclipse.ui.PlatformUI;
624

@@ -9,14 +27,81 @@ public class SelectionTester extends PropertyTester {
927
@Override
1028
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
1129
if ("hasNonEmptyTextSelection".equals(property)) {
30+
if(!(receiver instanceof ITreeSelection)) {
31+
return false;
32+
}
33+
34+
TreeSelection treeSelection = (TreeSelection)receiver;
35+
if(!(treeSelection.getFirstElement() instanceof ICompilationUnit)) {
36+
return false;
37+
}
38+
39+
ICompilationUnit compilationUnit = (ICompilationUnit)treeSelection.getFirstElement();
40+
IResource resource = extractSelection(treeSelection);
41+
IProject project = resource.getProject();
42+
IImportDeclaration importDeclaration = compilationUnit.getImport("net.servicestack.client");
43+
if(importDeclaration == null) {
44+
return false;
45+
}
46+
47+
IFile file = project.getFile(stripFirstDirectoryFromPath(compilationUnit.getPath()));
48+
InputStream contents = null;
1249
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.
50+
contents = file.getContents();
51+
InputStreamReader is = new InputStreamReader(contents);
52+
BufferedReader br = new BufferedReader(is);
53+
String read;
54+
try {
55+
for(int i = 0; i < 10; i++) {
56+
String line = br.readLine();
57+
if(line.startsWith("/* Options:")) {
58+
return true;
59+
}
60+
}
61+
} catch (IOException e) {
62+
// TODO Auto-generated catch block
63+
e.printStackTrace();
64+
}
65+
66+
} catch (CoreException e) {
67+
// TODO Auto-generated catch block
68+
e.printStackTrace();
69+
}
70+
if(contents == null) {
71+
return false;
1772
}
73+
System.out.println("File selected");
74+
1875
}
1976
return true;
2077
}
78+
79+
IResource extractSelection(ITreeSelection sel) {
80+
if (!(sel instanceof IStructuredSelection))
81+
return null;
82+
IStructuredSelection ss = (IStructuredSelection) sel;
83+
Object element = ss.getFirstElement();
84+
if (element instanceof IResource)
85+
return (IResource) element;
86+
if (!(element instanceof IAdaptable))
87+
return null;
88+
IAdaptable adaptable = (IAdaptable) element;
89+
Object adapter = adaptable.getAdapter(IResource.class);
90+
return (IResource) adapter;
91+
}
92+
93+
private IPath stripFirstDirectoryFromPath(IPath path) {
94+
String constructedPath = "";
95+
// Skip first segment of project path as we want path relative to
96+
// project.
97+
for (int i = 1; i < path.segmentCount(); i++) {
98+
constructedPath += "/" + path.segment(i);
99+
}
100+
if (constructedPath.equals("")) {
101+
constructedPath = "/";
102+
}
103+
Path result = new Path(constructedPath);
104+
return result;
105+
}
21106

22107
}

0 commit comments

Comments
 (0)