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.TreeSet;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.window.Window;
019: import org.eclipse.jface.wizard.WizardPage;
020: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.core.plugin.PluginRegistry;
023: import org.eclipse.pde.internal.core.PDECore;
024: import org.eclipse.pde.internal.core.iproduct.IProduct;
025: import org.eclipse.pde.internal.ui.IHelpContextIds;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
028: import org.eclipse.pde.internal.ui.util.SWTUtil;
029: import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.events.ModifyEvent;
032: import org.eclipse.swt.events.ModifyListener;
033: import org.eclipse.swt.events.SelectionAdapter;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Group;
040: import org.eclipse.swt.widgets.Label;
041: import org.eclipse.swt.widgets.Text;
042: import org.eclipse.ui.PlatformUI;
043: import org.eclipse.ui.forms.events.HyperlinkEvent;
044: import org.eclipse.ui.forms.events.IHyperlinkListener;
045: import org.eclipse.ui.forms.widgets.FormText;
046: import org.eclipse.ui.forms.widgets.FormToolkit;
047:
048: public class ProductIntroWizardPage extends WizardPage implements
049: IHyperlinkListener {
050:
051: private Text fPluginText;
052: private Text fIntroIdText;
053: private TreeSet fIntroIds;
054: private IProduct fProduct;
055:
056: private ModifyListener fListener = new ModifyListener() {
057: public void modifyText(ModifyEvent e) {
058: validatePage();
059: }
060: };
061:
062: public ProductIntroWizardPage(String pageName, IProduct product) {
063: super (pageName);
064: setTitle(PDEUIMessages.ProductIntroWizardPage_title);
065: setDescription(PDEUIMessages.ProductIntroWizardPage_description);
066: fIntroIds = getCurrentIntroIds();
067: fProduct = product;
068: }
069:
070: public void createControl(Composite parent) {
071: Composite comp = new Composite(parent, SWT.NONE);
072: GridLayout layout = new GridLayout();
073: layout.verticalSpacing = 20;
074: comp.setLayout(layout);
075:
076: createProductGroup(comp);
077:
078: setControl(comp);
079: setPageComplete(getPluginId() != null);
080: Dialog.applyDialogFont(comp);
081: PlatformUI.getWorkbench().getHelpSystem().setHelp(comp,
082: IHelpContextIds.PRODUCT_DEFINITIONS_WIZARD);
083: }
084:
085: private void createProductGroup(Composite comp) {
086: Group group = new Group(comp, SWT.NONE);
087: group.setText(PDEUIMessages.ProductIntroWizardPage_groupText);
088: group.setLayout(new GridLayout(3, false));
089: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
090:
091: FormToolkit toolkit = new FormToolkit(group.getDisplay());
092: FormText text = toolkit.createFormText(group, false);
093: text.setText(PDEUIMessages.ProductIntroWizardPage_formText,
094: true, false);
095: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
096: gd.horizontalSpan = 3;
097: gd.widthHint = 400;
098: text.setLayoutData(gd);
099: text.setBackground(null);
100: text.addHyperlinkListener(this );
101:
102: Label label = new Label(group, SWT.NONE);
103: label.setText(PDEUIMessages.ProductIntroWizardPage_targetLabel);
104:
105: fPluginText = new Text(group, SWT.SINGLE | SWT.BORDER);
106: fPluginText
107: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108:
109: Button button = new Button(group, SWT.PUSH);
110: button.setText(PDEUIMessages.ProductIntroWizardPage_browse);
111: SWTUtil.setButtonDimensionHint(button);
112: button.addSelectionListener(new SelectionAdapter() {
113: public void widgetSelected(SelectionEvent e) {
114: handleBrowse();
115: }
116: });
117:
118: label = new Label(group, SWT.NONE);
119: label.setText(PDEUIMessages.ProductIntroWizardPage_introLabel);
120:
121: fIntroIdText = new Text(group, SWT.SINGLE | SWT.BORDER);
122: gd = new GridData(GridData.FILL_HORIZONTAL);
123: gd.horizontalSpan = 2;
124: fIntroIdText.setLayoutData(gd);
125:
126: String pluginId = getPluginId();
127: if (pluginId != null) {
128: fPluginText.setText(pluginId);
129: fIntroIdText.setText(getAvailableIntroId(pluginId));
130: }
131: fPluginText.addModifyListener(fListener);
132: fIntroIdText.addModifyListener(fListener);
133: }
134:
135: public void setVisible(boolean visible) {
136: super .setVisible(visible);
137: if (visible) {
138: fPluginText.setVisible(visible);
139: fPluginText.setFocus();
140: }
141: }
142:
143: private void validatePage() {
144: String error = null;
145: String pluginId = getDefiningPlugin();
146: IPluginModelBase model = PluginRegistry.findModel(pluginId);
147: if (model == null) {
148: error = PDEUIMessages.ProductDefinitonWizardPage_noPlugin;
149: } else if (model.getUnderlyingResource() == null) {
150: error = PDEUIMessages.ProductDefinitonWizardPage_notInWorkspace;
151: } else if (pluginId.length() == 0) {
152: error = PDEUIMessages.ProductIntroWizardPage_targetNotSet;
153: }
154: validateId(error);
155:
156: }
157:
158: private void validateId(String error) {
159: if (error == null) {
160: String id = fIntroIdText.getText().trim();
161:
162: if (id.length() == 0)
163: error = PDEUIMessages.ProductIntroWizardPage_introNotSet;
164:
165: if (error == null)
166: for (int i = 0; i < id.length(); i++)
167: if (!id.substring(i, i + 1).matches("[a-zA-Z0-9.]")) //$NON-NLS-1$
168: error = PDEUIMessages.ProductIntroWizardPage_invalidIntroId;
169:
170: if (error == null && fIntroIds.contains(id))
171: error = PDEUIMessages.ProductIntroWizardPage_introIdExists;
172: }
173: setErrorMessage(error);
174: setPageComplete(error == null);
175: }
176:
177: private void handleBrowse() {
178: PluginSelectionDialog dialog = new PluginSelectionDialog(
179: getShell(), PluginRegistry.getWorkspaceModels(), false);
180: if (dialog.open() == Window.OK) {
181: IPluginModelBase model = (IPluginModelBase) dialog
182: .getFirstResult();
183: String id = model.getPluginBase().getId();
184: fPluginText.setText(id);
185: fIntroIdText.setText(getAvailableIntroId(id));
186: }
187: }
188:
189: private String getAvailableIntroId(String id) {
190: String introId = "intro"; //$NON-NLS-1$
191: String numString = ""; //$NON-NLS-1$
192: int idNum = 1;
193: while (fIntroIds.contains(id + "." + introId + numString)) { //$NON-NLS-1$
194: numString = Integer.toString(idNum++);
195: }
196: return id + "." + introId + numString; //$NON-NLS-1$
197: }
198:
199: private TreeSet getCurrentIntroIds() {
200: String introId;
201: TreeSet result = new TreeSet();
202: IExtension[] extensions = PDECore.getDefault()
203: .getExtensionsRegistry().findExtensions(
204: "org.eclipse.ui.intro"); //$NON-NLS-1$
205: for (int i = 0; i < extensions.length; i++) {
206: IConfigurationElement[] children = extensions[i]
207: .getConfigurationElements();
208: for (int j = 0; j < children.length; j++) {
209: if ("intro".equals(children[j].getName())) {//$NON-NLS-1$
210: introId = children[j].getAttribute("id"); //$NON-NLS-1$
211: if (introId != null)
212: result.add(introId);
213: }
214: }
215: }
216: return result;
217: }
218:
219: public String getDefiningPlugin() {
220: return fPluginText.getText().trim();
221: }
222:
223: public String getIntroId() {
224: return fIntroIdText.getText().trim();
225: }
226:
227: private String getPluginId() {
228: IProject project = fProduct.getModel().getUnderlyingResource()
229: .getProject();
230: IPluginModelBase model = PluginRegistry.findModel(project);
231: return (model == null) ? null : model.getPluginBase().getId();
232: }
233:
234: public void linkEntered(HyperlinkEvent e) {
235: }
236:
237: public void linkExited(HyperlinkEvent e) {
238: }
239:
240: public void linkActivated(HyperlinkEvent e) {
241: String extPoint = "org.eclipse.ui." + e.getHref().toString(); //$NON-NLS-1$
242: IPluginExtensionPoint point = PDECore.getDefault()
243: .getExtensionsRegistry().findExtensionPoint(extPoint);
244: if (point != null)
245: new ShowDescriptionAction(point, true).run();
246:
247: }
248: }
|