1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*******************************************************************************
* Copyright (c) 2007, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.jsdt.internal.ui.util;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.wst.jsdt.core.IIncludePathEntry;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.IPackageFragmentRoot;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.internal.core.JavaProject;
import org.eclipse.wst.jsdt.internal.core.util.ConvertUtility;
import org.eclipse.wst.jsdt.internal.core.util.Messages;
import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin;
import org.eclipse.wst.jsdt.internal.ui.Logger;
/**
* Not API
*/
public class ConvertAction implements IObjectActionDelegate, IActionDelegate {
IWorkbenchPart fPart;
Object[] fTarget;
private static final String FACET_NATURE = "org.eclipse.wst.common.project.facet.core.nature"; //$NON-NLS-1$
private static final String FACET_PROPERTY_PAGE = "org.eclipse.wst.common.project.facet.ui.FacetsPropertyPage"; //$NON-NLS-1$
private void doInstall(IProject project, final boolean openProperties, IProgressMonitor monitor) {
boolean configured = false;
monitor.beginTask(Messages.converter_ConfiguringForJavaScript, 5);
ConvertUtility convertor = new ConvertUtility(project);
try {
boolean hadBasicNature = ConvertUtility.hasNature(project);
convertor.configure(new SubProgressMonitor(monitor, 1));
convertor.addBrowserSupport(!hadBasicNature, new SubProgressMonitor(monitor, 1));
if (!hadBasicNature) {
/*
* No nature before, so no existing include path. Define the
* project itself as an source folder.
*/
JavaProject jp = (JavaProject) JavaScriptCore.create(project);
IIncludePathEntry[] oldEntries = null;
try {
oldEntries = jp.getRawIncludepath();
List entries = new ArrayList();
for (int i = 0; i < oldEntries.length; i++) {
if (oldEntries[i].getContentKind() != IPackageFragmentRoot.K_SOURCE || oldEntries[i].getEntryKind() != IIncludePathEntry.CPE_SOURCE) {
entries.add(oldEntries[i]);
}
}
oldEntries = (IIncludePathEntry[]) entries.toArray(new IIncludePathEntry[entries.size()]);
}
catch (JavaScriptModelException ex1) {
Logger.log(Logger.ERROR_DEBUG, null, ex1);
oldEntries = new IIncludePathEntry[0];
}
IIncludePathEntry[] sourcePaths = convertor.getDefaultSourcePaths(project);
IIncludePathEntry[] newEntries = new IIncludePathEntry[oldEntries.length + sourcePaths.length];
System.arraycopy(sourcePaths, 0, newEntries, 0, sourcePaths.length);
System.arraycopy(oldEntries, 0, newEntries, sourcePaths.length, oldEntries.length);
try {
jp.setRawIncludepath(newEntries, project.getFullPath(), new SubProgressMonitor(monitor, 1));
}
catch (JavaScriptModelException ex1) {
Logger.log(Logger.ERROR_DEBUG, null, ex1);
}
}
configured = true;
}
catch (CoreException ex) {
Logger.logException(ex);
}
if (configured && openProperties) {
showPropertiesOn(project, new SubProgressMonitor(monitor, 1));
}
monitor.done();
}
private void doUninstall(IProject project, IProgressMonitor monitor) {
// ConvertUtility nature = new ConvertUtility(project);
// try {
// nature.deconfigure();
// } catch (CoreException ex) {
// Logger.logException(ex);
// }
}
void enableForFacets(IProject project) {
try {
IProjectDescription description = project.getDescription();
boolean hasNature = project.hasNature(FACET_NATURE);
if (!hasNature) {
List natures = new ArrayList(Arrays.asList(description.getNatureIds()));
natures.add(FACET_NATURE);
description.setNatureIds((String[]) natures.toArray(new String[natures.size()]));
}
if (!hasNature)
project.setDescription(description, new NullProgressMonitor());
}
catch (CoreException e) {
Logger.logException(e);
}
}
private void install(final IProject project, final boolean openProperties) {
IProgressService service = null;
if (fPart != null) {
service = (IProgressService) fPart.getSite().getService(IProgressService.class);
}
if (service == null) {
doInstall(project, openProperties, new NullProgressMonitor());
}
else {
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
doInstall(project, openProperties, monitor);
}
};
try {
service.run(false, false, runnable);
}
catch (InvocationTargetException e) {
Logger.logException(e);
}
catch (InterruptedException e) {
Logger.logException(e);
}
}
}
public void run(IAction action) {
if (fTarget == null)
return;
for (int i = 0; i < fTarget.length; i++) {
if (fTarget[i] instanceof IResource) {
final IProject project = ((IResource) fTarget[i]).getProject();
// Temporary until https://bugs.eclipse.org/bugs/show_bug.cgi?id=298483 is resolved
// enableForFacets(project);
if (!ConvertUtility.hasNature(project)) {
/* Doesn't have nature, do a full install. */
install(project, i == fTarget.length - 1);
}
else {
/*
* Has nature, check for browser library on include path
* and setup if not found.
*/
IJavaScriptProject jp = JavaScriptCore.create(project);
IIncludePathEntry[] rawClasspath = null;
try {
rawClasspath = jp.getRawIncludepath();
}
catch (JavaScriptModelException ex1) {
Logger.log(Logger.ERROR_DEBUG, null, ex1);
}
boolean browserFound = false;
for (int k = 0; rawClasspath != null && !browserFound && k < rawClasspath.length; k++) {
if (rawClasspath[k].getPath().equals(ConvertUtility.BROWSER_LIBRARY_PATH)) {
browserFound = true;
}
}
if (!browserFound) {
install(project, false);
}
}
}
}
}
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof IStructuredSelection) {
fTarget = ((IStructuredSelection) selection).toArray();
}
else {
fTarget = null;
}
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
fPart = targetPart;
}
private void showPropertiesOn(final IProject project, final IProgressMonitor monitor) {
IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint("org.eclipse.ui.propertyPages").getExtensions(); //$NON-NLS-1$
final List pageIds = new ArrayList(8);
pageIds.add(FACET_PROPERTY_PAGE);
for (int i = 0; i < extensions.length; i++) {
if (extensions[i].getNamespaceIdentifier().startsWith("org.eclipse.wst.jsdt.")) { //$NON-NLS-1$
IConfigurationElement[] configurationElements = extensions[i].getConfigurationElements();
for (int j = 0; j < configurationElements.length; j++) {
if ("page".equals(configurationElements[j].getName())) { //$NON-NLS-1$
pageIds.add(configurationElements[j].getAttribute("id")); //$NON-NLS-1$
}
}
}
}
Shell shell = (Shell) fPart.getAdapter(Shell.class);
if (shell == null) {
IWorkbenchWindow activeWorkbenchWindow = JavaScriptPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
if (activeWorkbenchWindow != null)
shell = activeWorkbenchWindow.getShell();
}
final Shell finalShell = shell;
if (finalShell != null) {
finalShell.getDisplay().asyncExec(new Runnable() {
public void run() {
PreferenceDialog dialog = PreferencesUtil.createPropertyDialogOn(finalShell, project, "org.eclipse.wst.jsdt.ui.propertyPages.BuildPathsPropertyPage", (String[]) pageIds.toArray(new String[pageIds.size()]), null); //$NON-NLS-1$
if (dialog.open() == Window.CANCEL) {
doUninstall(project, monitor);
}
}
});
}
}
}
|