KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > FragmentGeneralInfoSection


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.window.Window;
15 import org.eclipse.jface.wizard.WizardDialog;
16 import org.eclipse.osgi.util.ManifestElement;
17 import org.eclipse.pde.core.plugin.IFragment;
18 import org.eclipse.pde.core.plugin.IPlugin;
19 import org.eclipse.pde.core.plugin.IPluginModel;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.core.plugin.PluginRegistry;
22 import org.eclipse.pde.internal.core.ibundle.IBundle;
23 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
27 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
28 import org.eclipse.pde.internal.ui.editor.validation.ControlValidationUtility;
29 import org.eclipse.pde.internal.ui.editor.validation.TextValidator;
30 import org.eclipse.pde.internal.ui.parts.ComboPart;
31 import org.eclipse.pde.internal.ui.parts.FormEntry;
32 import org.eclipse.pde.internal.ui.util.SWTUtil;
33 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
34 import org.eclipse.pde.internal.ui.wizards.plugin.NewPluginProjectWizard;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.forms.IFormColors;
42 import org.eclipse.ui.forms.events.HyperlinkEvent;
43 import org.eclipse.ui.forms.widgets.FormToolkit;
44 import org.eclipse.ui.forms.widgets.TableWrapData;
45 import org.osgi.framework.BundleException;
46 import org.osgi.framework.Constants;
47
48 public class FragmentGeneralInfoSection extends GeneralInfoSection {
49
50     private FormEntry fPluginIdEntry;
51     private FormEntry fPluginMinVersionEntry;
52     private FormEntry fPluginMaxVersionEntry;
53     private ComboPart fPluginMinVersionBound;
54     private ComboPart fPluginMaxVersionBound;
55     private ComboPart fMatchCombo;
56
57     private TextValidator fPluginIdValidator;
58
59     private TextValidator fPluginMinVersionValidator;
60
61     private TextValidator fPluginMaxVersionValidator;
62     
63     
64     public FragmentGeneralInfoSection(PDEFormPage page, Composite parent) {
65         super(page, parent);
66     }
67     
68     protected String JavaDoc getSectionDescription() {
69         return PDEUIMessages.ManifestEditor_PluginSpecSection_fdesc;
70     }
71     
72     protected void createSpecificControls(Composite parent, FormToolkit toolkit, IActionBars actionBars) {
73         createPluginIdEntry(parent, toolkit, actionBars);
74         createPluginVersionEntry(parent, toolkit, actionBars);
75         if (!isBundle())
76             createMatchCombo(parent, toolkit, actionBars);
77     }
78     
79     private void createPluginIdEntry(Composite parent, FormToolkit toolkit, IActionBars actionBars) {
80         fPluginIdEntry = new FormEntry(
81                 parent,
82                 toolkit,
83                 PDEUIMessages.GeneralInfoSection_pluginId,
84                 PDEUIMessages.GeneralInfoSection_browse, //
85
isEditable());
86         fPluginIdEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
87             public void textValueChanged(FormEntry entry) {
88                 try {
89                     ((IFragment) getPluginBase()).setPluginId(fPluginIdEntry.getValue());
90                 } catch (CoreException e1) {
91                     PDEPlugin.logException(e1);
92                 }
93             }
94             public void linkActivated(HyperlinkEvent e) {
95                 String JavaDoc plugin = fPluginIdEntry.getValue();
96                 if (!(PluginRegistry.findModel(plugin) instanceof IPluginModel)) {
97                     createFragmentPlugin();
98                 }
99                 ManifestEditor.openPluginEditor(fPluginIdEntry.getValue());
100             }
101             public void browseButtonSelected(FormEntry entry) {
102                 handleOpenDialog();
103             }
104             private void createFragmentPlugin() {
105                 NewPluginProjectWizard wizard = new NewPluginProjectWizard("Equinox"); //$NON-NLS-1$
106
WizardDialog dialog = new WizardDialog(PDEPlugin
107                         .getActiveWorkbenchShell(), wizard);
108                 dialog.create();
109                 SWTUtil.setDialogSize(dialog, 400, 500);
110                 if (dialog.open() == Window.OK) {
111                     String JavaDoc plugin = wizard.getPluginId();
112                     try {
113                         ((IFragment) getPluginBase()).setPluginId(plugin);
114                         fPluginIdEntry.setValue(plugin, false);
115                     } catch (CoreException ce) {
116                         PDEPlugin.logException(ce);
117                     }
118                 }
119             }
120         });
121         fPluginIdEntry.setEditable(isEditable());
122         // Create validator
123
fPluginIdValidator = new TextValidator(getManagedForm(),
124                 fPluginIdEntry.getText(), getProject(), true) {
125             protected boolean validateControl() {
126                 return validatePluginId();
127             }
128         };
129     }
130     
131     /**
132      * @return
133      */

134     private boolean validatePluginId() {
135         // Validate host plugin
136
return ControlValidationUtility.validateFragmentHostPluginField(
137                 fPluginIdEntry.getText().getText(), fPluginIdValidator,
138                 getProject());
139     }
140
141     protected void handleOpenDialog() {
142         PluginSelectionDialog dialog = new PluginSelectionDialog(getSection().getShell(), false, false);
143         dialog.create();
144         if (dialog.open() == Window.OK) {
145             IPluginModel model = (IPluginModel) dialog.getFirstResult();
146             IPlugin plugin = model.getPlugin();
147             try {
148                 ((IFragment) getPluginBase()).setPluginId(plugin.getId());
149                 fPluginMinVersionEntry.setValue(plugin.getVersion());
150                 ((IFragment) getPluginBase()).setPluginVersion(getVersion());
151             } catch (CoreException e) {
152             }
153             
154         }
155     }
156
157     private void createPluginVersionEntry(Composite client,
158             FormToolkit toolkit, IActionBars actionBars) {
159         if (isBundle()) {
160             createBundlePluginVersionEntry(client, toolkit, actionBars);
161         } else {
162             createNonBundlePluginVersionEntry(client, toolkit, actionBars);
163         }
164         
165     }
166     
167     private void createBundlePluginVersionEntry(Composite client,
168             FormToolkit toolkit, IActionBars actionBars) {
169
170         String JavaDoc[] items = new String JavaDoc[] {
171                 PDEUIMessages.DependencyPropertiesDialog_comboInclusive,
172                 PDEUIMessages.DependencyPropertiesDialog_comboExclusive };
173         fPluginMinVersionEntry = new FormEntry(client, toolkit,
174                 PDEUIMessages.GeneralInfoSection_hostMinVersionRange, 0, 1);
175         fPluginMinVersionEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
176             public void textValueChanged(FormEntry entry) {
177                 try {
178                     ((IFragment) getPluginBase()).setPluginVersion(getVersion());
179                 } catch (CoreException e) {
180                     PDEPlugin.logException(e);
181                 }
182             }
183             public void textDirty(FormEntry entry) {
184                 setMaxFieldsEnabled();
185                 super.textDirty(entry);
186             }
187         });
188         fPluginMinVersionEntry.setEditable(isEditable());
189         // Create validator
190
fPluginMinVersionValidator = new TextValidator(getManagedForm(),
191                 fPluginMinVersionEntry.getText(), getProject(), true) {
192             protected boolean validateControl() {
193                 return validatePluginMinVersion();
194             }
195         };
196         
197         fPluginMinVersionBound = new ComboPart();
198         fPluginMinVersionBound.createControl(client, toolkit, SWT.READ_ONLY);
199         fPluginMinVersionBound.getControl().setLayoutData(new TableWrapData(TableWrapData.FILL));
200         fPluginMinVersionBound.setItems(items);
201         fPluginMinVersionBound.getControl().setEnabled(isEditable());
202         fPluginMinVersionBound.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent event) {
204                 try {
205                     ((IFragment) getPluginBase()).setPluginVersion(getVersion());
206                 } catch (CoreException e) {
207                     PDEPlugin.logException(e);
208                 }
209             }
210         });
211         
212         fPluginMaxVersionEntry = new FormEntry(client, toolkit,
213                 PDEUIMessages.GeneralInfoSection_hostMaxVersionRange, 0, 1);
214         fPluginMaxVersionEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
215             public void textValueChanged(FormEntry entry) {
216                 try {
217                     ((IFragment) getPluginBase()).setPluginVersion(getVersion());
218                 } catch (CoreException e) {
219                     PDEPlugin.logException(e);
220                 }
221             }
222         });
223         fPluginMaxVersionEntry.setEditable(isEditable());
224         // Create validator
225
fPluginMaxVersionValidator = new TextValidator(getManagedForm(),
226                 fPluginMaxVersionEntry.getText(), getProject(), true) {
227             protected boolean validateControl() {
228                 return validatePluginMaxVersion();
229             }
230         };
231         fPluginMaxVersionBound = new ComboPart();
232         fPluginMaxVersionBound.createControl(client, toolkit, SWT.READ_ONLY);
233         fPluginMaxVersionBound.getControl().setLayoutData(new TableWrapData(TableWrapData.FILL));
234         fPluginMaxVersionBound.setItems(items);
235         fPluginMaxVersionBound.getControl().setEnabled(isEditable());
236         fPluginMaxVersionBound.addSelectionListener(new SelectionAdapter() {
237             public void widgetSelected(SelectionEvent event) {
238                 try {
239                     ((IFragment) getPluginBase()).setPluginVersion(getVersion());
240                 } catch (CoreException e) {
241                     PDEPlugin.logException(e);
242                 }
243             }
244         });
245     }
246
247     /**
248      * @return
249      */

250     private boolean validatePluginMaxVersion() {
251         // No validation required for an optional field
252
if (fPluginMaxVersionEntry.getText().getText().length() == 0) {
253             return true;
254         }
255         // Value must be a valid version
256
return ControlValidationUtility.validateVersionField(
257                 fPluginMaxVersionEntry.getText().getText(), fPluginMaxVersionValidator);
258     }
259
260     /**
261      * @return
262      */

263     private boolean validatePluginMinVersion() {
264         // No validation required for an optional field
265
if (fPluginMinVersionEntry.getText().getText().length() == 0) {
266             return true;
267         }
268         // Value must be a valid version
269
return ControlValidationUtility.validateVersionField(
270                 fPluginMinVersionEntry.getText().getText(), fPluginMinVersionValidator);
271     }
272
273     private void createNonBundlePluginVersionEntry(Composite client,
274             FormToolkit toolkit, IActionBars actionBars) {
275         fPluginMinVersionEntry = new FormEntry(
276                 client,
277                 toolkit,
278                 PDEUIMessages.GeneralInfoSection_pluginVersion, null, false);
279         fPluginMinVersionEntry.setFormEntryListener(new FormEntryAdapter(this,
280                 actionBars) {
281             public void textValueChanged(FormEntry entry) {
282                 try {
283                     ((IFragment) getPluginBase()).setPluginVersion(entry.getValue());
284                 } catch (CoreException e) {
285                     PDEPlugin.logException(e);
286                 }
287             }
288         });
289         fPluginMinVersionEntry.setEditable(isEditable());
290     }
291     
292     private void createMatchCombo(Composite client, FormToolkit toolkit,
293             IActionBars actionBars) {
294         Label matchLabel = toolkit.createLabel(client, PDEUIMessages.ManifestEditor_PluginSpecSection_versionMatch);
295         matchLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
296         TableWrapData td = new TableWrapData();
297         td.valign = TableWrapData.MIDDLE;
298         matchLabel.setLayoutData(td);
299         
300         fMatchCombo = new ComboPart();
301         fMatchCombo.createControl(client, toolkit, SWT.READ_ONLY);
302         td = new TableWrapData(TableWrapData.FILL);
303         td.colspan = 2;
304         td.valign = TableWrapData.MIDDLE;
305         fMatchCombo.getControl().setLayoutData(td);
306         
307         String JavaDoc[] items = new String JavaDoc[]{"", //$NON-NLS-1$
308
PDEUIMessages.ManifestEditor_MatchSection_equivalent,
309                 PDEUIMessages.ManifestEditor_MatchSection_compatible,
310                 PDEUIMessages.ManifestEditor_MatchSection_perfect,
311                 PDEUIMessages.ManifestEditor_MatchSection_greater};
312         fMatchCombo.setItems(items);
313         fMatchCombo.addSelectionListener(new SelectionAdapter() {
314             public void widgetSelected(SelectionEvent event) {
315                 int match = fMatchCombo.getSelectionIndex();
316                 try {
317                     ((IFragment) getPluginBase()).setRule(match);
318                 } catch (CoreException e) {
319                     PDEPlugin.logException(e);
320                 }
321             }
322         });
323         fMatchCombo.getControl().setEnabled(isEditable());
324     }
325     
326     public void commit(boolean onSave) {
327         fPluginIdEntry.commit();
328         fPluginMinVersionEntry.commit();
329         fPluginMaxVersionEntry.commit();
330         super.commit(onSave);
331     }
332     
333     public void cancelEdit() {
334         fPluginIdEntry.cancelEdit();
335         fPluginMinVersionEntry.cancelEdit();
336         fPluginMaxVersionEntry.cancelEdit();
337         super.cancelEdit();
338     }
339     
340     public void refresh() {
341         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
342         IFragment fragment = (IFragment) model.getPluginBase();
343         fPluginIdEntry.setValue(fragment.getPluginId(), true);
344         if (isBundle()) {
345             refreshVersion();
346         } else {
347             fPluginMinVersionEntry.setValue(fragment.getPluginVersion(), true);
348         }
349         if (fMatchCombo != null)
350             fMatchCombo.select(fragment.getRule());
351         super.refresh();
352     }
353     
354     protected String JavaDoc getAttribute(String JavaDoc header, String JavaDoc attribute) {
355         IBundle bundle = getBundle();
356         if (bundle == null)
357             return null;
358         String JavaDoc value = bundle.getHeader(header);
359         if (value == null)
360             return null;
361         try {
362             ManifestElement[] elements = ManifestElement.parseHeader(header, value);
363             if (elements.length > 0)
364                 return elements[0].getAttribute(attribute);
365         } catch (BundleException e) {
366         }
367         return null;
368     }
369
370     private void setMaxFieldsEnabled() {
371         boolean enabled = fPluginMinVersionEntry.getText().getText().trim().length() != 0;
372         fPluginMaxVersionEntry.getText().setEnabled(enabled);
373         fPluginMaxVersionBound.getControl().setEnabled(enabled && isEditable());
374     }
375     
376     private String JavaDoc getVersion() {
377         if (isBundle()) {
378             if (!fPluginMinVersionEntry.getValue().equals(fPluginMaxVersionEntry.getValue()) &&
379                     fPluginMaxVersionEntry.getText().getEnabled()) {
380                 if (fPluginMaxVersionEntry.getValue().length() == 0)
381                     return fPluginMinVersionEntry.getValue();
382                 String JavaDoc version;
383                 if (fPluginMinVersionBound.getSelectionIndex() == 0)
384                     version = "["; //$NON-NLS-1$
385
else
386                     version = "("; //$NON-NLS-1$
387
version += fPluginMinVersionEntry.getValue() + "," + fPluginMaxVersionEntry.getValue(); //$NON-NLS-1$
388
if (fPluginMaxVersionBound.getSelectionIndex() == 0)
389                     version += "]"; //$NON-NLS-1$
390
else
391                     version += ")"; //$NON-NLS-1$
392
return version;
393             }
394         }
395         return fPluginMinVersionEntry.getValue();
396     }
397     
398     private void refreshVersion() {
399         String JavaDoc version = getAttribute(Constants.FRAGMENT_HOST, Constants.BUNDLE_VERSION_ATTRIBUTE);
400         if (version == null) {
401             setVersionFields("", true, "", false); //$NON-NLS-1$ //$NON-NLS-2$
402
setMaxFieldsEnabled();
403             return;
404         }
405         version = version.trim();
406         int comInd = version.indexOf(","); //$NON-NLS-1$
407
int lastPos = version.length() - 1;
408         char first = version.charAt(0);
409         char last = version.charAt(lastPos);
410         if (comInd == -1) {
411             setVersionFields(version, true, "", false); //$NON-NLS-1$
412
} else if ((first == '[' || first == '(') &&
413                 (last == ']' || last == ')')) {
414             version = version.substring(1, lastPos);
415             setVersionFields(
416                     version.substring(0, comInd - 1),
417                     first == '[',
418                     version.substring(comInd),
419                     last == ']');
420         }
421         setMaxFieldsEnabled();
422     }
423     
424     private void setVersionFields(String JavaDoc minVersion, boolean minInclusive, String JavaDoc maxVersion, boolean maxInclusive) {
425         fPluginMinVersionEntry.setValue(minVersion, true);
426         fPluginMinVersionBound.select(minInclusive ? 0 : 1);
427         fPluginMaxVersionEntry.setValue(maxVersion, true);
428         fPluginMaxVersionBound.select(maxInclusive ? 0 : 1);
429     }
430     
431     // added for bug 172675
432
protected void addListeners() {
433         if (isBundle()) {
434             IBundleModel model = getBundle().getModel();
435             if (model != null)
436                 model.addModelChangedListener(this);
437         }
438         super.addListeners();
439     }
440     
441     protected void removeListeners() {
442         if (isBundle()) {
443             IBundleModel model = getBundle().getModel();
444             if (model != null)
445                 model.removeModelChangedListener(this);
446         }
447         super.removeListeners();
448     }
449 }
450
Popular Tags