KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.wizard.Wizard;
16 import org.eclipse.jface.wizard.WizardPage;
17 import org.eclipse.pde.core.plugin.IPluginBase;
18 import org.eclipse.pde.core.plugin.IPluginElement;
19 import org.eclipse.pde.core.plugin.IPluginExtension;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.core.plugin.IPluginModelFactory;
22 import org.eclipse.pde.internal.ui.IHelpContextIds;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.ui.IFieldData;
25 import org.eclipse.pde.ui.templates.TemplateOption;
26
27 public class HelloWorldTemplate extends PDETemplateSection {
28     public static final String JavaDoc KEY_CLASS_NAME = "className"; //$NON-NLS-1$
29
public static final String JavaDoc KEY_MESSAGE = "message"; //$NON-NLS-1$
30
public static final String JavaDoc CLASS_NAME = "SampleAction"; //$NON-NLS-1$
31

32     /**
33      * Constructor for HelloWorldTemplate.
34      */

35     public HelloWorldTemplate() {
36         setPageCount(1);
37         createOptions();
38     }
39
40     public String JavaDoc getSectionId() {
41         return "helloWorld"; //$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         addOption(
52             KEY_PACKAGE_NAME,
53             PDEUIMessages.HelloWorldTemplate_packageName,
54             (String JavaDoc) null,
55             0);
56         addOption(
57             KEY_CLASS_NAME,
58             PDEUIMessages.HelloWorldTemplate_className,
59             CLASS_NAME,
60             0);
61         addOption(
62             KEY_MESSAGE,
63             PDEUIMessages.HelloWorldTemplate_messageText,
64             PDEUIMessages.HelloWorldTemplate_defaultMessage,
65             0);
66     }
67
68     public void addPages(Wizard wizard) {
69         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_HELLO_WORLD);
70         page.setTitle(PDEUIMessages.HelloWorldTemplate_title);
71         page.setDescription(PDEUIMessages.HelloWorldTemplate_desc);
72         wizard.addPage(page);
73         markPagesAdded();
74     }
75
76     public void validateOptions(TemplateOption source) {
77         if (source.isRequired() && source.isEmpty()) {
78             flagMissingRequiredOption(source);
79         } else {
80             validateContainerPage(source);
81         }
82     }
83
84     private void validateContainerPage(TemplateOption source) {
85         TemplateOption[] allPageOptions = getOptions(0);
86         for (int i = 0; i < allPageOptions.length; i++) {
87             TemplateOption nextOption = allPageOptions[i];
88             if (nextOption.isRequired() && nextOption.isEmpty()) {
89                 flagMissingRequiredOption(nextOption);
90                 return;
91             }
92         }
93         resetPageState();
94     }
95
96
97     public boolean isDependentOnParentWizard() {
98         return true;
99     }
100
101     protected void initializeFields(IFieldData data) {
102         // In a new project wizard, we don't know this yet - the
103
// model has not been created
104
String JavaDoc id = data.getId();
105         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
106     }
107     public void initializeFields(IPluginModelBase model) {
108         // In the new extension wizard, the model exists so
109
// we can initialize directly from it
110
String JavaDoc pluginId = model.getPluginBase().getId();
111         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
112     }
113
114     public String JavaDoc getUsedExtensionPoint() {
115         return "org.eclipse.ui.actionSets"; //$NON-NLS-1$
116
}
117
118     protected void updateModel(IProgressMonitor monitor) throws CoreException {
119         IPluginBase plugin = model.getPluginBase();
120         IPluginExtension extension = createExtension("org.eclipse.ui.actionSets", true); //$NON-NLS-1$
121
IPluginModelFactory factory = model.getPluginFactory();
122
123         IPluginElement setElement = factory.createElement(extension);
124         setElement.setName("actionSet"); //$NON-NLS-1$
125
setElement.setAttribute("id", plugin.getId() + ".actionSet"); //$NON-NLS-1$ //$NON-NLS-2$
126
setElement.setAttribute("label", PDEUIMessages.HelloWorldTemplate_sampleActionSet); //$NON-NLS-1$
127
setElement.setAttribute("visible", "true"); //$NON-NLS-1$ //$NON-NLS-2$
128

129         IPluginElement menuElement = factory.createElement(setElement);
130         menuElement.setName("menu"); //$NON-NLS-1$
131
menuElement.setAttribute("label", PDEUIMessages.HelloWorldTemplate_sampleMenu); //$NON-NLS-1$
132
menuElement.setAttribute("id", "sampleMenu"); //$NON-NLS-1$ //$NON-NLS-2$
133

134         IPluginElement groupElement = factory.createElement(menuElement);
135         groupElement.setName("separator"); //$NON-NLS-1$
136
groupElement.setAttribute("name", "sampleGroup"); //$NON-NLS-1$ //$NON-NLS-2$
137
menuElement.add(groupElement);
138         setElement.add(menuElement);
139
140         String JavaDoc fullClassName =
141             getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_CLASS_NAME); //$NON-NLS-1$
142

143         IPluginElement actionElement = factory.createElement(setElement);
144         actionElement.setName("action"); //$NON-NLS-1$
145
actionElement.setAttribute("id", fullClassName); //$NON-NLS-1$
146
actionElement.setAttribute("label", PDEUIMessages.HelloWorldTemplate_sampleAction); //$NON-NLS-1$
147
actionElement.setAttribute("menubarPath", "sampleMenu/sampleGroup"); //$NON-NLS-1$ //$NON-NLS-2$
148
actionElement.setAttribute("toolbarPath", "sampleGroup"); //$NON-NLS-1$ //$NON-NLS-2$
149
actionElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
150
actionElement.setAttribute("tooltip", PDEUIMessages.HelloWorldTemplate_defaultMessage); //$NON-NLS-1$
151
actionElement.setAttribute("class", fullClassName); //$NON-NLS-1$
152
setElement.add(actionElement);
153         extension.add(setElement);
154         if (!extension.isInTheModel())
155             plugin.add(extension);
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.pde.ui.templates.ITemplateSection#getFoldersToInclude()
160      */

161     public String JavaDoc[] getNewFiles() {
162         return new String JavaDoc[] {"icons/"}; //$NON-NLS-1$
163
}
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
167      */

168     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
169         String JavaDoc packageName = super.getFormattedPackageName(id);
170         if (packageName.length() != 0)
171             return packageName + ".actions"; //$NON-NLS-1$
172
return "actions"; //$NON-NLS-1$
173
}
174 }
175
Popular Tags