11package 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+
38import 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 ;
422import org .eclipse .ui .IWorkbenchPart ;
523import 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