KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > 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.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.PDETemplateSection;
26 import org.eclipse.pde.internal.ui.templates.PluginReference;
27 import org.eclipse.pde.ui.IFieldData;
28
29 public class PreferencePageTemplate extends PDETemplateSection {
30     private static final String JavaDoc KEY_PAGE_NAME = "pageName"; //$NON-NLS-1$
31
private static final String JavaDoc KEY_PAGE_CLASS_NAME = "pageClassName"; //$NON-NLS-1$
32

33     public PreferencePageTemplate() {
34         setPageCount(1);
35         createOptions();
36     }
37
38     public String JavaDoc getSectionId() {
39         return "preferences"; //$NON-NLS-1$
40
}
41     /*
42      * @see ITemplateSection#getNumberOfWorkUnits()
43      */

44     public int getNumberOfWorkUnits() {
45         return super.getNumberOfWorkUnits() + 1;
46     }
47
48     private void createOptions() {
49         // first page
50
addOption(
51             KEY_PACKAGE_NAME,
52             PDETemplateMessages.PreferencePageTemplate_packageName,
53             (String JavaDoc) null,
54             0);
55         addOption(
56             KEY_PAGE_CLASS_NAME,
57             PDETemplateMessages.PreferencePageTemplate_className,
58             "SamplePreferencePage", //$NON-NLS-1$
59
0);
60         addOption(
61             KEY_PAGE_NAME,
62             PDETemplateMessages.PreferencePageTemplate_pageName,
63             PDETemplateMessages.PreferencePageTemplate_defaultPageName,
64             0);
65     }
66
67     protected void initializeFields(IFieldData data) {
68         // In a new project wizard, we don't know this yet - the
69
// model has not been created
70
String JavaDoc id = data.getId();
71         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
72     }
73     
74     public void initializeFields(IPluginModelBase model) {
75         // In the new extension wizard, the model exists so
76
// we can initialize directly from it
77
String JavaDoc pluginId = model.getPluginBase().getId();
78         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
79     }
80
81     protected String JavaDoc getTemplateDirectory() {
82         String JavaDoc schemaVersion = model.getPluginBase().getSchemaVersion();
83         return "templates_" + schemaVersion == null ? "3.0" : schemaVersion; //$NON-NLS-1$ //$NON-NLS-2$
84
}
85     
86     public boolean isDependentOnParentWizard() {
87         return true;
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
92      */

93     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
94         if (schemaVersion == null)
95             return super.getDependencies(schemaVersion);
96         PluginReference[] deps = new PluginReference[2];
97         deps[0] = new PluginReference("org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
98
deps[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
99
return deps;
100     }
101
102     public void addPages(Wizard wizard) {
103         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
104         page.setTitle(PDETemplateMessages.PreferencePageTemplate_title);
105         page.setDescription(PDETemplateMessages.PreferencePageTemplate_desc);
106         wizard.addPage(page);
107         markPagesAdded();
108     }
109
110     public String JavaDoc getUsedExtensionPoint() {
111         return "org.eclipse.ui.preferencePages"; //$NON-NLS-1$
112
}
113
114     protected void updateModel(IProgressMonitor monitor) throws CoreException {
115         IPluginBase plugin = model.getPluginBase();
116         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
117         IPluginModelFactory factory = model.getPluginFactory();
118
119         String JavaDoc fullClassName =
120             getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_PAGE_CLASS_NAME); //$NON-NLS-1$
121

122         IPluginElement pageElement = factory.createElement(extension);
123         pageElement.setName("page"); //$NON-NLS-1$
124
pageElement.setAttribute("id", fullClassName); //$NON-NLS-1$
125
pageElement.setAttribute("name", getStringOption(KEY_PAGE_NAME)); //$NON-NLS-1$
126
pageElement.setAttribute("class", fullClassName); //$NON-NLS-1$
127
extension.add(pageElement);
128         if (!extension.isInTheModel())
129             plugin.add(extension);
130
131         IPluginExtension extension2 = createExtension("org.eclipse.core.runtime.preferences", true); //$NON-NLS-1$
132
IPluginElement prefElement = factory.createElement(extension);
133         prefElement.setName("initializer"); //$NON-NLS-1$
134
prefElement.setAttribute("class", getStringOption(KEY_PACKAGE_NAME)+".PreferenceInitializer"); //$NON-NLS-1$ //$NON-NLS-2$
135
extension2.add(prefElement);
136         if (!extension2.isInTheModel())
137             plugin.add(extension2);
138     }
139
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
143      */

144     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
145         String JavaDoc packageName = super.getFormattedPackageName(id);
146         if (packageName.length() != 0)
147             return packageName + ".preferences"; //$NON-NLS-1$
148
return "preferences"; //$NON-NLS-1$
149
}
150 }
151
Popular Tags