KickJava   Java API By Example, From Geeks To Geeks.

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


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.BooleanOption;
32 import org.eclipse.pde.ui.templates.TemplateOption;
33
34 public class ViewTemplate extends PDETemplateSection {
35     private BooleanOption addToPerspective;
36     /**
37      * Constructor for HelloWorldTemplate.
38      */

39     public ViewTemplate() {
40         setPageCount(2);
41         createOptions();
42     }
43     
44     public String JavaDoc getSectionId() {
45         return "view"; //$NON-NLS-1$
46
}
47     /*
48      * @see ITemplateSection#getNumberOfWorkUnits()
49      */

50     public int getNumberOfWorkUnits() {
51         return super.getNumberOfWorkUnits()+1;
52     }
53     
54     private void createOptions() {
55         // first page
56
addOption(KEY_PACKAGE_NAME, PDEUIMessages.ViewTemplate_packageName, (String JavaDoc)null, 0);
57         addOption("className", PDEUIMessages.ViewTemplate_className, "SampleView", 0); //$NON-NLS-1$ //$NON-NLS-2$
58
addOption("viewName", PDEUIMessages.ViewTemplate_name, PDEUIMessages.ViewTemplate_defaultName, 0); //$NON-NLS-1$
59
addOption("viewCategoryId", PDEUIMessages.ViewTemplate_categoryId, (String JavaDoc)null, 0); //$NON-NLS-1$
60
addOption("viewCategoryName", PDEUIMessages.ViewTemplate_categoryName, PDEUIMessages.ViewTemplate_defaultCategoryName, 0); //$NON-NLS-1$
61
addOption("viewType", PDEUIMessages.ViewTemplate_select, //$NON-NLS-1$
62
new String JavaDoc [][] {
63                         {"tableViewer", PDEUIMessages.ViewTemplate_table}, //$NON-NLS-1$
64
{"treeViewer", PDEUIMessages.ViewTemplate_tree}}, //$NON-NLS-1$
65
"tableViewer", 0); //$NON-NLS-1$
66
addToPerspective = (BooleanOption)addOption("addToPerspective",PDEUIMessages.ViewTemplate_addToPerspective,true,0); //$NON-NLS-1$
67
// second page
68
addOption("doubleClick", PDEUIMessages.ViewTemplate_doubleClick, true, 1); //$NON-NLS-1$
69
addOption("popup", PDEUIMessages.ViewTemplate_popup, true, 1); //$NON-NLS-1$
70
addOption("localToolbar", PDEUIMessages.ViewTemplate_toolbar, true, 1); //$NON-NLS-1$
71
addOption("localPulldown", PDEUIMessages.ViewTemplate_pulldown, true, 1); //$NON-NLS-1$
72
addOption("sorter", PDEUIMessages.ViewTemplate_sorting, true, 1); //$NON-NLS-1$
73
//addOption("filter", PDEPlugin.getResourceString(NL_FILTER), true, lists[1]);
74
}
75
76     protected void initializeFields(IFieldData data) {
77         // In a new project wizard, we don't know this yet - the
78
// model has not been created
79
String JavaDoc id = data.getId();
80         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
81         initializeOption("viewCategoryId", id); //$NON-NLS-1$
82
}
83     public void initializeFields(IPluginModelBase model) {
84         // In the new extension wizard, the model exists so
85
// we can initialize directly from it
86
String JavaDoc pluginId = model.getPluginBase().getId();
87         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
88         initializeOption("viewCategoryId", pluginId); //$NON-NLS-1$
89
}
90     
91     public boolean isDependentOnParentWizard() {
92         return true;
93     }
94     
95     public void addPages(Wizard wizard) {
96         WizardPage page0 = createPage(0, IHelpContextIds.TEMPLATE_VIEW);
97         page0.setTitle(PDEUIMessages.ViewTemplate_title0);
98         page0.setDescription(PDEUIMessages.ViewTemplate_desc0);
99         wizard.addPage(page0);
100         
101         WizardPage page1 = createPage(1, IHelpContextIds.TEMPLATE_VIEW);
102         page1.setTitle(PDEUIMessages.ViewTemplate_title1);
103         page1.setDescription(PDEUIMessages.ViewTemplate_desc1);
104         wizard.addPage(page1);
105         markPagesAdded();
106     }
107
108     public void validateOptions(TemplateOption source) {
109         if (source.isRequired() && source.isEmpty()) {
110             flagMissingRequiredOption(source);
111         } else {
112             validateContainerPage(source);
113         }
114     }
115
116     private void validateContainerPage(TemplateOption source) {
117         TemplateOption[] allPageOptions = getOptions(0);
118         for (int i = 0; i < allPageOptions.length; i++) {
119             TemplateOption nextOption = allPageOptions[i];
120             if (nextOption.isRequired() && nextOption.isEmpty()) {
121                 flagMissingRequiredOption(nextOption);
122                 return;
123             }
124         }
125         resetPageState();
126     }
127     
128     public String JavaDoc getUsedExtensionPoint() {
129         return "org.eclipse.ui.views"; //$NON-NLS-1$
130
}
131     
132     protected void updateModel(IProgressMonitor monitor) throws CoreException {
133         IPluginBase plugin = model.getPluginBase();
134         IPluginExtension extension = createExtension("org.eclipse.ui.views", true); //$NON-NLS-1$
135
IPluginModelFactory factory = model.getPluginFactory();
136         
137         String JavaDoc cid = getStringOption("viewCategoryId"); //$NON-NLS-1$
138

139         createCategory(extension, cid);
140         String JavaDoc fullClassName = getStringOption(KEY_PACKAGE_NAME)+"."+getStringOption("className"); //$NON-NLS-1$ //$NON-NLS-2$
141

142         IPluginElement viewElement = factory.createElement(extension);
143         viewElement.setName("view"); //$NON-NLS-1$
144
viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
145
viewElement.setAttribute("name", getStringOption("viewName")); //$NON-NLS-1$ //$NON-NLS-2$
146
viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
147

148         viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
149
viewElement.setAttribute("category", cid); //$NON-NLS-1$
150
extension.add(viewElement);
151         if (!extension.isInTheModel())
152             plugin.add(extension);
153             
154         if (addToPerspective.isSelected()) {
155             IPluginExtension perspectiveExtension =
156                 createExtension("org.eclipse.ui.perspectiveExtensions", true); //$NON-NLS-1$
157

158             IPluginElement perspectiveElement = factory.createElement(perspectiveExtension);
159             perspectiveElement.setName("perspectiveExtension"); //$NON-NLS-1$
160
perspectiveElement.setAttribute(
161                 "targetID", //$NON-NLS-1$
162
"org.eclipse.ui.resourcePerspective"); //$NON-NLS-1$
163

164             IPluginElement view = factory.createElement(perspectiveElement);
165             view.setName("view"); //$NON-NLS-1$
166
view.setAttribute("id", fullClassName); //$NON-NLS-1$
167
view.setAttribute("relative", "org.eclipse.ui.views.TaskList"); //$NON-NLS-1$ //$NON-NLS-2$
168
view.setAttribute("relationship","right"); //$NON-NLS-1$ //$NON-NLS-2$
169
view.setAttribute("ratio", "0.5"); //$NON-NLS-1$ //$NON-NLS-2$
170
perspectiveElement.add(view);
171
172             perspectiveExtension.add(perspectiveElement);
173             if (!perspectiveExtension.isInTheModel())
174                 plugin.add(perspectiveExtension);
175         }
176     }
177
178     private void createCategory(IPluginExtension extension, String JavaDoc id) throws CoreException {
179         IPluginObject [] elements = extension.getChildren();
180         for (int i=0; i<elements.length; i++) {
181             IPluginElement element = (IPluginElement)elements[i];
182             if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
183
IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
184
if (att!=null) {
185                     String JavaDoc cid = att.getValue();
186                     if (cid!=null && cid.equals(id))
187                         return;
188                 }
189             }
190         }
191         IPluginElement categoryElement = model.getFactory().createElement(extension);
192         categoryElement.setName("category"); //$NON-NLS-1$
193
categoryElement.setAttribute("name", getStringOption("viewCategoryName")); //$NON-NLS-1$ //$NON-NLS-2$
194
categoryElement.setAttribute("id", id); //$NON-NLS-1$
195
extension.add(categoryElement);
196     }
197     
198     /* (non-Javadoc)
199      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
200      */

201     public String JavaDoc[] getNewFiles() {
202         return new String JavaDoc[] {"icons/"}; //$NON-NLS-1$
203
}
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
207      */

208     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
209         ArrayList JavaDoc result = new ArrayList JavaDoc();
210         if (schemaVersion != null)
211             result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
212
result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
213
return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]);
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
218      */

219     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
220         String JavaDoc packageName = super.getFormattedPackageName(id);
221         if (packageName.length() != 0)
222             return packageName + ".views"; //$NON-NLS-1$
223
return "views"; //$NON-NLS-1$
224
}
225 }
226
Popular Tags