KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > 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.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 import org.eclipse.pde.ui.templates.BooleanOption;
34
35 public class ViewTemplate extends PDETemplateSection {
36     private BooleanOption addToPerspective;
37     /**
38      * Constructor for HelloWorldTemplate.
39      */

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

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

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

123         IPluginElement viewElement = factory.createElement(extension);
124         viewElement.setName("view"); //$NON-NLS-1$
125
viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
126
viewElement.setAttribute("name", getStringOption("viewName")); //$NON-NLS-1$ //$NON-NLS-2$
127
viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
128

129         viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
130
viewElement.setAttribute("category", cid); //$NON-NLS-1$
131
extension.add(viewElement);
132         if (!extension.isInTheModel())
133             plugin.add(extension);
134             
135         if (addToPerspective.isSelected()) {
136             IPluginExtension perspectiveExtension =
137                 createExtension("org.eclipse.ui.perspectiveExtensions", true); //$NON-NLS-1$
138

139             IPluginElement perspectiveElement = factory.createElement(perspectiveExtension);
140             perspectiveElement.setName("perspectiveExtension"); //$NON-NLS-1$
141
perspectiveElement.setAttribute(
142                 "targetID", //$NON-NLS-1$
143
"org.eclipse.ui.resourcePerspective"); //$NON-NLS-1$
144

145             IPluginElement view = factory.createElement(perspectiveElement);
146             view.setName("view"); //$NON-NLS-1$
147
view.setAttribute("id", fullClassName); //$NON-NLS-1$
148
view.setAttribute("relative", "org.eclipse.ui.views.TaskList"); //$NON-NLS-1$ //$NON-NLS-2$
149
view.setAttribute("relationship","right"); //$NON-NLS-1$ //$NON-NLS-2$
150
view.setAttribute("ratio", "0.5"); //$NON-NLS-1$ //$NON-NLS-2$
151
perspectiveElement.add(view);
152
153             perspectiveExtension.add(perspectiveElement);
154             if (!perspectiveExtension.isInTheModel())
155                 plugin.add(perspectiveExtension);
156         }
157     }
158
159     private void createCategory(IPluginExtension extension, String JavaDoc id) throws CoreException {
160         IPluginObject [] elements = extension.getChildren();
161         for (int i=0; i<elements.length; i++) {
162             IPluginElement element = (IPluginElement)elements[i];
163             if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
164
IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
165
if (att!=null) {
166                     String JavaDoc cid = att.getValue();
167                     if (cid!=null && cid.equals(id))
168                         return;
169                 }
170             }
171         }
172         IPluginElement categoryElement = model.getFactory().createElement(extension);
173         categoryElement.setName("category"); //$NON-NLS-1$
174
categoryElement.setAttribute("name", getStringOption("viewCategoryName")); //$NON-NLS-1$ //$NON-NLS-2$
175
categoryElement.setAttribute("id", id); //$NON-NLS-1$
176
extension.add(categoryElement);
177     }
178     
179     /* (non-Javadoc)
180      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
181      */

182     public String JavaDoc[] getNewFiles() {
183         return new String JavaDoc[] {"icons/"}; //$NON-NLS-1$
184
}
185     
186     /* (non-Javadoc)
187      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
188      */

189     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
190         ArrayList JavaDoc result = new ArrayList JavaDoc();
191         if (schemaVersion != null)
192             result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
193
result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
194
return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]);
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
199      */

200     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
201         String JavaDoc packageName = super.getFormattedPackageName(id);
202         if (packageName.length() != 0)
203             return packageName + ".views"; //$NON-NLS-1$
204
return "views"; //$NON-NLS-1$
205
}
206 }
207
Popular Tags