KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.IPluginReference;
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
28 public class HelloRCPTemplate extends PDETemplateSection {
29     
30     public static final String JavaDoc KEY_PERSPECTIVE_NAME = "perspectiveName"; //$NON-NLS-1$
31
public static final String JavaDoc KEY_APPLICATION_CLASS = "applicationClass"; //$NON-NLS-1$
32
public static final String JavaDoc KEY_APPLICATION_ID = "applicationID"; //$NON-NLS-1$
33
public static final String JavaDoc KEY_WINDOW_TITLE = "windowTitle"; //$NON-NLS-1$
34

35     public HelloRCPTemplate() {
36         setPageCount(1);
37         createOptions();
38     }
39     
40     public void addPages(Wizard wizard) {
41         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_RCP_MAIL);
42         page.setTitle(PDEUIMessages.HelloRCPTemplate_title);
43         page.setDescription(PDEUIMessages.HelloRCPTemplate_desc);
44         wizard.addPage(page);
45         markPagesAdded();
46     }
47
48     
49     private void createOptions() {
50         addOption(KEY_WINDOW_TITLE, PDEUIMessages.HelloRCPTemplate_windowTitle, "Hello RCP", 0); //$NON-NLS-1$
51

52         addOption(KEY_APPLICATION_ID, PDEUIMessages.HelloRCPTemplate_appId, "application", 0); //$NON-NLS-1$
53

54         addOption(KEY_PACKAGE_NAME, PDEUIMessages.MailTemplate_packageName, (String JavaDoc) null, 0);
55         
56         addOption(KEY_APPLICATION_CLASS, PDEUIMessages.HelloRCPTemplate_appClass, "Application", 0); //$NON-NLS-1$
57

58         addOption(KEY_PERSPECTIVE_NAME, PDEUIMessages.HelloRCPTemplate_perspective, (String JavaDoc) null, 0);
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 packageName = getFormattedPackageName(data.getId());
65         initializeOption(KEY_PACKAGE_NAME, packageName);
66         
67         int index = packageName.lastIndexOf('.');
68         String JavaDoc name = packageName.substring(index + 1) + " Perspective"; //$NON-NLS-1$
69
initializeOption(KEY_PERSPECTIVE_NAME, Character.toUpperCase(name.charAt(0)) + name.substring(1));
70     }
71     
72     public void initializeFields(IPluginModelBase model) {
73         String JavaDoc packageName = getFormattedPackageName(model.getPluginBase().getId());
74         initializeOption(KEY_PACKAGE_NAME, packageName);
75
76         int index = packageName.lastIndexOf('.');
77         String JavaDoc name = packageName.substring(index + 1) + " Perspective"; //$NON-NLS-1$
78
initializeOption(KEY_PERSPECTIVE_NAME, Character.toUpperCase(name.charAt(0)) + name.substring(1));
79     }
80     
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
85      */

86     public String JavaDoc getSectionId() {
87         return "helloRCP"; //$NON-NLS-1$
88
}
89
90     /* (non-Javadoc)
91      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#validateOptions(org.eclipse.pde.ui.templates.TemplateOption)
92      */

93     public void validateOptions(TemplateOption source) {
94         if (source.isRequired() && source.isEmpty()) {
95             flagMissingRequiredOption(source);
96         } else {
97             validateContainerPage(source);
98         }
99     }
100     
101     private void validateContainerPage(TemplateOption source) {
102         TemplateOption[] allPageOptions = getOptions(0);
103         for (int i = 0; i < allPageOptions.length; i++) {
104             TemplateOption nextOption = allPageOptions[i];
105             if (nextOption.isRequired() && nextOption.isEmpty()) {
106                 flagMissingRequiredOption(nextOption);
107                 return;
108             }
109         }
110         resetPageState();
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#updateModel(org.eclipse.core.runtime.IProgressMonitor)
115      */

116     protected void updateModel(IProgressMonitor monitor) throws CoreException {
117         createApplicationExtension();
118         createPerspectiveExtension();
119     }
120     
121     private void createApplicationExtension() throws CoreException {
122         IPluginBase plugin = model.getPluginBase();
123         
124         IPluginExtension extension = createExtension("org.eclipse.core.runtime.applications", true); //$NON-NLS-1$
125
extension.setId(getStringOption(KEY_APPLICATION_ID));
126         
127         IPluginElement element = model.getPluginFactory().createElement(extension);
128         element.setName("application"); //$NON-NLS-1$
129
extension.add(element);
130         
131         IPluginElement run = model.getPluginFactory().createElement(element);
132         run.setName("run"); //$NON-NLS-1$
133
run.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_APPLICATION_CLASS)); //$NON-NLS-1$ //$NON-NLS-2$
134
element.add(run);
135         
136         if (!extension.isInTheModel())
137             plugin.add(extension);
138     }
139
140     private void createPerspectiveExtension() throws CoreException {
141         IPluginBase plugin = model.getPluginBase();
142         
143         IPluginExtension extension = createExtension("org.eclipse.ui.perspectives", true); //$NON-NLS-1$
144
IPluginElement element = model.getPluginFactory().createElement(extension);
145         element.setName("perspective"); //$NON-NLS-1$
146
element.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + ".Perspective"); //$NON-NLS-1$ //$NON-NLS-2$
147
element.setAttribute("name", getStringOption(KEY_PERSPECTIVE_NAME)); //$NON-NLS-1$
148
element.setAttribute("id", plugin.getId() + ".perspective"); //$NON-NLS-1$ //$NON-NLS-2$
149
extension.add(element);
150         
151         if (!extension.isInTheModel())
152             plugin.add(extension);
153     }
154     
155     /* (non-Javadoc)
156      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
157      */

158     public String JavaDoc getUsedExtensionPoint() {
159         return null;
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
164      */

165     public boolean isDependentOnParentWizard() {
166         return true;
167     }
168     
169     /* (non-Javadoc)
170      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getNumberOfWorkUnits()
171      */

172     public int getNumberOfWorkUnits() {
173         return super.getNumberOfWorkUnits() + 1;
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
178      */

179     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
180         IPluginReference[] dep = new IPluginReference[2];
181         dep[0] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
182
dep[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
183
return dep;
184     }
185     
186 }
187
Popular Tags