KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jface.wizard.Wizard;
17 import org.eclipse.jface.wizard.WizardPage;
18 import org.eclipse.pde.core.plugin.IPluginBase;
19 import org.eclipse.pde.core.plugin.IPluginElement;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.eclipse.pde.core.plugin.IPluginModelFactory;
23 import org.eclipse.pde.core.plugin.IPluginReference;
24 import org.eclipse.pde.internal.ui.IHelpContextIds;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.ui.IFieldData;
27 import org.eclipse.pde.ui.templates.TemplateOption;
28
29 public class EditorTemplate extends BaseEditorTemplate {
30     public static final String JavaDoc EDITOR_CLASS_NAME = "editorClass"; //$NON-NLS-1$
31
public static final String JavaDoc EDITOR_NAME = "editorName"; //$NON-NLS-1$
32
public static final String JavaDoc EXTENSIONS = "extensions"; //$NON-NLS-1$
33
/**
34      * Constructor for EditorTemplate.
35      */

36     public EditorTemplate() {
37         setPageCount(1);
38         createOptions();
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
43      */

44     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
45         if (schemaVersion != null) {
46             IPluginReference[] dep = new IPluginReference[5];
47             dep[0] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
48
dep[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
49
dep[2] = new PluginReference("org.eclipse.jface.text", null, 0); //$NON-NLS-1$
50
dep[3] = new PluginReference("org.eclipse.ui.editors", null, 0); //$NON-NLS-1$
51
dep[4] = new PluginReference("org.eclipse.ui.workbench.texteditor", null, 0); //$NON-NLS-1$
52
return dep;
53         }
54         return super.getDependencies(schemaVersion);
55     }
56
57     public void addPages(Wizard wizard) {
58         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_EDITOR);
59         page.setTitle(PDEUIMessages.EditorTemplate_title);
60         page.setDescription(PDEUIMessages.EditorTemplate_desc);
61         wizard.addPage(page);
62         markPagesAdded();
63     }
64
65     private void createOptions() {
66         // first page
67
addOption(
68             KEY_PACKAGE_NAME,
69             PDEUIMessages.EditorTemplate_packageName,
70             (String JavaDoc) null,
71             0);
72         addOption(
73             EDITOR_CLASS_NAME,
74             PDEUIMessages.EditorTemplate_editorClass,
75             "XMLEditor", //$NON-NLS-1$
76
0);
77         addOption(
78             EDITOR_NAME,
79             PDEUIMessages.EditorTemplate_editorName,
80             PDEUIMessages.EditorTemplate_defaultEditorName,
81             0);
82         addOption(
83             EXTENSIONS,
84             PDEUIMessages.EditorTemplate_fileExtension,
85             "xml", //$NON-NLS-1$
86
0);
87     }
88
89     public String JavaDoc getSectionId() {
90         return "editor"; //$NON-NLS-1$
91
}
92     /*
93      * @see ITemplateSection#getNumberOfWorkUnits()
94      */

95     public int getNumberOfWorkUnits() {
96         return super.getNumberOfWorkUnits() + 1;
97     }
98
99     protected void initializeFields(IFieldData data) {
100         // In a new project wizard, we don't know this yet - the
101
// model has not been created
102
String JavaDoc id = data.getId();
103         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
104     }
105     public void initializeFields(IPluginModelBase model) {
106         // In the new extension wizard, the model exists so
107
// we can initialize directly from it
108
String JavaDoc pluginId = model.getPluginBase().getId();
109         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
110     }
111
112     public boolean isDependentOnParentWizard() {
113         return true;
114     }
115
116     /**
117      * @see GenericTemplateSection#validateOptions(TemplateOption)
118      */

119     public void validateOptions(TemplateOption source) {
120         if (source.isRequired() && source.isEmpty()) {
121             flagMissingRequiredOption(source);
122         } else {
123             validateContainerPage(source);
124         }
125     }
126
127     private void validateContainerPage(TemplateOption source) {
128         TemplateOption[] options = getOptions(0);
129         for (int i = 0; i < options.length; i++) {
130             TemplateOption nextOption = options[i];
131             if (nextOption.isRequired() && nextOption.isEmpty()) {
132                 flagMissingRequiredOption(nextOption);
133                 return;
134             }
135         }
136         resetPageState();
137     }
138
139     protected void updateModel(IProgressMonitor monitor) throws CoreException {
140         IPluginBase plugin = model.getPluginBase();
141         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
142         IPluginModelFactory factory = model.getPluginFactory();
143
144         IPluginElement editorElement = factory.createElement(extension);
145         editorElement.setName("editor"); //$NON-NLS-1$
146
editorElement.setAttribute(
147             "id", //$NON-NLS-1$
148
getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(EDITOR_CLASS_NAME)); //$NON-NLS-1$
149
editorElement.setAttribute("name", getStringOption(EDITOR_NAME)); //$NON-NLS-1$
150
editorElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
151
editorElement.setAttribute("extensions", getStringOption(EXTENSIONS)); //$NON-NLS-1$
152

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

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