1 11 package org.eclipse.pde.internal.ui.wizards.templates; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jface.wizard.Wizard; 19 import org.eclipse.jface.wizard.WizardPage; 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.IPluginModelBase; 24 import org.eclipse.pde.core.plugin.IPluginModelFactory; 25 import org.eclipse.pde.core.plugin.IPluginReference; 26 import org.eclipse.pde.internal.ui.IHelpContextIds; 27 import org.eclipse.pde.internal.ui.PDEUIMessages; 28 import org.eclipse.pde.ui.IFieldData; 29 import org.eclipse.pde.ui.templates.BooleanOption; 30 import org.eclipse.pde.ui.templates.TemplateOption; 31 32 public class BuilderTemplate extends PDETemplateSection { 33 34 private static final String KEY_BUILDER_CLASS_NAME = "builderClassName"; 36 private static final String KEY_BUILDER_ID = "builderId"; 38 private static final String KEY_BUILDER_NAME = "builderName"; 40 private static final String KEY_NATURE_CLASS_NAME = "natureClassName"; 42 private static final String KEY_NATURE_ID = "natureId"; 44 private static final String KEY_NATURE_NAME = "natureName"; 46 private static final String KEY_GEN_ACTION = "genAction"; 48 private BooleanOption actionOption; 49 50 53 public BuilderTemplate() { 54 setPageCount(1); 55 createOptions(); 56 } 57 58 63 public String getSectionId() { 64 return "builder"; } 66 67 70 public int getNumberOfWorkUnits() { 71 return super.getNumberOfWorkUnits() + 1; 72 } 73 74 private void createOptions() { 75 addOption(KEY_PACKAGE_NAME, PDEUIMessages.BuilderTemplate_packageLabel, (String ) null, 0); 76 77 addOption(KEY_BUILDER_CLASS_NAME, PDEUIMessages.BuilderTemplate_builderClass, "SampleBuilder", 0); addOption(KEY_BUILDER_ID, PDEUIMessages.BuilderTemplate_builderId, 79 "sampleBuilder", 0); addOption(KEY_BUILDER_NAME, PDEUIMessages.BuilderTemplate_builderName, PDEUIMessages.BuilderTemplate_defaultBuilderName, 0); 81 82 addOption(KEY_NATURE_CLASS_NAME, PDEUIMessages.BuilderTemplate_natureClass, "SampleNature", 0); addOption(KEY_NATURE_ID, PDEUIMessages.BuilderTemplate_natureId, 84 "sampleNature", 0); addOption(KEY_NATURE_NAME, PDEUIMessages.BuilderTemplate_natureName, 86 PDEUIMessages.BuilderTemplate_defaultNatureName, 0); 87 88 actionOption = (BooleanOption) addOption(KEY_GEN_ACTION, PDEUIMessages.BuilderTemplate_generateAction, true, 0); 89 } 90 91 public void addPages(Wizard wizard) { 92 WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_BUILDER); 93 page.setTitle(PDEUIMessages.BuilderTemplate_title); 94 page.setDescription(PDEUIMessages.BuilderTemplate_desc); 95 wizard.addPage(page); 96 markPagesAdded(); 97 } 98 99 public void validateOptions(TemplateOption source) { 100 if (source.isRequired() && source.isEmpty()) { 101 flagMissingRequiredOption(source); 102 } else { 103 validateContainerPage(source); 104 } 105 } 106 107 private void validateContainerPage(TemplateOption source) { 108 TemplateOption[] allPageOptions = getOptions(0); 109 for (int i = 0; i < allPageOptions.length; i++) { 110 TemplateOption nextOption = allPageOptions[i]; 111 if (nextOption.isRequired() && nextOption.isEmpty()) { 112 flagMissingRequiredOption(nextOption); 113 return; 114 } 115 } 116 resetPageState(); 117 } 118 119 public boolean isDependentOnParentWizard() { 120 return true; 121 } 122 123 protected void initializeFields(IFieldData data) { 124 String id = data.getId(); 127 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 128 } 129 130 public void initializeFields(IPluginModelBase model) { 131 String pluginId = model.getPluginBase().getId(); 134 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 135 } 136 137 142 public String getUsedExtensionPoint() { 143 return null; 144 } 145 146 protected void updateModel(IProgressMonitor monitor) throws CoreException { 147 IPluginBase plugin = model.getPluginBase(); 148 IPluginModelFactory factory = model.getPluginFactory(); 149 150 IPluginExtension extension1 = createExtension( 152 "org.eclipse.core.resources.builders", true); extension1.setId(getStringOption(KEY_BUILDER_ID)); 154 extension1.setName(getStringOption(KEY_BUILDER_NAME)); 155 156 IPluginElement builder = factory.createElement(extension1); 157 builder.setName("builder"); builder.setAttribute("hasNature", "true"); extension1.add(builder); 160 161 IPluginElement run = factory.createElement(builder); 162 run.setName("run"); run.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_BUILDER_CLASS_NAME)); builder.add(run); 166 167 if (!extension1.isInTheModel()) 168 plugin.add(extension1); 169 170 IPluginExtension extension2 = createExtension( 172 "org.eclipse.core.resources.natures", true); extension2.setId(getStringOption(KEY_NATURE_ID)); 174 extension2.setName(getStringOption(KEY_NATURE_NAME)); 175 176 IPluginElement runtime = factory.createElement(extension2); 177 runtime.setName("runtime"); extension2.add(runtime); 179 180 IPluginElement run2 = factory.createElement(runtime); 181 run2.setName("run"); run2.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_NATURE_CLASS_NAME)); runtime.add(run2); 185 186 IPluginElement builder2 = factory.createElement(extension2); 187 builder2.setName("builder"); builder2.setAttribute("id", model.getPluginBase().getId() + "." + getStringOption(KEY_BUILDER_ID)); extension2.add(builder2); 191 192 if (!extension2.isInTheModel()) 193 plugin.add(extension2); 194 195 if (actionOption.isSelected()) { 197 IPluginExtension extension3 = createExtension( 198 "org.eclipse.ui.popupMenus", true); IPluginElement objectContribution = factory 200 .createElement(extension3); 201 objectContribution.setName("objectContribution"); objectContribution.setAttribute("objectClass", "org.eclipse.core.resources.IProject"); objectContribution.setAttribute("adaptable", "true"); objectContribution.setAttribute("nameFilter", "*"); objectContribution.setAttribute("id", model.getPluginBase().getId() + ".contribution1"); extension3.add(objectContribution); 209 210 IPluginElement action = factory.createElement(objectContribution); 211 action.setName("action"); action.setAttribute( 213 "label", PDEUIMessages.BuilderTemplate_actionLabel); action.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + ".ToggleNatureAction"); action.setAttribute("menubarPath", "additions"); action.setAttribute("enablesFor", "+"); action.setAttribute("id", model.getPluginBase().getId() + ".addRemoveNatureAction"); objectContribution.add(action); 221 222 if (!extension3.isInTheModel()) 223 plugin.add(extension3); 224 } 225 226 IPluginExtension extension4 = createExtension( 228 "org.eclipse.core.resources.markers", false); extension4.setId("xmlProblem"); extension4.setName(PDEUIMessages.BuilderTemplate_markerName); 231 232 IPluginElement superElement = factory.createElement(extension4); 233 superElement.setName("super"); superElement.setAttribute("type", "org.eclipse.core.resources.problemmarker"); extension4.add(superElement); 237 238 IPluginElement persistent = factory.createElement(extension4); 239 persistent.setName("persistent"); persistent.setAttribute("value", "true"); extension4.add(persistent); 242 243 if (!extension4.isInTheModel()) 244 plugin.add(extension4); 245 } 246 247 252 public IPluginReference[] getDependencies(String schemaVersion) { 253 ArrayList result = new ArrayList (); 254 result.add(new PluginReference("org.eclipse.core.resources", null, 0)); if (schemaVersion != null) 256 result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); 258 if (actionOption.isSelected()) 259 result.add(new PluginReference("org.eclipse.ui", null, 0)); 261 return (IPluginReference[]) result.toArray(new IPluginReference[result 262 .size()]); 263 } 264 265 270 protected String getFormattedPackageName(String id) { 271 String packageName = super.getFormattedPackageName(id); 272 if (packageName.length() != 0) 273 return packageName + ".builder"; return "builder"; } 276 277 280 protected boolean isOkToCreateFile(File sourceFile) { 281 String fileName = sourceFile.getName(); 282 if (fileName.equals("ToggleNatureAction.java")) { return actionOption.isSelected(); 284 } 285 return true; 286 } 287 } 288 | Popular Tags |