001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.templates.ide;
011:
012: import java.io.File;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.jface.wizard.Wizard;
017: import org.eclipse.jface.wizard.WizardPage;
018: import org.eclipse.pde.core.plugin.IPluginModelBase;
019: import org.eclipse.pde.core.plugin.IPluginReference;
020: import org.eclipse.pde.core.plugin.TargetPlatform;
021: import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
022: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
023: import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
024: import org.eclipse.pde.internal.ui.templates.PluginReference;
025: import org.eclipse.pde.internal.ui.wizards.product.ISplashHandlerConstants;
026: import org.eclipse.pde.internal.ui.wizards.product.UpdateSplashHandlerAction;
027: import org.eclipse.pde.ui.IFieldData;
028: import org.eclipse.pde.ui.templates.ComboChoiceOption;
029: import org.eclipse.pde.ui.templates.StringOption;
030: import org.eclipse.pde.ui.templates.TemplateOption;
031:
032: /**
033: * SplashHandlersTemplate
034: *
035: */
036: public class SplashHandlersTemplate extends PDETemplateSection {
037:
038: private final static int F_PAGE_INDEX = 0;
039:
040: private final static String F_DEFAULT_PRODUCT = "org.eclipse.sdk.ide"; //$NON-NLS-1$
041:
042: private final static String F_FIELD_TEMPLATE = "fieldTemplate"; //$NON-NLS-1$
043:
044: private final static String F_FIELD_PRODUCTS = "fieldProducts"; //$NON-NLS-1$
045:
046: private final static String F_FIELD_CLASS = "fieldClass"; //$NON-NLS-1$
047:
048: private final static String F_FIELD_SPLASH = "fieldSplash"; //$NON-NLS-1$
049:
050: private final static String F_SPLASH_SCREEN_FILE = "splash.bmp"; //$NON-NLS-1$
051:
052: private WizardPage fPage;
053:
054: private TemplateOption fFieldTemplate;
055:
056: private ComboChoiceOption fFieldProducts;
057:
058: private TemplateOption fFieldPackage;
059:
060: private StringOption fFieldClass;
061:
062: private TemplateOption fFieldSplash;
063:
064: /**
065: *
066: */
067: public SplashHandlersTemplate() {
068: initialize();
069: }
070:
071: /**
072: *
073: */
074: private void initialize() {
075: // Default field values
076: fFieldTemplate = null;
077: fFieldProducts = null;
078: fFieldPackage = null;
079: fFieldClass = null;
080: fFieldSplash = null;
081: // One wizard page
082: setPageCount(1);
083: // GUI
084: createUI();
085: }
086:
087: /* (non-Javadoc)
088: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#addPages(org.eclipse.jface.wizard.Wizard)
089: */
090: public void addPages(Wizard wizard) {
091: // Create the page
092: fPage = createPage(0, IHelpContextIds.TEMPLATE_SPLASH_HANDLERS);
093: fPage
094: .setTitle(PDETemplateMessages.SplashHandlersTemplate_titleSplashHandlerOptions);
095: fPage
096: .setDescription(PDETemplateMessages.SplashHandlersTemplate_descSplashHandlerOptions);
097: // Add the page
098: wizard.addPage(fPage);
099: // 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 getFormattedPackageName(String id) {
107: // Package name addition to create a location for containing
108: // any classes required by the splash handlers.
109: String packageName = super .getFormattedPackageName(id);
110: // Unqualifed
111: if (packageName.length() == 0) {
112: return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
113: }
114: // Qualified
115: return packageName + '.'
116: + ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.pde.internal.ui.templates.PDETemplateSection#getNewFiles()
121: */
122: public String[] getNewFiles() {
123: // Note: This does not even get called for non-project templates
124: // As a result, listed files are not added to the binary build
125: // section
126: if (isSplashFieldSelected()) {
127: return new String[] { F_SPLASH_SCREEN_FILE };
128: }
129: // TODO: MP: SPLASH: Investigate if this is necessary, does not get called for non-project templates
130: return super .getNewFiles();
131: }
132:
133: /**
134: * @return
135: */
136: private boolean isSplashFieldSelected() {
137: if ((Boolean) fFieldSplash.getValue() == Boolean.TRUE) {
138: return true;
139: }
140: return false;
141: }
142:
143: /* (non-Javadoc)
144: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getNumberOfWorkUnits()
145: */
146: public int getNumberOfWorkUnits() {
147: return super .getNumberOfWorkUnits() + 1;
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.ui.IFieldData)
152: */
153: protected void initializeFields(IFieldData data) {
154: String id = data.getId();
155: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
156: }
157:
158: /* (non-Javadoc)
159: * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#initializeFields(org.eclipse.pde.core.plugin.IPluginModelBase)
160: */
161: public void initializeFields(IPluginModelBase model) {
162: String pluginId = model.getPluginBase().getId();
163: initializeOption(KEY_PACKAGE_NAME,
164: getFormattedPackageName(pluginId));
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
169: */
170: public boolean isDependentOnParentWizard() {
171: return true;
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#validateOptions(org.eclipse.pde.ui.templates.TemplateOption)
176: */
177: public void validateOptions(TemplateOption source) {
178: // Update class name
179: if (source == fFieldTemplate) {
180: updateUIFieldClass();
181: }
182: super .validateOptions(source);
183: }
184:
185: /**
186: *
187: */
188: private void updateUIFieldClass() {
189: // Update the class name depending on the splash screen type
190: for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) {
191: String choice = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][0];
192: if (fFieldTemplate.getValue().equals(choice)) {
193: fFieldClass
194: .setValue(ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[i]);
195: break;
196: }
197: }
198: }
199:
200: /**
201: *
202: */
203: private void createUI() {
204: // Field: template
205: createUIFieldTemplate();
206: // Field: product ID
207: createUIFieldProductID();
208: // Field: package
209: createUIFieldPackage();
210: // Field: class
211: createUIFieldClass();
212: // Field: splash
213: createUIFieldSplash();
214: }
215:
216: /**
217: *
218: */
219: private void createUIFieldSplash() {
220: fFieldSplash = addOption(
221: F_FIELD_SPLASH,
222: PDETemplateMessages.SplashHandlersTemplate_fieldAddSplash,
223: false, F_PAGE_INDEX);
224: }
225:
226: /**
227: *
228: */
229: private void createUIFieldClass() {
230: fFieldClass = (StringOption) addOption(
231: F_FIELD_CLASS,
232: PDETemplateMessages.SplashHandlersTemplate_fieldClassName,
233: ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[0],
234: F_PAGE_INDEX);
235: fFieldClass.setReadOnly(true);
236: }
237:
238: /**
239: *
240: */
241: private void createUIFieldPackage() {
242: fFieldPackage = addOption(
243: KEY_PACKAGE_NAME,
244: PDETemplateMessages.SplashHandlersTemplate_fieldJavaPackage,
245: null, F_PAGE_INDEX);
246: }
247:
248: /**
249: *
250: */
251: private void createUIFieldTemplate() {
252: fFieldTemplate = addOption(
253: F_FIELD_TEMPLATE,
254: PDETemplateMessages.SplashHandlersTemplate_fieldSplashScreenType,
255: ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES,
256: ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[0][0],
257: F_PAGE_INDEX);
258: }
259:
260: /**
261: *
262: */
263: private void createUIFieldProductID() {
264:
265: String[] products = TargetPlatform.getProducts();
266: String[][] choices = new String[products.length][2];
267: String initialChoice = null;
268: boolean foundInitialChoice = false;
269: // Populate choices with products
270: for (int i = 0; i < products.length; i++) {
271: // ID
272: choices[i][0] = products[i];
273: // Name
274: choices[i][1] = products[i];
275: // Determine whether default product is present
276: if ((foundInitialChoice == false)
277: && (products[i].equals(F_DEFAULT_PRODUCT))) {
278: foundInitialChoice = true;
279: }
280: }
281: // Use default product as the initial product choice if found;
282: // otherwise, use the first item found
283: if (foundInitialChoice) {
284: initialChoice = F_DEFAULT_PRODUCT;
285: } else {
286: initialChoice = choices[0][0];
287: }
288: // Create the field
289: fFieldProducts = addComboChoiceOption(
290: F_FIELD_PRODUCTS,
291: PDETemplateMessages.SplashHandlersTemplate_fieldProductID,
292: choices, initialChoice, F_PAGE_INDEX);
293: }
294:
295: /* (non-Javadoc)
296: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
297: */
298: public IPluginReference[] getDependencies(String schemaVersion) {
299: // Ensure schema version was defined
300: if (schemaVersion == null) {
301: return super .getDependencies(schemaVersion);
302: }
303: // Create the dependencies for the splash handler extension template addition
304: IPluginReference[] dependencies = new IPluginReference[4];
305: dependencies[0] = new PluginReference(
306: "org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
307: dependencies[1] = new PluginReference(
308: "org.eclipse.swt", null, 0); //$NON-NLS-1$
309: dependencies[2] = new PluginReference(
310: "org.eclipse.jface", null, 0); //$NON-NLS-1$
311: dependencies[3] = new PluginReference(
312: "org.eclipse.ui.workbench", null, 0); //$NON-NLS-1$
313:
314: return dependencies;
315: }
316:
317: /* (non-Javadoc)
318: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
319: */
320: public String getSectionId() {
321: return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID;
322: }
323:
324: /* (non-Javadoc)
325: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
326: */
327: protected void updateModel(IProgressMonitor monitor)
328: throws CoreException {
329: // Create the action to update the model with the associated
330: // splash handler extensions, extension points, elements and attributes
331: UpdateSplashHandlerAction action = new UpdateSplashHandlerAction();
332: // Configure the acation
333: String id = createAttributeValueID();
334: action.setFieldID(id);
335: action.setFieldClass(createAttributeValueClass());
336: action.setFieldSplashID(id);
337: action.setFieldProductID((String) fFieldProducts.getValue());
338: action.setFieldTemplate((String) fFieldTemplate.getValue());
339: action.setFieldPluginID(model.getPluginBase().getId());
340: action.setModel(model);
341: action.setMonitor(monitor);
342: // Execute the action
343: action.run();
344: // If an exception was caught, release it
345: action.hasException();
346: }
347:
348: /**
349: * @return
350: */
351: private String createAttributeValueID() {
352: // Create the ID based on the splash screen type
353: return fFieldPackage.getValue() + "." + //$NON-NLS-1$
354: fFieldTemplate.getValue();
355: }
356:
357: /**
358: * @return
359: */
360: private String createAttributeValueClass() {
361: // Create the class based on the splash screen type
362: return fFieldPackage.getValue() + "." + //$NON-NLS-1$
363: fFieldClass.getValue();
364: }
365:
366: /* (non-Javadoc)
367: * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
368: */
369: public String getUsedExtensionPoint() {
370: return ISplashHandlerConstants.F_SPLASH_HANDLERS_EXTENSION;
371: }
372:
373: /* (non-Javadoc)
374: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFile(java.io.File)
375: */
376: protected boolean isOkToCreateFile(File sourceFile) {
377: // TODO: MP: SPLASH: Sync this with org.eclipse.pde.internal.ui.util.TemplateFileGenerator
378: String javaSuffix = ".java"; //$NON-NLS-1$
379: String targetFile = fFieldClass.getValue() + javaSuffix;
380: String copyFile = sourceFile.toString();
381:
382: if (copyFile.endsWith(javaSuffix)
383: && (copyFile.endsWith(targetFile) == false)) {
384: return false;
385: }
386:
387: return true;
388: }
389:
390: /* (non-Javadoc)
391: * @see org.eclipse.pde.internal.ui.templates.PDETemplateSection#copyBrandingDirectory()
392: */
393: protected boolean copyBrandingDirectory() {
394: return isSplashFieldSelected();
395: }
396:
397: /* (non-Javadoc)
398: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#isOkToCreateFolder(java.io.File)
399: */
400: protected boolean isOkToCreateFolder(File sourceFolder) {
401: // TODO: MP: SPLASH: Sync this with org.eclipse.pde.internal.ui.util.TemplateFileGenerator
402: boolean extensibleTemplateSelected = UpdateSplashHandlerAction
403: .isExtensibleTemplateSelected((String) fFieldTemplate
404: .getValue());
405: String sourceFolderString = sourceFolder.toString();
406:
407: if ((extensibleTemplateSelected == false)
408: && sourceFolderString.endsWith("icons")) { //$NON-NLS-1$
409: return false;
410: } else if ((extensibleTemplateSelected == false)
411: && sourceFolderString.endsWith("schema")) { //$NON-NLS-1$
412: return false;
413: }
414:
415: return true;
416: }
417:
418: /* (non-Javadoc)
419: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getLabel()
420: */
421: public String getLabel() {
422: return getPluginResourceString("wizard.name.splash.handler"); //$NON-NLS-1$
423: }
424:
425: /* (non-Javadoc)
426: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getDescription()
427: */
428: public String getDescription() {
429: return getPluginResourceString("wizard.description.splash.handler"); //$NON-NLS-1$
430: }
431:
432: }
|