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: * Peter Friese <peter.friese@gentleware.com> - bug 199431
011: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
012:
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.jface.window.Window;
015: import org.eclipse.jface.wizard.WizardDialog;
016: import org.eclipse.osgi.util.ManifestElement;
017: import org.eclipse.pde.core.plugin.IFragment;
018: import org.eclipse.pde.core.plugin.IPlugin;
019: import org.eclipse.pde.core.plugin.IPluginModel;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.core.plugin.PluginRegistry;
022: import org.eclipse.pde.internal.core.ibundle.IBundle;
023: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEUIMessages;
026: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
027: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
028: import org.eclipse.pde.internal.ui.editor.validation.ControlValidationUtility;
029: import org.eclipse.pde.internal.ui.editor.validation.TextValidator;
030: import org.eclipse.pde.internal.ui.parts.ComboPart;
031: import org.eclipse.pde.internal.ui.parts.FormEntry;
032: import org.eclipse.pde.internal.ui.util.SWTUtil;
033: import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
034: import org.eclipse.pde.internal.ui.wizards.plugin.NewPluginProjectWizard;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.events.SelectionAdapter;
037: import org.eclipse.swt.events.SelectionEvent;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Label;
040: import org.eclipse.ui.IActionBars;
041: import org.eclipse.ui.forms.IFormColors;
042: import org.eclipse.ui.forms.events.HyperlinkEvent;
043: import org.eclipse.ui.forms.widgets.FormToolkit;
044: import org.eclipse.ui.forms.widgets.TableWrapData;
045: import org.osgi.framework.BundleException;
046: import org.osgi.framework.Constants;
047:
048: public class FragmentGeneralInfoSection extends GeneralInfoSection {
049:
050: private FormEntry fPluginIdEntry;
051: private FormEntry fPluginMinVersionEntry;
052: private FormEntry fPluginMaxVersionEntry;
053: private ComboPart fPluginMinVersionBound;
054: private ComboPart fPluginMaxVersionBound;
055: private ComboPart fMatchCombo;
056:
057: private TextValidator fPluginIdValidator;
058:
059: private TextValidator fPluginMinVersionValidator;
060:
061: private TextValidator fPluginMaxVersionValidator;
062:
063: public FragmentGeneralInfoSection(PDEFormPage page, Composite parent) {
064: super (page, parent);
065: }
066:
067: protected String getSectionDescription() {
068: return PDEUIMessages.ManifestEditor_PluginSpecSection_fdesc;
069: }
070:
071: protected void createSpecificControls(Composite parent,
072: FormToolkit toolkit, IActionBars actionBars) {
073: createPluginIdEntry(parent, toolkit, actionBars);
074: createPluginVersionEntry(parent, toolkit, actionBars);
075: if (!isBundle())
076: createMatchCombo(parent, toolkit, actionBars);
077: createSingleton(parent, toolkit, actionBars,
078: PDEUIMessages.FragmentGeneralInfoSection_singleton);
079: }
080:
081: private void createPluginIdEntry(Composite parent,
082: FormToolkit toolkit, IActionBars actionBars) {
083: fPluginIdEntry = new FormEntry(parent, toolkit,
084: PDEUIMessages.GeneralInfoSection_pluginId,
085: PDEUIMessages.GeneralInfoSection_browse, //
086: isEditable());
087: fPluginIdEntry.setFormEntryListener(new FormEntryAdapter(this ,
088: actionBars) {
089: public void textValueChanged(FormEntry entry) {
090: try {
091: ((IFragment) getPluginBase())
092: .setPluginId(fPluginIdEntry.getValue());
093: } catch (CoreException e1) {
094: PDEPlugin.logException(e1);
095: }
096: }
097:
098: public void linkActivated(HyperlinkEvent e) {
099: String plugin = fPluginIdEntry.getValue();
100: if (!(PluginRegistry.findModel(plugin) instanceof IPluginModel)) {
101: createFragmentPlugin();
102: }
103: ManifestEditor.openPluginEditor(fPluginIdEntry
104: .getValue());
105: }
106:
107: public void browseButtonSelected(FormEntry entry) {
108: handleOpenDialog();
109: }
110:
111: private void createFragmentPlugin() {
112: NewPluginProjectWizard wizard = new NewPluginProjectWizard(
113: "Equinox"); //$NON-NLS-1$
114: WizardDialog dialog = new WizardDialog(PDEPlugin
115: .getActiveWorkbenchShell(), wizard);
116: dialog.create();
117: SWTUtil.setDialogSize(dialog, 400, 500);
118: if (dialog.open() == Window.OK) {
119: String plugin = wizard.getPluginId();
120: try {
121: ((IFragment) getPluginBase())
122: .setPluginId(plugin);
123: fPluginIdEntry.setValue(plugin, false);
124: } catch (CoreException ce) {
125: PDEPlugin.logException(ce);
126: }
127: }
128: }
129: });
130: fPluginIdEntry.setEditable(isEditable());
131: // Create validator
132: fPluginIdValidator = new TextValidator(getManagedForm(),
133: fPluginIdEntry.getText(), getProject(), true) {
134: protected boolean validateControl() {
135: return validatePluginId();
136: }
137: };
138: }
139:
140: /**
141: * @return
142: */
143: private boolean validatePluginId() {
144: // Validate host plugin
145: return ControlValidationUtility
146: .validateFragmentHostPluginField(fPluginIdEntry
147: .getText().getText(), fPluginIdValidator,
148: getProject());
149: }
150:
151: protected void handleOpenDialog() {
152: PluginSelectionDialog dialog = new PluginSelectionDialog(
153: getSection().getShell(), false, false);
154: dialog.create();
155: if (dialog.open() == Window.OK) {
156: IPluginModel model = (IPluginModel) dialog.getFirstResult();
157: IPlugin plugin = model.getPlugin();
158: try {
159: ((IFragment) getPluginBase()).setPluginId(plugin
160: .getId());
161: fPluginMinVersionEntry.setValue(plugin.getVersion());
162: ((IFragment) getPluginBase())
163: .setPluginVersion(getVersion());
164: } catch (CoreException e) {
165: }
166:
167: }
168: }
169:
170: private void createPluginVersionEntry(Composite client,
171: FormToolkit toolkit, IActionBars actionBars) {
172: if (isBundle()) {
173: createBundlePluginVersionEntry(client, toolkit, actionBars);
174: } else {
175: createNonBundlePluginVersionEntry(client, toolkit,
176: actionBars);
177: }
178:
179: }
180:
181: private void createBundlePluginVersionEntry(Composite client,
182: FormToolkit toolkit, IActionBars actionBars) {
183:
184: FormEntryAdapter textListener = new FormEntryAdapter(this ,
185: actionBars) {
186: public void textValueChanged(FormEntry entry) {
187: try {
188: ((IFragment) getPluginBase())
189: .setPluginVersion(getVersion());
190: } catch (CoreException e) {
191: PDEPlugin.logException(e);
192: }
193: }
194:
195: public void textDirty(FormEntry entry) {
196: setFieldsEnabled();
197: super .textDirty(entry);
198: }
199: };
200: String[] items = new String[] {
201: PDEUIMessages.DependencyPropertiesDialog_comboInclusive,
202: PDEUIMessages.DependencyPropertiesDialog_comboExclusive };
203: fPluginMinVersionEntry = new FormEntry(client, toolkit,
204: PDEUIMessages.GeneralInfoSection_hostMinVersionRange,
205: 0, 1);
206: fPluginMinVersionEntry.setFormEntryListener(textListener);
207: fPluginMinVersionEntry.setEditable(isEditable());
208: // Create validator
209: fPluginMinVersionValidator = new TextValidator(
210: getManagedForm(), fPluginMinVersionEntry.getText(),
211: getProject(), true) {
212: protected boolean validateControl() {
213: return validatePluginMinVersion();
214: }
215: };
216:
217: SelectionAdapter comboListener = new SelectionAdapter() {
218: public void widgetSelected(SelectionEvent event) {
219: try {
220: ((IFragment) getPluginBase())
221: .setPluginVersion(getVersion());
222: } catch (CoreException e) {
223: PDEPlugin.logException(e);
224: }
225: }
226: };
227: fPluginMinVersionBound = new ComboPart();
228: fPluginMinVersionBound.createControl(client, toolkit,
229: SWT.READ_ONLY);
230: fPluginMinVersionBound.getControl().setLayoutData(
231: new TableWrapData(TableWrapData.FILL));
232: fPluginMinVersionBound.setItems(items);
233: fPluginMinVersionBound.getControl().setEnabled(isEditable());
234: fPluginMinVersionBound.addSelectionListener(comboListener);
235:
236: fPluginMaxVersionEntry = new FormEntry(client, toolkit,
237: PDEUIMessages.GeneralInfoSection_hostMaxVersionRange,
238: 0, 1);
239: fPluginMaxVersionEntry.setFormEntryListener(textListener);
240: fPluginMaxVersionEntry.setEditable(isEditable());
241: // Create validator
242: fPluginMaxVersionValidator = new TextValidator(
243: getManagedForm(), fPluginMaxVersionEntry.getText(),
244: getProject(), true) {
245: protected boolean validateControl() {
246: return validatePluginMaxVersion();
247: }
248: };
249: fPluginMaxVersionBound = new ComboPart();
250: fPluginMaxVersionBound.createControl(client, toolkit,
251: SWT.READ_ONLY);
252: fPluginMaxVersionBound.getControl().setLayoutData(
253: new TableWrapData(TableWrapData.FILL));
254: fPluginMaxVersionBound.setItems(items);
255: fPluginMaxVersionBound.getControl().setEnabled(isEditable());
256: fPluginMaxVersionBound.addSelectionListener(comboListener);
257: }
258:
259: /**
260: * @return
261: */
262: private boolean validatePluginMaxVersion() {
263: // No validation required for an optional field
264: if (fPluginMaxVersionEntry.getText().getText().length() == 0) {
265: return true;
266: }
267: // Value must be a valid version
268: return ControlValidationUtility.validateVersionField(
269: fPluginMaxVersionEntry.getText().getText(),
270: fPluginMaxVersionValidator);
271: }
272:
273: /**
274: * @return
275: */
276: private boolean validatePluginMinVersion() {
277: // No validation required for an optional field
278: if (fPluginMinVersionEntry.getText().getText().length() == 0) {
279: return true;
280: }
281: // Value must be a valid version
282: return ControlValidationUtility.validateVersionField(
283: fPluginMinVersionEntry.getText().getText(),
284: fPluginMinVersionValidator);
285: }
286:
287: private void createNonBundlePluginVersionEntry(Composite client,
288: FormToolkit toolkit, IActionBars actionBars) {
289: fPluginMinVersionEntry = new FormEntry(client, toolkit,
290: PDEUIMessages.GeneralInfoSection_pluginVersion, null,
291: false);
292: fPluginMinVersionEntry
293: .setFormEntryListener(new FormEntryAdapter(this ,
294: actionBars) {
295: public void textValueChanged(FormEntry entry) {
296: try {
297: ((IFragment) getPluginBase())
298: .setPluginVersion(entry.getValue());
299: } catch (CoreException e) {
300: PDEPlugin.logException(e);
301: }
302: }
303: });
304: fPluginMinVersionEntry.setEditable(isEditable());
305: }
306:
307: private void createMatchCombo(Composite client,
308: FormToolkit toolkit, IActionBars actionBars) {
309: Label matchLabel = toolkit
310: .createLabel(
311: client,
312: PDEUIMessages.ManifestEditor_PluginSpecSection_versionMatch);
313: matchLabel.setForeground(toolkit.getColors().getColor(
314: IFormColors.TITLE));
315: TableWrapData td = new TableWrapData();
316: td.valign = TableWrapData.MIDDLE;
317: matchLabel.setLayoutData(td);
318:
319: fMatchCombo = new ComboPart();
320: fMatchCombo.createControl(client, toolkit, SWT.READ_ONLY);
321: td = new TableWrapData(TableWrapData.FILL);
322: td.colspan = 2;
323: td.valign = TableWrapData.MIDDLE;
324: fMatchCombo.getControl().setLayoutData(td);
325:
326: String[] items = new String[] {
327: "", //$NON-NLS-1$
328: PDEUIMessages.ManifestEditor_MatchSection_equivalent,
329: PDEUIMessages.ManifestEditor_MatchSection_compatible,
330: PDEUIMessages.ManifestEditor_MatchSection_perfect,
331: PDEUIMessages.ManifestEditor_MatchSection_greater };
332: fMatchCombo.setItems(items);
333: fMatchCombo.addSelectionListener(new SelectionAdapter() {
334: public void widgetSelected(SelectionEvent event) {
335: int match = fMatchCombo.getSelectionIndex();
336: try {
337: ((IFragment) getPluginBase()).setRule(match);
338: } catch (CoreException e) {
339: PDEPlugin.logException(e);
340: }
341: }
342: });
343: fMatchCombo.getControl().setEnabled(isEditable());
344: }
345:
346: public void commit(boolean onSave) {
347: fPluginIdEntry.commit();
348: fPluginMinVersionEntry.commit();
349: fPluginMaxVersionEntry.commit();
350: super .commit(onSave);
351: }
352:
353: public void cancelEdit() {
354: fPluginIdEntry.cancelEdit();
355: fPluginMinVersionEntry.cancelEdit();
356: fPluginMaxVersionEntry.cancelEdit();
357: super .cancelEdit();
358: }
359:
360: public void refresh() {
361: IPluginModelBase model = (IPluginModelBase) getPage()
362: .getModel();
363: IFragment fragment = (IFragment) model.getPluginBase();
364: fPluginIdEntry.setValue(fragment.getPluginId(), true);
365: if (isBundle()) {
366: refreshVersion();
367: } else {
368: fPluginMinVersionEntry.setValue(
369: fragment.getPluginVersion(), true);
370: }
371: if (fMatchCombo != null)
372: fMatchCombo.select(fragment.getRule());
373: super .refresh();
374: }
375:
376: protected String getAttribute(String header, String attribute) {
377: IBundle bundle = getBundle();
378: if (bundle == null)
379: return null;
380: String value = bundle.getHeader(header);
381: if (value == null)
382: return null;
383: try {
384: ManifestElement[] elements = ManifestElement.parseHeader(
385: header, value);
386: if (elements.length > 0)
387: return elements[0].getAttribute(attribute);
388: } catch (BundleException e) {
389: }
390: return null;
391: }
392:
393: private void setFieldsEnabled() {
394: boolean singleVersion = fPluginMaxVersionEntry.getText()
395: .getText().trim().length() == 0;
396: boolean enabled = fPluginMinVersionEntry.getText().getText()
397: .trim().length() != 0;
398: fPluginMaxVersionEntry.getText().setEnabled(enabled);
399: fPluginMaxVersionBound.getControl().setEnabled(
400: !singleVersion && enabled && isEditable());
401: fPluginMinVersionBound.getControl().setEnabled(
402: !singleVersion && isEditable());
403: }
404:
405: private String getVersion() {
406: if (isBundle()) {
407: if (!fPluginMinVersionEntry.getValue().equals(
408: fPluginMaxVersionEntry.getValue())
409: && fPluginMaxVersionEntry.getText().getEnabled()) {
410: if (fPluginMaxVersionEntry.getValue().length() == 0)
411: return fPluginMinVersionEntry.getValue();
412: String version;
413: if (fPluginMinVersionBound.getSelectionIndex() == 0)
414: version = "["; //$NON-NLS-1$
415: else
416: version = "("; //$NON-NLS-1$
417: version += fPluginMinVersionEntry.getValue()
418: + "," + fPluginMaxVersionEntry.getValue(); //$NON-NLS-1$
419: if (fPluginMaxVersionBound.getSelectionIndex() == 0)
420: version += "]"; //$NON-NLS-1$
421: else
422: version += ")"; //$NON-NLS-1$
423: return version;
424: }
425: }
426: return fPluginMinVersionEntry.getValue();
427: }
428:
429: private void refreshVersion() {
430: String version = getAttribute(Constants.FRAGMENT_HOST,
431: Constants.BUNDLE_VERSION_ATTRIBUTE);
432: if (version == null) {
433: setVersionFields("", true, "", false); //$NON-NLS-1$ //$NON-NLS-2$
434: setFieldsEnabled();
435: return;
436: }
437: version = version.trim();
438: int comInd = version.indexOf(","); //$NON-NLS-1$
439: int lastPos = version.length() - 1;
440: char first = version.charAt(0);
441: char last = version.charAt(lastPos);
442: if (comInd == -1) {
443: setVersionFields(version, true, "", false); //$NON-NLS-1$
444: } else if ((first == '[' || first == '(')
445: && (last == ']' || last == ')')) {
446: version = version.substring(1, lastPos);
447: setVersionFields(version.substring(0, comInd - 1),
448: first == '[', version.substring(comInd),
449: last == ']');
450: }
451: setFieldsEnabled();
452: }
453:
454: private void setVersionFields(String minVersion,
455: boolean minInclusive, String maxVersion,
456: boolean maxInclusive) {
457: fPluginMinVersionEntry.setValue(minVersion, true);
458: fPluginMinVersionBound.select(minInclusive ? 0 : 1);
459: fPluginMaxVersionEntry.setValue(maxVersion, true);
460: fPluginMaxVersionBound.select(maxInclusive ? 0 : 1);
461: }
462:
463: // added for bug 172675
464: protected void addListeners() {
465: if (isBundle()) {
466: IBundleModel model = getBundle().getModel();
467: if (model != null)
468: model.addModelChangedListener(this );
469: }
470: super .addListeners();
471: }
472:
473: protected void removeListeners() {
474: if (isBundle()) {
475: IBundleModel model = getBundle().getModel();
476: if (model != null)
477: model.removeModelChangedListener(this);
478: }
479: super.removeListeners();
480: }
481: }
|