001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.product;
011:
012: import java.util.Set;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.window.Window;
018: import org.eclipse.jface.wizard.WizardPage;
019: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.core.plugin.PluginRegistry;
022: import org.eclipse.pde.core.plugin.TargetPlatform;
023: import org.eclipse.pde.internal.core.PDECore;
024: import org.eclipse.pde.internal.core.TargetPlatformHelper;
025: import org.eclipse.pde.internal.core.iproduct.IProduct;
026: import org.eclipse.pde.internal.ui.IHelpContextIds;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
029: import org.eclipse.pde.internal.ui.util.SWTUtil;
030: import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.ModifyEvent;
033: import org.eclipse.swt.events.ModifyListener;
034: import org.eclipse.swt.events.SelectionAdapter;
035: import org.eclipse.swt.events.SelectionEvent;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Combo;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Group;
042: import org.eclipse.swt.widgets.Label;
043: import org.eclipse.swt.widgets.Text;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.forms.events.HyperlinkEvent;
046: import org.eclipse.ui.forms.events.IHyperlinkListener;
047: import org.eclipse.ui.forms.widgets.FormText;
048: import org.eclipse.ui.forms.widgets.FormToolkit;
049:
050: public class ProductDefinitonWizardPage extends WizardPage implements
051: IHyperlinkListener {
052:
053: private Text fProductName;
054: private Text fPluginText;
055: private Text fProductText;
056: private Set fProductSet;
057: private Combo fApplicationCombo;
058: private IProduct fProduct;
059:
060: private ModifyListener fListener = new ModifyListener() {
061: public void modifyText(ModifyEvent e) {
062: validatePage();
063: }
064: };
065:
066: public ProductDefinitonWizardPage(String pageName, IProduct product) {
067: super (pageName);
068: fProduct = product;
069: setTitle(PDEUIMessages.ProductDefinitonWizardPage_title);
070: if (productNameDefined())
071: setDescription(PDEUIMessages.ProductDefinitonWizardPage_desc);
072: else
073: setDescription(PDEUIMessages.ProductDefinitonWizardPage_descNoName);
074: }
075:
076: public void createControl(Composite parent) {
077: Composite comp = new Composite(parent, SWT.NONE);
078: GridLayout layout = new GridLayout();
079: layout.verticalSpacing = 20;
080: comp.setLayout(layout);
081:
082: FormToolkit toolkit = new FormToolkit(parent.getDisplay());
083: createProductGroup(toolkit, comp);
084: createApplicationGroup(toolkit, comp);
085: toolkit.dispose();
086: setControl(comp);
087: setPageComplete(getPluginId() != null && productNameDefined());
088: Dialog.applyDialogFont(comp);
089: PlatformUI.getWorkbench().getHelpSystem().setHelp(comp,
090: IHelpContextIds.PRODUCT_DEFINITIONS_WIZARD);
091: }
092:
093: private void createFormText(FormToolkit toolkit, Composite parent,
094: String content, int span) {
095: FormText text = toolkit.createFormText(parent, false);
096: text.setText(content, true, false);
097: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
098: gd.horizontalSpan = span;
099: gd.widthHint = 400;
100: text.setLayoutData(gd);
101: text.setBackground(null);
102: text.addHyperlinkListener(this );
103: }
104:
105: private void createProductGroup(FormToolkit toolkit, Composite comp) {
106: Group group = new Group(comp, SWT.NONE);
107: group
108: .setText(PDEUIMessages.ProductDefinitonWizardPage_productGroup);
109: group.setLayout(new GridLayout(3, false));
110: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111:
112: createFormText(
113: toolkit,
114: group,
115: PDEUIMessages.ProductDefinitonWizardPage_productDefinition,
116: 3);
117:
118: Label label;
119: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
120: gd.horizontalSpan = 2;
121:
122: if (!productNameDefined()) { //$NON-NLS-1$
123: label = new Label(group, SWT.NONE);
124: label
125: .setText(PDEUIMessages.ProductDefinitonWizardPage_productName);
126:
127: fProductName = new Text(group, SWT.SINGLE | SWT.BORDER);
128: fProductName.setLayoutData(gd);
129: fProductName.addModifyListener(fListener);
130: }
131:
132: label = new Label(group, SWT.NONE);
133: label.setText(PDEUIMessages.ProductDefinitonWizardPage_plugin);
134:
135: fPluginText = new Text(group, SWT.SINGLE | SWT.BORDER);
136: fPluginText
137: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
138:
139: Button button = new Button(group, SWT.PUSH);
140: button.setText(PDEUIMessages.ProductDefinitonWizardPage_browse);
141: SWTUtil.setButtonDimensionHint(button);
142: button.addSelectionListener(new SelectionAdapter() {
143: public void widgetSelected(SelectionEvent e) {
144: handleBrowse();
145: }
146: });
147:
148: label = new Label(group, SWT.NONE);
149: label
150: .setText(PDEUIMessages.ProductDefinitonWizardPage_productId);
151:
152: fProductText = new Text(group, SWT.SINGLE | SWT.BORDER);
153: fProductText.setLayoutData(gd);
154:
155: String pluginId = getPluginId();
156: if (pluginId != null) {
157: fPluginText.setText(pluginId);
158: String productId = "product"; //$NON-NLS-1$
159: String numString = ""; //$NON-NLS-1$
160: int idNum = 1;
161: while (getProductNameSet().contains(
162: pluginId + "." + productId + numString)) { //$NON-NLS-1$
163: numString = Integer.toString(idNum++);
164: }
165: fProductText.setText(productId + numString);
166: }
167: fPluginText.addModifyListener(fListener);
168: fProductText.addModifyListener(fListener);
169:
170: }
171:
172: private void createApplicationGroup(FormToolkit toolkit,
173: Composite comp) {
174: Group group = new Group(comp, SWT.NONE);
175: group
176: .setText(PDEUIMessages.ProductDefinitonWizardPage_applicationGroup);
177: group.setLayout(new GridLayout(2, false));
178: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
179:
180: createFormText(
181: toolkit,
182: group,
183: PDEUIMessages.ProductDefinitonWizardPage_applicationDefinition,
184: 2);
185:
186: Label label = new Label(group, SWT.NONE);
187: label
188: .setText(PDEUIMessages.ProductDefinitonWizardPage_application);
189:
190: fApplicationCombo = new Combo(group, SWT.SINGLE | SWT.READ_ONLY);
191: fApplicationCombo.setLayoutData(new GridData(
192: GridData.FILL_HORIZONTAL));
193: fApplicationCombo.setItems(TargetPlatform.getApplications());
194: if (fApplicationCombo.getItemCount() > 0)
195: fApplicationCombo.setText(fApplicationCombo.getItem(0));
196: }
197:
198: public void setVisible(boolean visible) {
199: if (visible) {
200: if (fProductName != null)
201: fProductName.setFocus();
202: else
203: fPluginText.setFocus();
204: }
205: super .setVisible(visible);
206: }
207:
208: private void validatePage() {
209: String error = null;
210: String productName = getProductName();
211: if (productName != null && productName.length() == 0) {
212: error = PDEUIMessages.ProductDefinitonWizardPage_noProductName;
213: }
214: validateIdAndProduct(error);
215: }
216:
217: private void validateIdAndProduct(String error) {
218: if (error == null) {
219: String pluginId = getDefiningPlugin();
220: IPluginModelBase model = PluginRegistry.findModel(pluginId);
221: if (pluginId.length() == 0) {
222: error = PDEUIMessages.ProductDefinitonWizardPage_noPluginId;
223: } else if (model == null) {
224: error = PDEUIMessages.ProductDefinitonWizardPage_noPlugin;
225: } else if (model.getUnderlyingResource() == null) {
226: error = PDEUIMessages.ProductDefinitonWizardPage_notInWorkspace;
227: }
228: if (error == null)
229: error = validateId();
230: if (error == null
231: && getProductNameSet()
232: .contains(
233: pluginId
234: + "." + fProductText.getText().trim())) { //$NON-NLS-1$
235: error = PDEUIMessages.ProductDefinitonWizardPage_productExists;
236: }
237: }
238: setErrorMessage(error);
239: setPageComplete(error == null);
240: }
241:
242: private String validateId() {
243: String id = fProductText.getText().trim();
244: if (id.length() == 0)
245: return PDEUIMessages.ProductDefinitonWizardPage_noProductID;
246:
247: for (int i = 0; i < id.length(); i++) {
248: if (!id.substring(i, i + 1).matches("[a-zA-Z0-9_]")) //$NON-NLS-1$
249: return PDEUIMessages.ProductDefinitonWizardPage_invalidId;
250: }
251: return null;
252: }
253:
254: public void linkEntered(HyperlinkEvent e) {
255: }
256:
257: public void linkExited(HyperlinkEvent e) {
258: }
259:
260: public void linkActivated(HyperlinkEvent e) {
261: String extPoint = Platform.PI_RUNTIME
262: + "." + e.getHref().toString(); //$NON-NLS-1$
263: IPluginExtensionPoint point = PDECore.getDefault()
264: .getExtensionsRegistry().findExtensionPoint(extPoint);
265: if (point != null)
266: new ShowDescriptionAction(point, true).run();
267: }
268:
269: private void handleBrowse() {
270: PluginSelectionDialog dialog = new PluginSelectionDialog(
271: getShell(), PluginRegistry.getWorkspaceModels(), false);
272: if (dialog.open() == Window.OK) {
273: IPluginModelBase model = (IPluginModelBase) dialog
274: .getFirstResult();
275: fPluginText.setText(model.getPluginBase().getId());
276: }
277: }
278:
279: private Set getProductNameSet() {
280: if (fProductSet == null)
281: fProductSet = TargetPlatformHelper.getProductNameSet();
282: return fProductSet;
283: }
284:
285: public String getDefiningPlugin() {
286: return fPluginText.getText().trim();
287: }
288:
289: public String getProductId() {
290: return fProductText.getText().trim();
291: }
292:
293: public String getApplication() {
294: return fApplicationCombo.getText();
295: }
296:
297: public String getProductName() {
298: return (fProductName == null) ? null : fProductName.getText()
299: .trim();
300: }
301:
302: private boolean productNameDefined() {
303: return (fProduct.getName() != null && !fProduct.getName()
304: .equals("")); //$NON-NLS-1$
305: }
306:
307: private String getPluginId() {
308: IProject project = fProduct.getModel().getUnderlyingResource()
309: .getProject();
310: IPluginModelBase model = PluginRegistry.findModel(project);
311: return (model == null) ? null : model.getPluginBase().getId();
312: }
313: }
|