KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.ui.templates.ide;
13
14 import java.io.File 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.IPluginModelBase;
21 import org.eclipse.pde.core.plugin.IPluginReference;
22 import org.eclipse.pde.core.plugin.TargetPlatform;
23 import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
24 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
25 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
26 import org.eclipse.pde.internal.ui.templates.PluginReference;
27 import org.eclipse.pde.internal.ui.wizards.product.ISplashHandlerConstants;
28 import org.eclipse.pde.internal.ui.wizards.product.UpdateSplashHandlerAction;
29 import org.eclipse.pde.ui.IFieldData;
30 import org.eclipse.pde.ui.templates.ComboChoiceOption;
31 import org.eclipse.pde.ui.templates.StringOption;
32 import org.eclipse.pde.ui.templates.TemplateOption;
33
34 /**
35  * SplashHandlersTemplate
36  *
37  */

38 public class SplashHandlersTemplate extends PDETemplateSection {
39
40     private final static int F_PAGE_INDEX = 0;
41
42     private final static String JavaDoc F_DEFAULT_PRODUCT = "org.eclipse.sdk.ide"; //$NON-NLS-1$
43

44     private final static String JavaDoc F_FIELD_TEMPLATE = "fieldTemplate"; //$NON-NLS-1$
45

46     private final static String JavaDoc F_FIELD_PRODUCTS = "fieldProducts"; //$NON-NLS-1$
47

48     private final static String JavaDoc F_FIELD_CLASS = "fieldClass"; //$NON-NLS-1$
49

50     private final static String JavaDoc F_FIELD_SPLASH = "fieldSplash"; //$NON-NLS-1$
51

52     private final static String JavaDoc F_SPLASH_SCREEN_FILE = "splash.bmp"; //$NON-NLS-1$
53

54     private WizardPage fPage;
55     
56     private TemplateOption fFieldTemplate;
57
58     private ComboChoiceOption fFieldProducts;
59     
60     private TemplateOption fFieldPackage;
61
62     private StringOption fFieldClass;
63     
64     private TemplateOption fFieldSplash;
65     
66     /**
67      *
68      */

69     public SplashHandlersTemplate() {
70         initialize();
71     }
72
73     /**
74      *
75      */

76     private void initialize() {
77         // Default field values
78
fFieldTemplate = null;
79         fFieldProducts = null;
80         fFieldPackage = null;
81         fFieldClass = null;
82         fFieldSplash = null;
83         // One wizard page
84
setPageCount(1);
85         // GUI
86
createUI();
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#addPages(org.eclipse.jface.wizard.Wizard)
91      */

92     public void addPages(Wizard wizard) {
93         // Create the page
94
fPage = createPage(0, IHelpContextIds.TEMPLATE_SPLASH_HANDLERS);
95         fPage.setTitle(PDETemplateMessages.SplashHandlersTemplate_titleSplashHandlerOptions);
96         fPage.setDescription(PDETemplateMessages.SplashHandlersTemplate_descSplashHandlerOptions);
97         // Add the page
98
wizard.addPage(fPage);
99         // Mark as added
100
markPagesAdded();
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.pde.internal.ui.templates.PDETemplateSection#getFormattedPackageName(java.lang.String)
105      */

106     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
107         // Package name addition to create a location for containing
108
// any classes required by the splash handlers.
109
String JavaDoc packageName = super.getFormattedPackageName(id);
110         // Unqualifed
111
if (packageName.length() == 0) {
112             return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
113         }
114         // Qualified
115
return packageName + '.' + ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
116     }
117     
118     /* (non-Javadoc)
119      * @see org.eclipse.pde.internal.ui.templates.PDETemplateSection#getNewFiles()
120      */

121     public String JavaDoc[] getNewFiles() {
122         // Note: This does not even get called for non-project templates
123
// As a result, listed files are not added to the binary build
124
// section
125
if (isSplashFieldSelected()) {
126             return new String JavaDoc[] { F_SPLASH_SCREEN_FILE };
127         }
128         // TODO: MP: SPLASH: Investigate if this is necessary, does not get called for non-project templates
129
return super.getNewFiles();
130     }
131     
132     /**
133      * @return
134      */

135     private boolean isSplashFieldSelected() {
136         if ((Boolean JavaDoc)fFieldSplash.getValue() == Boolean.TRUE) {
137             return true;
138         }
139         return false;
140     }
141     
142     /* (non-Javadoc)
143      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getNumberOfWorkUnits()
144      */

145     public int getNumberOfWorkUnits() {
146         return super.getNumberOfWorkUnits() + 1;
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
151      */

152     protected void initializeFields(IFieldData data) {
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         String JavaDoc pluginId = model.getPluginBase().getId();
162         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
167      */

168     public boolean isDependentOnParentWizard() {
169         return true;
170     }
171     
172     /* (non-Javadoc)
173      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#validateOptions(org.eclipse.pde.ui.templates.TemplateOption)
174      */

175     public void validateOptions(TemplateOption source) {
176         // Update class name
177
if (source == fFieldTemplate) {
178             updateUIFieldClass();
179         }
180         super.validateOptions(source);
181     }
182     
183     /**
184      *
185      */

186     private void updateUIFieldClass() {
187         // Update the class name depending on the splash screen type
188
for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) {
189             String JavaDoc choice = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][0];
190             if (fFieldTemplate.getValue().equals(choice)) {
191                 fFieldClass.setValue(ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[i]);
192                 break;
193             }
194         }
195     }
196
197     /**
198      *
199      */

200     private void createUI() {
201         // Field: template
202
createUIFieldTemplate();
203         // Field: product ID
204
createUIFieldProductID();
205         // Field: package
206
createUIFieldPackage();
207         // Field: class
208
createUIFieldClass();
209         // Field: splash
210
createUIFieldSplash();
211     }
212
213     /**
214      *
215      */

216     private void createUIFieldSplash() {
217         fFieldSplash = addOption(
218                 F_FIELD_SPLASH,
219                 PDETemplateMessages.SplashHandlersTemplate_fieldAddSplash,
220                 false,
221                 F_PAGE_INDEX);
222     }
223
224     /**
225      *
226      */

227     private void createUIFieldClass() {
228         fFieldClass = (StringOption)addOption(
229                 F_FIELD_CLASS,
230                 PDETemplateMessages.SplashHandlersTemplate_fieldClassName,
231                 ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[0],
232                 F_PAGE_INDEX);
233         fFieldClass.setReadOnly(true);
234     }
235
236     /**
237      *
238      */

239     private void createUIFieldPackage() {
240         fFieldPackage = addOption(
241                 KEY_PACKAGE_NAME,
242                 PDETemplateMessages.SplashHandlersTemplate_fieldJavaPackage,
243                 null,
244                 F_PAGE_INDEX);
245     }
246
247     /**
248      *
249      */

250     private void createUIFieldTemplate() {
251         fFieldTemplate = addOption(
252                 F_FIELD_TEMPLATE,
253                 PDETemplateMessages.SplashHandlersTemplate_fieldSplashScreenType,
254                 ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES,
255                 ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[0][0],
256                 F_PAGE_INDEX);
257     }
258
259     /**
260      *
261      */

262     private void createUIFieldProductID() {
263         
264         String JavaDoc[] products = TargetPlatform.getProducts();
265         String JavaDoc[][] choices = new String JavaDoc[products.length][2];
266         String JavaDoc initialChoice = null;
267         boolean foundInitialChoice = false;
268         // Populate choices with products
269
for (int i = 0; i < products.length; i++) {
270             // ID
271
choices[i][0] = products[i];
272             // Name
273
choices[i][1] = products[i];
274             // Determine whether default product is present
275
if ((foundInitialChoice == false) &&
276                     (products[i].equals(F_DEFAULT_PRODUCT))) {
277                 foundInitialChoice = true;
278             }
279         }
280         // Use default product as the initial product choice if found;
281
// otherwise, use the first item found
282
if (foundInitialChoice) {
283             initialChoice = F_DEFAULT_PRODUCT;
284         } else {
285             initialChoice = choices[0][0];
286         }
287         // Create the field
288
fFieldProducts = addComboChoiceOption(
289                 F_FIELD_PRODUCTS,
290                 PDETemplateMessages.SplashHandlersTemplate_fieldProductID,
291                 choices,
292                 initialChoice,
293                 F_PAGE_INDEX);
294     }
295     
296     /* (non-Javadoc)
297      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
298      */

299     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
300         // Ensure schema version was defined
301
if (schemaVersion == null) {
302             return super.getDependencies(schemaVersion);
303         }
304         // Create the dependencies for the splash handler extension template addition
305
IPluginReference[] dependencies = new IPluginReference[4];
306         dependencies[0] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
307
dependencies[1] = new PluginReference("org.eclipse.swt", null, 0); //$NON-NLS-1$
308
dependencies[2] = new PluginReference("org.eclipse.jface", null, 0); //$NON-NLS-1$
309
dependencies[3] = new PluginReference("org.eclipse.ui.workbench", null, 0); //$NON-NLS-1$
310

311         return dependencies;
312     }
313     
314     /* (non-Javadoc)
315      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
316      */

317     public String JavaDoc getSectionId() {
318         return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
319     }
320
321     /* (non-Javadoc)
322      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
323      */

324     protected void updateModel(IProgressMonitor monitor) throws CoreException {
325         // Create the action to update the model with the associated
326
// splash handler extensions, extension points, elements and attributes
327
UpdateSplashHandlerAction action = new UpdateSplashHandlerAction();
328         // Configure the acation
329
String JavaDoc id = createAttributeValueID();
330         action.setFieldID(id);
331         action.setFieldClass(createAttributeValueClass());
332         action.setFieldSplashID(id);
333         action.setFieldProductID((String JavaDoc)fFieldProducts.getValue());
334         action.setFieldTemplate((String JavaDoc)fFieldTemplate.getValue());
335         action.setFieldPluginID(model.getPluginBase().getId());
336         action.setModel(model);
337         action.setMonitor(monitor);
338         // Execute the action
339
action.run();
340         // If an exception was caught, release it
341
action.hasException();
342     }
343
344     /**
345      * @return
346      */

347     private String JavaDoc createAttributeValueID() {
348         // Create the ID based on the splash screen type
349
return fFieldPackage.getValue() +
350                "." + //$NON-NLS-1$
351
fFieldTemplate.getValue();
352     }
353
354     /**
355      * @return
356      */

357     private String JavaDoc createAttributeValueClass() {
358         // Create the class based on the splash screen type
359
return fFieldPackage.getValue() +
360                "." + //$NON-NLS-1$
361
fFieldClass.getValue();
362     }
363
364     /* (non-Javadoc)
365      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
366      */

367     public String JavaDoc getUsedExtensionPoint() {
368         return ISplashHandlerConstants.F_SPLASH_HANDLERS_EXTENSION;
369     }
370     
371     /* (non-Javadoc)
372      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFile(java.io.File)
373      */

374     protected boolean isOkToCreateFile(File JavaDoc sourceFile) {
375         // TODO: MP: SPLASH: Sync this with org.eclipse.pde.internal.ui.util.TemplateFileGenerator
376
String JavaDoc javaSuffix = ".java"; //$NON-NLS-1$
377
String JavaDoc targetFile = fFieldClass.getValue() + javaSuffix;
378         String JavaDoc copyFile = sourceFile.toString();
379         
380         if (copyFile.endsWith(javaSuffix) &&
381                 (copyFile.endsWith(targetFile) == false)) {
382             return false;
383         }
384         
385         return true;
386     }
387     
388     /* (non-Javadoc)
389      * @see org.eclipse.pde.internal.ui.templates.PDETemplateSection#copyBrandingDirectory()
390      */

391     protected boolean copyBrandingDirectory() {
392         return isSplashFieldSelected();
393     }
394     
395     /* (non-Javadoc)
396      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFolder(java.io.File)
397      */

398     protected boolean isOkToCreateFolder(File JavaDoc sourceFolder) {
399         // TODO: MP: SPLASH: Sync this with org.eclipse.pde.internal.ui.util.TemplateFileGenerator
400
boolean extensibleTemplateSelected =
401             UpdateSplashHandlerAction.isExtensibleTemplateSelected((String JavaDoc)fFieldTemplate.getValue());
402         String JavaDoc sourceFolderString = sourceFolder.toString();
403         
404         if ((extensibleTemplateSelected == false) &&
405                 sourceFolderString.endsWith("icons")) { //$NON-NLS-1$
406
return false;
407         } else if ((extensibleTemplateSelected == false) &&
408                 sourceFolderString.endsWith("schema")) { //$NON-NLS-1$
409
return false;
410         }
411             
412         return true;
413     }
414     
415     /* (non-Javadoc)
416      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getLabel()
417      */

418     public String JavaDoc getLabel() {
419         return getPluginResourceString("wizard.name.splash.handler"); //$NON-NLS-1$
420
}
421     
422     /* (non-Javadoc)
423      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getDescription()
424      */

425     public String JavaDoc getDescription() {
426         return getPluginResourceString("wizard.description.splash.handler"); //$NON-NLS-1$
427
}
428     
429 }
430
Popular Tags