KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > extension > 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.extension;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jface.wizard.Wizard;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.core.plugin.IPluginImport;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginReference;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.PDEPluginImages;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.ui.IExtensionWizard;
28 import org.eclipse.pde.ui.templates.BaseOptionTemplateSection;
29 import org.eclipse.pde.ui.templates.ITemplateSection;
30 import org.eclipse.ui.actions.WorkspaceModifyOperation;
31 /**
32  * This wizard should be used as a base class for
33  * wizards that provide new plug-in templates.
34  * These wizards are loaded during new plug-in or fragment
35  * creation and are used to provide initial
36  * content (Java classes, directory structure and
37  * extensions).
38  * <p>
39  * The wizard provides a common first page that will
40  * initialize the plug-in itself. This plug-in will
41  * 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
49     extends Wizard
50     implements IExtensionWizard {
51     private ITemplateSection section;
52     IProject project;
53     IPluginModelBase model;
54     boolean fUpdatedDependencies;
55     /**
56      * Creates a new template wizard.
57      */

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