KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.wizard.Wizard;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginModelFactory;
25 import org.eclipse.pde.core.plugin.IPluginReference;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.ui.IFieldData;
29 import org.eclipse.pde.ui.templates.BooleanOption;
30 import org.eclipse.pde.ui.templates.TemplateOption;
31
32 public class DecoratorTemplate extends PDETemplateSection {
33     public static final String JavaDoc DECORATOR_CLASS_NAME = "decoratorClassName"; //$NON-NLS-1$
34
public static final String JavaDoc DECORATOR_ICON_PLACEMENT = "decoratorPlacement"; //$NON-NLS-1$
35
public static final String JavaDoc DECORATOR_BLN_PROJECT = "decorateProjects"; //$NON-NLS-1$
36
public static final String JavaDoc DECORATOR_BLN_READONLY = "decorateReadOnly"; //$NON-NLS-1$
37

38     private WizardPage page;
39     private TemplateOption packageOption;
40     private TemplateOption classOption;
41     private BooleanOption projectOption;
42     private BooleanOption readOnlyOption;
43     
44     /**
45      * Constructor for DecoratorTemplate.
46      */

47     public DecoratorTemplate() {
48         setPageCount(1);
49         createOptions();
50         alterOptionStates();
51     }
52     
53     /* (non-Javadoc)
54      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
55      */

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

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

80     public int getNumberOfWorkUnits() {
81         return super.getNumberOfWorkUnits() + 1;
82     }
83     
84     /**
85      * Creates the options to be displayed on the template wizard.
86      * A multiple choice option (radio buttons) and a boolean option
87      * are used.
88      */

89     private void createOptions() {
90         String JavaDoc[][] choices = fromCommaSeparated(PDEUIMessages.DecoratorTemplate_placementChoices);
91         
92         addOption(DECORATOR_ICON_PLACEMENT,
93             PDEUIMessages.DecoratorTemplate_placement,
94             choices,
95             choices[0][0],
96             0);
97         
98         projectOption = (BooleanOption) addOption(DECORATOR_BLN_PROJECT,
99                 PDEUIMessages.DecoratorTemplate_decorateProject, true, 0);
100         
101         readOnlyOption = (BooleanOption) addOption(DECORATOR_BLN_READONLY,
102                 PDEUIMessages.DecoratorTemplate_decorateReadOnly, false, 0);
103         
104         packageOption = addOption(
105                 KEY_PACKAGE_NAME,
106                 PDEUIMessages.DecoratorTemplate_packageName,
107                 (String JavaDoc) null,
108                 0);
109         classOption = addOption(
110                 DECORATOR_CLASS_NAME,
111                 PDEUIMessages.DecoratorTemplate_decoratorClass,
112                 PDEUIMessages.DecoratorTemplate_decoratorClassName,
113                 0);
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#addPages(org.eclipse.jface.wizard.Wizard)
118      */

119     public void addPages(Wizard wizard) {
120         int pageIndex = 0;
121
122         page = createPage(pageIndex, IHelpContextIds.TEMPLATE_EDITOR);
123         page.setTitle(PDEUIMessages.DecoratorTemplate_title);
124         page.setDescription(PDEUIMessages.DecoratorTemplate_desc);
125
126         wizard.addPage(page);
127         markPagesAdded();
128     }
129     
130     private void alterOptionStates() {
131         projectOption.setEnabled(!readOnlyOption.isSelected());
132         packageOption.setEnabled(!projectOption.isEnabled());
133         classOption.setEnabled(!projectOption.isEnabled());
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFolder(java.io.File)
138      */

139     protected boolean isOkToCreateFolder(File JavaDoc sourceFolder) {
140         //Define rules for creating folders from the Templates_3.X folders
141
boolean isOk = true;
142         String JavaDoc folderName = sourceFolder.getName();
143         if (folderName.equals("java")) { //$NON-NLS-1$
144
isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected();
145         }
146         return isOk;
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFile(java.io.File)
151      */

152     protected boolean isOkToCreateFile(File JavaDoc sourceFile) {
153         //Define rules for creating files from the Templates_3.X folders
154
boolean isOk = true;
155         String JavaDoc fileName = sourceFile.getName();
156         if (fileName.equals("read_only.gif")) { //$NON-NLS-1$
157
isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected();
158         } else if (fileName.equals("sample_decorator.gif")) { //$NON-NLS-1$
159
isOk = !readOnlyOption.isSelected();
160         } else if (fileName.equals("$decoratorClassName$.java")) { //$NON-NLS-1$
161
isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected();
162         }
163         return isOk;
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#validateOptions(org.eclipse.pde.ui.templates.TemplateOption)
168      */

169     public void validateOptions(TemplateOption source) {
170         //Validate page upon change in option state and alter
171
//the page if the read-only boolean changes
172
if (source == readOnlyOption){
173             alterOptionStates();
174         }
175         
176         if (source.isRequired() && source.isEmpty()) {
177             flagMissingRequiredOption(source);
178         } else {
179             validateContainerPage(source);
180         }
181     }
182
183     /**
184      * Given a required option whose value has been changed by the user,
185      * this method elects to check all options on the wizard page to
186      * confirm that none of the required options are empty.
187      *
188      * @param source
189      * the TemplateOption whose value has been changed by the user
190      */

191     private void validateContainerPage(TemplateOption source) {
192         TemplateOption[] allPageOptions = getOptions(0);
193         for (int i = 0; i < allPageOptions.length; i++) {
194             TemplateOption nextOption = allPageOptions[i];
195             if (nextOption.isRequired() && nextOption.isEmpty()) {
196                 flagMissingRequiredOption(nextOption);
197                 return;
198             }
199         }
200         resetPageState();
201     }
202
203
204     /* (non-Javadoc)
205      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
206      */

207     public boolean isDependentOnParentWizard() {
208         return true;
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
213      */

214     protected void initializeFields(IFieldData data) {
215          // In a new project wizard, we don't know this yet - the
216
// model has not been created
217
String JavaDoc id = data.getId();
218         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
219     }
220     
221     /* (non-Javadoc)
222      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.core.plugin.IPluginModelBase)
223      */

224     public void initializeFields(IPluginModelBase model) {
225          // In the new extension wizard, the model exists so
226
// we can initialize directly from it
227
String JavaDoc pluginId = model.getPluginBase().getId();
228         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
229     }
230
231     /* (non-Javadoc)
232      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
233      */

234     protected void updateModel(IProgressMonitor monitor) throws CoreException {
235          // This method creates the extension point structure through the use
236
// of IPluginElement objects. The element attributes are set based on
237
// user input from the wizard page as well as values required for the
238
// operation of the extension point.
239
IPluginBase plugin = model.getPluginBase();
240         IPluginExtension extension = createExtension(getUsedExtensionPoint(),true);
241         IPluginModelFactory factory = model.getPluginFactory();
242
243         IPluginElement decoratorElement = factory.createElement(extension);
244         decoratorElement.setName("decorator"); //$NON-NLS-1$
245
decoratorElement.setAttribute("adaptable", "true"); //$NON-NLS-1$ //$NON-NLS-2$
246
decoratorElement.setAttribute("state", "true"); //$NON-NLS-1$ //$NON-NLS-2$
247
decoratorElement.setAttribute("lightweight", "true"); //$NON-NLS-1$ //$NON-NLS-2$
248

249         if(!readOnlyOption.isSelected()){
250             decoratorElement.setAttribute(
251                     "id", plugin.getId() + "." + getSectionId()); //$NON-NLS-1$ //$NON-NLS-2$
252
decoratorElement.setAttribute(
253                     "label", PDEUIMessages.DecoratorTemplate_resourceLabel); //$NON-NLS-1$
254
decoratorElement.setAttribute("icon", "icons/sample_decorator.gif"); //$NON-NLS-1$ //$NON-NLS-2$
255
decoratorElement.setAttribute(
256                     "location", getValue(DECORATOR_ICON_PLACEMENT).toString()); //$NON-NLS-1$
257
}
258         else {
259             decoratorElement.setAttribute(
260                     "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
261
decoratorElement.setAttribute(
262                     "label", PDEUIMessages.DecoratorTemplate_readOnlyLabel); //$NON-NLS-1$
263
decoratorElement.setAttribute(
264                     "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
265
}
266
267         IPluginElement enablementElement = factory
268                 .createElement(decoratorElement);
269         enablementElement.setName("enablement"); //$NON-NLS-1$
270

271         IPluginElement andElement = factory.createElement(enablementElement);
272         andElement.setName("and"); //$NON-NLS-1$
273

274         IPluginElement resourceObjectElement = factory.createElement(andElement);
275         resourceObjectElement.setName("objectClass"); //$NON-NLS-1$
276
resourceObjectElement.setAttribute(
277                 "name", "org.eclipse.core.resources.IResource"); //$NON-NLS-1$ //$NON-NLS-2$
278

279         IPluginElement orElement = factory.createElement(andElement);
280         orElement.setName("or"); //$NON-NLS-1$
281

282         IPluginElement fileObjectElement = factory.createElement(orElement);
283         fileObjectElement.setName("objectClass"); //$NON-NLS-1$
284
fileObjectElement.setAttribute(
285                 "name", "org.eclipse.core.resources.IFile"); //$NON-NLS-1$ //$NON-NLS-2$
286

287         IPluginElement folderObjectElement = factory.createElement(orElement);
288         folderObjectElement.setName("objectClass"); //$NON-NLS-1$
289
folderObjectElement.setAttribute(
290                 "name", "org.eclipse.core.resources.IFolder"); //$NON-NLS-1$ //$NON-NLS-2$
291

292         IPluginElement projectObjectElement = factory.createElement(orElement);
293         projectObjectElement.setName("objectClass"); //$NON-NLS-1$
294
projectObjectElement.setAttribute(
295                 "name", "org.eclipse.core.resources.IProject"); //$NON-NLS-1$ //$NON-NLS-2$
296

297         if(readOnlyOption.isSelected())
298             orElement.add(folderObjectElement);
299         else if (projectOption.isSelected())
300             orElement.add(projectObjectElement);
301         orElement.add(fileObjectElement);
302         andElement.add(resourceObjectElement);
303         andElement.add(orElement);
304         enablementElement.add(andElement);
305         decoratorElement.add(enablementElement);
306
307         extension.add(decoratorElement);
308         if (!extension.isInTheModel())
309             plugin.add(extension);
310     }
311
312     /* (non-Javadoc)
313      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getNewFiles()
314      */

315     public String JavaDoc[] getNewFiles() {
316         return new String JavaDoc[] { "icons/" }; //$NON-NLS-1$
317
}
318
319     /* (non-Javadoc)
320      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFormattedPackageName(java.lang.String)
321      */

322     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
323          // Package name addition to create a location for containing
324
// any classes required by the decorator.
325
String JavaDoc packageName = super.getFormattedPackageName(id);
326         if (packageName.length() != 0)
327             return packageName + ".decorators"; //$NON-NLS-1$
328
return "decorators"; //$NON-NLS-1$
329
}
330
331     /* (non-Javadoc)
332      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
333      */

334     public String JavaDoc getUsedExtensionPoint() {
335         return "org.eclipse.ui.decorators"; //$NON-NLS-1$
336
}
337
338     /**
339      * Returns a 2-D String array based on a comma seperated
340      * string of choices.
341      *
342      * @param iconLocations
343      * comma seperated string of icon placement options
344      * @return the 2-D array of choices
345      *
346      */

347     protected String JavaDoc[][] fromCommaSeparated(String JavaDoc iconLocations) {
348         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(iconLocations, ","); //$NON-NLS-1$
349
String JavaDoc[][] choices = new String JavaDoc[tokens.countTokens() / 2][2];
350         int x = 0, y = 0;
351         while (tokens.hasMoreTokens()) {
352             choices[x][y++] = tokens.nextToken();
353             choices[x++][y--] = tokens.nextToken();
354         }
355         return choices;
356     }
357 }
358
Popular Tags