001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.templates;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.SubProgressMonitor;
018: import org.eclipse.jface.wizard.Wizard;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.core.plugin.IPluginReference;
021: import org.eclipse.pde.internal.ui.PDEPlugin;
022: import org.eclipse.pde.internal.ui.PDEPluginImages;
023: import org.eclipse.pde.internal.ui.PDEUIMessages;
024: import org.eclipse.pde.ui.IBundleContentWizard;
025: import org.eclipse.pde.ui.IFieldData;
026:
027: /**
028: * This class is used as a common base for plug-in content wizards that are
029: * implemented using PDE template support. The assumption is that one or more
030: * templates will be used to generate plug-in content. Dependencies, new files
031: * and wizard pages are all computed based on the templates.
032: *
033: * @since 2.0
034: */
035: public abstract class AbstractNewPluginTemplateWizard extends Wizard
036: implements IBundleContentWizard {
037: private IFieldData data;
038:
039: /**
040: * Creates a new template wizard.
041: */
042: public AbstractNewPluginTemplateWizard() {
043: super ();
044: setDialogSettings(PDEPlugin.getDefault().getDialogSettings());
045: setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEXPRJ_WIZ);
046: setNeedsProgressMonitor(true);
047: }
048:
049: /**
050: * @see org.eclipse.pde.ui.IPluginContentWizard#init(IFieldData)
051: */
052: public void init(IFieldData data) {
053: this .data = data;
054: setWindowTitle(PDEUIMessages.PluginCodeGeneratorWizard_title);
055: }
056:
057: /**
058: * Returns the field data passed to the wizard during the initialization.
059: *
060: * @return the parent wizard field data
061: */
062: public IFieldData getData() {
063: return data;
064: }
065:
066: /**
067: * This wizard adds a mandatory first page. Subclasses implement this method
068: * to add additional pages to the wizard.
069: */
070: protected abstract void addAdditionalPages();
071:
072: /**
073: * Implements wizard method. Subclasses cannot override it.
074: */
075: public final void addPages() {
076: addAdditionalPages();
077: }
078:
079: /**
080: * @see org.eclipse.jface.wizard.Wizard#performFinish()
081: */
082: public boolean performFinish() {
083: // do nothing - all the work is in the other 'performFinish'
084: return true;
085: }
086:
087: /**
088: * Implements the interface method by looping through template sections and
089: * executing them sequentially.
090: *
091: * @param project
092: * the project
093: * @param model
094: * the plug-in model
095: * @param monitor
096: * the progress monitor to track the execution progress as part
097: * of the overall new project creation operation
098: * @return <code>true</code> if the wizard completed the operation with
099: * success, <code>false</code> otherwise.
100: */
101: public boolean performFinish(IProject project,
102: IPluginModelBase model, IProgressMonitor monitor) {
103: try {
104: ITemplateSection[] sections = getTemplateSections();
105: monitor.beginTask("", sections.length); //$NON-NLS-1$
106: for (int i = 0; i < sections.length; i++) {
107: sections[i].execute(project, model,
108: new SubProgressMonitor(monitor, 1));
109: }
110: //No reason to do this any more with the new editors
111: //saveTemplateFile(project, null);
112: } catch (CoreException e) {
113: PDEPlugin.logException(e);
114: return false;
115: } finally {
116: monitor.done();
117: }
118: return true;
119: }
120:
121: /**
122: * Returns the template sections used in this wizard.
123: *
124: * @return the array of template sections
125: */
126: public abstract ITemplateSection[] getTemplateSections();
127:
128: /**
129: * @see org.eclipse.pde.ui.IPluginContentWizard#getDependencies(String)
130: */
131: public IPluginReference[] getDependencies(String schemaVersion) {
132: ArrayList result = new ArrayList();
133: ITemplateSection[] sections = getTemplateSections();
134: for (int i = 0; i < sections.length; i++) {
135: IPluginReference[] refs = sections[i]
136: .getDependencies(schemaVersion);
137: for (int j = 0; j < refs.length; j++) {
138: if (!result.contains(refs[j]))
139: result.add(refs[j]);
140: }
141: }
142: return (IPluginReference[]) result
143: .toArray(new IPluginReference[result.size()]);
144: }
145:
146: /**
147: * @see org.eclipse.pde.ui.IPluginContentWizard#getNewFiles()
148: */
149: public String[] getNewFiles() {
150: ArrayList result = new ArrayList();
151: ITemplateSection[] sections = getTemplateSections();
152: for (int i = 0; i < sections.length; i++) {
153: String[] newFiles = sections[i].getNewFiles();
154: for (int j = 0; j < newFiles.length; j++) {
155: if (!result.contains(newFiles[j]))
156: result.add(newFiles[j]);
157: }
158: }
159: return (String[]) result.toArray(new String[result.size()]);
160: }
161:
162: /* (non-Javadoc)
163: * @see org.eclipse.pde.ui.IPluginContentWizard#hasPages()
164: */
165: public boolean hasPages() {
166: return getTemplateSections().length > 0;
167: }
168:
169: public String[] getImportPackages() {
170: return new String[0];
171: }
172: }
|