1 11 package org.eclipse.pde.internal.ui.wizards.templates; 12 13 import java.io.File ; 14 import java.util.StringTokenizer ; 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 DecoratorTemplate extends PDETemplateSection { 33 public static final String DECORATOR_CLASS_NAME = "decoratorClassName"; public static final String DECORATOR_ICON_PLACEMENT = "decoratorPlacement"; public static final String DECORATOR_BLN_PROJECT = "decorateProjects"; public static final String DECORATOR_BLN_READONLY = "decorateReadOnly"; 38 private WizardPage page; 39 private TemplateOption packageOption; 40 private TemplateOption classOption; 41 private BooleanOption projectOption; 42 private BooleanOption readOnlyOption; 43 44 47 public DecoratorTemplate() { 48 setPageCount(1); 49 createOptions(); 50 alterOptionStates(); 51 } 52 53 56 public IPluginReference[] getDependencies(String schemaVersion) { 57 if (schemaVersion != null) { 59 IPluginReference[] dep = new IPluginReference[1]; 60 dep[0] = new PluginReference("org.eclipse.core.resources", null, 0); return dep; 62 } 63 return super.getDependencies(schemaVersion); 64 } 65 66 70 public String getSectionId() { 71 return "decorator"; } 76 77 80 public int getNumberOfWorkUnits() { 81 return super.getNumberOfWorkUnits() + 1; 82 } 83 84 89 private void createOptions() { 90 String [][] choices = fromCommaSeparated(PDEUIMessages.DecoratorTemplate_placementChoices); 91 92 addOption(DECORATOR_ICON_PLACEMENT, 93 PDEUIMessages.DecoratorTemplate_placement, 94 choices, 95 choices[0][0], 96 0); 97 98 projectOption = (BooleanOption) addOption(DECORATOR_BLN_PROJECT, 99 PDEUIMessages.DecoratorTemplate_decorateProject, true, 0); 100 101 readOnlyOption = (BooleanOption) addOption(DECORATOR_BLN_READONLY, 102 PDEUIMessages.DecoratorTemplate_decorateReadOnly, false, 0); 103 104 packageOption = addOption( 105 KEY_PACKAGE_NAME, 106 PDEUIMessages.DecoratorTemplate_packageName, 107 (String ) null, 108 0); 109 classOption = addOption( 110 DECORATOR_CLASS_NAME, 111 PDEUIMessages.DecoratorTemplate_decoratorClass, 112 PDEUIMessages.DecoratorTemplate_decoratorClassName, 113 0); 114 } 115 116 119 public void addPages(Wizard wizard) { 120 int pageIndex = 0; 121 122 page = createPage(pageIndex, IHelpContextIds.TEMPLATE_EDITOR); 123 page.setTitle(PDEUIMessages.DecoratorTemplate_title); 124 page.setDescription(PDEUIMessages.DecoratorTemplate_desc); 125 126 wizard.addPage(page); 127 markPagesAdded(); 128 } 129 130 private void alterOptionStates() { 131 projectOption.setEnabled(!readOnlyOption.isSelected()); 132 packageOption.setEnabled(!projectOption.isEnabled()); 133 classOption.setEnabled(!projectOption.isEnabled()); 134 } 135 136 139 protected boolean isOkToCreateFolder(File sourceFolder) { 140 boolean isOk = true; 142 String folderName = sourceFolder.getName(); 143 if (folderName.equals("java")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 145 } 146 return isOk; 147 } 148 149 152 protected boolean isOkToCreateFile(File sourceFile) { 153 boolean isOk = true; 155 String fileName = sourceFile.getName(); 156 if (fileName.equals("read_only.gif")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 158 } else if (fileName.equals("sample_decorator.gif")) { isOk = !readOnlyOption.isSelected(); 160 } else if (fileName.equals("$decoratorClassName$.java")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 162 } 163 return isOk; 164 } 165 166 169 public void validateOptions(TemplateOption source) { 170 if (source == readOnlyOption){ 173 alterOptionStates(); 174 } 175 176 if (source.isRequired() && source.isEmpty()) { 177 flagMissingRequiredOption(source); 178 } else { 179 validateContainerPage(source); 180 } 181 } 182 183 191 private void validateContainerPage(TemplateOption source) { 192 TemplateOption[] allPageOptions = getOptions(0); 193 for (int i = 0; i < allPageOptions.length; i++) { 194 TemplateOption nextOption = allPageOptions[i]; 195 if (nextOption.isRequired() && nextOption.isEmpty()) { 196 flagMissingRequiredOption(nextOption); 197 return; 198 } 199 } 200 resetPageState(); 201 } 202 203 204 207 public boolean isDependentOnParentWizard() { 208 return true; 209 } 210 211 214 protected void initializeFields(IFieldData data) { 215 String id = data.getId(); 218 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 219 } 220 221 224 public void initializeFields(IPluginModelBase model) { 225 String pluginId = model.getPluginBase().getId(); 228 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 229 } 230 231 234 protected void updateModel(IProgressMonitor monitor) throws CoreException { 235 IPluginBase plugin = model.getPluginBase(); 240 IPluginExtension extension = createExtension(getUsedExtensionPoint(),true); 241 IPluginModelFactory factory = model.getPluginFactory(); 242 243 IPluginElement decoratorElement = factory.createElement(extension); 244 decoratorElement.setName("decorator"); decoratorElement.setAttribute("adaptable", "true"); decoratorElement.setAttribute("state", "true"); decoratorElement.setAttribute("lightweight", "true"); 249 if(!readOnlyOption.isSelected()){ 250 decoratorElement.setAttribute( 251 "id", plugin.getId() + "." + getSectionId()); decoratorElement.setAttribute( 253 "label", PDEUIMessages.DecoratorTemplate_resourceLabel); decoratorElement.setAttribute("icon", "icons/sample_decorator.gif"); decoratorElement.setAttribute( 256 "location", getValue(DECORATOR_ICON_PLACEMENT).toString()); } 258 else { 259 decoratorElement.setAttribute( 260 "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); decoratorElement.setAttribute( 262 "label", PDEUIMessages.DecoratorTemplate_readOnlyLabel); decoratorElement.setAttribute( 264 "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); } 266 267 IPluginElement enablementElement = factory 268 .createElement(decoratorElement); 269 enablementElement.setName("enablement"); 271 IPluginElement andElement = factory.createElement(enablementElement); 272 andElement.setName("and"); 274 IPluginElement resourceObjectElement = factory.createElement(andElement); 275 resourceObjectElement.setName("objectClass"); resourceObjectElement.setAttribute( 277 "name", "org.eclipse.core.resources.IResource"); 279 IPluginElement orElement = factory.createElement(andElement); 280 orElement.setName("or"); 282 IPluginElement fileObjectElement = factory.createElement(orElement); 283 fileObjectElement.setName("objectClass"); fileObjectElement.setAttribute( 285 "name", "org.eclipse.core.resources.IFile"); 287 IPluginElement folderObjectElement = factory.createElement(orElement); 288 folderObjectElement.setName("objectClass"); folderObjectElement.setAttribute( 290 "name", "org.eclipse.core.resources.IFolder"); 292 IPluginElement projectObjectElement = factory.createElement(orElement); 293 projectObjectElement.setName("objectClass"); projectObjectElement.setAttribute( 295 "name", "org.eclipse.core.resources.IProject"); 297 if(readOnlyOption.isSelected()) 298 orElement.add(folderObjectElement); 299 else if (projectOption.isSelected()) 300 orElement.add(projectObjectElement); 301 orElement.add(fileObjectElement); 302 andElement.add(resourceObjectElement); 303 andElement.add(orElement); 304 enablementElement.add(andElement); 305 decoratorElement.add(enablementElement); 306 307 extension.add(decoratorElement); 308 if (!extension.isInTheModel()) 309 plugin.add(extension); 310 } 311 312 315 public String [] getNewFiles() { 316 return new String [] { "icons/" }; } 318 319 322 protected String getFormattedPackageName(String id) { 323 String packageName = super.getFormattedPackageName(id); 326 if (packageName.length() != 0) 327 return packageName + ".decorators"; return "decorators"; } 330 331 334 public String getUsedExtensionPoint() { 335 return "org.eclipse.ui.decorators"; } 337 338 347 protected String [][] fromCommaSeparated(String iconLocations) { 348 StringTokenizer tokens = new StringTokenizer (iconLocations, ","); String [][] choices = new String [tokens.countTokens() / 2][2]; 350 int x = 0, y = 0; 351 while (tokens.hasMoreTokens()) { 352 choices[x][y++] = tokens.nextToken(); 353 choices[x++][y--] = tokens.nextToken(); 354 } 355 return choices; 356 } 357 } 358 | Popular Tags |