1 /*******************************************************************************
2  * Copyright (c) 2005, 2015 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  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.wizards.product;
15 
16 import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
17 
18 import java.util.Set;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.pde.core.plugin.*;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.core.TargetPlatformHelper;
27 import org.eclipse.pde.internal.core.iproduct.IProduct;
28 import org.eclipse.pde.internal.ui.IHelpContextIds;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.dialogs.PluginSelectionDialog;
31 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
32 import org.eclipse.pde.internal.ui.util.SWTUtil;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.*;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.forms.events.HyperlinkEvent;
40 import org.eclipse.ui.forms.events.IHyperlinkListener;
41 import org.eclipse.ui.forms.widgets.FormText;
42 import org.eclipse.ui.forms.widgets.FormToolkit;
43 
44 public class ProductDefinitonWizardPage extends WizardPage implements IHyperlinkListener {
45 
46 	private Text fProductName;
47 	private Text fPluginText;
48 	private Text fProductText;
49 	private Set<?> fProductSet;
50 	private Combo fApplicationCombo;
51 	private IProduct fProduct;
52 
53 	private ModifyListener fListener = e -> validatePage();
54 
ProductDefinitonWizardPage(String pageName, IProduct product)55 	public ProductDefinitonWizardPage(String pageName, IProduct product) {
56 		super(pageName);
57 		fProduct = product;
58 		setTitle(PDEUIMessages.ProductDefinitonWizardPage_title);
59 		if (productNameDefined())
60 			setDescription(PDEUIMessages.ProductDefinitonWizardPage_desc);
61 		else
62 			setDescription(PDEUIMessages.ProductDefinitonWizardPage_descNoName);
63 	}
64 
65 	@Override
createControl(Composite parent)66 	public void createControl(Composite parent) {
67 		Composite comp = new Composite(parent, SWT.NONE);
68 		GridLayout layout = new GridLayout();
69 		layout.verticalSpacing = 20;
70 		comp.setLayout(layout);
71 
72 		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
73 		createProductGroup(toolkit, comp);
74 		createApplicationGroup(toolkit, comp);
75 		toolkit.dispose();
76 		setControl(comp);
77 		setPageComplete(getPluginId() != null && productNameDefined());
78 		Dialog.applyDialogFont(comp);
79 		PlatformUI.getWorkbench().getHelpSystem().setHelp(comp, IHelpContextIds.PRODUCT_DEFINITIONS_WIZARD);
80 	}
81 
createFormText(FormToolkit toolkit, Composite parent, String content, int span)82 	private void createFormText(FormToolkit toolkit, Composite parent, String content, int span) {
83 		FormText text = toolkit.createFormText(parent, false);
84 		text.setText(content, true, false);
85 		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
86 		gd.horizontalSpan = span;
87 		gd.widthHint = 400;
88 		text.setLayoutData(gd);
89 		text.setBackground(parent.getBackground());
90 		text.addHyperlinkListener(this);
91 	}
92 
createProductGroup(FormToolkit toolkit, Composite comp)93 	private void createProductGroup(FormToolkit toolkit, Composite comp) {
94 		Group group = new Group(comp, SWT.NONE);
95 		group.setText(PDEUIMessages.ProductDefinitonWizardPage_productGroup);
96 		group.setLayout(new GridLayout(3, false));
97 		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
98 
99 		createFormText(toolkit, group, PDEUIMessages.ProductDefinitonWizardPage_productDefinition, 3);
100 
101 		Label label;
102 		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
103 		gd.horizontalSpan = 2;
104 
105 		if (!productNameDefined()) {
106 			label = new Label(group, SWT.NONE);
107 			label.setText(PDEUIMessages.ProductDefinitonWizardPage_productName);
108 
109 			fProductName = new Text(group, SWT.SINGLE | SWT.BORDER);
110 			fProductName.setLayoutData(gd);
111 			fProductName.addModifyListener(fListener);
112 		}
113 
114 		label = new Label(group, SWT.NONE);
115 		label.setText(PDEUIMessages.ProductDefinitonWizardPage_plugin);
116 
117 		fPluginText = new Text(group, SWT.SINGLE | SWT.BORDER);
118 		fPluginText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119 
120 		Button button = new Button(group, SWT.PUSH);
121 		button.setText(PDEUIMessages.ProductDefinitonWizardPage_browse);
122 		SWTUtil.setButtonDimensionHint(button);
123 		button.addSelectionListener(widgetSelectedAdapter(e -> handleBrowse()));
124 
125 		label = new Label(group, SWT.NONE);
126 		label.setText(PDEUIMessages.ProductDefinitonWizardPage_productId);
127 
128 		fProductText = new Text(group, SWT.SINGLE | SWT.BORDER);
129 		fProductText.setLayoutData(gd);
130 
131 		String pluginId = getPluginId();
132 		if (pluginId != null) {
133 			fPluginText.setText(pluginId);
134 			String productId = "product"; //$NON-NLS-1$
135 			String numString = ""; //$NON-NLS-1$
136 			int idNum = 1;
137 			while (getProductNameSet().contains(pluginId + "." + productId + numString)) { //$NON-NLS-1$
138 				numString = Integer.toString(idNum++);
139 			}
140 			fProductText.setText(productId + numString);
141 		}
142 		fPluginText.addModifyListener(fListener);
143 		fProductText.addModifyListener(fListener);
144 
145 	}
146 
createApplicationGroup(FormToolkit toolkit, Composite comp)147 	private void createApplicationGroup(FormToolkit toolkit, Composite comp) {
148 		Group group = new Group(comp, SWT.NONE);
149 		group.setText(PDEUIMessages.ProductDefinitonWizardPage_applicationGroup);
150 		group.setLayout(new GridLayout(2, false));
151 		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
152 
153 		createFormText(toolkit, group, PDEUIMessages.ProductDefinitonWizardPage_applicationDefinition, 2);
154 
155 		Label label = new Label(group, SWT.NONE);
156 		label.setText(PDEUIMessages.ProductDefinitonWizardPage_application);
157 
158 		fApplicationCombo = new Combo(group, SWT.SINGLE | SWT.READ_ONLY);
159 		fApplicationCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
160 		fApplicationCombo.setItems(TargetPlatform.getApplications());
161 		if (fApplicationCombo.getItemCount() > 0)
162 			fApplicationCombo.setText(fApplicationCombo.getItem(0));
163 	}
164 
165 	@Override
setVisible(boolean visible)166 	public void setVisible(boolean visible) {
167 		if (visible) {
168 			if (fProductName != null)
169 				fProductName.setFocus();
170 			else
171 				fPluginText.setFocus();
172 		}
173 		super.setVisible(visible);
174 	}
175 
validatePage()176 	private void validatePage() {
177 		String error = null;
178 		String productName = getProductName();
179 		if (productName != null && productName.length() == 0) {
180 			error = PDEUIMessages.ProductDefinitonWizardPage_noProductName;
181 		}
182 		validateIdAndProduct(error);
183 	}
184 
validateIdAndProduct(String error)185 	private void validateIdAndProduct(String error) {
186 		if (error == null) {
187 			String pluginId = getDefiningPlugin();
188 			IPluginModelBase model = PluginRegistry.findModel(pluginId);
189 			if (pluginId.length() == 0) {
190 				error = PDEUIMessages.ProductDefinitonWizardPage_noPluginId;
191 			} else if (model == null) {
192 				error = PDEUIMessages.ProductDefinitonWizardPage_noPlugin;
193 			} else if (model.getUnderlyingResource() == null) {
194 				error = PDEUIMessages.ProductDefinitonWizardPage_notInWorkspace;
195 			}
196 			if (error == null)
197 				error = validateId();
198 			if (error == null && getProductNameSet().contains(pluginId + "." + fProductText.getText().trim())) { //$NON-NLS-1$
199 				error = PDEUIMessages.ProductDefinitonWizardPage_productExists;
200 			}
201 		}
202 		setErrorMessage(error);
203 		setPageComplete(error == null);
204 	}
205 
validateId()206 	private String validateId() {
207 		String id = fProductText.getText().trim();
208 		if (id.length() == 0)
209 			return PDEUIMessages.ProductDefinitonWizardPage_noProductID;
210 
211 		for (int i = 0; i < id.length(); i++) {
212 			if (!id.substring(i, i + 1).matches("[a-zA-Z0-9_]")) //$NON-NLS-1$
213 				return PDEUIMessages.ProductDefinitonWizardPage_invalidId;
214 		}
215 		return null;
216 	}
217 
218 	@Override
linkEntered(HyperlinkEvent e)219 	public void linkEntered(HyperlinkEvent e) {
220 	}
221 
222 	@Override
linkExited(HyperlinkEvent e)223 	public void linkExited(HyperlinkEvent e) {
224 	}
225 
226 	@Override
linkActivated(HyperlinkEvent e)227 	public void linkActivated(HyperlinkEvent e) {
228 		String extPoint = Platform.PI_RUNTIME + "." + e.getHref().toString(); //$NON-NLS-1$
229 		IPluginExtensionPoint point = PDECore.getDefault().getExtensionsRegistry().findExtensionPoint(extPoint);
230 		if (point != null)
231 			new ShowDescriptionAction(point, true).run();
232 	}
233 
handleBrowse()234 	private void handleBrowse() {
235 		PluginSelectionDialog dialog = new PluginSelectionDialog(getShell(), PluginRegistry.getWorkspaceModels(), false);
236 		if (dialog.open() == Window.OK) {
237 			IPluginModelBase model = (IPluginModelBase) dialog.getFirstResult();
238 			fPluginText.setText(model.getPluginBase().getId());
239 		}
240 	}
241 
getProductNameSet()242 	private Set<?> getProductNameSet() {
243 		if (fProductSet == null)
244 			fProductSet = TargetPlatformHelper.getProductNameSet();
245 		return fProductSet;
246 	}
247 
getDefiningPlugin()248 	public String getDefiningPlugin() {
249 		return fPluginText.getText().trim();
250 	}
251 
getProductId()252 	public String getProductId() {
253 		return fProductText.getText().trim();
254 	}
255 
getApplication()256 	public String getApplication() {
257 		return fApplicationCombo.getText();
258 	}
259 
getProductName()260 	public String getProductName() {
261 		return (fProductName == null) ? null : fProductName.getText().trim();
262 	}
263 
productNameDefined()264 	private boolean productNameDefined() {
265 		return (fProduct.getName() != null && !fProduct.getName().equals("")); //$NON-NLS-1$
266 	}
267 
getPluginId()268 	private String getPluginId() {
269 		IProject project = fProduct.getModel().getUnderlyingResource().getProject();
270 		IPluginModelBase model = PluginRegistry.findModel(project);
271 		return (model == null) ? null : model.getPluginBase().getId();
272 	}
273 }
274