1 11 12 package org.eclipse.pde.internal.ui.wizards.templates; 13 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.IPluginAttribute; 21 import org.eclipse.pde.core.plugin.IPluginBase; 22 import org.eclipse.pde.core.plugin.IPluginElement; 23 import org.eclipse.pde.core.plugin.IPluginExtension; 24 import org.eclipse.pde.core.plugin.IPluginModelBase; 25 import org.eclipse.pde.core.plugin.IPluginModelFactory; 26 import org.eclipse.pde.core.plugin.IPluginObject; 27 import org.eclipse.pde.core.plugin.IPluginReference; 28 import org.eclipse.pde.internal.ui.IHelpContextIds; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 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 ViewTemplate extends PDETemplateSection { 35 private BooleanOption addToPerspective; 36 39 public ViewTemplate() { 40 setPageCount(2); 41 createOptions(); 42 } 43 44 public String getSectionId() { 45 return "view"; } 47 50 public int getNumberOfWorkUnits() { 51 return super.getNumberOfWorkUnits()+1; 52 } 53 54 private void createOptions() { 55 addOption(KEY_PACKAGE_NAME, PDEUIMessages.ViewTemplate_packageName, (String )null, 0); 57 addOption("className", PDEUIMessages.ViewTemplate_className, "SampleView", 0); addOption("viewName", PDEUIMessages.ViewTemplate_name, PDEUIMessages.ViewTemplate_defaultName, 0); addOption("viewCategoryId", PDEUIMessages.ViewTemplate_categoryId, (String )null, 0); addOption("viewCategoryName", PDEUIMessages.ViewTemplate_categoryName, PDEUIMessages.ViewTemplate_defaultCategoryName, 0); addOption("viewType", PDEUIMessages.ViewTemplate_select, new String [][] { 63 {"tableViewer", PDEUIMessages.ViewTemplate_table}, {"treeViewer", PDEUIMessages.ViewTemplate_tree}}, "tableViewer", 0); addToPerspective = (BooleanOption)addOption("addToPerspective",PDEUIMessages.ViewTemplate_addToPerspective,true,0); addOption("doubleClick", PDEUIMessages.ViewTemplate_doubleClick, true, 1); addOption("popup", PDEUIMessages.ViewTemplate_popup, true, 1); addOption("localToolbar", PDEUIMessages.ViewTemplate_toolbar, true, 1); addOption("localPulldown", PDEUIMessages.ViewTemplate_pulldown, true, 1); addOption("sorter", PDEUIMessages.ViewTemplate_sorting, true, 1); } 75 76 protected void initializeFields(IFieldData data) { 77 String id = data.getId(); 80 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 81 initializeOption("viewCategoryId", id); } 83 public void initializeFields(IPluginModelBase model) { 84 String pluginId = model.getPluginBase().getId(); 87 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 88 initializeOption("viewCategoryId", pluginId); } 90 91 public boolean isDependentOnParentWizard() { 92 return true; 93 } 94 95 public void addPages(Wizard wizard) { 96 WizardPage page0 = createPage(0, IHelpContextIds.TEMPLATE_VIEW); 97 page0.setTitle(PDEUIMessages.ViewTemplate_title0); 98 page0.setDescription(PDEUIMessages.ViewTemplate_desc0); 99 wizard.addPage(page0); 100 101 WizardPage page1 = createPage(1, IHelpContextIds.TEMPLATE_VIEW); 102 page1.setTitle(PDEUIMessages.ViewTemplate_title1); 103 page1.setDescription(PDEUIMessages.ViewTemplate_desc1); 104 wizard.addPage(page1); 105 markPagesAdded(); 106 } 107 108 public void validateOptions(TemplateOption source) { 109 if (source.isRequired() && source.isEmpty()) { 110 flagMissingRequiredOption(source); 111 } else { 112 validateContainerPage(source); 113 } 114 } 115 116 private void validateContainerPage(TemplateOption source) { 117 TemplateOption[] allPageOptions = getOptions(0); 118 for (int i = 0; i < allPageOptions.length; i++) { 119 TemplateOption nextOption = allPageOptions[i]; 120 if (nextOption.isRequired() && nextOption.isEmpty()) { 121 flagMissingRequiredOption(nextOption); 122 return; 123 } 124 } 125 resetPageState(); 126 } 127 128 public String getUsedExtensionPoint() { 129 return "org.eclipse.ui.views"; } 131 132 protected void updateModel(IProgressMonitor monitor) throws CoreException { 133 IPluginBase plugin = model.getPluginBase(); 134 IPluginExtension extension = createExtension("org.eclipse.ui.views", true); IPluginModelFactory factory = model.getPluginFactory(); 136 137 String cid = getStringOption("viewCategoryId"); 139 createCategory(extension, cid); 140 String fullClassName = getStringOption(KEY_PACKAGE_NAME)+"."+getStringOption("className"); 142 IPluginElement viewElement = factory.createElement(extension); 143 viewElement.setName("view"); viewElement.setAttribute("id", fullClassName); viewElement.setAttribute("name", getStringOption("viewName")); viewElement.setAttribute("icon", "icons/sample.gif"); 148 viewElement.setAttribute("class", fullClassName); viewElement.setAttribute("category", cid); extension.add(viewElement); 151 if (!extension.isInTheModel()) 152 plugin.add(extension); 153 154 if (addToPerspective.isSelected()) { 155 IPluginExtension perspectiveExtension = 156 createExtension("org.eclipse.ui.perspectiveExtensions", true); 158 IPluginElement perspectiveElement = factory.createElement(perspectiveExtension); 159 perspectiveElement.setName("perspectiveExtension"); perspectiveElement.setAttribute( 161 "targetID", "org.eclipse.ui.resourcePerspective"); 164 IPluginElement view = factory.createElement(perspectiveElement); 165 view.setName("view"); view.setAttribute("id", fullClassName); view.setAttribute("relative", "org.eclipse.ui.views.TaskList"); view.setAttribute("relationship","right"); view.setAttribute("ratio", "0.5"); perspectiveElement.add(view); 171 172 perspectiveExtension.add(perspectiveElement); 173 if (!perspectiveExtension.isInTheModel()) 174 plugin.add(perspectiveExtension); 175 } 176 } 177 178 private void createCategory(IPluginExtension extension, String id) throws CoreException { 179 IPluginObject [] elements = extension.getChildren(); 180 for (int i=0; i<elements.length; i++) { 181 IPluginElement element = (IPluginElement)elements[i]; 182 if (element.getName().equalsIgnoreCase("category")) { IPluginAttribute att = element.getAttribute("id"); if (att!=null) { 185 String cid = att.getValue(); 186 if (cid!=null && cid.equals(id)) 187 return; 188 } 189 } 190 } 191 IPluginElement categoryElement = model.getFactory().createElement(extension); 192 categoryElement.setName("category"); categoryElement.setAttribute("name", getStringOption("viewCategoryName")); categoryElement.setAttribute("id", id); extension.add(categoryElement); 196 } 197 198 201 public String [] getNewFiles() { 202 return new String [] {"icons/"}; } 204 205 208 public IPluginReference[] getDependencies(String schemaVersion) { 209 ArrayList result = new ArrayList (); 210 if (schemaVersion != null) 211 result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); result.add(new PluginReference("org.eclipse.ui", null, 0)); return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]); 214 } 215 216 219 protected String getFormattedPackageName(String id) { 220 String packageName = super.getFormattedPackageName(id); 221 if (packageName.length() != 0) 222 return packageName + ".views"; return "views"; } 225 } 226 | Popular Tags |