KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.wizard.IWizardPage;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
24 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
25 import org.eclipse.ui.internal.ide.registry.Capability;
26 import org.eclipse.ui.internal.ide.registry.CapabilityRegistry;
27
28 /**
29  * Standard workbench wizard that guides the user to supply
30  * the necessary information to configure new capabilities
31  * on an existing project.
32  */

33 public class UpdateProjectCapabilityWizard extends MultiStepCapabilityWizard {
34     private IProject project;
35
36     private Capability[] addCapabilities;
37
38     private Capability[] removeCapabilities;
39
40     /**
41      * Creates a wizard.
42      *
43      * @param project the project to configure new capabilities
44      * @param addCapabilities the new capabilities to configure on the project
45      * @param removeCapabilities the old capabilities to remove from the project
46      * in reverse order (first item last to be removed)
47      */

48     public UpdateProjectCapabilityWizard(IProject project,
49             Capability[] addCapabilities, Capability[] removeCapabilities) {
50         super();
51         this.project = project;
52         this.addCapabilities = addCapabilities;
53         this.removeCapabilities = removeCapabilities;
54         initializeDefaultPageImageDescriptor();
55         setWindowTitle(IDEWorkbenchMessages.UpdateProjectCapabilityWizard_windowTitle);
56     }
57
58     /**
59      * Builds the collection of steps
60      */

61     private void buildSteps() {
62         int stepNumber = 1;
63         ArrayList JavaDoc steps = new ArrayList JavaDoc(removeCapabilities.length
64                 + addCapabilities.length);
65
66         // collect the minimum remove capability steps
67
if (removeCapabilities.length > 0) {
68             // Reserve the order so prereq aren't removed before dependents
69
for (int i = removeCapabilities.length - 1; i >= 0; i--) {
70                 if (removeCapabilities[i] != null) {
71                     // Collect all the nature ids this capability should
72
// remove. Includes itself and any ones that it
73
// handles the ui for.
74
ArrayList JavaDoc natureIds = new ArrayList JavaDoc();
75                     natureIds.add(removeCapabilities[i].getNatureId());
76                     ArrayList JavaDoc uiIds = removeCapabilities[i].getHandleUIs();
77                     if (uiIds != null) {
78                         Iterator JavaDoc it = uiIds.iterator();
79                         while (it.hasNext()) {
80                             String JavaDoc id = (String JavaDoc) it.next();
81                             for (int j = 0; j < removeCapabilities.length; j++) {
82                                 if (removeCapabilities[j] != null) {
83                                     if (removeCapabilities[j].getId()
84                                             .equals(id)) {
85                                         natureIds.add(removeCapabilities[j]
86                                                 .getNatureId());
87                                         removeCapabilities[j] = null;
88                                     }
89                                 }
90                             }
91                         }
92                     }
93                     // Create a step to remove this capability and prereq natures
94
String JavaDoc[] ids = new String JavaDoc[natureIds.size()];
95                     natureIds.toArray(ids);
96                     steps.add(new RemoveCapabilityStep(stepNumber,
97                             removeCapabilities[i], ids, project));
98                     stepNumber++;
99                 }
100             }
101         }
102
103         // Collect the minimum add capability steps
104
if (addCapabilities.length > 0) {
105             IWorkbench workbench = PlatformUI.getWorkbench();
106             CapabilityRegistry reg = IDEWorkbenchPlugin.getDefault()
107                     .getCapabilityRegistry();
108             Capability[] results = reg.pruneCapabilities(addCapabilities);
109             for (int i = 0; i < results.length; i++, stepNumber++)
110                 steps.add(new InstallCapabilityStep(stepNumber, results[i],
111                         workbench, this));
112         }
113
114         // Set the list of steps to do
115
WizardStep[] results = new WizardStep[steps.size()];
116         steps.toArray(results);
117         setSteps(results);
118     }
119
120     /* (non-Javadoc)
121      * Method declared on MultiStepWizard.
122      */

123     protected void addCustomPages() {
124     }
125
126     /* (non-Javadoc)
127      * Method declared on MultiStepWizard.
128      */

129     protected boolean canFinishOnReviewPage() {
130         WizardStep[] steps = getSteps();
131         // yes if the only step is to remove capabilities
132
return steps.length == 1 && steps[0] instanceof RemoveCapabilityStep;
133     }
134
135     /* (non-Javadoc)
136      * Method declared on MultiStepWizard.
137      */

138     protected String JavaDoc getConfigurePageTitle() {
139         return IDEWorkbenchMessages.UpdateProjectCapabilityWizard_title;
140     }
141
142     /* (non-Javadoc)
143      * Method declared on MultiStepWizard.
144      */

145     protected String JavaDoc getConfigurePageDescription() {
146         return IDEWorkbenchMessages.WizardProjectConfigurePage_description;
147     }
148
149     /* (non-Javadoc)
150      * Method declared on MultiStepWizard.
151      */

152     protected String JavaDoc getFinishStepLabel(WizardStep[] steps) {
153         int count = 0;
154         for (int i = 0; i < steps.length; i++) {
155             if (!(steps[i] instanceof RemoveCapabilityStep)) {
156                 count++;
157                 if (count > 1)
158                     return super.getFinishStepLabel(steps);
159             }
160         }
161
162         return null;
163     }
164
165     /* (non-Javadoc)
166      * Method declared on MultiStepWizard.
167      */

168     protected String JavaDoc[] getPerspectiveChoices() {
169         ArrayList JavaDoc results = new ArrayList JavaDoc();
170         for (int i = 0; i < addCapabilities.length; i++) {
171             ArrayList JavaDoc ids = addCapabilities[i].getPerspectiveChoices();
172             if (ids != null) {
173                 Iterator JavaDoc it = ids.iterator();
174                 while (it.hasNext()) {
175                     String JavaDoc id = (String JavaDoc) it.next();
176                     if (!results.contains(id))
177                         results.add(id);
178                 }
179             }
180         }
181         String JavaDoc[] ids = new String JavaDoc[results.size()];
182         results.toArray(ids);
183         return ids;
184     }
185
186     /* (non-Javadoc)
187      * Method declared on IProjectProvider.
188      */

189     public IProject getProject() {
190         return project;
191     }
192
193     /* (non-Javadoc)
194      * Method declared on MultiStepWizard.
195      */

196     protected String JavaDoc getReviewPageTitle() {
197         return IDEWorkbenchMessages.UpdateProjectCapabilityWizard_title;
198     }
199
200     /* (non-Javadoc)
201      * Method declared on MultiStepWizard.
202      */

203     protected String JavaDoc getReviewPageDescription() {
204         return IDEWorkbenchMessages.WizardProjectReviewPage_description;
205     }
206
207     /* (non-Javadoc)
208      * Method declared on IWizard.
209      */

210     public IWizardPage getStartingPage() {
211         IWizardPage page = super.getStartingPage();
212         buildSteps();
213         return page;
214     }
215
216     /* (non-Javadoc)
217      * Method declared on MultiStepWizard.
218      */

219     /* package */boolean handleMissingStepWizard(WizardStep step) {
220         MessageDialog
221                 .openError(
222                         getShell(),
223                         IDEWorkbenchMessages.UpdateProjectCapabilityWizard_errorTitle,
224                         NLS.bind(IDEWorkbenchMessages.UpdateProjectCapabilityWizard_noWizard, step.getLabel()));
225         return false;
226     }
227
228     /**
229      * Sets the image banner for the wizard
230      */

231     protected void initializeDefaultPageImageDescriptor() {
232         ImageDescriptor desc = IDEWorkbenchPlugin.getIDEImageDescriptor("wizban/newprj_wiz.gif");//$NON-NLS-1$
233

234         setDefaultPageImageDescriptor(desc);
235       
236     }
237 }
238
Popular Tags