KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.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.templates.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
28 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
29 import org.eclipse.pde.internal.ui.templates.PluginReference;
30 import org.eclipse.pde.ui.IFieldData;
31 import org.eclipse.pde.ui.templates.BooleanOption;
32 import org.eclipse.pde.ui.templates.TemplateOption;
33
34 public class DecoratorTemplate extends PDETemplateSection {
35     public static final String JavaDoc DECORATOR_CLASS_NAME = "decoratorClassName"; //$NON-NLS-1$
36
public static final String JavaDoc DECORATOR_ICON_PLACEMENT = "decoratorPlacement"; //$NON-NLS-1$
37
public static final String JavaDoc DECORATOR_BLN_PROJECT = "decorateProjects"; //$NON-NLS-1$
38
public static final String JavaDoc DECORATOR_BLN_READONLY = "decorateReadOnly"; //$NON-NLS-1$
39

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

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

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

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

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

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

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

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

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

171     public void validateOptions(TemplateOption source) {
172         if (source == readOnlyOption){
173             alterOptionStates();
174         }
175         super.validateOptions(source);
176     }
177
178     /* (non-Javadoc)
179      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
180      */

181     public boolean isDependentOnParentWizard() {
182         return true;
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
187      */

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

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

208     protected void updateModel(IProgressMonitor monitor) throws CoreException {
209          // This method creates the extension point structure through the use
210
// of IPluginElement objects. The element attributes are set based on
211
// user input from the wizard page as well as values required for the
212
// operation of the extension point.
213
IPluginBase plugin = model.getPluginBase();
214         IPluginExtension extension = createExtension(getUsedExtensionPoint(),true);
215         IPluginModelFactory factory = model.getPluginFactory();
216
217         IPluginElement decoratorElement = factory.createElement(extension);
218         decoratorElement.setName("decorator"); //$NON-NLS-1$
219
decoratorElement.setAttribute("adaptable", "true"); //$NON-NLS-1$ //$NON-NLS-2$
220
decoratorElement.setAttribute("state", "true"); //$NON-NLS-1$ //$NON-NLS-2$
221
decoratorElement.setAttribute("lightweight", "true"); //$NON-NLS-1$ //$NON-NLS-2$
222

223         if(!readOnlyOption.isSelected()){
224             decoratorElement.setAttribute(
225                     "id", plugin.getId() + "." + getSectionId()); //$NON-NLS-1$ //$NON-NLS-2$
226
decoratorElement.setAttribute(
227                     "label", PDETemplateMessages.DecoratorTemplate_resourceLabel); //$NON-NLS-1$
228
decoratorElement.setAttribute("icon", "icons/sample_decorator.gif"); //$NON-NLS-1$ //$NON-NLS-2$
229
decoratorElement.setAttribute(
230                     "location", getValue(DECORATOR_ICON_PLACEMENT).toString()); //$NON-NLS-1$
231
}
232         else {
233             decoratorElement.setAttribute(
234                     "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
235
decoratorElement.setAttribute(
236                     "label", PDETemplateMessages.DecoratorTemplate_readOnlyLabel); //$NON-NLS-1$
237
decoratorElement.setAttribute(
238                     "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
239
}
240
241         IPluginElement enablementElement = factory
242                 .createElement(decoratorElement);
243         enablementElement.setName("enablement"); //$NON-NLS-1$
244

245         IPluginElement andElement = factory.createElement(enablementElement);
246         andElement.setName("and"); //$NON-NLS-1$
247

248         IPluginElement resourceObjectElement = factory.createElement(andElement);
249         resourceObjectElement.setName("objectClass"); //$NON-NLS-1$
250
resourceObjectElement.setAttribute(
251                 "name", "org.eclipse.core.resources.IResource"); //$NON-NLS-1$ //$NON-NLS-2$
252

253         IPluginElement orElement = factory.createElement(andElement);
254         orElement.setName("or"); //$NON-NLS-1$
255

256         IPluginElement fileObjectElement = factory.createElement(orElement);
257         fileObjectElement.setName("objectClass"); //$NON-NLS-1$
258
fileObjectElement.setAttribute(
259                 "name", "org.eclipse.core.resources.IFile"); //$NON-NLS-1$ //$NON-NLS-2$
260

261         IPluginElement folderObjectElement = factory.createElement(orElement);
262         folderObjectElement.setName("objectClass"); //$NON-NLS-1$
263
folderObjectElement.setAttribute(
264                 "name", "org.eclipse.core.resources.IFolder"); //$NON-NLS-1$ //$NON-NLS-2$
265

266         IPluginElement projectObjectElement = factory.createElement(orElement);
267         projectObjectElement.setName("objectClass"); //$NON-NLS-1$
268
projectObjectElement.setAttribute(
269                 "name", "org.eclipse.core.resources.IProject"); //$NON-NLS-1$ //$NON-NLS-2$
270

271         if(readOnlyOption.isSelected())
272             orElement.add(folderObjectElement);
273         else if (projectOption.isSelected())
274             orElement.add(projectObjectElement);
275         orElement.add(fileObjectElement);
276         andElement.add(resourceObjectElement);
277         andElement.add(orElement);
278         enablementElement.add(andElement);
279         decoratorElement.add(enablementElement);
280
281         extension.add(decoratorElement);
282         if (!extension.isInTheModel())
283             plugin.add(extension);
284     }
285
286     /* (non-Javadoc)
287      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getNewFiles()
288      */

289     public String JavaDoc[] getNewFiles() {
290         return new String JavaDoc[] { "icons/" }; //$NON-NLS-1$
291
}
292
293     /* (non-Javadoc)
294      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFormattedPackageName(java.lang.String)
295      */

296     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
297          // Package name addition to create a location for containing
298
// any classes required by the decorator.
299
String JavaDoc packageName = super.getFormattedPackageName(id);
300         if (packageName.length() != 0)
301             return packageName + ".decorators"; //$NON-NLS-1$
302
return "decorators"; //$NON-NLS-1$
303
}
304
305     /* (non-Javadoc)
306      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
307      */

308     public String JavaDoc getUsedExtensionPoint() {
309         return "org.eclipse.ui.decorators"; //$NON-NLS-1$
310
}
311
312     /**
313      * Returns a 2-D String array based on a comma seperated
314      * string of choices.
315      *
316      * @param iconLocations
317      * comma seperated string of icon placement options
318      * @return the 2-D array of choices
319      *
320      */

321     protected String JavaDoc[][] fromCommaSeparated(String JavaDoc iconLocations) {
322         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(iconLocations, ","); //$NON-NLS-1$
323
String JavaDoc[][] choices = new String JavaDoc[tokens.countTokens() / 2][2];
324         int x = 0, y = 0;
325         while (tokens.hasMoreTokens()) {
326             choices[x][y++] = tokens.nextToken();
327             choices[x++][y--] = tokens.nextToken();
328         }
329         return choices;
330     }
331 }
332
Popular Tags