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.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.IHelpContextIds; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.pde.ui.IFieldData; 26 import org.eclipse.pde.ui.templates.TemplateOption; 27 28 public class PopupMenuTemplate extends PDETemplateSection { 29 30 public static final String KEY_TARGET_OBJECT = "objectClass"; public static final String KEY_NAME_FILTER = "nameFilter"; public static final String KEY_SUBMENU_LABEL = "subMenuLabel"; public static final String KEY_ACTION_LABEL = "actionLabel"; public static final String KEY_ACTION_CLASS = "actionClass"; public static final String KEY_SELECTION = "selection"; 37 40 public PopupMenuTemplate() { 41 setPageCount(1); 42 createOptions(); 43 } 44 45 public void addPages(Wizard wizard) { 46 WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_POPUP_MENU); 47 page.setTitle(PDEUIMessages.PopupMenuTemplate_title); 48 page.setDescription(PDEUIMessages.PopupMenuTemplate_desc); 49 wizard.addPage(page); 50 markPagesAdded(); 51 } 52 53 private void createOptions() { 54 addOption( 55 KEY_TARGET_OBJECT, 56 PDEUIMessages.PopupMenuTemplate_targetClass, 57 "org.eclipse.core.resources.IFile", 0); 59 addOption( 60 KEY_NAME_FILTER, 61 PDEUIMessages.PopupMenuTemplate_nameFilter, 62 "plugin.xml", 0); 64 addOption( 65 KEY_SUBMENU_LABEL, 66 PDEUIMessages.PopupMenuTemplate_submenuName, 67 PDEUIMessages.PopupMenuTemplate_defaultSubmenuName, 68 0); 69 addOption( 70 KEY_ACTION_LABEL, 71 PDEUIMessages.PopupMenuTemplate_actionLabel, 72 PDEUIMessages.PopupMenuTemplate_defaultActionName, 73 0); 74 addOption( 75 KEY_PACKAGE_NAME, 76 PDEUIMessages.PopupMenuTemplate_packageName, 77 (String ) null, 78 0); 79 addOption( 80 KEY_ACTION_CLASS, 81 PDEUIMessages.PopupMenuTemplate_actionClass, 82 PDEUIMessages.PopupMenuTemplate_newAction, 83 0); 84 addOption( 85 KEY_SELECTION, 86 PDEUIMessages.PopupMenuTemplate_enabledFor, 87 new String [][] { 88 { "singleSelection", PDEUIMessages.PopupMenuTemplate_singleSelection}, { 90 "multipleSelection", PDEUIMessages.PopupMenuTemplate_multipleSelection } 92 }, "singleSelection", 0); } 94 97 public String getSectionId() { 98 return "popupMenus"; } 100 101 public boolean isDependentOnParentWizard() { 102 return true; 103 } 104 105 protected void initializeFields(IFieldData data) { 106 String id = data.getId(); 109 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 110 } 111 112 public void initializeFields(IPluginModelBase model) { 113 String pluginId = model.getPluginBase().getId(); 116 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 117 } 118 119 122 public void validateOptions(TemplateOption source) { 123 if (source.isRequired() && source.isEmpty()) { 124 flagMissingRequiredOption(source); 125 } else { 126 validateContainerPage(source); 127 } 128 } 129 130 private void validateContainerPage(TemplateOption source) { 131 TemplateOption[] allPageOptions = getOptions(0); 132 for (int i = 0; i < allPageOptions.length; i++) { 133 TemplateOption nextOption = allPageOptions[i]; 134 if (nextOption.isRequired() && nextOption.isEmpty()) { 135 flagMissingRequiredOption(nextOption); 136 return; 137 } 138 } 139 resetPageState(); 140 } 141 142 145 protected void updateModel(IProgressMonitor monitor) throws CoreException { 146 IPluginBase plugin = model.getPluginBase(); 147 IPluginExtension extension = createExtension(getUsedExtensionPoint(), true); 148 IPluginModelFactory factory = model.getPluginFactory(); 149 150 IPluginElement objectContributionElement = factory.createElement(extension); 151 objectContributionElement.setName("objectContribution"); objectContributionElement.setAttribute( 153 "objectClass", getStringOption(KEY_TARGET_OBJECT)); 155 objectContributionElement.setAttribute( 156 "nameFilter", getStringOption(KEY_NAME_FILTER)); 158 objectContributionElement.setAttribute( 159 "id", model.getPluginBase().getId() + ".contribution1"); 162 IPluginElement menuElement = factory.createElement(objectContributionElement); 163 menuElement.setName("menu"); menuElement.setAttribute("label", getStringOption(KEY_SUBMENU_LABEL)); menuElement.setAttribute("path", "additions"); menuElement.setAttribute("id", model.getPluginBase().getId() + ".menu1"); 168 IPluginElement separatorElement = factory.createElement(menuElement); 169 separatorElement.setName("separator"); separatorElement.setAttribute("name", "group1"); menuElement.add(separatorElement); 172 objectContributionElement.add(menuElement); 173 174 IPluginElement actionElement = factory.createElement(objectContributionElement); 175 actionElement.setName("action"); actionElement.setAttribute("label", getStringOption(KEY_ACTION_LABEL)); actionElement.setAttribute( 178 "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_ACTION_CLASS)); actionElement.setAttribute( 181 "menubarPath", model.getPluginBase().getId() + ".menu1/group1"); actionElement.setAttribute( 184 "enablesFor", getValue(KEY_SELECTION).toString().equals("singleSelection") ? "1" : "multiple"); actionElement.setAttribute("id", model.getPluginBase().getId() + ".newAction"); objectContributionElement.add(actionElement); 190 191 extension.add(objectContributionElement); 192 if (!extension.isInTheModel()) 193 plugin.add(extension); 194 } 195 196 199 public String getUsedExtensionPoint() { 200 return "org.eclipse.ui.popupMenus"; } 202 203 206 protected String getFormattedPackageName(String id) { 207 String packageName = super.getFormattedPackageName(id); 208 if (packageName.length() !=0) 209 return packageName + ".popup.actions"; return "popup.actions"; } 212 213 public IPluginReference[] getDependencies(String schemaVersion) { 214 IPluginReference[] result = new IPluginReference[2]; 215 result[0] = new PluginReference("org.eclipse.ui", null, 0); result[1] = new PluginReference("org.eclipse.core.resources", null, 0); return result; 218 } 219 220 } 221 | Popular Tags |