1 11 package org.eclipse.pde.internal.ui.templates.ide; 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.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 import org.eclipse.pde.ui.templates.TemplateOption; 33 34 public class DecoratorTemplate extends PDETemplateSection { 35 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"; 40 private WizardPage page; 41 private TemplateOption packageOption; 42 private TemplateOption classOption; 43 private BooleanOption projectOption; 44 private BooleanOption readOnlyOption; 45 46 49 public DecoratorTemplate() { 50 setPageCount(1); 51 createOptions(); 52 alterOptionStates(); 53 } 54 55 58 public IPluginReference[] getDependencies(String schemaVersion) { 59 if (schemaVersion != null) { 61 IPluginReference[] dep = new IPluginReference[1]; 62 dep[0] = new PluginReference("org.eclipse.core.resources", null, 0); return dep; 64 } 65 return super.getDependencies(schemaVersion); 66 } 67 68 72 public String getSectionId() { 73 return "decorator"; } 78 79 82 public int getNumberOfWorkUnits() { 83 return super.getNumberOfWorkUnits() + 1; 84 } 85 86 91 private void createOptions() { 92 String [][] choices = fromCommaSeparated(PDETemplateMessages.DecoratorTemplate_placementChoices); 93 94 addOption(DECORATOR_ICON_PLACEMENT, 95 PDETemplateMessages.DecoratorTemplate_placement, 96 choices, 97 choices[0][0], 98 0); 99 100 projectOption = (BooleanOption) addOption(DECORATOR_BLN_PROJECT, 101 PDETemplateMessages.DecoratorTemplate_decorateProject, true, 0); 102 103 readOnlyOption = (BooleanOption) addOption(DECORATOR_BLN_READONLY, 104 PDETemplateMessages.DecoratorTemplate_decorateReadOnly, false, 0); 105 106 packageOption = addOption( 107 KEY_PACKAGE_NAME, 108 PDETemplateMessages.DecoratorTemplate_packageName, 109 (String ) null, 110 0); 111 classOption = addOption( 112 DECORATOR_CLASS_NAME, 113 PDETemplateMessages.DecoratorTemplate_decoratorClass, 114 "ReadOnly", 0); 116 } 117 118 121 public void addPages(Wizard wizard) { 122 int pageIndex = 0; 123 124 page = createPage(pageIndex, IHelpContextIds.TEMPLATE_EDITOR); 125 page.setTitle(PDETemplateMessages.DecoratorTemplate_title); 126 page.setDescription(PDETemplateMessages.DecoratorTemplate_desc); 127 128 wizard.addPage(page); 129 markPagesAdded(); 130 } 131 132 private void alterOptionStates() { 133 projectOption.setEnabled(!readOnlyOption.isSelected()); 134 packageOption.setEnabled(!projectOption.isEnabled()); 135 classOption.setEnabled(!projectOption.isEnabled()); 136 } 137 138 141 protected boolean isOkToCreateFolder(File sourceFolder) { 142 boolean isOk = true; 144 String folderName = sourceFolder.getName(); 145 if (folderName.equals("java")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 147 } 148 return isOk; 149 } 150 151 154 protected boolean isOkToCreateFile(File sourceFile) { 155 boolean isOk = true; 157 String fileName = sourceFile.getName(); 158 if (fileName.equals("read_only.gif")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 160 } else if (fileName.equals("sample_decorator.gif")) { isOk = !readOnlyOption.isSelected(); 162 } else if (fileName.equals("$decoratorClassName$.java")) { isOk = readOnlyOption.isEnabled() && readOnlyOption.isSelected(); 164 } 165 return isOk; 166 } 167 168 171 public void validateOptions(TemplateOption source) { 172 if (source == readOnlyOption){ 173 alterOptionStates(); 174 } 175 super.validateOptions(source); 176 } 177 178 181 public boolean isDependentOnParentWizard() { 182 return true; 183 } 184 185 188 protected void initializeFields(IFieldData data) { 189 String id = data.getId(); 192 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 193 } 194 195 198 public void initializeFields(IPluginModelBase model) { 199 String pluginId = model.getPluginBase().getId(); 202 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 203 } 204 205 208 protected void updateModel(IProgressMonitor monitor) throws CoreException { 209 IPluginBase plugin = model.getPluginBase(); 214 IPluginExtension extension = createExtension(getUsedExtensionPoint(),true); 215 IPluginModelFactory factory = model.getPluginFactory(); 216 217 IPluginElement decoratorElement = factory.createElement(extension); 218 decoratorElement.setName("decorator"); decoratorElement.setAttribute("adaptable", "true"); decoratorElement.setAttribute("state", "true"); decoratorElement.setAttribute("lightweight", "true"); 223 if(!readOnlyOption.isSelected()){ 224 decoratorElement.setAttribute( 225 "id", plugin.getId() + "." + getSectionId()); decoratorElement.setAttribute( 227 "label", PDETemplateMessages.DecoratorTemplate_resourceLabel); decoratorElement.setAttribute("icon", "icons/sample_decorator.gif"); decoratorElement.setAttribute( 230 "location", getValue(DECORATOR_ICON_PLACEMENT).toString()); } 232 else { 233 decoratorElement.setAttribute( 234 "id", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); decoratorElement.setAttribute( 236 "label", PDETemplateMessages.DecoratorTemplate_readOnlyLabel); decoratorElement.setAttribute( 238 "class", getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(DECORATOR_CLASS_NAME)); } 240 241 IPluginElement enablementElement = factory 242 .createElement(decoratorElement); 243 enablementElement.setName("enablement"); 245 IPluginElement andElement = factory.createElement(enablementElement); 246 andElement.setName("and"); 248 IPluginElement resourceObjectElement = factory.createElement(andElement); 249 resourceObjectElement.setName("objectClass"); resourceObjectElement.setAttribute( 251 "name", "org.eclipse.core.resources.IResource"); 253 IPluginElement orElement = factory.createElement(andElement); 254 orElement.setName("or"); 256 IPluginElement fileObjectElement = factory.createElement(orElement); 257 fileObjectElement.setName("objectClass"); fileObjectElement.setAttribute( 259 "name", "org.eclipse.core.resources.IFile"); 261 IPluginElement folderObjectElement = factory.createElement(orElement); 262 folderObjectElement.setName("objectClass"); folderObjectElement.setAttribute( 264 "name", "org.eclipse.core.resources.IFolder"); 266 IPluginElement projectObjectElement = factory.createElement(orElement); 267 projectObjectElement.setName("objectClass"); projectObjectElement.setAttribute( 269 "name", "org.eclipse.core.resources.IProject"); 271 if(readOnlyOption.isSelected()) 272 orElement.add(folderObjectElement); 273 else if (projectOption.isSelected()) 274 orElement.add(projectObjectElement); 275 orElement.add(fileObjectElement); 276 andElement.add(resourceObjectElement); 277 andElement.add(orElement); 278 enablementElement.add(andElement); 279 decoratorElement.add(enablementElement); 280 281 extension.add(decoratorElement); 282 if (!extension.isInTheModel()) 283 plugin.add(extension); 284 } 285 286 289 public String [] getNewFiles() { 290 return new String [] { "icons/" }; } 292 293 296 protected String getFormattedPackageName(String id) { 297 String packageName = super.getFormattedPackageName(id); 300 if (packageName.length() != 0) 301 return packageName + ".decorators"; return "decorators"; } 304 305 308 public String getUsedExtensionPoint() { 309 return "org.eclipse.ui.decorators"; } 311 312 321 protected String [][] fromCommaSeparated(String iconLocations) { 322 StringTokenizer tokens = new StringTokenizer (iconLocations, ","); String [][] choices = new String [tokens.countTokens() / 2][2]; 324 int x = 0, y = 0; 325 while (tokens.hasMoreTokens()) { 326 choices[x][y++] = tokens.nextToken(); 327 choices[x++][y--] = tokens.nextToken(); 328 } 329 return choices; 330 } 331 } 332 | Popular Tags |