KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > ImportWizardTemplate


1 /*******************************************************************************
2  * Copyright (c) 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.pde.internal.ui.wizards.templates;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.jface.wizard.WizardPage;
19 import org.eclipse.pde.core.plugin.IPluginBase;
20 import org.eclipse.pde.core.plugin.IPluginElement;
21 import org.eclipse.pde.core.plugin.IPluginExtension;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginModelFactory;
24 import org.eclipse.pde.core.plugin.IPluginReference;
25 import org.eclipse.pde.internal.ui.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.ui.IFieldData;
28 import org.eclipse.pde.ui.templates.TemplateOption;
29
30 public class ImportWizardTemplate extends PDETemplateSection {
31     public static final String JavaDoc WIZARD_CLASS_NAME = "wizardClassName"; //$NON-NLS-1$
32
public static final String JavaDoc WIZARD_CATEGORY_NAME = "wizardCategoryName"; //$NON-NLS-1$
33
public static final String JavaDoc WIZARD_PAGE_CLASS_NAME = "wizardPageClassName"; //$NON-NLS-1$
34
public static final String JavaDoc WIZARD_IMPORT_NAME = "wizardImportName"; //$NON-NLS-1$
35
public static final String JavaDoc WIZARD_FILE_FILTERS = "wizardFileFilters"; //$NON-NLS-1$
36

37     private WizardPage page;
38     
39     /**
40      * Constructor for ImportWizardTemplate.
41      */

42     public ImportWizardTemplate() {
43         setPageCount(1);
44         createOptions();
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
49      */

50     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
51         // Additional dependency required to provide WizardNewFileCreationPage
52
if (schemaVersion != null) {
53             IPluginReference[] dep = new IPluginReference[2];
54             dep[0] = new PluginReference("org.eclipse.ui.ide", null, 0); //$NON-NLS-1$
55
dep[1] = new PluginReference("org.eclipse.core.resources", null, 0); //$NON-NLS-1$
56
return dep;
57         }
58         return super.getDependencies(schemaVersion);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
63      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getDirectoryCandidates()
64      */

65     public String JavaDoc getSectionId() {
66          // Identifier used for the folder name within the templates_3.X
67
// hierarchy and as part of the lookup key for the template label
68
// variable.
69
return "importWizard"; //$NON-NLS-1$
70
}
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getNumberOfWorkUnits()
74      */

75     public int getNumberOfWorkUnits() {
76         return super.getNumberOfWorkUnits() + 1;
77     }
78     
79     /**
80      * Creates the options to be displayed on the template wizard.
81      * Various string options, blank fields and a multiple choice
82      * option are used.
83      */

84     private void createOptions() {
85         String JavaDoc[][] choices = fromCommaSeparated(PDEUIMessages.ImportWizardTemplate_filterChoices);
86         
87         addOption(
88                 KEY_PACKAGE_NAME,
89                 PDEUIMessages.ImportWizardTemplate_packageName,
90                 (String JavaDoc) null,
91                 0);
92         addOption(
93                 WIZARD_CLASS_NAME,
94                 PDEUIMessages.ImportWizardTemplate_wizardClass,
95                 PDEUIMessages.ImportWizardTemplate_wizardClassName,
96                 0);
97         addOption(
98                 WIZARD_PAGE_CLASS_NAME,
99                 PDEUIMessages.ImportWizardTemplate_pageClass,
100                 PDEUIMessages.ImportWizardTemplate_pageClassName,
101                 0);
102         
103         addBlankField(0);
104         
105         addOption(
106                 WIZARD_CATEGORY_NAME,
107                 PDEUIMessages.ImportWizardTemplate_importWizardCategory,
108                 PDEUIMessages.ImportWizardTemplate_importWizardCategoryName,
109                 0);
110         addOption(
111                 WIZARD_IMPORT_NAME,
112                 PDEUIMessages.ImportWizardTemplate_wizardName,
113                 PDEUIMessages.ImportWizardTemplate_wizardDefaultName,
114                 0);
115
116         addBlankField(0);
117
118         addOption(WIZARD_FILE_FILTERS,
119                 PDEUIMessages.ImportWizardTemplate_filters,
120                 choices,
121                 choices[0][0],
122                 0);
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#addPages(org.eclipse.jface.wizard.Wizard)
127      */

128     public void addPages(Wizard wizard) {
129         int pageIndex = 0;
130
131         page = createPage(pageIndex, IHelpContextIds.TEMPLATE_EDITOR);
132         page.setTitle(PDEUIMessages.ImportWizardTemplate_title);
133         page.setDescription(PDEUIMessages.ImportWizardTemplate_desc);
134
135         wizard.addPage(page);
136         markPagesAdded();
137     }
138     
139     /* (non-Javadoc)
140      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#validateOptions(org.eclipse.pde.ui.templates.TemplateOption)
141      */

142     public void validateOptions(TemplateOption source) {
143         //Validate page upon change in option state and alter
144
//the page if the read-only boolean changes
145
if (source.isRequired() && source.isEmpty()) {
146             flagMissingRequiredOption(source);
147         } else {
148             validateContainerPage(source);
149         }
150     }
151
152     /**
153      * Given a required option whose value has been changed by the user,
154      * this method elects to check all options on the wizard page to
155      * confirm that none of the required options are empty.
156      *
157      * @param source
158      * the TemplateOption whose value has been changed by the user
159      */

160     private void validateContainerPage(TemplateOption source) {
161         TemplateOption[] allPageOptions = getOptions(0);
162         for (int i = 0; i < allPageOptions.length; i++) {
163             TemplateOption nextOption = allPageOptions[i];
164             if (nextOption.isRequired() && nextOption.isEmpty()) {
165                 flagMissingRequiredOption(nextOption);
166                 return;
167             }
168         }
169         resetPageState();
170     }
171
172
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
175      */

176     public boolean isDependentOnParentWizard() {
177         return true;
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
182      */

183     protected void initializeFields(IFieldData data) {
184          // In a new project wizard, we don't know this yet - the
185
// model has not been created
186
String JavaDoc id = data.getId();
187         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
188     }
189     
190     /* (non-Javadoc)
191      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.core.plugin.IPluginModelBase)
192      */

193     public void initializeFields(IPluginModelBase model) {
194          // In the new extension wizard, the model exists so
195
// we can initialize directly from it
196
String JavaDoc pluginId = model.getPluginBase().getId();
197         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
202      */

203     protected void updateModel(IProgressMonitor monitor) throws CoreException {
204          // This method creates the extension point structure through the use
205
// of IPluginElement objects. The element attributes are set based on
206
// user input from the wizard page as well as values required for the
207
// operation of the extension point.
208
IPluginBase plugin = model.getPluginBase();
209         IPluginExtension extension = createExtension(getUsedExtensionPoint(),true);
210         IPluginModelFactory factory = model.getPluginFactory();
211
212         IPluginElement categoryElement = factory.createElement(extension);
213         categoryElement.setName("category"); //$NON-NLS-1$
214
categoryElement.setAttribute(
215                 "id", getStringOption(KEY_PACKAGE_NAME) + ".sampleCategory"); //$NON-NLS-1$ //$NON-NLS-2$
216
categoryElement.setAttribute(
217                 "name", getStringOption(WIZARD_CATEGORY_NAME)); //$NON-NLS-1$
218

219         IPluginElement wizardElement = factory.createElement(extension);
220         wizardElement.setName("wizard"); //$NON-NLS-1$
221
wizardElement.setAttribute(
222                         "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(WIZARD_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
223
wizardElement.setAttribute("name", getStringOption(WIZARD_IMPORT_NAME)); //$NON-NLS-1$
224
wizardElement.setAttribute(
225                         "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(WIZARD_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
226
wizardElement.setAttribute(
227                         "category", getStringOption(KEY_PACKAGE_NAME) + ".sampleCategory"); //$NON-NLS-1$ //$NON-NLS-2$
228
wizardElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
229

230         IPluginElement descriptionElement = factory.createElement(extension);
231         descriptionElement.setName("description"); //$NON-NLS-1$
232
descriptionElement.setText(PDEUIMessages.ImportWizardTemplate_wizardDescription);
233
234         wizardElement.add(descriptionElement);
235         extension.add(categoryElement);
236         extension.add(wizardElement);
237         if (!extension.isInTheModel())
238             plugin.add(extension);
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getNewFiles()
243      */

244     public String JavaDoc[] getNewFiles() {
245         return new String JavaDoc[] { "icons/" }; //$NON-NLS-1$
246
}
247
248     /* (non-Javadoc)
249      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFormattedPackageName(java.lang.String)
250      */

251     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
252          // Package name addition to create a location for containing
253
// any classes required by the decorator.
254
String JavaDoc packageName = super.getFormattedPackageName(id);
255         if (packageName.length() != 0)
256             return packageName + ".importWizards"; //$NON-NLS-1$
257
return "importWizards"; //$NON-NLS-1$
258
}
259
260     /* (non-Javadoc)
261      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
262      */

263     public String JavaDoc getUsedExtensionPoint() {
264         return "org.eclipse.ui.importWizards"; //$NON-NLS-1$
265
}
266
267     /**
268      * Returns a 2-D String array based on a comma seperated
269      * string of choices.
270      *
271      * @param iconLocations
272      * comma seperated string of icon placement options
273      * @return the 2-D array of choices
274      *
275      */

276     protected String JavaDoc[][] fromCommaSeparated(String JavaDoc iconLocations) {
277         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(iconLocations, ","); //$NON-NLS-1$
278
String JavaDoc[][] choices = new String JavaDoc[tokens.countTokens() / 2][2];
279         int x = 0, y = 0;
280         while (tokens.hasMoreTokens()) {
281             choices[x][y++] = tokens.nextToken();
282             choices[x++][y--] = tokens.nextToken();
283         }
284         return choices;
285     }
286 }
287
Popular Tags