14
14
package net .sf .j2s .ui .actions ;
15
15
16
16
import java .io .File ;
17
+ import java .io .FileNotFoundException ;
18
+ import java .io .IOException ;
19
+ import java .io .InputStream ;
20
+ import java .util .ArrayList ;
17
21
18
22
import net .sf .j2s .ui .Java2ScriptUIPlugin ;
19
23
24
+ import org .eclipse .core .filesystem .EFS ;
25
+ import org .eclipse .core .filesystem .IFileStore ;
20
26
import org .eclipse .core .resources .IFile ;
27
+ import org .eclipse .core .resources .IWorkspace ;
28
+ import org .eclipse .core .resources .ResourcesPlugin ;
29
+ import org .eclipse .core .runtime .CoreException ;
21
30
import org .eclipse .core .runtime .IPath ;
22
31
import org .eclipse .core .runtime .Path ;
23
32
import org .eclipse .core .runtime .Platform ;
33
+ import org .eclipse .core .runtime .content .IContentType ;
24
34
import org .eclipse .jdt .core .ICompilationUnit ;
25
35
import org .eclipse .jdt .core .IJavaElement ;
26
36
import org .eclipse .jdt .core .IJavaModel ;
32
42
import org .eclipse .ui .IEditorDescriptor ;
33
43
import org .eclipse .ui .IEditorInput ;
34
44
import org .eclipse .ui .IEditorRegistry ;
45
+ import org .eclipse .ui .IWorkbench ;
46
+ import org .eclipse .ui .IWorkbenchPage ;
47
+ import org .eclipse .ui .IWorkbenchWindow ;
35
48
import org .eclipse .ui .PartInitException ;
36
49
import org .eclipse .ui .editors .text .EditorsUI ;
50
+ import org .eclipse .ui .internal .editors .text .EditorsPlugin ;
37
51
import org .eclipse .ui .internal .editors .text .JavaFileEditorInput ;
38
52
import org .eclipse .ui .part .FileEditorInput ;
39
53
@@ -58,33 +72,129 @@ protected static String getEditorID() {
58
72
}
59
73
return editorID ;
60
74
}
61
- public static boolean openEditor (ICompilationUnit unit ) {
75
+ public static boolean openEditor ( ICompilationUnit unit ) {
62
76
String relativePath = getRelativeJSPath (unit );
63
77
IJavaModel javaModel = unit .getJavaModel ();
64
78
File file = new File (javaModel .getResource ()
65
79
.getLocation ().toOSString (), relativePath );
66
-
80
+
67
81
IFile [] files = javaModel .getWorkspace ().getRoot ()
68
82
.findFilesForLocation (
69
83
Path .fromPortableString (relativePath ));
70
84
IEditorInput editorInput = null ;
71
85
if (files != null && files .length != 0 ) {
72
86
editorInput = new FileEditorInput (files [0 ]);
87
+ try {
88
+ Java2ScriptUIPlugin .getDefault ().getWorkbench ()
89
+ .getActiveWorkbenchWindow ().getActivePage ()
90
+ .openEditor (editorInput , getEditorID ());
91
+ return true ;
92
+ } catch (PartInitException e ) {
93
+ e .printStackTrace ();
94
+ }
73
95
} else {
74
- //editorInput = new JavaFileEditorInput(file);
75
- // FIXME open editor for *.js
76
- return false ;
96
+ IFileStore fileStore = EFS .getLocalFileSystem ().getStore (new Path (file .getParent ()));
97
+ fileStore = fileStore .getChild (file .getName ());
98
+ if (!fileStore .fetchInfo ().isDirectory () && fileStore .fetchInfo ().exists ()) {
99
+ IEditorInput input = createEditorInput (fileStore );
100
+ if (input == null ) {
101
+ return false ;
102
+ }
103
+ IWorkbenchWindow fWindow = Java2ScriptUIPlugin .getDefault ().getWorkbench ()
104
+ .getActiveWorkbenchWindow ();
105
+ String editorId = getEditorId (fWindow , fileStore );
106
+ IWorkbenchPage page = fWindow .getActivePage ();
107
+ try {
108
+ page .openEditor (input , editorId );
109
+ return true ;
110
+ } catch (PartInitException e ) {
111
+ EditorsPlugin .log (e .getStatus ());
112
+ }
113
+ }
77
114
}
115
+ return false ;
116
+ }
117
+ /*
118
+ * XXX: Requested a helper to get the correct editor descriptor
119
+ * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=110203
120
+ */
121
+ private static String getEditorId (IWorkbenchWindow fWindow , IFileStore file ) {
122
+ IWorkbench workbench = fWindow .getWorkbench ();
123
+ IEditorRegistry editorRegistry = workbench .getEditorRegistry ();
124
+ IEditorDescriptor descriptor = editorRegistry .getDefaultEditor (file .getName (), getContentType (file ));
125
+
126
+ // check the OS for in-place editor (OLE on Win32)
127
+ if (descriptor == null && editorRegistry .isSystemInPlaceEditorAvailable (file .getName ()))
128
+ descriptor = editorRegistry .findEditor (IEditorRegistry .SYSTEM_INPLACE_EDITOR_ID );
129
+
130
+ // // check the OS for external editor
131
+ // if (descriptor == null && editorRegistry.isSystemExternalEditorAvailable(file.getName()))
132
+ // descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
133
+
134
+ if (descriptor != null )
135
+ return descriptor .getId ();
136
+
137
+ return EditorsUI .DEFAULT_TEXT_EDITOR_ID ;
138
+ }
139
+
140
+ private static IContentType getContentType (IFileStore fileStore ) {
141
+ if (fileStore == null )
142
+ return null ;
143
+
144
+ InputStream stream = null ;
78
145
try {
79
- Java2ScriptUIPlugin .getDefault ().getWorkbench ()
80
- .getActiveWorkbenchWindow ().getActivePage ()
81
- .openEditor (editorInput , getEditorID ());
82
- return true ;
83
- } catch (PartInitException e ) {
84
- e .printStackTrace ();
146
+ stream = fileStore .openInputStream (EFS .NONE , null );
147
+ return Platform .getContentTypeManager ().findContentTypeFor (stream , fileStore .getName ());
148
+ } catch (IOException x ) {
149
+ EditorsPlugin .log (x );
150
+ return null ;
151
+ } catch (CoreException x ) {
152
+ // Do not log FileNotFoundException (no access)
153
+ if (!(x .getStatus ().getException () instanceof FileNotFoundException ))
154
+ EditorsPlugin .log (x );
155
+
156
+ return null ;
157
+ } finally {
158
+ try {
159
+ if (stream != null )
160
+ stream .close ();
161
+ } catch (IOException x ) {
162
+ EditorsPlugin .log (x );
163
+ }
85
164
}
86
- return false ;
87
165
}
166
+
167
+ private static IEditorInput createEditorInput (IFileStore fileStore ) {
168
+ IFile workspaceFile = getWorkspaceFile (fileStore );
169
+ if (workspaceFile != null )
170
+ return new FileEditorInput (workspaceFile );
171
+ return new JavaFileEditorInput (fileStore );
172
+ }
173
+
174
+ private static IFile getWorkspaceFile (IFileStore fileStore ) {
175
+ IWorkspace workspace = ResourcesPlugin .getWorkspace ();
176
+ IFile [] files = workspace .getRoot ().findFilesForLocation (new Path (fileStore .toURI ().getPath ()));
177
+ files = filterNonExistentFiles (files );
178
+ if (files == null || files .length == 0 )
179
+ return null ;
180
+ if (files .length == 1 )
181
+ return files [0 ];
182
+ return null ;
183
+ }
184
+
185
+ private static IFile [] filterNonExistentFiles (IFile [] files ){
186
+ if (files == null )
187
+ return null ;
188
+
189
+ int length = files .length ;
190
+ ArrayList existentFiles = new ArrayList (length );
191
+ for (int i = 0 ; i < length ; i ++) {
192
+ if (files [i ].exists ())
193
+ existentFiles .add (files [i ]);
194
+ }
195
+ return (IFile [])existentFiles .toArray (new IFile [existentFiles .size ()]);
196
+ }
197
+
88
198
protected static String getRelativeJSPath (ICompilationUnit unit ) {
89
199
if (unit == null ) {
90
200
return null ;
0 commit comments