Skip to content

Commit c9dfa90

Browse files
author
jossonsmith
committed
Implement @j2s* auto completion for Eclipse 3.2
Implement contextmenu's "Edit converted *.js" for Eclipse 3.2
1 parent 6ae03ea commit c9dfa90

File tree

5 files changed

+546
-43
lines changed

5 files changed

+546
-43
lines changed

META-INF/MANIFEST.MF

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@ Bundle-Activator: net.sf.j2s.ui.Java2ScriptUIPlugin
77
Bundle-Vendor: j2s.sourceforge.net
88
Bundle-Localization: plugin
99
Export-Package: net.sf.j2s.ui.resources
10-
Require-Bundle: org.eclipse.ui,
11-
org.eclipse.core.runtime,
12-
org.eclipse.ui.ide,
13-
org.eclipse.ui.views,
14-
org.eclipse.jface.text,
15-
org.eclipse.ui.workbench.texteditor,
16-
org.eclipse.ui.editors,
17-
org.eclipse.core.resources,
18-
org.eclipse.jdt.core,
19-
org.eclipse.core.filebuffers,
20-
org.eclipse.jdt.ui,
21-
org.eclipse.debug.core,
22-
org.eclipse.debug.ui,
23-
org.eclipse.jdt.debug.ui,
24-
org.eclipse.jdt.debug,
25-
org.eclipse.jdt.launching,
26-
org.eclipse.core.variables,
27-
org.eclipse.ltk.core.refactoring,
28-
org.eclipse.ltk.ui.refactoring,
29-
org.eclipse.ui.console,
30-
net.sf.j2s.core
10+
Require-Bundle: org.eclipse.ui,
11+
org.eclipse.core.runtime,
12+
org.eclipse.ui.ide,
13+
org.eclipse.ui.views,
14+
org.eclipse.jface.text,
15+
org.eclipse.ui.workbench.texteditor,
16+
org.eclipse.ui.editors,
17+
org.eclipse.core.resources,
18+
org.eclipse.jdt.core,
19+
org.eclipse.core.filebuffers,
20+
org.eclipse.jdt.ui,
21+
org.eclipse.debug.core,
22+
org.eclipse.debug.ui,
23+
org.eclipse.jdt.debug.ui,
24+
org.eclipse.jdt.debug,
25+
org.eclipse.jdt.launching,
26+
org.eclipse.core.variables,
27+
org.eclipse.ltk.core.refactoring,
28+
org.eclipse.ltk.ui.refactoring,
29+
org.eclipse.ui.console,
30+
net.sf.j2s.core,
31+
org.eclipse.core.filesystem
3132
Eclipse-AutoStart: true
3233
Main-Class: net.sf.j2s.ui.jdtenhancer.JDTCoreEnhancer

plugin.xml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
</extension>
138138
<extension
139139
point="org.eclipse.ui.popupMenus">
140-
<!--
141140
<objectContribution
142141
id="net.sf.j2s.ui.actions.openjs"
143142
objectClass="org.eclipse.jdt.core.ICompilationUnit">
@@ -148,7 +147,6 @@
148147
label="Edit Con&amp;verted *.js"
149148
menubarPath="additions"/>
150149
</objectContribution>
151-
-->
152150
<objectContribution
153151
adaptable="false"
154152
id="net.sf.j2s.ui.actions.genPropsJS"
@@ -161,7 +159,6 @@
161159
menubarPath="additions"/>
162160
</objectContribution>
163161
</extension>
164-
<!--
165162
<extension
166163
point="org.eclipse.ui.popupMenus">
167164
<viewerContribution
@@ -175,7 +172,6 @@
175172
</action>
176173
</viewerContribution>
177174
</extension>
178-
-->
179175
<!--
180176
<extension
181177
point="org.eclipse.ui.exportWizards">
@@ -187,12 +183,15 @@
187183
<description>Export Java2Script applications as standalone application</description>
188184
</wizard>
189185
</extension>
190-
<extension
191-
point="org.eclipse.jdt.ui.javadocCompletionProcessor">
192-
<javadocCompletionProcessor
193-
class="net.sf.j2s.ui.text.javadoc.JavaDocCompletionEvaluator"
194-
id="net.sf.j2s.javadocCompletionProcessor"/>
195-
</extension>
196186
-->
187+
<extension
188+
point="org.eclipse.jdt.ui.javaCompletionProposalComputer"
189+
id="J2SCompletionProposalComputer">
190+
<javaCompletionProposalComputer
191+
class="net.sf.j2s.ui.text.javadoc.J2SProposalComputer"
192+
categoryId="org.eclipse.jdt.ui.javaNoTypeProposalCategory">
193+
<partition type="__java_javadoc"/>
194+
</javaCompletionProposalComputer>
195+
</extension>
197196

198197
</plugin>

src/net/sf/j2s/ui/actions/UnitJavaScriptUtil.java

Lines changed: 122 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414
package net.sf.j2s.ui.actions;
1515

1616
import java.io.File;
17+
import java.io.FileNotFoundException;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.ArrayList;
1721

1822
import net.sf.j2s.ui.Java2ScriptUIPlugin;
1923

24+
import org.eclipse.core.filesystem.EFS;
25+
import org.eclipse.core.filesystem.IFileStore;
2026
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;
2130
import org.eclipse.core.runtime.IPath;
2231
import org.eclipse.core.runtime.Path;
2332
import org.eclipse.core.runtime.Platform;
33+
import org.eclipse.core.runtime.content.IContentType;
2434
import org.eclipse.jdt.core.ICompilationUnit;
2535
import org.eclipse.jdt.core.IJavaElement;
2636
import org.eclipse.jdt.core.IJavaModel;
@@ -32,8 +42,12 @@
3242
import org.eclipse.ui.IEditorDescriptor;
3343
import org.eclipse.ui.IEditorInput;
3444
import org.eclipse.ui.IEditorRegistry;
45+
import org.eclipse.ui.IWorkbench;
46+
import org.eclipse.ui.IWorkbenchPage;
47+
import org.eclipse.ui.IWorkbenchWindow;
3548
import org.eclipse.ui.PartInitException;
3649
import org.eclipse.ui.editors.text.EditorsUI;
50+
import org.eclipse.ui.internal.editors.text.EditorsPlugin;
3751
import org.eclipse.ui.internal.editors.text.JavaFileEditorInput;
3852
import 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;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2005, 2006 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
package net.sf.j2s.ui.text.javadoc;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
import org.eclipse.core.runtime.IProgressMonitor;
17+
18+
import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
19+
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
20+
21+
22+
/**
23+
* A computer wrapper for the hippie processor.
24+
*
25+
* @since 3.2
26+
*/
27+
public final class J2SProposalComputer implements IJavaCompletionProposalComputer {
28+
/** The wrapped processor. */
29+
private final J2SProposalProcessor fProcessor= new J2SProposalProcessor();
30+
31+
/**
32+
* Default ctor to make it instantiatable via the extension mechanism.
33+
*/
34+
public J2SProposalComputer() {
35+
}
36+
37+
/*
38+
* @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
39+
*/
40+
public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
41+
return Arrays.asList(fProcessor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset()));
42+
}
43+
44+
/*
45+
* @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
46+
*/
47+
public List computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
48+
return Arrays.asList(fProcessor.computeContextInformation(context.getViewer(), context.getInvocationOffset()));
49+
}
50+
51+
/*
52+
* @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
53+
*/
54+
public String getErrorMessage() {
55+
return fProcessor.getErrorMessage();
56+
}
57+
58+
/*
59+
* @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
60+
*/
61+
public void sessionStarted() {
62+
}
63+
64+
/*
65+
* @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
66+
*/
67+
public void sessionEnded() {
68+
}
69+
}

0 commit comments

Comments
 (0)