1 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 KEY_PAGE_NAME = "pageName"; private static final String KEY_PAGE_CLASS_NAME = "pageClassName"; private static final String KEY_PLUGIN_CLASS_NAME = "pluginClassName"; private static final String KEY_FULL_PLUGIN_CLASS_NAME = "fullPluginClassName"; 40 private String mainClassName; 41 42 public PreferencePageTemplate() { 43 setPageCount(1); 44 createOptions(); 45 } 46 47 public String getSectionId() { 48 return "preferences"; } 50 53 public int getNumberOfWorkUnits() { 54 return super.getNumberOfWorkUnits() + 1; 55 } 56 57 private void createOptions() { 58 addOption( 60 KEY_PACKAGE_NAME, 61 PDEUIMessages.PreferencePageTemplate_packageName, 62 (String ) null, 63 0); 64 addOption( 65 KEY_PAGE_CLASS_NAME, 66 PDEUIMessages.PreferencePageTemplate_className, 67 "SamplePreferencePage", 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 String id = data.getId(); 80 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 81 mainClassName = id + ".PreferenceClass"; } 83 public void initializeFields(IPluginModelBase model) { 84 String 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 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"; } 107 } 108 109 protected String getTemplateDirectory() { 110 String schemaVersion = model.getPluginBase().getSchemaVersion(); 111 return "templates_" + schemaVersion == null ? "3.0" : schemaVersion; } 113 114 public String getReplacementString(String fileName, String 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 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 138 public IPluginReference[] getDependencies(String 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); deps[1] = new PluginReference("org.eclipse.ui", null, 0); 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 getUsedExtensionPoint() { 163 return "org.eclipse.ui.preferencePages"; } 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 fullClassName = 172 getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_PAGE_CLASS_NAME); 174 IPluginElement pageElement = factory.createElement(extension); 175 pageElement.setName("page"); pageElement.setAttribute("id", fullClassName); pageElement.setAttribute("name", getStringOption(KEY_PAGE_NAME)); pageElement.setAttribute("class", fullClassName); extension.add(pageElement); 180 if (!extension.isInTheModel()) 181 plugin.add(extension); 182 183 IPluginExtension extension2 = createExtension("org.eclipse.core.runtime.preferences", true); IPluginElement prefElement = factory.createElement(extension); 185 prefElement.setName("initializer"); prefElement.setAttribute("class", getStringOption(KEY_PACKAGE_NAME)+".PreferenceInitializer"); extension2.add(prefElement); 188 if (!extension2.isInTheModel()) 189 plugin.add(extension2); 190 } 191 192 193 196 protected String getFormattedPackageName(String id) { 197 String packageName = super.getFormattedPackageName(id); 198 if (packageName.length() != 0) 199 return packageName + ".preferences"; return "preferences"; } 202 } 203 | Popular Tags |