1414package net .sf .j2s .ui .actions ;
1515
1616import java .io .File ;
17+ import java .io .FileNotFoundException ;
18+ import java .io .IOException ;
19+ import java .io .InputStream ;
20+ import java .util .ArrayList ;
1721
1822import net .sf .j2s .ui .Java2ScriptUIPlugin ;
1923
24+ import org .eclipse .core .filesystem .EFS ;
25+ import org .eclipse .core .filesystem .IFileStore ;
2026import 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 ;
2130import org .eclipse .core .runtime .IPath ;
2231import org .eclipse .core .runtime .Path ;
2332import org .eclipse .core .runtime .Platform ;
33+ import org .eclipse .core .runtime .content .IContentType ;
2434import org .eclipse .jdt .core .ICompilationUnit ;
2535import org .eclipse .jdt .core .IJavaElement ;
2636import org .eclipse .jdt .core .IJavaModel ;
3242import org .eclipse .ui .IEditorDescriptor ;
3343import org .eclipse .ui .IEditorInput ;
3444import org .eclipse .ui .IEditorRegistry ;
45+ import org .eclipse .ui .IWorkbench ;
46+ import org .eclipse .ui .IWorkbenchPage ;
47+ import org .eclipse .ui .IWorkbenchWindow ;
3548import org .eclipse .ui .PartInitException ;
3649import org .eclipse .ui .editors .text .EditorsUI ;
50+ import org .eclipse .ui .internal .editors .text .EditorsPlugin ;
3751import org .eclipse .ui .internal .editors .text .JavaFileEditorInput ;
3852import org .eclipse .ui .part .FileEditorInput ;
3953
@@ -58,33 +72,129 @@ protected static String getEditorID() {
5872 }
5973 return editorID ;
6074 }
61- public static boolean openEditor (ICompilationUnit unit ) {
75+ public static boolean openEditor ( ICompilationUnit unit ) {
6276 String relativePath = getRelativeJSPath (unit );
6377 IJavaModel javaModel = unit .getJavaModel ();
6478 File file = new File (javaModel .getResource ()
6579 .getLocation ().toOSString (), relativePath );
66-
80+
6781 IFile [] files = javaModel .getWorkspace ().getRoot ()
6882 .findFilesForLocation (
6983 Path .fromPortableString (relativePath ));
7084 IEditorInput editorInput = null ;
7185 if (files != null && files .length != 0 ) {
7286 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+ }
7395 } 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+ }
77114 }
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 ;
78145 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+ }
85164 }
86- return false ;
87165 }
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+
88198 protected static String getRelativeJSPath (ICompilationUnit unit ) {
89199 if (unit == null ) {
90200 return null ;
0 commit comments