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.internal.ui.wizards.templates;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.jface.operation.IRunnableWithProgress;
019: import org.eclipse.jface.wizard.Wizard;
020: import org.eclipse.pde.core.plugin.IPluginBase;
021: import org.eclipse.pde.core.plugin.IPluginImport;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.core.plugin.IPluginReference;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEPluginImages;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.ui.IExtensionWizard;
028: import org.eclipse.pde.ui.templates.BaseOptionTemplateSection;
029: import org.eclipse.pde.ui.templates.ITemplateSection;
030: import org.eclipse.ui.actions.WorkspaceModifyOperation;
031:
032: /**
033: * This wizard should be used as a base class for
034: * wizards that provide new plug-in templates.
035: * These wizards are loaded during new plug-in or fragment
036: * creation and are used to provide initial
037: * content (Java classes, directory structure and
038: * extensions).
039: * <p>
040: * This plug-in will be passed on to the templates to generate additional
041: * content. After all templates have executed,
042: * the wizard will use the collected list of required
043: * plug-ins to set up Java buildpath so that all the
044: * generated Java classes can be resolved during the build.
045: */
046:
047: public class NewExtensionTemplateWizard extends Wizard implements
048: IExtensionWizard {
049: private ITemplateSection fSection;
050: private IProject fProject;
051: private IPluginModelBase fModel;
052: private boolean fUpdatedDependencies;
053:
054: /**
055: * Creates a new template wizard.
056: */
057:
058: public NewExtensionTemplateWizard(ITemplateSection section) {
059: Assert.isNotNull(section);
060: setDialogSettings(PDEPlugin.getDefault().getDialogSettings());
061: setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEX_WIZ);
062: setNeedsProgressMonitor(true);
063: fSection = section;
064: }
065:
066: public void init(IProject project, IPluginModelBase model) {
067: this .fProject = project;
068: this .fModel = model;
069: }
070:
071: public void addPages() {
072: fSection.addPages(this );
073: setWindowTitle(fSection.getLabel());
074: if (fSection instanceof BaseOptionTemplateSection) {
075: ((BaseOptionTemplateSection) fSection)
076: .initializeFields(fModel);
077: }
078: }
079:
080: public boolean performFinish() {
081: IRunnableWithProgress operation = new WorkspaceModifyOperation() {
082: public void execute(IProgressMonitor monitor) {
083: try {
084: int totalWork = fSection.getNumberOfWorkUnits();
085: monitor
086: .beginTask(
087: PDEUIMessages.NewExtensionTemplateWizard_generating,
088: totalWork);
089: updateDependencies();
090: fSection.execute(fProject, fModel, monitor); // nsteps
091: } catch (CoreException e) {
092: PDEPlugin.logException(e);
093: } finally {
094: monitor.done();
095: }
096: }
097: };
098: try {
099: getContainer().run(false, true, operation);
100: } catch (InvocationTargetException e) {
101: PDEPlugin.logException(e);
102: return false;
103: } catch (InterruptedException e) {
104: PDEPlugin.logException(e);
105: return false;
106: }
107: return true;
108: }
109:
110: private void updateDependencies() throws CoreException {
111: IPluginReference[] refs = fSection.getDependencies(fModel
112: .getPluginBase().getSchemaVersion());
113: for (int i = 0; i < refs.length; i++) {
114: IPluginReference ref = refs[i];
115: if (!modelContains(ref)) {
116: IPluginImport iimport = fModel.getPluginFactory()
117: .createImport();
118: iimport.setId(ref.getId());
119: iimport.setMatch(ref.getMatch());
120: iimport.setVersion(ref.getVersion());
121: fModel.getPluginBase().add(iimport);
122: fUpdatedDependencies = true;
123: }
124: }
125: }
126:
127: private boolean modelContains(IPluginReference ref) {
128: IPluginBase plugin = fModel.getPluginBase();
129: IPluginImport[] imports = plugin.getImports();
130: for (int i = 0; i < imports.length; i++) {
131: IPluginImport iimport = imports[i];
132: if (iimport.getId().equals(ref.getId())) {
133: // good enough
134: return true;
135: }
136: }
137: return false;
138: }
139:
140: public boolean updatedDependencies() {
141: return fUpdatedDependencies;
142: }
143: }
|