1 11 package org.eclipse.pde.internal.ui.templates.ide; 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.templates.IHelpContextIds; 27 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages; 28 import org.eclipse.pde.internal.ui.templates.PDETemplateSection; 29 import org.eclipse.pde.internal.ui.templates.PluginReference; 30 import org.eclipse.pde.ui.IFieldData; 31 import org.eclipse.pde.ui.templates.BooleanOption; 32 33 public class BuilderTemplate extends PDETemplateSection { 34 35 private static final String KEY_BUILDER_CLASS_NAME = "builderClassName"; 37 private static final String KEY_BUILDER_ID = "builderId"; 39 private static final String KEY_BUILDER_NAME = "builderName"; 41 private static final String KEY_NATURE_CLASS_NAME = "natureClassName"; 43 private static final String KEY_NATURE_ID = "natureId"; 45 private static final String KEY_NATURE_NAME = "natureName"; 47 private static final String KEY_GEN_ACTION = "genAction"; 49 private BooleanOption actionOption; 50 51 54 public BuilderTemplate() { 55 setPageCount(1); 56 createOptions(); 57 } 58 59 64 public String getSectionId() { 65 return "builder"; } 67 68 71 public int getNumberOfWorkUnits() { 72 return super.getNumberOfWorkUnits() + 1; 73 } 74 75 private void createOptions() { 76 addOption(KEY_PACKAGE_NAME, PDETemplateMessages.BuilderTemplate_packageLabel, (String ) null, 0); 77 78 addOption(KEY_BUILDER_CLASS_NAME, PDETemplateMessages.BuilderTemplate_builderClass, "SampleBuilder", 0); addOption(KEY_BUILDER_ID, PDETemplateMessages.BuilderTemplate_builderId, 80 "sampleBuilder", 0); addOption(KEY_BUILDER_NAME, PDETemplateMessages.BuilderTemplate_builderName, PDETemplateMessages.BuilderTemplate_defaultBuilderName, 0); 82 83 addOption(KEY_NATURE_CLASS_NAME, PDETemplateMessages.BuilderTemplate_natureClass, "SampleNature", 0); addOption(KEY_NATURE_ID, PDETemplateMessages.BuilderTemplate_natureId, 85 "sampleNature", 0); addOption(KEY_NATURE_NAME, PDETemplateMessages.BuilderTemplate_natureName, 87 PDETemplateMessages.BuilderTemplate_defaultNatureName, 0); 88 89 actionOption = (BooleanOption) addOption(KEY_GEN_ACTION, PDETemplateMessages.BuilderTemplate_generateAction, true, 0); 90 } 91 92 public void addPages(Wizard wizard) { 93 WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_BUILDER); 94 page.setTitle(PDETemplateMessages.BuilderTemplate_title); 95 page.setDescription(PDETemplateMessages.BuilderTemplate_desc); 96 wizard.addPage(page); 97 markPagesAdded(); 98 } 99 100 public boolean isDependentOnParentWizard() { 101 return true; 102 } 103 104 protected void initializeFields(IFieldData data) { 105 String id = data.getId(); 108 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 109 } 110 111 public void initializeFields(IPluginModelBase model) { 112 String pluginId = model.getPluginBase().getId(); 115 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 116 } 117 118 123 public String getUsedExtensionPoint() { 124 return "org.eclipse.core.resources.builders"; } 126 127 protected void updateModel(IProgressMonitor monitor) throws CoreException { 128 IPluginBase plugin = model.getPluginBase(); 129 IPluginModelFactory factory = model.getPluginFactory(); 130 131 IPluginExtension extension1 = createExtension( 133 "org.eclipse.core.resources.builders", true); extension1.setId(getStringOption(KEY_BUILDER_ID)); 135 extension1.setName(getStringOption(KEY_BUILDER_NAME)); 136 137 IPluginElement builder = factory.createElement(extension1); 138 builder.setName("builder"); builder.setAttribute("hasNature", "true"); extension1.add(builder); 141 142 IPluginElement run = factory.createElement(builder); 143 run.setName("run"); run.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_BUILDER_CLASS_NAME)); builder.add(run); 147 148 if (!extension1.isInTheModel()) 149 plugin.add(extension1); 150 151 IPluginExtension extension2 = createExtension( 153 "org.eclipse.core.resources.natures", true); extension2.setId(getStringOption(KEY_NATURE_ID)); 155 extension2.setName(getStringOption(KEY_NATURE_NAME)); 156 157 IPluginElement runtime = factory.createElement(extension2); 158 runtime.setName("runtime"); extension2.add(runtime); 160 161 IPluginElement run2 = factory.createElement(runtime); 162 run2.setName("run"); run2.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_NATURE_CLASS_NAME)); runtime.add(run2); 166 167 IPluginElement builder2 = factory.createElement(extension2); 168 builder2.setName("builder"); builder2.setAttribute("id", model.getPluginBase().getId() + "." + getStringOption(KEY_BUILDER_ID)); extension2.add(builder2); 172 173 if (!extension2.isInTheModel()) 174 plugin.add(extension2); 175 176 if (actionOption.isSelected()) { 178 IPluginExtension extension3 = createExtension( 179 "org.eclipse.ui.popupMenus", true); IPluginElement objectContribution = factory 181 .createElement(extension3); 182 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); 190 191 IPluginElement action = factory.createElement(objectContribution); 192 action.setName("action"); action.setAttribute( 194 "label", PDETemplateMessages.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); 202 203 if (!extension3.isInTheModel()) 204 plugin.add(extension3); 205 } 206 207 IPluginExtension extension4 = createExtension( 209 "org.eclipse.core.resources.markers", false); extension4.setId("xmlProblem"); extension4.setName(PDETemplateMessages.BuilderTemplate_markerName); 212 213 IPluginElement superElement = factory.createElement(extension4); 214 superElement.setName("super"); superElement.setAttribute("type", "org.eclipse.core.resources.problemmarker"); extension4.add(superElement); 218 219 IPluginElement persistent = factory.createElement(extension4); 220 persistent.setName("persistent"); persistent.setAttribute("value", "true"); extension4.add(persistent); 223 224 if (!extension4.isInTheModel()) 225 plugin.add(extension4); 226 } 227 228 233 public IPluginReference[] getDependencies(String schemaVersion) { 234 ArrayList result = new ArrayList (); 235 result.add(new PluginReference("org.eclipse.core.resources", null, 0)); if (schemaVersion != null) 237 result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); 239 if (actionOption.isSelected()) 240 result.add(new PluginReference("org.eclipse.ui", null, 0)); 242 return (IPluginReference[]) result.toArray(new IPluginReference[result 243 .size()]); 244 } 245 246 251 protected String getFormattedPackageName(String id) { 252 String packageName = super.getFormattedPackageName(id); 253 if (packageName.length() != 0) 254 return packageName + ".builder"; return "builder"; } 257 258 261 protected boolean isOkToCreateFile(File sourceFile) { 262 String fileName = sourceFile.getName(); 263 if (fileName.equals("ToggleNatureAction.java")) { return actionOption.isSelected(); 265 } 266 return true; 267 } 268 269 272 public String getLabel() { 273 return getPluginResourceString("newExtension.templates.builder.name"); } 275 276 279 public String getDescription() { 280 return getPluginResourceString("newExtension.templates.builder.desc"); } 282 283 } 284 | Popular Tags |