KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.jface.wizard.WizardPage;
19 import org.eclipse.pde.core.plugin.IPluginBase;
20 import org.eclipse.pde.core.plugin.IPluginElement;
21 import org.eclipse.pde.core.plugin.IPluginExtension;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginModelFactory;
24 import org.eclipse.pde.core.plugin.IPluginReference;
25 import org.eclipse.pde.internal.ui.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.ui.IFieldData;
28 import org.eclipse.pde.ui.templates.TemplateOption;
29
30 public class PropertyPageTemplate extends PDETemplateSection {
31     public static final String JavaDoc KEY_CLASSNAME = "className"; //$NON-NLS-1$
32
public static final String JavaDoc KEY_PAGE_NAME = "pageName"; //$NON-NLS-1$
33
public static final String JavaDoc KEY_TARGET_CLASS = "targetClass"; //$NON-NLS-1$
34
public static final String JavaDoc KEY_NAME_FILTER = "nameFilter"; //$NON-NLS-1$
35

36     /**
37      * Constructor for PropertyPageTemplate.
38      */

39     public PropertyPageTemplate() {
40         setPageCount(1);
41         createOptions();
42     }
43
44     public void addPages(Wizard wizard) {
45         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_PROPERTY_PAGE);
46         page.setTitle(PDEUIMessages.PropertyPageTemplate_title);
47         page.setDescription(PDEUIMessages.PropertyPageTemplate_desc);
48         wizard.addPage(page);
49         markPagesAdded();
50     }
51
52     private void createOptions() {
53         addOption(
54             KEY_PACKAGE_NAME,
55             PDEUIMessages.PropertyPageTemplate_packageName,
56             (String JavaDoc) null,
57             0);
58         addOption(
59             KEY_CLASSNAME,
60             PDEUIMessages.PropertyPageTemplate_pageClass,
61             "SamplePropertyPage", //$NON-NLS-1$
62
0);
63         addOption(
64             KEY_PAGE_NAME,
65             PDEUIMessages.PropertyPageTemplate_pageName,
66             PDEUIMessages.PropertyPageTemplate_defaultPageName,
67             0);
68         addOption(
69             KEY_TARGET_CLASS,
70             PDEUIMessages.PropertyPageTemplate_targetClass,
71             "org.eclipse.core.resources.IFile", //$NON-NLS-1$
72
0);
73         addOption(
74             KEY_NAME_FILTER,
75             PDEUIMessages.PropertyPageTemplate_nameFilter,
76             "*.*", //$NON-NLS-1$
77
0);
78     }
79     /**
80      * @see PDETemplateSection#getSectionId()
81      */

82     public String JavaDoc getSectionId() {
83         return "propertyPages"; //$NON-NLS-1$
84
}
85
86     public boolean isDependentOnParentWizard() {
87         return true;
88     }
89     
90
91     protected void initializeFields(IFieldData data) {
92         // In a new project wizard, we don't know this yet - the
93
// model has not been created
94
String JavaDoc id = data.getId();
95         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
96     }
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     /**
106      * @see GenericTemplateSection#validateOptions(TemplateOption)
107      */

108     public void validateOptions(TemplateOption source) {
109         if (source.isRequired() && source.isEmpty()) {
110             flagMissingRequiredOption(source);
111         } else {
112             validateContainerPage(source);
113         }
114     }
115
116     private void validateContainerPage(TemplateOption source) {
117         TemplateOption[] allPageOptions = getOptions(0);
118         for (int i = 0; i < allPageOptions.length; i++) {
119             TemplateOption nextOption = allPageOptions[i];
120             if (nextOption.isRequired() && nextOption.isEmpty()) {
121                 flagMissingRequiredOption(nextOption);
122                 return;
123             }
124         }
125         resetPageState();
126     }
127
128     /**
129      * @see AbstractTemplateSection#updateModel(IProgressMonitor)
130      */

131     protected void updateModel(IProgressMonitor monitor) throws CoreException {
132         IPluginBase plugin = model.getPluginBase();
133         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
134         IPluginModelFactory factory = model.getPluginFactory();
135
136         IPluginElement pageElement = factory.createElement(extension);
137         pageElement.setName("page"); //$NON-NLS-1$
138
pageElement.setAttribute(
139             "id", //$NON-NLS-1$
140
getStringOption(KEY_PACKAGE_NAME) + ".samplePropertyPage"); //$NON-NLS-1$
141
pageElement.setAttribute("name", getStringOption(KEY_PAGE_NAME)); //$NON-NLS-1$
142
pageElement.setAttribute("objectClass", getStringOption(KEY_TARGET_CLASS)); //$NON-NLS-1$
143
pageElement.setAttribute(
144             "class", //$NON-NLS-1$
145
getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_CLASSNAME)); //$NON-NLS-1$
146
pageElement.setAttribute("nameFilter", getStringOption(KEY_NAME_FILTER)); //$NON-NLS-1$
147

148         extension.add(pageElement);
149         if (!extension.isInTheModel())
150             plugin.add(extension);
151     }
152
153     /**
154      * @see ITemplateSection#getUsedExtensionPoint()
155      */

156     public String JavaDoc getUsedExtensionPoint() {
157         return "org.eclipse.ui.propertyPages"; //$NON-NLS-1$
158
}
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
162      */

163     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
164         ArrayList JavaDoc result = new ArrayList JavaDoc();
165         result.add(new PluginReference("org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
166
if (schemaVersion != null)
167             result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
168
result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
169

170         return (IPluginReference[])result.toArray(new IPluginReference[result.size()]);
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
175      */

176     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
177         String JavaDoc packageName = super.getFormattedPackageName(id);
178         if (packageName.length() != 0)
179             return packageName + ".properties"; //$NON-NLS-1$
180
return "properties"; //$NON-NLS-1$
181
}
182
183 }
184
Popular Tags