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