KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.IFragment;
18 import org.eclipse.pde.core.plugin.IFragmentModel;
19 import org.eclipse.pde.core.plugin.IPlugin;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginModel;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.IPluginModelFactory;
26 import org.eclipse.pde.core.plugin.IPluginReference;
27 import org.eclipse.pde.internal.core.ModelEntry;
28 import org.eclipse.pde.internal.core.PDECore;
29 import org.eclipse.pde.internal.ui.IHelpContextIds;
30 import org.eclipse.pde.internal.ui.PDEUIMessages;
31 import org.eclipse.pde.ui.IFieldData;
32 import org.eclipse.pde.ui.templates.TemplateOption;
33
34 public class PreferencePageTemplate extends PDETemplateSection {
35     private static final String JavaDoc KEY_PAGE_NAME = "pageName"; //$NON-NLS-1$
36
private static final String JavaDoc KEY_PAGE_CLASS_NAME = "pageClassName"; //$NON-NLS-1$
37
private static final String JavaDoc KEY_PLUGIN_CLASS_NAME = "pluginClassName"; //$NON-NLS-1$
38
private static final String JavaDoc KEY_FULL_PLUGIN_CLASS_NAME = "fullPluginClassName"; //$NON-NLS-1$
39

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

53     public int getNumberOfWorkUnits() {
54         return super.getNumberOfWorkUnits() + 1;
55     }
56
57     private void createOptions() {
58         // first page
59
addOption(
60             KEY_PACKAGE_NAME,
61             PDEUIMessages.PreferencePageTemplate_packageName,
62             (String JavaDoc) null,
63             0);
64         addOption(
65             KEY_PAGE_CLASS_NAME,
66             PDEUIMessages.PreferencePageTemplate_className,
67             "SamplePreferencePage", //$NON-NLS-1$
68
0);
69         addOption(
70             KEY_PAGE_NAME,
71             PDEUIMessages.PreferencePageTemplate_pageName,
72             PDEUIMessages.PreferencePageTemplate_defaultPageName,
73             0);
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         mainClassName = id + ".PreferenceClass"; //$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         if (model instanceof IPluginModel) {
89             IPlugin plugin = (IPlugin) model.getPluginBase();
90             mainClassName = plugin.getClassName();
91         } else if (model instanceof IFragmentModel) {
92             IFragment fragment = (IFragment) model.getPluginBase();
93             String JavaDoc pluginPluginId = fragment.getPluginId();
94             ModelEntry entry = PDECore.getDefault().getModelManager()
95                     .findEntry(pluginPluginId);
96             if (entry != null) {
97                 IPluginModelBase pluginModelBase = entry.getActiveModel();
98                 if (pluginModelBase instanceof IPluginModel) {
99                     IPlugin plugin = (IPlugin) pluginModelBase.getPluginBase();
100                     mainClassName = plugin.getClassName();
101                 }
102             }
103         }
104         if (mainClassName == null) {
105             mainClassName = pluginId + ".PreferenceClass"; //$NON-NLS-1$
106
}
107     }
108
109     protected String JavaDoc getTemplateDirectory() {
110         String JavaDoc schemaVersion = model.getPluginBase().getSchemaVersion();
111         return "templates_" + schemaVersion == null ? "3.0" : schemaVersion; //$NON-NLS-1$ //$NON-NLS-2$
112
}
113     
114     public String JavaDoc getReplacementString(String JavaDoc fileName, String JavaDoc key) {
115         if (key.equals(KEY_FULL_PLUGIN_CLASS_NAME))
116             return mainClassName;
117         if (key.equals(KEY_PLUGIN_CLASS_NAME))
118             return getPluginClassName();
119         
120         return super.getReplacementString(fileName, key);
121     }
122
123     private String JavaDoc getPluginClassName() {
124         int dot = mainClassName.lastIndexOf('.');
125         if (dot != -1) {
126             return mainClassName.substring(dot + 1);
127         }
128         return mainClassName;
129     }
130
131     public boolean isDependentOnParentWizard() {
132         return true;
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
137      */

138     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
139         if (schemaVersion == null)
140             return super.getDependencies(schemaVersion);
141         PluginReference[] deps = new PluginReference[2];
142         deps[0] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
143
deps[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
144
return deps;
145     }
146
147     public void addPages(Wizard wizard) {
148         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
149         page.setTitle(PDEUIMessages.PreferencePageTemplate_title);
150         page.setDescription(PDEUIMessages.PreferencePageTemplate_desc);
151         wizard.addPage(page);
152         markPagesAdded();
153     }
154
155     public void validateOptions(TemplateOption source) {
156         if (source.isRequired() && source.isEmpty()) {
157             flagMissingRequiredOption(source);
158         } else
159             resetPageState();
160     }
161
162     public String JavaDoc getUsedExtensionPoint() {
163         return "org.eclipse.ui.preferencePages"; //$NON-NLS-1$
164
}
165
166     protected void updateModel(IProgressMonitor monitor) throws CoreException {
167         IPluginBase plugin = model.getPluginBase();
168         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
169         IPluginModelFactory factory = model.getPluginFactory();
170
171         String JavaDoc fullClassName =
172             getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_PAGE_CLASS_NAME); //$NON-NLS-1$
173

174         IPluginElement pageElement = factory.createElement(extension);
175         pageElement.setName("page"); //$NON-NLS-1$
176
pageElement.setAttribute("id", fullClassName); //$NON-NLS-1$
177
pageElement.setAttribute("name", getStringOption(KEY_PAGE_NAME)); //$NON-NLS-1$
178
pageElement.setAttribute("class", fullClassName); //$NON-NLS-1$
179
extension.add(pageElement);
180         if (!extension.isInTheModel())
181             plugin.add(extension);
182
183         IPluginExtension extension2 = createExtension("org.eclipse.core.runtime.preferences", true); //$NON-NLS-1$
184
IPluginElement prefElement = factory.createElement(extension);
185         prefElement.setName("initializer"); //$NON-NLS-1$
186
prefElement.setAttribute("class", getStringOption(KEY_PACKAGE_NAME)+".PreferenceInitializer"); //$NON-NLS-1$ //$NON-NLS-2$
187
extension2.add(prefElement);
188         if (!extension2.isInTheModel())
189             plugin.add(extension2);
190     }
191
192     
193     /* (non-Javadoc)
194      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
195      */

196     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
197         String JavaDoc packageName = super.getFormattedPackageName(id);
198         if (packageName.length() != 0)
199             return packageName + ".preferences"; //$NON-NLS-1$
200
return "preferences"; //$NON-NLS-1$
201
}
202 }
203
Popular Tags