KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > MultiPageEditorTemplate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.templates.ide;
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.core.plugin.IPluginReference;
23 import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
24 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
25 import org.eclipse.pde.internal.ui.templates.PluginReference;
26 import org.eclipse.pde.ui.IFieldData;
27 import org.eclipse.pde.ui.templates.TemplateOption;
28
29 public class MultiPageEditorTemplate extends BaseEditorTemplate {
30     /**
31      * Constructor for MultiPageEditorTemplate.
32      */

33     public MultiPageEditorTemplate() {
34         setPageCount(1);
35         createOptions();
36     }
37
38     public String JavaDoc getSectionId() {
39         return "multiPageEditor"; //$NON-NLS-1$
40
}
41     
42     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
43         if (schemaVersion != null) {
44             IPluginReference[] dep = new IPluginReference[6];
45             dep[0] = new PluginReference("org.eclipse.jface.text", null, 0); //$NON-NLS-1$
46
dep[1] = new PluginReference("org.eclipse.core.resources", null, 0); //$NON-NLS-1$
47
dep[2] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
48
dep[3] = new PluginReference("org.eclipse.ui.editors", null, 0); //$NON-NLS-1$
49
dep[4] = new PluginReference("org.eclipse.ui.ide", null, 0); //$NON-NLS-1$
50
dep[5] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
51
return dep;
52         }
53         return super.getDependencies(schemaVersion);
54     }
55     
56     /*
57      * @see ITemplateSection#getNumberOfWorkUnits()
58      */

59     public int getNumberOfWorkUnits() {
60         return super.getNumberOfWorkUnits() + 1;
61     }
62
63     private void createOptions() {
64         // first page
65
addOption(
66             KEY_PACKAGE_NAME,
67             PDETemplateMessages.MultiPageEditorTemplate_packageName,
68             (String JavaDoc) null,
69             0);
70         addOption(
71             "editorClassName", //$NON-NLS-1$
72
PDETemplateMessages.MultiPageEditorTemplate_className,
73             "MultiPageEditor", //$NON-NLS-1$
74
0);
75         addOption(
76             "contributorClassName", //$NON-NLS-1$
77
PDETemplateMessages.MultiPageEditorTemplate_contributor,
78             "MultiPageEditorContributor", //$NON-NLS-1$
79
0);
80         addOption(
81             "editorName", //$NON-NLS-1$
82
PDETemplateMessages.MultiPageEditorTemplate_editorName,
83             PDETemplateMessages.MultiPageEditorTemplate_defaultEditorName,
84             0);
85         addOption(
86             "extensions", //$NON-NLS-1$
87
PDETemplateMessages.MultiPageEditorTemplate_extensions,
88             "mpe", //$NON-NLS-1$
89
0);
90     }
91
92     protected void initializeFields(IFieldData data) {
93         // In a new project wizard, we don't know this yet - the
94
// model has not been created
95
String JavaDoc id = data.getId();
96         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
97     }
98     public void initializeFields(IPluginModelBase model) {
99         // In the new extension wizard, the model exists so
100
// we can initialize directly from it
101
String JavaDoc pluginId = model.getPluginBase().getId();
102         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
103     }
104
105     public boolean isDependentOnParentWizard() {
106         return true;
107     }
108
109     public void addPages(Wizard wizard) {
110         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_MULTIPAGE_EDITOR);
111         page.setTitle(PDETemplateMessages.MultiPageEditorTemplate_title);
112         page.setDescription(PDETemplateMessages.MultiPageEditorTemplate_desc);
113         wizard.addPage(page);
114         markPagesAdded();
115     }
116
117     public void validateOptions(TemplateOption source) {
118         if (source.isRequired() && source.isEmpty()) {
119             flagMissingRequiredOption(source);
120         } else {
121             validateContainerPage(source);
122         }
123     }
124
125     private void validateContainerPage(TemplateOption source) {
126         TemplateOption[] allPageOptions = getOptions(0);
127         for (int i = 0; i < allPageOptions.length; i++) {
128             TemplateOption nextOption = allPageOptions[i];
129             if (nextOption.isRequired() && nextOption.isEmpty()) {
130                 flagMissingRequiredOption(nextOption);
131                 return;
132             }
133         }
134         resetPageState();
135     }
136
137     protected void updateModel(IProgressMonitor monitor) throws CoreException {
138         IPluginBase plugin = model.getPluginBase();
139         IPluginExtension extension = createExtension("org.eclipse.ui.editors", true); //$NON-NLS-1$
140
IPluginModelFactory factory = model.getPluginFactory();
141
142         String JavaDoc editorClassName =
143             getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption("editorClassName"); //$NON-NLS-1$ //$NON-NLS-2$
144
String JavaDoc contributorClassName =
145             getStringOption(KEY_PACKAGE_NAME)
146                 + "." //$NON-NLS-1$
147
+ getStringOption("contributorClassName"); //$NON-NLS-1$
148

149         IPluginElement editorElement = factory.createElement(extension);
150         editorElement.setName("editor"); //$NON-NLS-1$
151
editorElement.setAttribute("id", editorClassName); //$NON-NLS-1$
152
editorElement.setAttribute("name", getStringOption("editorName")); //$NON-NLS-1$ //$NON-NLS-2$
153
editorElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
154
editorElement.setAttribute("extensions", getStringOption("extensions")); //$NON-NLS-1$ //$NON-NLS-2$
155

156         editorElement.setAttribute("class", editorClassName); //$NON-NLS-1$
157
editorElement.setAttribute("contributorClass", contributorClassName); //$NON-NLS-1$
158
extension.add(editorElement);
159         if (!extension.isInTheModel())
160             plugin.add(extension);
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
165      */

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