KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > NewWizardTemplate


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
12 package org.eclipse.pde.internal.ui.templates.ide;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.wizard.Wizard;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.pde.core.plugin.IPluginAttribute;
21 import org.eclipse.pde.core.plugin.IPluginBase;
22 import org.eclipse.pde.core.plugin.IPluginElement;
23 import org.eclipse.pde.core.plugin.IPluginExtension;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.IPluginModelFactory;
26 import org.eclipse.pde.core.plugin.IPluginObject;
27 import org.eclipse.pde.core.plugin.IPluginReference;
28 import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
29 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
30 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
31 import org.eclipse.pde.internal.ui.templates.PluginReference;
32 import org.eclipse.pde.ui.IFieldData;
33
34 public class NewWizardTemplate extends PDETemplateSection {
35     public NewWizardTemplate() {
36         setPageCount(1);
37         createOptions();
38     }
39     
40     public String JavaDoc getSectionId() {
41         return "newWizard"; //$NON-NLS-1$
42
}
43     /*
44      * @see ITemplateSection#getNumberOfWorkUnits()
45      */

46     public int getNumberOfWorkUnits() {
47         return super.getNumberOfWorkUnits()+1;
48     }
49     
50     private void createOptions() {
51         // first page
52
addOption(KEY_PACKAGE_NAME, PDETemplateMessages.NewWizardTemplate_packageName, (String JavaDoc)null, 0);
53         addOption("categoryId", PDETemplateMessages.NewWizardTemplate_categoryId, (String JavaDoc)null, 0); //$NON-NLS-1$
54
addOption("categoryName", PDETemplateMessages.NewWizardTemplate_categoryName, "Sample Wizards", 0); //$NON-NLS-1$ //$NON-NLS-2$
55
addOption("wizardClassName", PDETemplateMessages.NewWizardTemplate_className, "SampleNewWizard", 0); //$NON-NLS-1$ //$NON-NLS-2$
56
addOption("wizardPageClassName", PDETemplateMessages.NewWizardTemplate_pageClassName, "SampleNewWizardPage", 0); //$NON-NLS-1$ //$NON-NLS-2$
57
addOption("wizardName", PDETemplateMessages.NewWizardTemplate_wizardName, PDETemplateMessages.NewWizardTemplate_defaultName, 0); //$NON-NLS-1$
58
addOption("extension", PDETemplateMessages.NewWizardTemplate_extension, "mpe", 0); //$NON-NLS-1$ //$NON-NLS-2$
59
addOption("initialFileName", PDETemplateMessages.NewWizardTemplate_fileName,"new_file.mpe", 0); //$NON-NLS-1$ //$NON-NLS-2$
60
}
61
62     protected void initializeFields(IFieldData data) {
63         // In a new project wizard, we don't know this yet - the
64
// model has not been created
65
String JavaDoc id = data.getId();
66         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
67         initializeOption("categoryId", id); //$NON-NLS-1$
68
}
69     public void initializeFields(IPluginModelBase model) {
70         // In the new extension wizard, the model exists so
71
// we can initialize directly from it
72
String JavaDoc pluginId = model.getPluginBase().getId();
73         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
74         initializeOption("categoryId", pluginId); //$NON-NLS-1$
75
}
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
79      */

80     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
81         ArrayList JavaDoc result = new ArrayList JavaDoc();
82         result.add(new PluginReference("org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
83
result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
84
if (schemaVersion != null) {
85             result.add(new PluginReference("org.eclipse.ui.ide", null, 0)); //$NON-NLS-1$
86
result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
87
}
88         return (IPluginReference[])result.toArray(new IPluginReference[result.size()]);
89     }
90     
91     public boolean isDependentOnParentWizard() {
92         return true;
93     }
94     
95     public void addPages(Wizard wizard) {
96         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_NEW_WIZARD);
97         page.setTitle(PDETemplateMessages.NewWizardTemplate_title);
98         page.setDescription(PDETemplateMessages.NewWizardTemplate_desc);
99         wizard.addPage(page);
100         markPagesAdded();
101     }
102     
103     public String JavaDoc getUsedExtensionPoint() {
104         return "org.eclipse.ui.newWizards"; //$NON-NLS-1$
105
}
106     
107     protected void updateModel(IProgressMonitor monitor) throws CoreException {
108         IPluginBase plugin = model.getPluginBase();
109         IPluginExtension extension = createExtension("org.eclipse.ui.newWizards", true); //$NON-NLS-1$
110
IPluginModelFactory factory = model.getPluginFactory();
111         
112         String JavaDoc cid = getStringOption("categoryId"); //$NON-NLS-1$
113

114         createCategory(extension, cid);
115         String JavaDoc fullClassName = getStringOption(KEY_PACKAGE_NAME)+"."+getStringOption("wizardClassName"); //$NON-NLS-1$ //$NON-NLS-2$
116

117         IPluginElement viewElement = factory.createElement(extension);
118         viewElement.setName("wizard"); //$NON-NLS-1$
119
viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
120
viewElement.setAttribute("name", getStringOption("wizardName")); //$NON-NLS-1$ //$NON-NLS-2$
121
viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
122
viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
123
viewElement.setAttribute("category", cid); //$NON-NLS-1$
124
extension.add(viewElement);
125         if (!extension.isInTheModel())
126             plugin.add(extension);
127     }
128
129     private void createCategory(IPluginExtension extension, String JavaDoc id) throws CoreException {
130         IPluginObject [] elements = extension.getChildren();
131         for (int i=0; i<elements.length; i++) {
132             IPluginElement element = (IPluginElement)elements[i];
133             if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
134
IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
135
if (att!=null) {
136                     String JavaDoc cid = att.getValue();
137                     if (cid!=null && cid.equals(id))
138                         return;
139                 }
140             }
141         }
142         IPluginElement categoryElement = model.getFactory().createElement(extension);
143         categoryElement.setName("category"); //$NON-NLS-1$
144
categoryElement.setAttribute("name", getStringOption("categoryName")); //$NON-NLS-1$ //$NON-NLS-2$
145
categoryElement.setAttribute("id", id); //$NON-NLS-1$
146
extension.add(categoryElement);
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
151      */

152     public String JavaDoc[] getNewFiles() {
153         return new String JavaDoc[] {"icons/"}; //$NON-NLS-1$
154
}
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
158      */

159     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
160         String JavaDoc packageName = super.getFormattedPackageName(id);
161         if (packageName.length() != 0 )
162             return packageName + ".wizards"; //$NON-NLS-1$
163
return "wizards"; //$NON-NLS-1$
164
}
165 }
166
Popular Tags