KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > 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.wizards.templates;
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.IHelpContextIds;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.ui.IFieldData;
31 import org.eclipse.pde.ui.templates.TemplateOption;
32
33 public class NewWizardTemplate extends PDETemplateSection {
34     public NewWizardTemplate() {
35         setPageCount(1);
36         createOptions();
37     }
38     
39     public String JavaDoc getSectionId() {
40         return "newWizard"; //$NON-NLS-1$
41
}
42     /*
43      * @see ITemplateSection#getNumberOfWorkUnits()
44      */

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

79     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
80         ArrayList JavaDoc result = new ArrayList JavaDoc();
81         result.add(new PluginReference("org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
82
result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
83
if (schemaVersion != null) {
84             result.add(new PluginReference("org.eclipse.ui.ide", null, 0)); //$NON-NLS-1$
85
result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
86
}
87         return (IPluginReference[])result.toArray(new IPluginReference[result.size()]);
88     }
89     
90     public boolean isDependentOnParentWizard() {
91         return true;
92     }
93     
94     public void addPages(Wizard wizard) {
95         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_NEW_WIZARD);
96         page.setTitle(PDEUIMessages.NewWizardTemplate_title);
97         page.setDescription(PDEUIMessages.NewWizardTemplate_desc);
98         wizard.addPage(page);
99         markPagesAdded();
100     }
101
102     public void validateOptions(TemplateOption source) {
103         if (source.isRequired() && source.isEmpty()) {
104             flagMissingRequiredOption(source);
105         } else {
106             validateContainerPage(source);
107         }
108     }
109
110     private void validateContainerPage(TemplateOption source) {
111         TemplateOption[] allPageOptions = getOptions(0);
112         for (int i = 0; i < allPageOptions.length; i++) {
113             TemplateOption nextOption = allPageOptions[i];
114             if (nextOption.isRequired() && nextOption.isEmpty()) {
115                 flagMissingRequiredOption(nextOption);
116                 return;
117             }
118         }
119         resetPageState();
120     }
121     
122     public String JavaDoc getUsedExtensionPoint() {
123         return "org.eclipse.ui.newWizards"; //$NON-NLS-1$
124
}
125     
126     protected void updateModel(IProgressMonitor monitor) throws CoreException {
127         IPluginBase plugin = model.getPluginBase();
128         IPluginExtension extension = createExtension("org.eclipse.ui.newWizards", true); //$NON-NLS-1$
129
IPluginModelFactory factory = model.getPluginFactory();
130         
131         String JavaDoc cid = getStringOption("categoryId"); //$NON-NLS-1$
132

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

136         IPluginElement viewElement = factory.createElement(extension);
137         viewElement.setName("wizard"); //$NON-NLS-1$
138
viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
139
viewElement.setAttribute("name", getStringOption("wizardName")); //$NON-NLS-1$ //$NON-NLS-2$
140
viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
141
viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
142
viewElement.setAttribute("category", cid); //$NON-NLS-1$
143
extension.add(viewElement);
144         if (!extension.isInTheModel())
145             plugin.add(extension);
146     }
147
148     private void createCategory(IPluginExtension extension, String JavaDoc id) throws CoreException {
149         IPluginObject [] elements = extension.getChildren();
150         for (int i=0; i<elements.length; i++) {
151             IPluginElement element = (IPluginElement)elements[i];
152             if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
153
IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
154
if (att!=null) {
155                     String JavaDoc cid = att.getValue();
156                     if (cid!=null && cid.equals(id))
157                         return;
158                 }
159             }
160         }
161         IPluginElement categoryElement = model.getFactory().createElement(extension);
162         categoryElement.setName("category"); //$NON-NLS-1$
163
categoryElement.setAttribute("name", getStringOption("categoryName")); //$NON-NLS-1$ //$NON-NLS-2$
164
categoryElement.setAttribute("id", id); //$NON-NLS-1$
165
extension.add(categoryElement);
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
170      */

171     public String JavaDoc[] getNewFiles() {
172         return new String JavaDoc[] {"icons/"}; //$NON-NLS-1$
173
}
174     
175     /* (non-Javadoc)
176      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
177      */

178     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
179         String JavaDoc packageName = super.getFormattedPackageName(id);
180         if (packageName.length() != 0 )
181             return packageName + ".wizards"; //$NON-NLS-1$
182
return "wizards"; //$NON-NLS-1$
183
}
184 }
185
Popular Tags