KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > NewExtensionTemplateWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.wizards.templates;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.wizard.Wizard;
21 import org.eclipse.pde.core.plugin.IPluginBase;
22 import org.eclipse.pde.core.plugin.IPluginImport;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginReference;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEPluginImages;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.ui.IExtensionWizard;
29 import org.eclipse.pde.ui.templates.BaseOptionTemplateSection;
30 import org.eclipse.pde.ui.templates.ITemplateSection;
31 import org.eclipse.ui.actions.WorkspaceModifyOperation;
32
33 /**
34  * This wizard should be used as a base class for
35  * wizards that provide new plug-in templates.
36  * These wizards are loaded during new plug-in or fragment
37  * creation and are used to provide initial
38  * content (Java classes, directory structure and
39  * extensions).
40  * <p>
41  * This plug-in will be passed on to the templates to generate additional
42  * content. After all templates have executed,
43  * the wizard will use the collected list of required
44  * plug-ins to set up Java buildpath so that all the
45  * generated Java classes can be resolved during the build.
46  */

47
48 public class NewExtensionTemplateWizard extends Wizard implements IExtensionWizard {
49     private ITemplateSection fSection;
50     private IProject fProject;
51     private IPluginModelBase fModel;
52     private boolean fUpdatedDependencies;
53
54     /**
55      * Creates a new template wizard.
56      */

57
58     public NewExtensionTemplateWizard(ITemplateSection section) {
59         Assert.isNotNull(section);
60         setDialogSettings(PDEPlugin.getDefault().getDialogSettings());
61         setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEX_WIZ);
62         setNeedsProgressMonitor(true);
63         fSection = section;
64     }
65     
66     public void init(IProject project, IPluginModelBase model) {
67         this.fProject = project;
68         this.fModel = model;
69     }
70
71     public void addPages() {
72         fSection.addPages(this);
73         setWindowTitle(fSection.getLabel());
74         if (fSection instanceof BaseOptionTemplateSection) {
75             ((BaseOptionTemplateSection)fSection).initializeFields(fModel);
76         }
77     }
78
79     public boolean performFinish() {
80         IRunnableWithProgress operation = new WorkspaceModifyOperation() {
81             public void execute(IProgressMonitor monitor) {
82                 try {
83                     int totalWork = fSection.getNumberOfWorkUnits();
84                     monitor.beginTask(PDEUIMessages.NewExtensionTemplateWizard_generating, totalWork);
85                     updateDependencies();
86                     fSection.execute(fProject, fModel, monitor); // nsteps
87
} catch (CoreException e) {
88                     PDEPlugin.logException(e);
89                 } finally {
90                     monitor.done();
91                 }
92             }
93         };
94         try {
95             getContainer().run(false, true, operation);
96         } catch (InvocationTargetException JavaDoc e) {
97             PDEPlugin.logException(e);
98             return false;
99         } catch (InterruptedException JavaDoc e) {
100             PDEPlugin.logException(e);
101             return false;
102         }
103         return true;
104     }
105
106     private void updateDependencies() throws CoreException {
107         IPluginReference[] refs = fSection.getDependencies(fModel.getPluginBase().getSchemaVersion());
108         for (int i = 0; i < refs.length; i++) {
109             IPluginReference ref = refs[i];
110             if (!modelContains(ref)) {
111                 IPluginImport iimport = fModel.getPluginFactory().createImport();
112                 iimport.setId(ref.getId());
113                 iimport.setMatch(ref.getMatch());
114                 iimport.setVersion(ref.getVersion());
115                 fModel.getPluginBase().add(iimport);
116                 fUpdatedDependencies = true;
117             }
118         }
119     }
120
121     private boolean modelContains(IPluginReference ref) {
122         IPluginBase plugin = fModel.getPluginBase();
123         IPluginImport[] imports = plugin.getImports();
124         for (int i = 0; i < imports.length; i++) {
125             IPluginImport iimport = imports[i];
126             if (iimport.getId().equals(ref.getId())) {
127                 // good enough
128
return true;
129             }
130         }
131         return false;
132     }
133     
134     public boolean updatedDependencies() {
135         return fUpdatedDependencies;
136     }
137 }
138
Popular Tags