Line 0
Link Here
|
|
|
1 |
/** |
2 |
* Copyright (c) 2008 Motorola. |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* Diego Sandin (Motorola) - Initial version |
11 |
* Feng Wang (Sybase) - Add configurations into Metadata for Multi-Configs |
12 |
* support. |
13 |
* Diego Sandin (Motorola) - Use Eclipse Message Bundles [Bug 255874] |
14 |
*/ |
15 |
package org.eclipse.mtj.internal.ui.wizards.projects; |
16 |
|
17 |
import java.io.File; |
18 |
import java.io.FileInputStream; |
19 |
import java.io.FileOutputStream; |
20 |
import java.io.IOException; |
21 |
import java.io.InputStream; |
22 |
import java.io.OutputStream; |
23 |
import java.lang.reflect.InvocationTargetException; |
24 |
import java.net.URI; |
25 |
import java.net.URISyntaxException; |
26 |
import java.util.ArrayList; |
27 |
import java.util.HashMap; |
28 |
import java.util.HashSet; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import java.util.Vector; |
32 |
|
33 |
import org.eclipse.core.filesystem.EFS; |
34 |
import org.eclipse.core.filesystem.IFileInfo; |
35 |
import org.eclipse.core.filesystem.IFileStore; |
36 |
import org.eclipse.core.resources.IFolder; |
37 |
import org.eclipse.core.resources.IProject; |
38 |
import org.eclipse.core.resources.IResource; |
39 |
import org.eclipse.core.resources.IResourceStatus; |
40 |
import org.eclipse.core.resources.IWorkspaceDescription; |
41 |
import org.eclipse.core.resources.IWorkspaceRoot; |
42 |
import org.eclipse.core.resources.ResourcesPlugin; |
43 |
import org.eclipse.core.runtime.Assert; |
44 |
import org.eclipse.core.runtime.CoreException; |
45 |
import org.eclipse.core.runtime.IAdaptable; |
46 |
import org.eclipse.core.runtime.IPath; |
47 |
import org.eclipse.core.runtime.IProgressMonitor; |
48 |
import org.eclipse.core.runtime.IStatus; |
49 |
import org.eclipse.core.runtime.NullProgressMonitor; |
50 |
import org.eclipse.core.runtime.OperationCanceledException; |
51 |
import org.eclipse.core.runtime.Path; |
52 |
import org.eclipse.core.runtime.Preferences; |
53 |
import org.eclipse.core.runtime.Status; |
54 |
import org.eclipse.core.runtime.SubProgressMonitor; |
55 |
import org.eclipse.jdt.core.IClasspathEntry; |
56 |
import org.eclipse.jdt.core.IJavaProject; |
57 |
import org.eclipse.jdt.core.JavaCore; |
58 |
import org.eclipse.jdt.ui.PreferenceConstants; |
59 |
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage; |
60 |
import org.eclipse.jface.dialogs.ErrorDialog; |
61 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
62 |
import org.eclipse.jface.preference.IPreferenceStore; |
63 |
import org.eclipse.mtj.core.MTJCore; |
64 |
import org.eclipse.mtj.core.project.IMetaData; |
65 |
import org.eclipse.mtj.core.project.ProjectType; |
66 |
import org.eclipse.mtj.core.project.runtime.MTJRuntimeList; |
67 |
import org.eclipse.mtj.core.sdk.device.IDevice; |
68 |
import org.eclipse.mtj.core.sdk.device.midp.IMIDPDevice; |
69 |
import org.eclipse.mtj.internal.core.IMTJCoreConstants; |
70 |
import org.eclipse.mtj.internal.core.build.preprocessor.PreprocessorBuilder; |
71 |
import org.eclipse.mtj.internal.core.externallibrary.classpath.ExternalLibraryClasspathContainer; |
72 |
import org.eclipse.mtj.internal.core.externallibrary.manager.ExternalLibraryManager; |
73 |
import org.eclipse.mtj.internal.core.externallibrary.model.IExternalLibrary; |
74 |
import org.eclipse.mtj.internal.core.project.midp.IJADConstants; |
75 |
import org.eclipse.mtj.internal.core.project.midp.JavaMEClasspathContainer; |
76 |
import org.eclipse.mtj.internal.core.project.midp.MidletSuiteFactory; |
77 |
import org.eclipse.mtj.internal.core.util.MTJStatusHandler; |
78 |
import org.eclipse.mtj.internal.ui.IMTJUIConstants; |
79 |
import org.eclipse.mtj.internal.ui.MTJUIMessages; |
80 |
import org.eclipse.mtj.internal.ui.util.ExceptionHandler; |
81 |
import org.eclipse.osgi.util.NLS; |
82 |
import org.eclipse.ui.IWorkingSet; |
83 |
import org.eclipse.ui.IWorkingSetManager; |
84 |
import org.eclipse.ui.PlatformUI; |
85 |
import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; |
86 |
|
87 |
/** |
88 |
* The New MIDlet Project Java Capability Wizard Page allows the user to |
89 |
* configure the build path and output location of the MIDlet Project. As |
90 |
* addition to the {@link JavaCapabilityConfigurationPage}, the wizard page does |
91 |
* an early project creation (so that linked folders can be defined). |
92 |
* |
93 |
* @author Diego Madruga Sandin |
94 |
* @since 0.9 |
95 |
*/ |
96 |
public class NewMidletProjectWizardPageJavaSettings extends |
97 |
JavaCapabilityConfigurationPage { |
98 |
|
99 |
private static final String FILENAME_CLASSPATH = ".classpath"; //$NON-NLS-1$ |
100 |
|
101 |
private static final String FILENAME_PROJECT = ".project"; //$NON-NLS-1$ |
102 |
|
103 |
/** |
104 |
* @param projectName |
105 |
* @param location |
106 |
* @return |
107 |
*/ |
108 |
private static URI getRealLocation(String projectName, URI location) { |
109 |
if (location == null) { // inside workspace |
110 |
try { |
111 |
URI rootLocation = MTJCore.getWorkspace().getRoot() |
112 |
.getLocationURI(); |
113 |
|
114 |
location = new URI(rootLocation.getScheme(), null, Path |
115 |
.fromPortableString(rootLocation.getPath()).append( |
116 |
projectName).toString(), null); |
117 |
} catch (URISyntaxException e) { |
118 |
Assert.isTrue(false, "Can't happen"); //$NON-NLS-1$ |
119 |
} |
120 |
} |
121 |
return location; |
122 |
} |
123 |
|
124 |
public Boolean isAutobuild; |
125 |
private IProject currProject; |
126 |
private URI currProjectLocation; |
127 |
private File fDotClasspathBackup; |
128 |
private File fDotProjectBackup; |
129 |
private NewMidletProjectWizardPageOne firstPage; |
130 |
private NewMidletProjectWizardPageProperties propertiesPage; |
131 |
private HashSet<IFileStore> fOrginalFolders; |
132 |
|
133 |
private boolean keepContent; |
134 |
|
135 |
private NewMidletProjectWizardPageLibrary pageLibrary; |
136 |
|
137 |
/** |
138 |
* Creates a new New MIDlet Project Java Capability Wizard Page. |
139 |
* |
140 |
* @param workbench |
141 |
*/ |
142 |
public NewMidletProjectWizardPageJavaSettings(NewMidletProjectWizardPageOne pageOne, |
143 |
NewMidletProjectWizardPageProperties pageTwo, |
144 |
NewMidletProjectWizardPageLibrary pageLibrary) { |
145 |
this.currProjectLocation = null; |
146 |
this.currProject = null; |
147 |
this.firstPage = pageOne; |
148 |
this.propertiesPage = pageTwo; |
149 |
this.pageLibrary = pageLibrary; |
150 |
isAutobuild = null; |
151 |
} |
152 |
|
153 |
/** |
154 |
* Called from the wizard on cancel. |
155 |
*/ |
156 |
public void performCancel() { |
157 |
if (currProject != null) { |
158 |
removeProvisonalProject(); |
159 |
} |
160 |
} |
161 |
|
162 |
/** |
163 |
* Called from the wizard on finish. |
164 |
* |
165 |
* @param monitor the progress monitor |
166 |
* @throws CoreException thrown when the project creation or configuration |
167 |
* failed |
168 |
* @throws InterruptedException thrown when the user canceled the project |
169 |
* creation |
170 |
*/ |
171 |
@SuppressWarnings("unchecked") |
172 |
public void performFinish(IProgressMonitor monitor) throws CoreException, |
173 |
InterruptedException { |
174 |
try { |
175 |
|
176 |
monitor |
177 |
.beginTask( |
178 |
MTJUIMessages.NewMidletProjectWizardPageThree_performFinish_monitor_taskname, |
179 |
3); |
180 |
if (currProject == null) { |
181 |
updateProject(new SubProgressMonitor(monitor, 1)); |
182 |
} |
183 |
|
184 |
configureJavaProject(new SubProgressMonitor(monitor, 2)); |
185 |
|
186 |
if (!keepContent) { |
187 |
IJavaProject project = JavaCore.create(currProject); |
188 |
Map options = project.getOptions(false); |
189 |
|
190 |
// complete compliance options |
191 |
project.setOptions(options); |
192 |
} |
193 |
|
194 |
} finally { |
195 |
monitor.done(); |
196 |
currProject = null; |
197 |
if (isAutobuild != null) { |
198 |
IWorkspaceDescription description = ResourcesPlugin |
199 |
.getWorkspace().getDescription(); |
200 |
description.setAutoBuilding(isAutobuild.booleanValue()); |
201 |
MTJCore.getWorkspace().setDescription(description); |
202 |
isAutobuild = null; |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
/* |
208 |
* (non-Javadoc) |
209 |
* @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean) |
210 |
*/ |
211 |
@Override |
212 |
public void setVisible(boolean visible) { |
213 |
boolean isShownFirstTime = visible && (currProject == null); |
214 |
if (visible) { |
215 |
if (isShownFirstTime) { |
216 |
// entering from the first page |
217 |
createProvisonalProject(); |
218 |
} |
219 |
} else { |
220 |
if ((getContainer().getCurrentPage() == firstPage) |
221 |
|| (getContainer().getCurrentPage() == pageLibrary)) { |
222 |
// leaving back to previous pages |
223 |
removeProvisonalProject(); |
224 |
} |
225 |
} |
226 |
super.setVisible(visible); |
227 |
if (isShownFirstTime) { |
228 |
setFocus(); |
229 |
} |
230 |
} |
231 |
|
232 |
/** |
233 |
* Update the java configuration before making the page visible. |
234 |
*/ |
235 |
public void updateConfiguration() { |
236 |
String projectName = firstPage.getProjectName(); |
237 |
|
238 |
currProject = MTJCore.getWorkspace().getRoot().getProject( |
239 |
projectName); |
240 |
|
241 |
IJavaProject javaProject = JavaCore.create(currProject); |
242 |
IPath projectPath = currProject.getFullPath(); |
243 |
|
244 |
// Initialize the classpath entries using the source directories |
245 |
// and classpath container |
246 |
ArrayList<IClasspathEntry> entryList = new ArrayList<IClasspathEntry>(); |
247 |
entryList.add(getSrcPathEntry(projectPath)); |
248 |
addResourcesDirectoryIfRequested(entryList, currProject); |
249 |
IPath entryPath = new Path(JavaMEClasspathContainer.JAVAME_CONTAINER |
250 |
+ "/" + firstPage.getSelectedDevice()); //$NON-NLS-1$ |
251 |
|
252 |
entryList.add(JavaCore.newContainerEntry(entryPath)); |
253 |
|
254 |
IClasspathEntry[] entries = entryList |
255 |
.toArray(new IClasspathEntry[entryList.size()]); |
256 |
|
257 |
init(javaProject, null, entries, false); |
258 |
} |
259 |
|
260 |
/** |
261 |
* Add a resources directory as a source path entry if the user preferences |
262 |
* requested. |
263 |
* |
264 |
* @param entryList |
265 |
* @param project |
266 |
*/ |
267 |
private void addResourcesDirectoryIfRequested( |
268 |
List<IClasspathEntry> entryList, IProject project) { |
269 |
Preferences prefs = MTJCore.getMTJCore().getPluginPreferences(); |
270 |
|
271 |
if (useSourceAndBinaryFolders() |
272 |
&& prefs.getBoolean(IMTJCoreConstants.PREF_USE_RESOURCES_DIR)) { |
273 |
// Create the resources directory if it doesn't already exist |
274 |
String resDirName = prefs |
275 |
.getString(IMTJCoreConstants.PREF_RESOURCES_DIR); |
276 |
IFolder resFolder = project.getFolder(resDirName); |
277 |
|
278 |
if (!resFolder.exists()) { |
279 |
try { |
280 |
resFolder.create(true, true, new NullProgressMonitor()); |
281 |
} catch (CoreException e) { |
282 |
e.printStackTrace(); |
283 |
} |
284 |
} |
285 |
|
286 |
// Add it as a source folder to the java project |
287 |
entryList.add(JavaCore.newSourceEntry(resFolder.getFullPath())); |
288 |
} |
289 |
} |
290 |
|
291 |
private IStatus changeToNewProject() { |
292 |
class UpdateRunnable implements IRunnableWithProgress { |
293 |
public IStatus infoStatus = Status.OK_STATUS; |
294 |
|
295 |
public void run(IProgressMonitor monitor) |
296 |
throws InvocationTargetException, InterruptedException { |
297 |
try { |
298 |
if (isAutobuild == null) { |
299 |
IWorkspaceDescription description = ResourcesPlugin |
300 |
.getWorkspace().getDescription(); |
301 |
isAutobuild = Boolean.valueOf(description |
302 |
.isAutoBuilding()); |
303 |
description.setAutoBuilding(false); |
304 |
MTJCore.getWorkspace().setDescription( |
305 |
description); |
306 |
} |
307 |
infoStatus = updateProject(monitor); |
308 |
} catch (CoreException e) { |
309 |
throw new InvocationTargetException(e); |
310 |
} catch (OperationCanceledException e) { |
311 |
throw new InterruptedException(); |
312 |
} finally { |
313 |
monitor.done(); |
314 |
} |
315 |
} |
316 |
} |
317 |
|
318 |
UpdateRunnable op = new UpdateRunnable(); |
319 |
try { |
320 |
getContainer().run(true, false, |
321 |
new WorkspaceModifyDelegatingOperation(op)); |
322 |
|
323 |
return op.infoStatus; |
324 |
} catch (InvocationTargetException e) { |
325 |
|
326 |
final String title = MTJUIMessages.NewMidletProjectWizardPageThree_changeToNewProject_errordialog_title; |
327 |
final String message = MTJUIMessages.NewMidletProjectWizardPageThree_changeToNewProject_errordialog_message; |
328 |
ExceptionHandler.handle(e, getShell(), title, message); |
329 |
|
330 |
} catch (InterruptedException e) { |
331 |
// cancel pressed |
332 |
} |
333 |
return null; |
334 |
} |
335 |
|
336 |
/** |
337 |
* @param source |
338 |
* @param target |
339 |
* @param monitor |
340 |
* @throws IOException |
341 |
* @throws CoreException |
342 |
*/ |
343 |
private void copyFile(File source, IFileStore target, |
344 |
IProgressMonitor monitor) throws IOException, CoreException { |
345 |
FileInputStream is = new FileInputStream(source); |
346 |
OutputStream os = target.openOutputStream(EFS.NONE, monitor); |
347 |
copyFile(is, os); |
348 |
} |
349 |
|
350 |
/** |
351 |
* @param source |
352 |
* @param target |
353 |
* @throws IOException |
354 |
* @throws CoreException |
355 |
*/ |
356 |
private void copyFile(IFileStore source, File target) throws IOException, |
357 |
CoreException { |
358 |
InputStream is = source.openInputStream(EFS.NONE, null); |
359 |
FileOutputStream os = new FileOutputStream(target); |
360 |
copyFile(is, os); |
361 |
} |
362 |
|
363 |
/** |
364 |
* @param is |
365 |
* @param os |
366 |
* @throws IOException |
367 |
*/ |
368 |
private void copyFile(InputStream is, OutputStream os) throws IOException { |
369 |
try { |
370 |
byte[] buffer = new byte[8192]; |
371 |
while (true) { |
372 |
int bytesRead = is.read(buffer); |
373 |
if (bytesRead == -1) { |
374 |
break; |
375 |
} |
376 |
|
377 |
os.write(buffer, 0, bytesRead); |
378 |
} |
379 |
} finally { |
380 |
try { |
381 |
is.close(); |
382 |
} finally { |
383 |
os.close(); |
384 |
} |
385 |
} |
386 |
} |
387 |
|
388 |
/** |
389 |
* @param source |
390 |
* @param name |
391 |
* @return |
392 |
* @throws CoreException |
393 |
*/ |
394 |
private File createBackup(IFileStore source, String name) |
395 |
throws CoreException { |
396 |
try { |
397 |
File bak = File.createTempFile("eclipse-" + name, ".bak"); //$NON-NLS-1$//$NON-NLS-2$ |
398 |
copyFile(source, bak); |
399 |
return bak; |
400 |
} catch (IOException e) { |
401 |
IStatus status = new Status( |
402 |
IStatus.ERROR, |
403 |
IMTJUIConstants.PLUGIN_ID, |
404 |
IStatus.ERROR, |
405 |
MTJUIMessages.NewMidletProjectWizardPageThree_createBackup_error_1 |
406 |
+ name |
407 |
+ MTJUIMessages.NewMidletProjectWizardPageThree_createBackup_error_2, |
408 |
e); |
409 |
throw new CoreException(status); |
410 |
} |
411 |
} |
412 |
|
413 |
private void deleteProjectFile(URI projectLocation) throws CoreException { |
414 |
IFileStore file = EFS.getStore(projectLocation); |
415 |
if (file.fetchInfo().exists()) { |
416 |
IFileStore projectFile = file.getChild(FILENAME_PROJECT); |
417 |
if (projectFile.fetchInfo().exists()) { |
418 |
projectFile.delete(EFS.NONE, null); |
419 |
} |
420 |
} |
421 |
} |
422 |
|
423 |
private final void doRemoveProject(IProgressMonitor monitor) |
424 |
throws InvocationTargetException { |
425 |
final boolean noProgressMonitor = (currProjectLocation == null); // inside |
426 |
// workspace |
427 |
if ((monitor == null) || noProgressMonitor) { |
428 |
monitor = new NullProgressMonitor(); |
429 |
} |
430 |
monitor |
431 |
.beginTask( |
432 |
MTJUIMessages.NewMidletProjectWizardPageThree_doRemoveProject_taskname, |
433 |
3); |
434 |
try { |
435 |
try { |
436 |
URI projLoc = currProject.getLocationURI(); |
437 |
|
438 |
boolean removeContent = !keepContent |
439 |
&& currProject.isSynchronized(IResource.DEPTH_INFINITE); |
440 |
if ((!removeContent) |
441 |
&& (currProject.getProject().getFullPath().toFile() |
442 |
.exists())) { |
443 |
restoreExistingFolders(projLoc); |
444 |
} |
445 |
currProject.delete(removeContent, false, |
446 |
new SubProgressMonitor(monitor, 2)); |
447 |
|
448 |
MidletSuiteFactory.removeMidletSuiteProject(getJavaProject()); |
449 |
|
450 |
restoreExistingFiles(projLoc, |
451 |
new SubProgressMonitor(monitor, 1)); |
452 |
} finally { |
453 |
IWorkspaceDescription description = ResourcesPlugin |
454 |
.getWorkspace().getDescription(); |
455 |
description.setAutoBuilding(isAutobuild.booleanValue()); |
456 |
MTJCore.getWorkspace().setDescription(description); |
457 |
// isAutobuild must be set |
458 |
isAutobuild = null; |
459 |
} |
460 |
} catch (CoreException e) { |
461 |
throw new InvocationTargetException(e); |
462 |
} finally { |
463 |
monitor.done(); |
464 |
currProject = null; |
465 |
keepContent = false; |
466 |
} |
467 |
} |
468 |
|
469 |
/** |
470 |
* Get the source path for the project taking into account the new project |
471 |
* preferences that the user has specified. |
472 |
* |
473 |
* @param projectPath |
474 |
* @return |
475 |
*/ |
476 |
private IPath getSrcPath(IPath projectPath) { |
477 |
IPath srcPath = projectPath; |
478 |
|
479 |
if (useSourceAndBinaryFolders()) { |
480 |
IPreferenceStore store = PreferenceConstants.getPreferenceStore(); |
481 |
String srcPathName = store |
482 |
.getString(PreferenceConstants.SRCBIN_SRCNAME); |
483 |
srcPath = projectPath.append(srcPathName); |
484 |
} |
485 |
|
486 |
return srcPath; |
487 |
} |
488 |
|
489 |
/** |
490 |
* Return an IClasspathEntry for the source path. |
491 |
* |
492 |
* @param projectPath |
493 |
* @return |
494 |
*/ |
495 |
private IClasspathEntry getSrcPathEntry(IPath projectPath) { |
496 |
IPath srcPath = getSrcPath(projectPath); |
497 |
|
498 |
// Set up exclusions for the verified and deployed directories |
499 |
// if the source and project directories are the same |
500 |
IPath[] exclusions = null; |
501 |
if (srcPath.equals(projectPath)) { |
502 |
exclusions = new IPath[3]; |
503 |
exclusions[0] = new Path(MTJCore.getDeploymentDirectoryName() + "/"); //$NON-NLS-1$ |
504 |
exclusions[1] = new Path(IMTJCoreConstants.TEMP_FOLDER_NAME + "/"); //$NON-NLS-1$ |
505 |
exclusions[2] = new Path(PreprocessorBuilder.PROCESSED_DIRECTORY |
506 |
+ "/"); //$NON-NLS-1$ |
507 |
} else { |
508 |
exclusions = new IPath[0]; |
509 |
} |
510 |
|
511 |
return JavaCore.newSourceEntry(srcPath, exclusions); |
512 |
} |
513 |
|
514 |
/** |
515 |
* @param realLocation |
516 |
* @return |
517 |
* @throws CoreException |
518 |
*/ |
519 |
private boolean hasExistingContent(URI realLocation) throws CoreException { |
520 |
IFileStore file = EFS.getStore(realLocation); |
521 |
return file.fetchInfo().exists(); |
522 |
} |
523 |
|
524 |
private void rememberExisitingFolders(URI projectLocation) { |
525 |
fOrginalFolders = new HashSet<IFileStore>(); |
526 |
|
527 |
try { |
528 |
IFileStore[] children = EFS.getStore(projectLocation).childStores( |
529 |
EFS.NONE, null); |
530 |
for (IFileStore child : children) { |
531 |
IFileInfo info = child.fetchInfo(); |
532 |
if (info.isDirectory() && info.exists() |
533 |
&& !fOrginalFolders.contains(child.getName())) { |
534 |
fOrginalFolders.add(child); |
535 |
} |
536 |
} |
537 |
} catch (CoreException e) { |
538 |
final String title = MTJUIMessages.NewMidletProjectWizardPageThree_rememberExisitingFolders_errordialog_title; |
539 |
final String message = MTJUIMessages.NewMidletProjectWizardPageThree_rememberExisitingFolders_errordialog_message; |
540 |
ExceptionHandler.handle(e, getShell(), title, message); |
541 |
} |
542 |
} |
543 |
|
544 |
private void rememberExistingFiles(URI projectLocation) |
545 |
throws CoreException { |
546 |
fDotProjectBackup = null; |
547 |
fDotClasspathBackup = null; |
548 |
|
549 |
IFileStore file = EFS.getStore(projectLocation); |
550 |
if (file.fetchInfo().exists()) { |
551 |
IFileStore projectFile = file.getChild(FILENAME_PROJECT); |
552 |
if (projectFile.fetchInfo().exists()) { |
553 |
fDotProjectBackup = createBackup(projectFile, "project-desc"); //$NON-NLS-1$ |
554 |
} |
555 |
IFileStore classpathFile = file.getChild(FILENAME_CLASSPATH); |
556 |
if (classpathFile.fetchInfo().exists()) { |
557 |
fDotClasspathBackup = createBackup(classpathFile, |
558 |
"classpath-desc"); //$NON-NLS-1$ |
559 |
} |
560 |
} |
561 |
} |
562 |
|
563 |
private void restoreExistingFiles(URI projectLocation, |
564 |
IProgressMonitor monitor) throws CoreException { |
565 |
int ticks = ((fDotProjectBackup != null ? 1 : 0) + (fDotClasspathBackup != null ? 1 |
566 |
: 0)) * 2; |
567 |
monitor.beginTask("", ticks); //$NON-NLS-1$ |
568 |
try { |
569 |
IFileStore projectFile = EFS.getStore(projectLocation).getChild( |
570 |
FILENAME_PROJECT); |
571 |
projectFile.delete(EFS.NONE, new SubProgressMonitor(monitor, 1)); |
572 |
if (fDotProjectBackup != null) { |
573 |
copyFile(fDotProjectBackup, projectFile, |
574 |
new SubProgressMonitor(monitor, 1)); |
575 |
} |
576 |
} catch (IOException e) { |
577 |
IStatus status = new Status( |
578 |
IStatus.ERROR, |
579 |
IMTJUIConstants.PLUGIN_ID, |
580 |
IStatus.ERROR, |
581 |
MTJUIMessages.NewMidletProjectWizardPageThree_restoreExistingFiles_problem_restoring_dotproject, |
582 |
e); |
583 |
throw new CoreException(status); |
584 |
} |
585 |
try { |
586 |
IFileStore classpathFile = EFS.getStore(projectLocation).getChild( |
587 |
FILENAME_CLASSPATH); |
588 |
classpathFile.delete(EFS.NONE, new SubProgressMonitor(monitor, 1)); |
589 |
if (fDotClasspathBackup != null) { |
590 |
copyFile(fDotClasspathBackup, classpathFile, |
591 |
new SubProgressMonitor(monitor, 1)); |
592 |
} |
593 |
} catch (IOException e) { |
594 |
IStatus status = new Status( |
595 |
IStatus.ERROR, |
596 |
IMTJUIConstants.PLUGIN_ID, |
597 |
IStatus.ERROR, |
598 |
MTJUIMessages.NewMidletProjectWizardPageThree_restoreExistingFiles_problem_restoring_dotclasspath, |
599 |
e); |
600 |
throw new CoreException(status); |
601 |
} |
602 |
} |
603 |
|
604 |
private void restoreExistingFolders(URI projectLocation) { |
605 |
try { |
606 |
IFileStore[] children = EFS.getStore(projectLocation).childStores( |
607 |
EFS.NONE, null); |
608 |
for (IFileStore child : children) { |
609 |
IFileInfo info = child.fetchInfo(); |
610 |
if (info.isDirectory() && info.exists() |
611 |
&& !fOrginalFolders.contains(child)) { |
612 |
child.delete(EFS.NONE, null); |
613 |
fOrginalFolders.remove(child); |
614 |
} |
615 |
} |
616 |
|
617 |
for (IFileStore fileStore : fOrginalFolders) { |
618 |
IFileStore deleted = fileStore; |
619 |
deleted.mkdir(EFS.NONE, null); |
620 |
} |
621 |
} catch (CoreException e) { |
622 |
final String title = MTJUIMessages.NewMidletProjectWizardPageThree_rememberExisitingFolders_errordialog_title; |
623 |
final String message = MTJUIMessages.NewMidletProjectWizardPageThree_rememberExisitingFolders_errordialog_message; |
624 |
ExceptionHandler.handle(e, getShell(), title, message); |
625 |
} |
626 |
} |
627 |
|
628 |
/** |
629 |
* @param monitor |
630 |
* @return |
631 |
* @throws CoreException |
632 |
* @throws InterruptedException |
633 |
*/ |
634 |
private final IStatus updateProject(IProgressMonitor monitor) |
635 |
throws CoreException, InterruptedException { |
636 |
IStatus result = Status.OK_STATUS; |
637 |
if (monitor == null) { |
638 |
monitor = new NullProgressMonitor(); |
639 |
} |
640 |
try { |
641 |
monitor |
642 |
.beginTask( |
643 |
MTJUIMessages.NewMidletProjectWizardPageThree_updateProject_taskname, |
644 |
7); |
645 |
if (monitor.isCanceled()) { |
646 |
throw new OperationCanceledException(); |
647 |
} |
648 |
|
649 |
String projectName = firstPage.getProjectName(); |
650 |
|
651 |
currProject = MTJCore.getWorkspace().getRoot().getProject( |
652 |
projectName); |
653 |
currProjectLocation = firstPage.getProjectLocationURI(); |
654 |
|
655 |
URI realLocation = getRealLocation(projectName, currProjectLocation); |
656 |
keepContent = hasExistingContent(realLocation); |
657 |
|
658 |
if (monitor.isCanceled()) { |
659 |
throw new OperationCanceledException(); |
660 |
} |
661 |
|
662 |
if (keepContent) { |
663 |
rememberExistingFiles(realLocation); |
664 |
rememberExisitingFolders(realLocation); |
665 |
} |
666 |
|
667 |
if (monitor.isCanceled()) { |
668 |
throw new OperationCanceledException(); |
669 |
} |
670 |
|
671 |
try { |
672 |
|
673 |
JavaCapabilityConfigurationPage.createProject(currProject, |
674 |
firstPage.getProjectLocationURI(), |
675 |
new SubProgressMonitor(monitor, 1)); |
676 |
|
677 |
// Set the device into the project metadata to make |
678 |
// the java project creation happy. |
679 |
IDevice device = firstPage.getSelectedDevice(); |
680 |
MTJRuntimeList configurations = firstPage.getConfigurations(); |
681 |
// Make up Metadata and save it. |
682 |
IMetaData metadata = MTJCore.createMetaData(currProject, |
683 |
ProjectType.MIDLET_SUITE); |
684 |
metadata.setMTJRuntimeList(configurations); |
685 |
try { |
686 |
metadata.saveMetaData(); |
687 |
} catch (CoreException e) { |
688 |
throw new InvocationTargetException(e); |
689 |
} |
690 |
|
691 |
updateConfiguration(); |
692 |
|
693 |
// Get the java nature |
694 |
try { |
695 |
IRunnableWithProgress progress = super.getRunnable(); |
696 |
progress.run(monitor); |
697 |
} catch (Throwable e) { |
698 |
throw new CoreException(new Status( |
699 |
IResourceStatus.FAILED_READ_METADATA, "", e //$NON-NLS-1$ |
700 |
.getMessage())); |
701 |
} |
702 |
|
703 |
// Get the J2ME nature and metadata set up |
704 |
String jadFileName = firstPage.getJadFileName(); |
705 |
|
706 |
IJavaProject javaProject = super.getJavaProject(); |
707 |
|
708 |
MidletSuiteFactory.MidletSuiteCreationRunnable runnable = MidletSuiteFactory |
709 |
.getMidletSuiteCreationRunnable(currProject, |
710 |
javaProject, (IMIDPDevice) device, jadFileName); |
711 |
|
712 |
Map<String, String> properties = new HashMap<String, String>(); |
713 |
properties.put(IJADConstants.JAD_MIDLET_NAME, propertiesPage.getMIDletName()); |
714 |
properties.put(IJADConstants.JAD_MIDLET_VENDOR, propertiesPage.getMIDletVendor()); |
715 |
properties.put(IJADConstants.JAD_MIDLET_VERSION, propertiesPage.getMIDletVersion()); |
716 |
properties.put(IJADConstants.JAD_MICROEDITION_CONFIG, propertiesPage.getMeConfiguration()); |
717 |
properties.put(IJADConstants.JAD_MICROEDITION_PROFILE, propertiesPage.getMeProfile()); |
718 |
|
719 |
runnable.setProperties(properties); |
720 |
|
721 |
|
722 |
runnable.setPreprocessingEnable(propertiesPage |
723 |
.isPreprocessingEnabled()); |
724 |
|
725 |
boolean localizationEnabled = propertiesPage |
726 |
.isLocalizationEnabled(); |
727 |
runnable.setLocalizationEnabled(localizationEnabled); |
728 |
if (localizationEnabled) { |
729 |
runnable.setPropertiesFolderName(propertiesPage |
730 |
.getPropertiesFolderName()); |
731 |
runnable.setPackageName(propertiesPage.getPackageName()); |
732 |
} |
733 |
runnable.setJMUnitSupport(propertiesPage.isJMUnitEnabled()); |
734 |
|
735 |
runnable.run(monitor); |
736 |
|
737 |
IAdaptable[] adaptableElements = new IAdaptable[] { currProject }; |
738 |
IWorkingSet workingSet = firstPage.getWorkingSet(); |
739 |
if (workingSet != null) { |
740 |
List<IAdaptable> list = new Vector<IAdaptable>(); |
741 |
|
742 |
for (IAdaptable adaptable : workingSet.getElements()) { |
743 |
list.add(adaptable); |
744 |
} |
745 |
for (IAdaptable adaptable : workingSet |
746 |
.adaptElements(adaptableElements)) { |
747 |
list.add(adaptable); |
748 |
} |
749 |
IAdaptable[] newElements = new IAdaptable[list.size()]; |
750 |
workingSet.setElements(list.toArray(newElements)); |
751 |
IWorkingSetManager workingSetManager = PlatformUI |
752 |
.getWorkbench().getWorkingSetManager(); |
753 |
workingSetManager.addRecentWorkingSet(workingSet); |
754 |
} |
755 |
|
756 |
|
757 |
} catch (CoreException e) { |
758 |
if (e.getStatus().getCode() == IResourceStatus.FAILED_READ_METADATA) { |
759 |
result = new Status( |
760 |
IStatus.INFO, |
761 |
IMTJUIConstants.PLUGIN_ID, |
762 |
MTJUIMessages.NewMidletProjectWizardPageThree_updateProject_fail_read_metadata); |
763 |
|
764 |
deleteProjectFile(realLocation); |
765 |
if (currProject.exists()) { |
766 |
currProject.delete(true, null); |
767 |
} |
768 |
|
769 |
createProject(currProject, currProjectLocation, null); |
770 |
} else { |
771 |
throw e; |
772 |
} |
773 |
} catch (Throwable e) { |
774 |
throw new CoreException(Status.CANCEL_STATUS); |
775 |
} |
776 |
|
777 |
if (monitor.isCanceled()) { |
778 |
throw new OperationCanceledException(); |
779 |
} |
780 |
|
781 |
initializeBuildPath(JavaCore.create(currProject), |
782 |
new SubProgressMonitor(monitor, 2)); |
783 |
|
784 |
configureJavaProject(new SubProgressMonitor(monitor, 3)); |
785 |
// create the Java project to allow the use of the new source folder |
786 |
// page |
787 |
} catch (Exception e) { |
788 |
throw new CoreException(Status.CANCEL_STATUS); |
789 |
} finally { |
790 |
monitor.done(); |
791 |
} |
792 |
|
793 |
return result; |
794 |
} |
795 |
|
796 |
/** |
797 |
* Return a boolean indicating whether there will be separate source and |
798 |
* binary folders in the project. |
799 |
* |
800 |
* @return |
801 |
*/ |
802 |
private boolean useSourceAndBinaryFolders() { |
803 |
IPreferenceStore store = PreferenceConstants.getPreferenceStore(); |
804 |
return store.getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ); |
805 |
} |
806 |
|
807 |
/** |
808 |
* Creates the provisional project on which the wizard is working on. The |
809 |
* provisional project is typically created when the page is entered the |
810 |
* first time. The early project creation is required to configure linked |
811 |
* folders. |
812 |
* |
813 |
* @return the provisional project |
814 |
*/ |
815 |
protected IProject createProvisonalProject() { |
816 |
IStatus status = changeToNewProject(); |
817 |
if ((status != null) && !status.isOK()) { |
818 |
ErrorDialog.openError(getShell(), "createProvisonalProject", null, //$NON-NLS-1$ |
819 |
status); |
820 |
} |
821 |
return currProject; |
822 |
} |
823 |
|
824 |
/** |
825 |
* Evaluates the new build path and output folder according to the settings |
826 |
* on the first page. The resulting build path is set by calling |
827 |
* {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)}. Clients |
828 |
* can override this method. |
829 |
* |
830 |
* @param javaProject the new project which is already created when this |
831 |
* method is called. |
832 |
* @param monitor the progress monitor |
833 |
* @throws CoreException thrown when initializing the build path failed |
834 |
*/ |
835 |
protected void initializeBuildPath(IJavaProject javaProject, |
836 |
IProgressMonitor monitor) throws CoreException { |
837 |
if (monitor == null) { |
838 |
monitor = new NullProgressMonitor(); |
839 |
} |
840 |
monitor |
841 |
.beginTask( |
842 |
MTJUIMessages.NewMidletProjectWizardPageThree_updateProject_monitor_buildpath_name, |
843 |
2); |
844 |
|
845 |
try { |
846 |
IClasspathEntry[] entries = null; |
847 |
IProject project = javaProject.getProject(); |
848 |
|
849 |
List<IClasspathEntry> cpEntries = new ArrayList<IClasspathEntry>(); |
850 |
IWorkspaceRoot root = project.getWorkspace().getRoot(); |
851 |
|
852 |
IClasspathEntry sourceClasspathEntry = getSrcPathEntry(project |
853 |
.getFullPath()); |
854 |
|
855 |
if (sourceClasspathEntry.getPath() != project.getFullPath()) { |
856 |
|
857 |
IFolder folder = root.getFolder(sourceClasspathEntry.getPath()); |
858 |
|
859 |
if (!folder.exists()) { |
860 |
folder.create(true, true, |
861 |
new SubProgressMonitor(monitor, 1)); |
862 |
} |
863 |
} |
864 |
cpEntries.add(sourceClasspathEntry); |
865 |
addResourcesDirectoryIfRequested(cpEntries, project); |
866 |
|
867 |
IPath entryPath = new Path( |
868 |
JavaMEClasspathContainer.JAVAME_CONTAINER + "/" //$NON-NLS-1$ |
869 |
+ firstPage.getSelectedDevice()); |
870 |
cpEntries.add(JavaCore.newContainerEntry(entryPath)); |
871 |
|
872 |
if (pageLibrary != null) { |
873 |
List<IPath> list = pageLibrary.getSelectedLibraries(); |
874 |
|
875 |
// Add the JMUnit library, if necessary |
876 |
if(propertiesPage.isJMUnitEnabled()) { |
877 |
String libName = "JMUnit for CLDC 1.1"; //$NON-NLS-1$ |
878 |
IExternalLibrary jmunitLibrary = ExternalLibraryManager.getInstance().getMidletLibrary(libName); |
879 |
if (jmunitLibrary == null) { |
880 |
String message = NLS.bind( |
881 |
"ExternalLibraryManager does not contain a {0} library.", |
882 |
libName); |
883 |
MTJStatusHandler.throwCoreException(IStatus.ERROR, -999, message); |
884 |
} |
885 |
|
886 |
IPath path = new Path(ExternalLibraryClasspathContainer.EXTERNAL_LIBRARY_CONTAINER_ID + File.separator + libName); |
887 |
list.add(path); |
888 |
} |
889 |
|
890 |
for (IPath path : list) { |
891 |
cpEntries.add(JavaCore.newContainerEntry(path, true)); |
892 |
} |
893 |
|
894 |
} |
895 |
|
896 |
entries = cpEntries.toArray(new IClasspathEntry[cpEntries.size()]); |
897 |
|
898 |
if (monitor.isCanceled()) { |
899 |
throw new OperationCanceledException(); |
900 |
} |
901 |
|
902 |
init(javaProject, getOutputLocation(), entries, true); |
903 |
} finally { |
904 |
monitor.done(); |
905 |
} |
906 |
} |
907 |
|
908 |
/** |
909 |
* Removes the provisional project. The provisional project is typically |
910 |
* removed when the user cancels the wizard or goes back to the first page. |
911 |
*/ |
912 |
protected void removeProvisonalProject() { |
913 |
if (!currProject.exists()) { |
914 |
currProject = null; |
915 |
return; |
916 |
} |
917 |
|
918 |
IRunnableWithProgress op = new IRunnableWithProgress() { |
919 |
public void run(IProgressMonitor monitor) |
920 |
throws InvocationTargetException, InterruptedException { |
921 |
doRemoveProject(monitor); |
922 |
} |
923 |
}; |
924 |
|
925 |
try { |
926 |
getContainer().run(true, true, |
927 |
new WorkspaceModifyDelegatingOperation(op)); |
928 |
} catch (InvocationTargetException e) { |
929 |
|
930 |
final String title = MTJUIMessages.NewMidletProjectWizardPageThree_updateProject_errordialog_title; |
931 |
final String message = MTJUIMessages.NewMidletProjectWizardPageThree_updateProject_errordialog_message; |
932 |
ExceptionHandler.handle(e, getShell(), title, message); |
933 |
|
934 |
} catch (InterruptedException e) { |
935 |
// cancel pressed |
936 |
} |
937 |
} |
938 |
|
939 |
/* |
940 |
* (non-Javadoc) |
941 |
* @see |
942 |
* org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage#useNewSourcePage |
943 |
* () |
944 |
*/ |
945 |
@Override |
946 |
protected final boolean useNewSourcePage() { |
947 |
return true; |
948 |
} |
949 |
} |