KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > ConfigureProjectWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ui.wizards;
12
13
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.wizard.IWizardPage;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.team.internal.ui.*;
19 import org.eclipse.team.ui.IConfigurationWizard;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.activities.IActivityManager;
23 import org.eclipse.ui.activities.IIdentifier;
24 import org.eclipse.ui.model.AdaptableList;
25
26 /**
27  * The wizard for associating projects with team providers
28  */

29 public class ConfigureProjectWizard extends Wizard implements IConfigurationWizard {
30     protected IWorkbench workbench;
31     protected IProject project;
32     protected IConfigurationWizard wizard;
33     
34     protected ConfigureProjectWizardMainPage mainPage;
35     private String JavaDoc pluginId = TeamUIPlugin.PLUGIN_ID;
36     
37     protected final static String JavaDoc PT_CONFIGURATION ="configurationWizards"; //$NON-NLS-1$
38
protected final static String JavaDoc TAG_WIZARD = "wizard"; //$NON-NLS-1$
39
protected final static String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
40
protected final static String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
41
protected final static String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
42
protected final static String JavaDoc ATT_ICON = "icon"; //$NON-NLS-1$
43
protected final static String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
44

45     public ConfigureProjectWizard() {
46         setNeedsProgressMonitor(true);
47         setWindowTitle(getWizardWindowTitle());
48     }
49     
50     protected String JavaDoc getExtensionPoint() {
51         return PT_CONFIGURATION;
52     }
53     
54     protected String JavaDoc getWizardWindowTitle() {
55         return TeamUIMessages.ConfigureProjectWizard_title;
56     }
57     
58     protected String JavaDoc getWizardLabel() {
59         return TeamUIMessages.ConfigureProjectWizard_configureProject;
60     }
61     
62     protected String JavaDoc getWizardDescription() {
63         return TeamUIMessages.ConfigureProjectWizard_description;
64     }
65     
66     /*
67      * @see Wizard#addPages
68      */

69     public void addPages() {
70         AdaptableList disabledWizards = new AdaptableList();
71         AdaptableList wizards = getAvailableWizards(disabledWizards);
72         if (wizards.size() == 1 && disabledWizards.size() == 0) {
73             // If there is only one wizard, skip the first page.
74
// Only skip the first page if the one wizard has at least one page.
75
ConfigurationWizardElement element = (ConfigurationWizardElement)wizards.getChildren()[0];
76             try {
77                 this.wizard = (IConfigurationWizard)element.createExecutableExtension();
78                 wizard.init(workbench, project);
79                 wizard.addPages();
80                 if (wizard.getPageCount() > 0) {
81                     wizard.setContainer(getContainer());
82                     IWizardPage[] pages = wizard.getPages();
83                     for (int i = 0; i < pages.length; i++) {
84                         addPage(pages[i]);
85                     }
86                     return;
87                 }
88             } catch (CoreException e) {
89                 TeamUIPlugin.log(e);
90                 return;
91             }
92         }
93         mainPage = new ConfigureProjectWizardMainPage("configurePage1", getWizardLabel(), TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_WIZBAN_SHARE), wizards, disabledWizards); //$NON-NLS-1$
94
mainPage.setDescription(getWizardDescription());
95         mainPage.setProject(project);
96         mainPage.setWorkbench(workbench);
97         addPage(mainPage);
98     }
99     public IWizardPage getNextPage(IWizardPage page) {
100         if (wizard != null) {
101             return wizard.getNextPage(page);
102         }
103         return super.getNextPage(page);
104     }
105     public boolean canFinish() {
106         // If we are on the first page, never allow finish unless the selected wizard has no pages.
107
if (getContainer().getCurrentPage() == mainPage) {
108             if (mainPage.getSelectedWizard() != null && mainPage.getNextPage() == null) {
109                 return true;
110             }
111             return false;
112         }
113         if (wizard != null) {
114             return wizard.canFinish();
115         }
116         return super.canFinish();
117     }
118     /*
119      * @see Wizard#performFinish
120      */

121     public boolean performFinish() {
122         // There is only one wizard with at least one page
123
if (wizard != null) {
124             return wizard.performFinish();
125         }
126         // If we are on the first page and the selected wizard has no pages then
127
// allow it to finish.
128
if (getContainer().getCurrentPage() == mainPage) {
129             IConfigurationWizard noPageWizard = mainPage.getSelectedWizard();
130             if (noPageWizard != null) {
131                 if (noPageWizard.canFinish())
132                 {
133                     return noPageWizard.performFinish();
134                 }
135             }
136         }
137         // If the wizard has pages and there are several
138
// wizards registered then the registered wizard
139
// will call it's own performFinish().
140
return true;
141     }
142     
143     /* (non-Javadoc)
144      * @see org.eclipse.jface.wizard.IWizard#performCancel()
145      */

146     public boolean performCancel() {
147         if (wizard != null) {
148             return wizard.performCancel();
149         }
150         return super.performCancel();
151     }
152     
153     /**
154      * Returns the configuration wizards that are available for invocation.
155      *
156      * @return the available wizards
157      */

158     protected AdaptableList getAvailableWizards(AdaptableList disabledWizards) {
159         AdaptableList result = new AdaptableList();
160         IExtensionRegistry registry = Platform.getExtensionRegistry();
161         IExtensionPoint point = registry.getExtensionPoint(pluginId, getExtensionPoint());
162         if (point != null) {
163             IExtension[] extensions = point.getExtensions();
164             for (int i = 0; i < extensions.length; i++) {
165                 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
166                 for (int j = 0; j < elements.length; j++) {
167                     IConfigurationElement element = elements[j];
168                     if (element.getName().equals(TAG_WIZARD)) {
169                         ConfigurationWizardElement wizard = createWizardElement(element);
170                         if (wizard != null && filterItem(element)) {
171                             disabledWizards.add(wizard);
172                         } else if (wizard != null) {
173                             result.add(wizard);
174                         }
175                     }
176                 }
177             }
178         }
179         return result;
180     }
181     
182     private boolean filterItem(IConfigurationElement element) {
183         String JavaDoc extensionId = element.getAttribute(ATT_ID);
184         String JavaDoc extensionPluginId = element.getNamespace();
185         IActivityManager activityMgr = PlatformUI.getWorkbench().getActivitySupport().getActivityManager();
186         IIdentifier id = activityMgr.getIdentifier(extensionPluginId + "/" + extensionId); //$NON-NLS-1$
187
return (!id.isEnabled());
188     }
189     
190     /**
191      * Returns a new ConfigurationWizardElement configured according to the parameters
192      * contained in the passed Registry.
193      *
194      * May answer null if there was not enough information in the Extension to create
195      * an adequate wizard
196      *
197      * @param element the element for which to create a wizard element
198      * @return the wizard element for the given element
199      */

200     protected ConfigurationWizardElement createWizardElement(IConfigurationElement element) {
201         // WizardElements must have a name attribute
202
String JavaDoc nameString = element.getAttribute(ATT_NAME);
203         if (nameString == null) {
204             // Missing attribute
205
return null;
206         }
207         ConfigurationWizardElement result = new ConfigurationWizardElement(nameString);
208         if (initializeWizard(result, element)) {
209             // initialization was successful
210
return result;
211         }
212         return null;
213     }
214     /**
215      * Initialize the passed element's properties based on the contents of
216      * the passed registry. Answer a boolean indicating whether the element
217      * was able to be adequately initialized.
218      *
219      * @param element the element to initialize the properties for
220      * @param config the registry to get properties from
221      * @return whether initialization was successful
222      */

223     protected boolean initializeWizard(ConfigurationWizardElement element, IConfigurationElement config) {
224         element.setID(config.getAttribute(ATT_ID));
225         String JavaDoc description = ""; //$NON-NLS-1$
226
IConfigurationElement [] children = config.getChildren(TAG_DESCRIPTION);
227         if (children.length >= 1) {
228             description = children[0].getValue();
229         }
230
231         element.setDescription(description);
232     
233         // apply CLASS and ICON properties
234
element.setConfigurationElement(config);
235         String JavaDoc iconName = config.getAttribute(ATT_ICON);
236         if (iconName != null) {
237             IExtension extension = config.getDeclaringExtension();
238             element.setImageDescriptor(TeamUIPlugin.getImageDescriptorFromExtension(extension, iconName));
239         }
240         // ensure that a class was specified
241
if (element.getConfigurationElement() == null) {
242             // Missing attribute
243
return false;
244         }
245         setForcePreviousAndNextButtons(true);
246         return true;
247     }
248     /*
249      * Method declared on IConfigurationWizard
250      */

251     public void init(IWorkbench workbench, IProject project) {
252         this.workbench = workbench;
253         this.project = project;
254     }
255 }
256
Popular Tags