1 /*******************************************************************************
2  *  Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 473694, 486261
14  *******************************************************************************/
15 
16 package org.eclipse.pde.internal.ui.templates.ide;
17 
18 import java.io.File;
19 import java.util.ArrayList;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.wizard.Wizard;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.pde.core.plugin.*;
25 import org.eclipse.pde.internal.core.ibundle.IBundle;
26 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
27 import org.eclipse.pde.internal.ui.templates.*;
28 import org.eclipse.pde.ui.IFieldData;
29 import org.eclipse.pde.ui.templates.*;
30 import org.osgi.framework.Constants;
31 
32 public class ViewTemplate extends PDETemplateSection {
33 	private BooleanOption addToPerspective;
34 	private BooleanOption contextHelp;
35 
36 	/**
37 	 * Constructor for HelloWorldTemplate.
38 	 */
ViewTemplate()39 	public ViewTemplate() {
40 		setPageCount(1);
41 		createOptions();
42 	}
43 
44 	@Override
getSectionId()45 	public String getSectionId() {
46 		return "view"; //$NON-NLS-1$
47 	}
48 
49 	@Override
getNumberOfWorkUnits()50 	public int getNumberOfWorkUnits() {
51 		return super.getNumberOfWorkUnits() + 1;
52 	}
53 
createOptions()54 	private void createOptions() {
55 		// first page
56 		addOption(KEY_PACKAGE_NAME, PDETemplateMessages.ViewTemplate_packageName, (String) null, 0);
57 		addOption("className", PDETemplateMessages.ViewTemplate_className, "SampleView", 0); //$NON-NLS-1$ //$NON-NLS-2$
58 		addOption("viewName", PDETemplateMessages.ViewTemplate_name, PDETemplateMessages.ViewTemplate_defaultName, 0); //$NON-NLS-1$
59 		addOption("viewCategoryId", PDETemplateMessages.ViewTemplate_categoryId, (String) null, 0); //$NON-NLS-1$
60 		addOption("viewCategoryName", PDETemplateMessages.ViewTemplate_categoryName, PDETemplateMessages.ViewTemplate_defaultCategoryName, 0); //$NON-NLS-1$
61 		addOption("viewType", PDETemplateMessages.ViewTemplate_select, //$NON-NLS-1$
62 				new String[][] { {"tableViewer", PDETemplateMessages.ViewTemplate_table}, //$NON-NLS-1$
63 						{"treeViewer", PDETemplateMessages.ViewTemplate_tree}}, //$NON-NLS-1$
64 				"tableViewer", 0); //$NON-NLS-1$
65 		addOption("addViewID", PDETemplateMessages.ViewTemplate_addViewID, true, 0); //$NON-NLS-1$
66 		addToPerspective = (BooleanOption) addOption("addToPerspective", PDETemplateMessages.ViewTemplate_addToPerspective, true, 0); //$NON-NLS-1$
67 		contextHelp = (BooleanOption) addOption("contextHelp", PDETemplateMessages.ViewTemplate_contextHelp, true, 0); //$NON-NLS-1$
68 	}
69 
70 	@Override
initializeFields(IFieldData data)71 	protected void initializeFields(IFieldData data) {
72 		// In a new project wizard, we don't know this yet - the
73 		// model has not been created
74 		initializeFields(data.getId());
75 
76 	}
77 
78 	@Override
initializeFields(IPluginModelBase model)79 	public void initializeFields(IPluginModelBase model) {
80 		// In the new extension wizard, the model exists so
81 		// we can initialize directly from it
82 		initializeFields(model.getPluginBase().getId());
83 	}
84 
initializeFields(String id)85 	public void initializeFields(String id) {
86 		initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
87 		initializeOption("viewCategoryId", id); //$NON-NLS-1$
88 	}
89 
90 	@Override
isDependentOnParentWizard()91 	public boolean isDependentOnParentWizard() {
92 		return true;
93 	}
94 
95 	@Override
addPages(Wizard wizard)96 	public void addPages(Wizard wizard) {
97 		WizardPage page0 = createPage(0, IHelpContextIds.TEMPLATE_VIEW);
98 		page0.setTitle(PDETemplateMessages.ViewTemplate_title0);
99 		page0.setDescription(PDETemplateMessages.ViewTemplate_desc0);
100 		wizard.addPage(page0);
101 
102 		markPagesAdded();
103 	}
104 
105 	/**
106 	 * @see AbstractTemplateSection#isOkToCreateFile(File)
107 	 */
108 	@Override
isOkToCreateFile(File sourceFile)109 	protected boolean isOkToCreateFile(File sourceFile) {
110 		boolean isOk = true;
111 		String fileName = sourceFile.getName();
112 		if (fileName.equals("contexts.xml")) { //$NON-NLS-1$
113 			isOk = contextHelp.isSelected();
114 		}
115 		return isOk;
116 	}
117 
118 	@Override
getUsedExtensionPoint()119 	public String getUsedExtensionPoint() {
120 		return "org.eclipse.ui.views"; //$NON-NLS-1$
121 	}
122 
123 	@Override
updateModel(IProgressMonitor monitor)124 	protected void updateModel(IProgressMonitor monitor) throws CoreException {
125 
126 		IBundle bundle = ((IBundlePluginModelBase) model).getBundleModel().getBundle();
127 		bundle.setHeader(Constants.IMPORT_PACKAGE, "javax.inject"); //$NON-NLS-1$
128 
129 		IPluginBase plugin = model.getPluginBase();
130 		IPluginExtension extension = createExtension("org.eclipse.ui.views", true); //$NON-NLS-1$
131 		IPluginModelFactory factory = model.getPluginFactory();
132 
133 		String cid = getStringOption("viewCategoryId"); //$NON-NLS-1$
134 
135 		createCategory(extension, cid);
136 		String fullClassName = getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption("className"); //$NON-NLS-1$ //$NON-NLS-2$
137 
138 		IPluginElement viewElement = factory.createElement(extension);
139 		viewElement.setName("view"); //$NON-NLS-1$
140 		viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
141 		viewElement.setAttribute("name", getStringOption("viewName")); //$NON-NLS-1$ //$NON-NLS-2$
142 		viewElement.setAttribute("icon", "icons/sample.png"); //$NON-NLS-1$ //$NON-NLS-2$
143 
144 		viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
145 		viewElement.setAttribute("category", cid); //$NON-NLS-1$
146 		viewElement.setAttribute("inject", "true"); //$NON-NLS-1$ //$NON-NLS-2$
147 		extension.add(viewElement);
148 		if (!extension.isInTheModel())
149 			plugin.add(extension);
150 
151 		if (addToPerspective.isSelected()) {
152 			IPluginExtension perspectiveExtension = createExtension("org.eclipse.ui.perspectiveExtensions", true); //$NON-NLS-1$
153 
154 			IPluginElement perspectiveElement = factory.createElement(perspectiveExtension);
155 			perspectiveElement.setName("perspectiveExtension"); //$NON-NLS-1$
156 			perspectiveElement.setAttribute("targetID", //$NON-NLS-1$
157 					"org.eclipse.jdt.ui.JavaPerspective"); //$NON-NLS-1$
158 
159 			IPluginElement view = factory.createElement(perspectiveElement);
160 			view.setName("view"); //$NON-NLS-1$
161 			view.setAttribute("id", fullClassName); //$NON-NLS-1$
162 			view.setAttribute("relative", "org.eclipse.ui.views.ProblemView"); //$NON-NLS-1$ //$NON-NLS-2$
163 			view.setAttribute("relationship", "right"); //$NON-NLS-1$ //$NON-NLS-2$
164 			view.setAttribute("ratio", "0.5"); //$NON-NLS-1$ //$NON-NLS-2$
165 			perspectiveElement.add(view);
166 
167 			perspectiveExtension.add(perspectiveElement);
168 			if (!perspectiveExtension.isInTheModel())
169 				plugin.add(perspectiveExtension);
170 		}
171 
172 		if (contextHelp.isSelected()) {
173 			IPluginExtension contextExtension = createExtension("org.eclipse.help.contexts", true); //$NON-NLS-1$
174 
175 			IPluginElement contextsElement = factory.createElement(contextExtension);
176 			contextsElement.setName("contexts"); //$NON-NLS-1$
177 			contextsElement.setAttribute("file", "contexts.xml"); //$NON-NLS-1$ //$NON-NLS-2$
178 			contextExtension.add(contextsElement);
179 			if (!contextExtension.isInTheModel())
180 				plugin.add(contextExtension);
181 		}
182 	}
183 
createCategory(IPluginExtension extension, String id)184 	private void createCategory(IPluginExtension extension, String id) throws CoreException {
185 		IPluginObject[] children = extension.getChildren();
186 		for (IPluginObject child : children) {
187 			IPluginElement element = (IPluginElement) child;
188 			if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
189 				IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
190 				if (att != null) {
191 					String cid = att.getValue();
192 					if (cid != null && cid.equals(id))
193 						return;
194 				}
195 			}
196 		}
197 		IPluginElement categoryElement = model.getFactory().createElement(extension);
198 		categoryElement.setName("category"); //$NON-NLS-1$
199 		categoryElement.setAttribute("name", getStringOption("viewCategoryName")); //$NON-NLS-1$ //$NON-NLS-2$
200 		categoryElement.setAttribute("id", id); //$NON-NLS-1$
201 		extension.add(categoryElement);
202 	}
203 
204 	@Override
getNewFiles()205 	public String[] getNewFiles() {
206 		if (contextHelp.isSelected())
207 			return new String[] {"icons/", "contexts.xml"}; //$NON-NLS-1$ //$NON-NLS-2$
208 		return new String[] {"icons/"}; //$NON-NLS-1$
209 	}
210 
211 	@Override
getDependencies(String schemaVersion)212 	public IPluginReference[] getDependencies(String schemaVersion) {
213 		ArrayList<PluginReference> result = new ArrayList<>();
214 		if (schemaVersion != null)
215 			result.add(new PluginReference("org.eclipse.core.runtime")); //$NON-NLS-1$
216 		result.add(new PluginReference("org.eclipse.ui")); //$NON-NLS-1$
217 		return result.toArray(new IPluginReference[result.size()]);
218 	}
219 
220 	@Override
getFormattedPackageName(String id)221 	protected String getFormattedPackageName(String id) {
222 		String packageName = super.getFormattedPackageName(id);
223 		if (packageName.length() != 0)
224 			return packageName + ".views"; //$NON-NLS-1$
225 		return "views"; //$NON-NLS-1$
226 	}
227 
228 	@Override
getValue(String name)229 	public Object getValue(String name) {
230 		if (name.equals("useEnablement")) //$NON-NLS-1$
231 			return Boolean.valueOf(getTargetVersion() >= 3.3);
232 		return super.getValue(name);
233 	}
234 }
235