KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > FeatureSpecSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.feature;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.jface.wizard.WizardDialog;
20 import org.eclipse.pde.core.IModelChangedEvent;
21 import org.eclipse.pde.core.plugin.IPlugin;
22 import org.eclipse.pde.core.plugin.IPluginModel;
23 import org.eclipse.pde.core.plugin.PluginRegistry;
24 import org.eclipse.pde.internal.core.feature.FeatureImport;
25 import org.eclipse.pde.internal.core.ifeature.IFeature;
26 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
27 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
28 import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
29 import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
30 import org.eclipse.pde.internal.core.util.VersionUtil;
31 import org.eclipse.pde.internal.ui.PDEPlugin;
32 import org.eclipse.pde.internal.ui.PDEUIMessages;
33 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
34 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
35 import org.eclipse.pde.internal.ui.editor.PDESection;
36 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
37 import org.eclipse.pde.internal.ui.parts.FormEntry;
38 import org.eclipse.pde.internal.ui.util.SWTUtil;
39 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
40 import org.eclipse.pde.internal.ui.wizards.plugin.NewPluginProjectWizard;
41 import org.eclipse.swt.dnd.Clipboard;
42 import org.eclipse.swt.dnd.RTFTransfer;
43 import org.eclipse.swt.dnd.TextTransfer;
44 import org.eclipse.swt.dnd.Transfer;
45 import org.eclipse.swt.dnd.TransferData;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.ui.forms.events.HyperlinkEvent;
49 import org.eclipse.ui.forms.widgets.FormToolkit;
50 import org.eclipse.ui.forms.widgets.Section;
51 import org.eclipse.ui.forms.widgets.TableWrapData;
52
53 public class FeatureSpecSection extends PDESection {
54     private FormEntry fIdText;
55
56     private FormEntry fTitleText;
57
58     private FormEntry fVersionText;
59
60     private FormEntry fProviderText;
61
62     private FormEntry fPluginText;
63
64     private FormEntry fUpdateSiteNameText;
65
66     private FormEntry fUpdateSiteUrlText;
67
68     private FormEntry fPatchedIdText;
69
70     private FormEntry fPatchedVersionText;
71
72     private boolean fPatch = false;
73
74     public FeatureSpecSection(FeatureFormPage page, Composite parent) {
75         super(page, parent, Section.DESCRIPTION);
76         getSection().setText(PDEUIMessages.FeatureEditor_SpecSection_title);
77         createClient(getSection(), page.getManagedForm().getToolkit());
78     }
79
80     public void commit(boolean onSave) {
81         fTitleText.commit();
82         fProviderText.commit();
83         fIdText.commit();
84         fPluginText.commit();
85         fVersionText.commit();
86         if (fPatchedIdText != null) {
87             fPatchedIdText.commit();
88             fPatchedVersionText.commit();
89         }
90         fUpdateSiteUrlText.commit();
91         fUpdateSiteNameText.commit();
92         super.commit(onSave);
93     }
94
95     private void commitSiteUrl(String JavaDoc value) {
96         IFeatureModel model = (IFeatureModel) getPage().getModel();
97         IFeature feature = model.getFeature();
98
99         IFeatureURL urlElement = feature.getURL();
100         if (urlElement == null) {
101             urlElement = model.getFactory().createURL();
102             try {
103                 feature.setURL(urlElement);
104             } catch (CoreException e) {
105                 return;
106             }
107         }
108         try {
109             IFeatureURLElement updateElement = urlElement.getUpdate();
110             if (value.length() > 0) {
111                 URL JavaDoc siteUrl = new URL JavaDoc(value);
112                 if (updateElement == null) {
113                     // element needed, create it
114
updateElement = model.getFactory().createURLElement(
115                             urlElement, IFeatureURLElement.UPDATE);
116                     updateElement.setURL(siteUrl);
117                     urlElement.setUpdate(updateElement);
118                 } else {
119                     updateElement.setURL(siteUrl);
120                 }
121             } else {
122                 if (updateElement == null) {
123                     // do nothing
124
} else {
125                     if (updateElement.getLabel() != null
126                             && updateElement.getLabel().length() > 0) {
127                         updateElement.setURL(null);
128                     } else {
129                         // element not needed, remove it
130
urlElement.setUpdate(null);
131                     }
132                 }
133             }
134         } catch (CoreException e) {
135             PDEPlugin.logException(e);
136         } catch (MalformedURLException JavaDoc e) {
137             PDEPlugin.logException(e);
138         }
139     }
140
141     private void commitSiteName(String JavaDoc value) {
142         IFeatureModel model = (IFeatureModel) getPage().getModel();
143         IFeature feature = model.getFeature();
144
145         IFeatureURL urlElement = feature.getURL();
146         if (urlElement == null) {
147             urlElement = model.getFactory().createURL();
148             try {
149                 feature.setURL(urlElement);
150             } catch (CoreException e) {
151                 return;
152             }
153         }
154         try {
155             IFeatureURLElement updateElement = urlElement.getUpdate();
156             if (value.length() > 0) {
157                 if (updateElement == null) {
158                     // element needed, create it
159
updateElement = model.getFactory().createURLElement(
160                             urlElement, IFeatureURLElement.UPDATE);
161                     updateElement.setLabel(value);
162                     // URL not set, so element will be flagged during validation
163
urlElement.setUpdate(updateElement);
164                 } else {
165                     updateElement.setLabel(value);
166                 }
167             } else {
168                 if (updateElement == null) {
169                     // do nothing
170
} else {
171                     if (updateElement.getURL() != null) {
172                         updateElement.setLabel(null);
173                     } else {
174                         // element not needed, remove it
175
urlElement.setUpdate(null);
176                     }
177                 }
178             }
179         } catch (CoreException e) {
180             PDEPlugin.logException(e);
181         }
182     }
183
184     /**
185      * Obtains or creates a feature import with patch="true"
186      *
187      * @return
188      */

189     private IFeatureImport getPatchedFeature() {
190         IFeatureModel model = (IFeatureModel) getPage().getModel();
191         IFeature feature = model.getFeature();
192         IFeatureImport[] imports = feature.getImports();
193         for (int i = 0; i < imports.length; i++) {
194             if (imports[i].isPatch()) {
195                 return imports[i];
196             }
197         }
198         // need to recreate the import element
199
FeatureImport fimport = (FeatureImport) model.getFactory()
200                 .createImport();
201         try {
202             fimport.setType(IFeatureImport.FEATURE);
203             fimport.setPatch(true);
204             feature.addImports(new IFeatureImport[] { fimport });
205         } catch (CoreException ce) {
206             PDEPlugin.logException(ce);
207         }
208         return null;
209     }
210
211     private boolean isPatch() {
212         return fPatch;
213     }
214
215     public void createClient(Section section, FormToolkit toolkit) {
216         
217         section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
218         TableWrapData twd = new TableWrapData();
219         twd.grabHorizontal = true;
220         section.setLayoutData(twd);
221         
222         fPatch = ((FeatureEditor) getPage().getEditor()).isPatchEditor();
223
224         final IFeatureModel model = (IFeatureModel) getPage().getModel();
225         final IFeature feature = model.getFeature();
226
227         if(isPatch()){
228             getSection().setDescription(
229                 PDEUIMessages.FeatureEditor_SpecSection_desc_patch);
230         }else{
231             getSection().setDescription(
232                     PDEUIMessages.FeatureEditor_SpecSection_desc);
233         }
234
235         Composite container = toolkit.createComposite(section);
236         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
237         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
238
239         fIdText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_id, null, false);
240         fIdText.setFormEntryListener(new FormEntryAdapter(this) {
241             public void textValueChanged(FormEntry text) {
242                 try {
243                     feature.setId(text.getValue());
244                 } catch (CoreException e) {
245                     PDEPlugin.logException(e);
246                 }
247             }
248         });
249
250         fVersionText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_version, null, false);
251         fVersionText.setFormEntryListener(new FormEntryAdapter(this) {
252             public void textValueChanged(FormEntry text) {
253                 if (verifySetVersion(feature, text.getValue()) == false) {
254                     warnBadVersionFormat(text.getValue());
255                     text.setValue(feature.getVersion());
256                 }
257             }
258         });
259
260         fTitleText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_name, null, false);
261         fTitleText.setFormEntryListener(new FormEntryAdapter(this) {
262             public void textValueChanged(FormEntry text) {
263                 try {
264                     feature.setLabel(text.getValue());
265                 } catch (CoreException e) {
266                     PDEPlugin.logException(e);
267                 }
268                 getPage().getManagedForm().getForm().setText(
269                         model.getResourceString(feature.getLabel()));
270                 ((FeatureEditor) getPage().getEditor()).updateTitle();
271             }
272         });
273         fProviderText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_provider, null, false);
274         fProviderText.setFormEntryListener(new FormEntryAdapter(this) {
275             public void textValueChanged(FormEntry text) {
276                 try {
277                     String JavaDoc value = text.getValue();
278                     feature
279                             .setProviderName((value.length() > 0 ? value : null));
280                 } catch (CoreException e) {
281                     PDEPlugin.logException(e);
282                 }
283             }
284         });
285
286         fPluginText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_plugin, PDEUIMessages.GeneralInfoSection_browse,
287                 isEditable());
288
289         fPluginText.setFormEntryListener(new FormEntryAdapter(this) {
290             public void textValueChanged(FormEntry text) {
291                 try {
292                     String JavaDoc value = text.getValue();
293                     feature.setPlugin((value.length() > 0 ? value : null));
294                 } catch (CoreException e) {
295                     PDEPlugin.logException(e);
296                 }
297             }
298             public void linkActivated(HyperlinkEvent e) {
299                 String JavaDoc plugin = fPluginText.getValue();
300                 if (PluginRegistry.findModel(plugin) == null) {
301                     createFeaturePlugin();
302                 }
303                 ManifestEditor.openPluginEditor(fPluginText.getValue());
304             }
305             public void browseButtonSelected(FormEntry entry) {
306                 handleOpenDialog();
307             }
308             private void createFeaturePlugin() {
309                 NewPluginProjectWizard wizard = new NewPluginProjectWizard();
310                 WizardDialog dialog = new WizardDialog(PDEPlugin
311                         .getActiveWorkbenchShell(), wizard);
312                 dialog.create();
313                 SWTUtil.setDialogSize(dialog, 400, 500);
314                 if (dialog.open() == Window.OK) {
315                     String JavaDoc plugin = wizard.getPluginId();
316                     try {
317                         feature.setPlugin(plugin);
318                         fPluginText.setValue(plugin, false);
319                     } catch (CoreException ce) {
320                         PDEPlugin.logException(ce);
321                     }
322                 }
323             }
324         });
325
326         if (isPatch()) {
327             fPatchedIdText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_patchedId, null, false);
328             fPatchedIdText.setFormEntryListener(new FormEntryAdapter(this) {
329                 public void textValueChanged(FormEntry text) {
330                     try {
331                         IFeatureImport patchImport = getPatchedFeature();
332                         if (patchImport != null) {
333                             patchImport.setId(text.getValue());
334                         }
335                     } catch (CoreException e) {
336                         PDEPlugin.logException(e);
337                     }
338                 }
339             });
340
341             fPatchedVersionText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_patchedVersion, null, false);
342             fPatchedVersionText
343                     .setFormEntryListener(new FormEntryAdapter(this) {
344                         public void textValueChanged(FormEntry text) {
345                             IFeatureImport patchImport = getPatchedFeature();
346                             if (patchImport != null) {
347                                 if (verifySetVersion(patchImport, text
348                                         .getValue()) == false) {
349                                     warnBadVersionFormat(text.getValue());
350                                     text.setValue(patchImport.getVersion());
351                                 }
352                             }
353                         }
354                     });
355
356         }
357
358         fUpdateSiteUrlText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_updateUrl, null, false);
359         fUpdateSiteUrlText.setFormEntryListener(new FormEntryAdapter(this) {
360             public void textValueChanged(FormEntry text) {
361                 String JavaDoc url = text.getValue() != null ? text.getValue() : ""; //$NON-NLS-1$
362
if (url.length() > 0 && !verifySiteUrl(feature, url)) {
363                     warnBadUrl(url);
364                     setUpdateSiteUrlText();
365                 } else {
366                     commitSiteUrl(url);
367                 }
368             }
369         });
370
371         fUpdateSiteNameText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_updateUrlLabel, null, false);
372         fUpdateSiteNameText.setFormEntryListener(new FormEntryAdapter(this) {
373             public void textValueChanged(FormEntry text) {
374                 String JavaDoc name = text.getValue() != null ? text.getValue() : ""; //$NON-NLS-1$
375
commitSiteName(name);
376             }
377         });
378
379         GridData gd = (GridData) fIdText.getText().getLayoutData();
380         gd.widthHint = 150;
381
382         toolkit.paintBordersFor(container);
383         section.setClient(container);
384         initialize();
385     }
386
387     private boolean verifySetVersion(IFeature feature, String JavaDoc value) {
388         try {
389             if (VersionUtil.validateVersion(value).isOK())
390                 feature.setVersion(value);
391         } catch (Exception JavaDoc e) {
392             return false;
393         }
394         return true;
395     }
396
397     private boolean verifySetVersion(IFeatureImport featureImport, String JavaDoc value) {
398         try {
399             if (VersionUtil.validateVersion(value).isOK())
400                 featureImport.setVersion(value);
401         } catch (Exception JavaDoc e) {
402             return false;
403         }
404         return true;
405     }
406
407     private boolean verifySiteUrl(IFeature feature, String JavaDoc value) {
408         try {
409             new URL JavaDoc(value);
410         } catch (MalformedURLException JavaDoc e) {
411             return false;
412         }
413         return true;
414     }
415
416     private void warnBadVersionFormat(String JavaDoc text) {
417         MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.FeatureEditor_SpecSection_badVersionTitle, PDEUIMessages.FeatureEditor_SpecSection_badVersionMessage);
418     }
419
420     private void warnBadUrl(String JavaDoc text) {
421         MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.FeatureEditor_SpecSection_badUrlTitle, PDEUIMessages.FeatureEditor_SpecSection_badUrlMessage);
422     }
423
424     public void dispose() {
425         IFeatureModel model = (IFeatureModel) getPage().getModel();
426         if (model != null)
427             model.removeModelChangedListener(this);
428         super.dispose();
429     }
430
431     public void initialize() {
432         IFeatureModel model = (IFeatureModel) getPage().getModel();
433         refresh();
434         if (!model.isEditable()) {
435             fIdText.getText().setEditable(false);
436             fTitleText.getText().setEditable(false);
437             fVersionText.getText().setEditable(false);
438             fProviderText.getText().setEditable(false);
439             fPluginText.getText().setEditable(false);
440             if (isPatch()) {
441                 fPatchedIdText.getText().setEditable(false);
442                 fPatchedVersionText.getText().setEditable(false);
443             }
444             fUpdateSiteUrlText.getText().setEditable(false);
445             fUpdateSiteNameText.getText().setEditable(false);
446         }
447         model.addModelChangedListener(this);
448     }
449
450     public void modelChanged(IModelChangedEvent e) {
451         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
452             markStale();
453             return;
454         }
455         if (e.getChangeType() == IModelChangedEvent.CHANGE) {
456             Object JavaDoc objs[] = e.getChangedObjects();
457             if (objs.length > 0 && objs[0] instanceof IFeature) {
458                 markStale();
459             }
460         }
461         if (e.getChangeType() == IModelChangedEvent.CHANGE) {
462             Object JavaDoc objs[] = e.getChangedObjects();
463             if (objs.length > 0 && objs[0] instanceof IFeatureURL) {
464                 markStale();
465             }
466         }
467         Object JavaDoc objs[] = e.getChangedObjects();
468         if (objs.length > 0 && objs[0] instanceof IFeatureURLElement) {
469             markStale();
470         }
471         if (isPatch() && objs.length > 0 && objs[0] instanceof IFeatureImport) {
472             markStale();
473         }
474     }
475
476     public void setFocus() {
477         if (fIdText != null)
478             fIdText.getText().setFocus();
479     }
480
481     private void setIfDefined(FormEntry formText, String JavaDoc value) {
482         if (value != null) {
483             formText.setValue(value, true);
484         }
485     }
486
487     public void refresh() {
488         IFeatureModel model = (IFeatureModel) getPage().getModel();
489         IFeature feature = model.getFeature();
490         setIfDefined(fIdText, feature.getId());
491         setIfDefined(fTitleText, feature.getLabel());
492         getPage().getManagedForm().getForm().setText(
493                 model.getResourceString(feature.getLabel()));
494         setIfDefined(fVersionText, feature.getVersion());
495         setIfDefined(fProviderText, feature.getProviderName());
496         setIfDefined(fPluginText, feature.getPlugin());
497         if (isPatch()) {
498             IFeatureImport featureImport = getPatchedFeature();
499             if (featureImport != null) {
500                 fPatchedIdText.setValue(
501                         featureImport.getId() != null ? featureImport.getId()
502                                 : "", true); //$NON-NLS-1$
503
fPatchedVersionText.setValue(
504                         featureImport.getVersion() != null ? featureImport
505                                 .getVersion() : "", true); //$NON-NLS-1$
506
} else {
507                 fPatchedIdText.setValue("", true); //$NON-NLS-1$
508
fPatchedVersionText.setValue("", true); //$NON-NLS-1$
509
}
510         }
511         setUpdateSiteUrlText();
512         setUpdateSiteNameText();
513         super.refresh();
514     }
515
516     private void setUpdateSiteUrlText() {
517         IFeatureModel model = (IFeatureModel) getPage().getModel();
518         IFeature feature = model.getFeature();
519
520         String JavaDoc updateSiteUrl = ""; //$NON-NLS-1$
521
IFeatureURL featureUrl = feature.getURL();
522         if (featureUrl != null) {
523             IFeatureURLElement urlElement = featureUrl.getUpdate();
524             if (urlElement != null) {
525                 updateSiteUrl = urlElement.getURL() != null ? urlElement
526                         .getURL().toExternalForm() : null;
527             }
528         }
529         fUpdateSiteUrlText.setValue(updateSiteUrl != null ? updateSiteUrl : "", //$NON-NLS-1$
530
true);
531
532     }
533
534     private void setUpdateSiteNameText() {
535         IFeatureModel model = (IFeatureModel) getPage().getModel();
536         IFeature feature = model.getFeature();
537
538         String JavaDoc updateSiteLabel = ""; //$NON-NLS-1$
539
IFeatureURL featureUrl = feature.getURL();
540         if (featureUrl != null) {
541             IFeatureURLElement urlElement = featureUrl.getUpdate();
542             if (urlElement != null) {
543                 updateSiteLabel = urlElement.getLabel();
544             }
545         }
546         fUpdateSiteNameText.setValue(updateSiteLabel != null ? updateSiteLabel
547                 : "", true); //$NON-NLS-1$
548
}
549
550     public void cancelEdit() {
551         fIdText.cancelEdit();
552         fTitleText.cancelEdit();
553         fVersionText.cancelEdit();
554         fProviderText.cancelEdit();
555         fPluginText.cancelEdit();
556         if (isPatch()) {
557             fPatchedIdText.cancelEdit();
558             fPatchedVersionText.cancelEdit();
559         }
560         fUpdateSiteNameText.cancelEdit();
561         fUpdateSiteUrlText.cancelEdit();
562         super.cancelEdit();
563     }
564
565     /**
566      * @see org.eclipse.update.ui.forms.internal.FormSection#canPaste(Clipboard)
567      */

568     public boolean canPaste(Clipboard clipboard) {
569         TransferData[] types = clipboard.getAvailableTypes();
570         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
571                 RTFTransfer.getInstance() };
572         for (int i = 0; i < types.length; i++) {
573             for (int j = 0; j < transfers.length; j++) {
574                 if (transfers[j].isSupportedType(types[i]))
575                     return true;
576             }
577         }
578         return false;
579     }
580     protected void handleOpenDialog() {
581         PluginSelectionDialog dialog = new PluginSelectionDialog(getSection().getShell(), false, false);
582         dialog.create();
583         if (dialog.open() == Window.OK) {
584             IPluginModel model = (IPluginModel) dialog.getFirstResult();
585             IPlugin plugin = model.getPlugin();
586             fPluginText.setValue(plugin.getId());
587         }
588     }
589 }
590
Popular Tags