KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > 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.templates.ide;
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.templates.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
27 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
28 import org.eclipse.pde.internal.ui.templates.PluginReference;
29 import org.eclipse.pde.ui.IFieldData;
30
31 public class ImportWizardTemplate extends PDETemplateSection {
32     public static final String JavaDoc WIZARD_CLASS_NAME = "wizardClassName"; //$NON-NLS-1$
33
public static final String JavaDoc WIZARD_CATEGORY_NAME = "wizardCategoryName"; //$NON-NLS-1$
34
public static final String JavaDoc WIZARD_PAGE_CLASS_NAME = "wizardPageClassName"; //$NON-NLS-1$
35
public static final String JavaDoc WIZARD_IMPORT_NAME = "wizardImportName"; //$NON-NLS-1$
36
public static final String JavaDoc WIZARD_FILE_FILTERS = "wizardFileFilters"; //$NON-NLS-1$
37

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

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

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

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

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

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

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

143     public boolean isDependentOnParentWizard() {
144         return true;
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
149      */

150     protected void initializeFields(IFieldData data) {
151          // In a new project wizard, we don't know this yet - the
152
// model has not been created
153
String JavaDoc id = data.getId();
154         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.core.plugin.IPluginModelBase)
159      */

160     public void initializeFields(IPluginModelBase model) {
161          // In the new extension wizard, the model exists so
162
// we can initialize directly from it
163
String JavaDoc pluginId = model.getPluginBase().getId();
164         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
169      */

170     protected void updateModel(IProgressMonitor monitor) throws CoreException {
171          // This method creates the extension point structure through the use
172
// of IPluginElement objects. The element attributes are set based on
173
// user input from the wizard page as well as values required for the
174
// operation of the extension point.
175
IPluginBase plugin = model.getPluginBase();
176         IPluginExtension extension = createExtension(getUsedExtensionPoint(),true);
177         IPluginModelFactory factory = model.getPluginFactory();
178
179         IPluginElement categoryElement = factory.createElement(extension);
180         categoryElement.setName("category"); //$NON-NLS-1$
181
categoryElement.setAttribute(
182                 "id", getStringOption(KEY_PACKAGE_NAME) + ".sampleCategory"); //$NON-NLS-1$ //$NON-NLS-2$
183
categoryElement.setAttribute(
184                 "name", getStringOption(WIZARD_CATEGORY_NAME)); //$NON-NLS-1$
185

186         IPluginElement wizardElement = factory.createElement(extension);
187         wizardElement.setName("wizard"); //$NON-NLS-1$
188
wizardElement.setAttribute(
189                         "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(WIZARD_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
190
wizardElement.setAttribute("name", getStringOption(WIZARD_IMPORT_NAME)); //$NON-NLS-1$
191
wizardElement.setAttribute(
192                         "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(WIZARD_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
193
wizardElement.setAttribute(
194                         "category", getStringOption(KEY_PACKAGE_NAME) + ".sampleCategory"); //$NON-NLS-1$ //$NON-NLS-2$
195
wizardElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
196

197         IPluginElement descriptionElement = factory.createElement(extension);
198         descriptionElement.setName("description"); //$NON-NLS-1$
199
descriptionElement.setText(PDETemplateMessages.ImportWizardTemplate_wizardDescription);
200
201         wizardElement.add(descriptionElement);
202         extension.add(categoryElement);
203         extension.add(wizardElement);
204         if (!extension.isInTheModel())
205             plugin.add(extension);
206     }
207
208     /* (non-Javadoc)
209      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getNewFiles()
210      */

211     public String JavaDoc[] getNewFiles() {
212         return new String JavaDoc[] { "icons/" }; //$NON-NLS-1$
213
}
214
215     /* (non-Javadoc)
216      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFormattedPackageName(java.lang.String)
217      */

218     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
219          // Package name addition to create a location for containing
220
// any classes required by the decorator.
221
String JavaDoc packageName = super.getFormattedPackageName(id);
222         if (packageName.length() != 0)
223             return packageName + ".importWizards"; //$NON-NLS-1$
224
return "importWizards"; //$NON-NLS-1$
225
}
226
227     /* (non-Javadoc)
228      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
229      */

230     public String JavaDoc getUsedExtensionPoint() {
231         return "org.eclipse.ui.importWizards"; //$NON-NLS-1$
232
}
233
234     /**
235      * Returns a 2-D String array based on a comma seperated
236      * string of choices.
237      *
238      * @param iconLocations
239      * comma seperated string of icon placement options
240      * @return the 2-D array of choices
241      *
242      */

243     protected String JavaDoc[][] fromCommaSeparated(String JavaDoc iconLocations) {
244         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(iconLocations, ","); //$NON-NLS-1$
245
String JavaDoc[][] choices = new String JavaDoc[tokens.countTokens() / 2][2];
246         int x = 0, y = 0;
247         while (tokens.hasMoreTokens()) {
248             choices[x][y++] = tokens.nextToken();
249             choices[x++][y--] = tokens.nextToken();
250         }
251         return choices;
252     }
253 }
254
Popular Tags