KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > RemoveCapabilityWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.ui.internal.ide.dialogs;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectDescription;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.wizard.Wizard;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.ui.ICapabilityUninstallWizard;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.WorkspaceModifyOperation;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
32 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
33
34 /**
35  * Internal workbench wizard to remove a capability
36  * from a project. Also removes prerequisite natures
37  * as specified in the <code>init</code> method.
38  * <p>
39  * This wizard is intended to be used by the
40  * <code>RemoveCapabilityStep</code> class only.
41  * </p>
42  */

43 public class RemoveCapabilityWizard extends Wizard implements
44         ICapabilityUninstallWizard {
45     private IProject project;
46
47     private String JavaDoc[] natureIds;
48
49     /**
50      * Creates an empty wizard for removing a capability
51      * from a project.
52      */

53     /* package */RemoveCapabilityWizard() {
54         super();
55     }
56
57     /* (non-Javadoc)
58      * Method declared on ICapabilityUninstallWizard.
59      */

60     public void init(IWorkbench workbench, IStructuredSelection selection,
61             IProject project, String JavaDoc[] natureIds) {
62         this.project = project;
63         this.natureIds = natureIds;
64     }
65
66     /* (non-Javadoc)
67      * Method declared on IWizard.
68      */

69     public boolean performFinish() {
70         return updateNatures();
71     }
72
73     /**
74      * Update the project natures
75      */

76     private boolean updateNatures() {
77         // define the operation to update natures
78
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
79             protected void execute(IProgressMonitor monitor)
80                     throws CoreException {
81                 try {
82                     IProjectDescription description = project.getDescription();
83                     String JavaDoc[] oldIds = description.getNatureIds();
84                     ArrayList JavaDoc newIds = new ArrayList JavaDoc(oldIds.length);
85                     for (int i = 0; i < oldIds.length; i++) {
86                         boolean keepNature = true;
87                         for (int j = 0; j < natureIds.length; j++) {
88                             if (natureIds[j].equals(oldIds[i])) {
89                                 keepNature = false;
90                                 break;
91                             }
92                         }
93                         if (keepNature)
94                             newIds.add(oldIds[i]);
95                     }
96                     String JavaDoc[] results = new String JavaDoc[newIds.size()];
97                     newIds.toArray(results);
98                     description.setNatureIds(results);
99                     project.setDescription(description, monitor);
100                 } finally {
101                     monitor.done();
102                 }
103             }
104         };
105
106         // run the update nature operation
107
try {
108             getContainer().run(true, true, op);
109         } catch (InterruptedException JavaDoc e) {
110             return false;
111         } catch (InvocationTargetException JavaDoc e) {
112             Throwable JavaDoc t = e.getTargetException();
113             if (t instanceof CoreException) {
114                 ErrorDialog.openError(getShell(), IDEWorkbenchMessages.RemoveCapabilityWizard_errorMessage,
115                         null, // no special message
116
((CoreException) t).getStatus());
117             } else {
118                 // Unexpected runtime exceptions and errors may still occur.
119
IDEWorkbenchPlugin.getDefault().getLog().log(
120                         new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, t
121                                 .toString(), t));
122                 MessageDialog
123                         .openError(
124                                 getShell(),
125                                 IDEWorkbenchMessages.RemoveCapabilityWizard_errorMessage,
126                                 NLS.bind(IDEWorkbenchMessages.RemoveCapabilityWizard_internalError, t.getMessage()));
127             }
128             return false;
129         }
130
131         return true;
132     }
133 }
134
Popular Tags