KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > product > PluginSection


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.product;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.TreeMap JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.core.runtime.QualifiedName;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.jface.viewers.ViewerComparator;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.jface.wizard.WizardDialog;
35 import org.eclipse.osgi.service.resolver.BundleDescription;
36 import org.eclipse.osgi.service.resolver.HostSpecification;
37 import org.eclipse.pde.core.IModelChangedEvent;
38 import org.eclipse.pde.core.plugin.IPluginBase;
39 import org.eclipse.pde.core.plugin.IPluginModelBase;
40 import org.eclipse.pde.core.plugin.PluginRegistry;
41 import org.eclipse.pde.internal.core.IPluginModelListener;
42 import org.eclipse.pde.internal.core.PluginModelDelta;
43 import org.eclipse.pde.internal.core.TargetPlatformHelper;
44 import org.eclipse.pde.internal.core.iproduct.IProduct;
45 import org.eclipse.pde.internal.core.iproduct.IProductModel;
46 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
47 import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
48 import org.eclipse.pde.internal.ui.IPDEUIConstants;
49 import org.eclipse.pde.internal.ui.PDEPlugin;
50 import org.eclipse.pde.internal.ui.PDEUIMessages;
51 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
52 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
53 import org.eclipse.pde.internal.ui.editor.TableSection;
54 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
55 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
56 import org.eclipse.pde.internal.ui.parts.TablePart;
57 import org.eclipse.pde.internal.ui.search.dependencies.DependencyCalculator;
58 import org.eclipse.pde.internal.ui.util.PersistablePluginObject;
59 import org.eclipse.pde.internal.ui.util.SWTUtil;
60 import org.eclipse.pde.internal.ui.wizards.plugin.NewFragmentProjectWizard;
61 import org.eclipse.pde.internal.ui.wizards.plugin.NewPluginProjectWizard;
62 import org.eclipse.swt.SWT;
63 import org.eclipse.swt.events.SelectionAdapter;
64 import org.eclipse.swt.events.SelectionEvent;
65 import org.eclipse.swt.layout.GridData;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Control;
69 import org.eclipse.swt.widgets.Table;
70 import org.eclipse.ui.IEditorInput;
71 import org.eclipse.ui.IFileEditorInput;
72 import org.eclipse.ui.IWorkingSet;
73 import org.eclipse.ui.IWorkingSetManager;
74 import org.eclipse.ui.PlatformUI;
75 import org.eclipse.ui.actions.ActionFactory;
76 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
77 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
78 import org.eclipse.ui.forms.widgets.FormToolkit;
79 import org.eclipse.ui.forms.widgets.Section;
80
81
82 public class PluginSection extends TableSection implements IPluginModelListener{
83     
84     class ContentProvider extends DefaultTableProvider {
85         public Object JavaDoc[] getElements(Object JavaDoc parent) {
86             return getProduct().getPlugins();
87         }
88     }
89
90     private TableViewer fPluginTable;
91     private Button fIncludeOptionalButton;
92     public static final QualifiedName OPTIONAL_PROPERTY = new QualifiedName(IPDEUIConstants.PLUGIN_ID, "product.includeOptional"); //$NON-NLS-1$
93

94     public PluginSection(PDEFormPage formPage, Composite parent) {
95         super(formPage, parent, Section.DESCRIPTION, getButtonLabels());
96     }
97     
98     private static String JavaDoc[] getButtonLabels() {
99         String JavaDoc[] labels = new String JavaDoc[9];
100         labels[0] = PDEUIMessages.Product_PluginSection_add;
101         labels[1] = PDEUIMessages.Product_PluginSection_working;
102         labels[2] = PDEUIMessages.Product_PluginSection_required;
103         labels[3] = PDEUIMessages.PluginSection_remove;
104         labels[4] = PDEUIMessages.Product_PluginSection_removeAll;
105         labels[5] = null;
106         labels[6] = null;
107         labels[7] = PDEUIMessages.Product_PluginSection_newPlugin;
108         labels[8] = PDEUIMessages.Product_PluginSection_newFragment;
109         return labels;
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
114      */

115     protected void createClient(Section section, FormToolkit toolkit) {
116         
117         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
118         GridData sectionData = new GridData(GridData.FILL_BOTH);
119         sectionData.verticalSpan = 2;
120         section.setLayoutData(sectionData);
121         
122         Composite container = createClientContainer(section, 2, toolkit);
123         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
124         container.setLayoutData(new GridData(GridData.FILL_BOTH));
125         
126         createOptionalDependenciesButton(container);
127
128         TablePart tablePart = getTablePart();
129         fPluginTable = tablePart.getTableViewer();
130         fPluginTable.setContentProvider(new ContentProvider());
131         fPluginTable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
132         fPluginTable.setComparator(new ViewerComparator() {
133             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
134                 IProductPlugin p1 = (IProductPlugin)e1;
135                 IProductPlugin p2 = (IProductPlugin)e2;
136                 return super.compare(viewer, p1.getId(), p2.getId());
137             }
138         });
139         GridData data = (GridData)tablePart.getControl().getLayoutData();
140         data.minimumWidth = 200;
141         fPluginTable.setInput(getProduct());
142         
143         tablePart.setButtonEnabled(0, isEditable());
144         tablePart.setButtonEnabled(1, isEditable());
145         tablePart.setButtonEnabled(2, isEditable());
146         
147         // remove buttons will be updated on refresh
148

149         tablePart.setButtonEnabled(7, isEditable());
150         tablePart.setButtonEnabled(8, isEditable());
151         
152         toolkit.paintBordersFor(container);
153         section.setClient(container);
154         
155         section.setText(PDEUIMessages.Product_PluginSection_title);
156         section.setDescription(PDEUIMessages.Product_PluginSection_desc);
157         getModel().addModelChangedListener(this);
158     }
159     
160     private void createOptionalDependenciesButton(Composite container) {
161         if (isEditable()) {
162             fIncludeOptionalButton = new Button(container, SWT.CHECK);
163             fIncludeOptionalButton.setText(PDEUIMessages.PluginSection_includeOptional);
164             // initialize value
165
IEditorInput input = getPage().getEditorInput();
166             if (input instanceof IFileEditorInput) {
167                 IFile file = ((IFileEditorInput)input).getFile();
168                 try {
169                     fIncludeOptionalButton.setSelection("true".equals(file.getPersistentProperty(OPTIONAL_PROPERTY))); //$NON-NLS-1$
170
} catch (CoreException e) {
171                 }
172             }
173             // create listener to save value when the checkbox is changed
174
fIncludeOptionalButton.addSelectionListener(new SelectionAdapter() {
175                 public void widgetSelected(SelectionEvent e) {
176                     IEditorInput input = getPage().getEditorInput();
177                     if (input instanceof IFileEditorInput) {
178                         IFile file = ((IFileEditorInput)input).getFile();
179                         try {
180                             file.setPersistentProperty(OPTIONAL_PROPERTY, fIncludeOptionalButton.getSelection() ? "true" : null); //$NON-NLS-1$
181
} catch (CoreException e1) {
182                         }
183                     }
184                 }
185             });
186         }
187     }
188     
189     /* (non-Javadoc)
190      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int)
191      */

192     protected void buttonSelected(int index) {
193         switch (index) {
194         case 0:
195             handleAdd();
196             break;
197         case 1:
198             handleAddWorkingSet();
199             break;
200         case 2:
201             handleAddRequired(getProduct().getPlugins(), fIncludeOptionalButton.getSelection());
202             break;
203         case 3:
204             handleDelete();
205             break;
206         case 4:
207             handleRemoveAll();
208             break;
209         case 7:
210             handleNewPlugin();
211             break;
212         case 8:
213             handleNewFragment();
214         }
215     }
216     
217     private void handleNewFragment() {
218         NewFragmentProjectWizard wizard = new NewFragmentProjectWizard();
219         WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
220         dialog.create();
221         SWTUtil.setDialogSize(dialog, 400, 500);
222         if (dialog.open() == Window.OK) {
223             addPlugin(wizard.getFragmentId());
224         }
225     }
226
227     private void handleNewPlugin() {
228         NewPluginProjectWizard wizard = new NewPluginProjectWizard();
229         WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
230         dialog.create();
231         SWTUtil.setDialogSize(dialog, 400, 500);
232         if (dialog.open() == Window.OK) {
233             addPlugin(wizard.getPluginId());
234         }
235     }
236
237     /* (non-Javadoc)
238      * @see org.eclipse.pde.internal.ui.editor.TableSection#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection)
239      */

240     protected void handleDoubleClick(IStructuredSelection selection) {
241         handleOpen(selection);
242     }
243     
244     /* (non-Javadoc)
245      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
246      */

247     public void dispose() {
248         IProductModel model = getModel();
249         if (model != null)
250             model.removeModelChangedListener(this);
251         super.dispose();
252     }
253     
254     /* (non-Javadoc)
255      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
256      */

257     public boolean doGlobalAction(String JavaDoc actionId) {
258         if (actionId.equals(ActionFactory.DELETE.getId())) {
259             handleDelete();
260             return true;
261         }
262         if (actionId.equals(ActionFactory.CUT.getId())) {
263             handleDelete();
264             return false;
265         }
266         if (actionId.equals(ActionFactory.PASTE.getId())) {
267             doPaste();
268             return true;
269         }
270         return false;
271     }
272     
273     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
274         for (int i = 0; i < objects.length; i++) {
275             if (objects[i] instanceof IProductPlugin)
276                 return true;
277         }
278         return false;
279     }
280     
281     /* (non-Javadoc)
282      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#fillContextMenu(org.eclipse.jface.action.IMenuManager)
283      */

284     protected void fillContextMenu(IMenuManager manager) {
285         IStructuredSelection ssel = (IStructuredSelection)fPluginTable.getSelection();
286         if (ssel == null)
287             return;
288         
289         Action openAction = new Action(PDEUIMessages.PluginSection_open) {
290             public void run() {
291                 handleDoubleClick((IStructuredSelection)fPluginTable.getSelection());
292             }
293         };
294         openAction.setEnabled(isEditable() && ssel.size() == 1);
295         manager.add(openAction);
296         
297         manager.add(new Separator());
298         
299         Action removeAction = new Action(PDEUIMessages.PluginSection_remove) {
300             public void run() {
301                 handleDelete();
302             }
303         };
304         removeAction.setEnabled(isEditable() && ssel.size() > 0);
305         manager.add(removeAction);
306         
307         Action removeAll = new Action(PDEUIMessages.PluginSection_removeAll) {
308             public void run() {
309                 handleRemoveAll();
310             }
311         };
312         removeAll.setEnabled(isEditable());
313         manager.add(removeAll);
314
315         manager.add(new Separator());
316         
317         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
318     }
319
320     private void handleOpen(IStructuredSelection selection) {
321         Object JavaDoc object = selection.getFirstElement();
322         if (object instanceof IProductPlugin) {
323             ManifestEditor.openPluginEditor(((IProductPlugin)object).getId());
324         }
325     }
326     
327     public static void handleAddRequired(IProductPlugin[] plugins, boolean includeOptional) {
328         if (plugins.length == 0)
329             return;
330         
331         ArrayList JavaDoc list = new ArrayList JavaDoc(plugins.length);
332         for (int i = 0; i < plugins.length; i++) {
333             list.add(TargetPlatformHelper.getState().getBundle(plugins[i].getId(), null));
334         }
335         DependencyCalculator calculator = new DependencyCalculator(includeOptional);
336         calculator.findDependencies(list.toArray());
337         
338         BundleDescription[] bundles = TargetPlatformHelper.getState().getBundles();
339         for (int i = 0; i < bundles.length; i++) {
340             HostSpecification host = bundles[i].getHost();
341             if (host != null && !("org.eclipse.ui.workbench.compatibility".equals(bundles[i].getSymbolicName())) //$NON-NLS-1$
342
&& calculator.containsPluginId(host.getName())) { //$NON-NLS-1$
343
calculator.findDependency(bundles[i]);
344             }
345         }
346         
347         Collection JavaDoc dependencies = calculator.getBundleIDs();
348         
349         IProduct product = plugins[0].getProduct();
350         IProductModelFactory factory = product.getModel().getFactory();
351         IProductPlugin[] requiredPlugins = new IProductPlugin[dependencies.size()];
352         int i = 0;
353         Iterator JavaDoc iter = dependencies.iterator();
354         while (iter.hasNext()) {
355             String JavaDoc id = iter.next().toString();
356             IProductPlugin plugin = factory.createPlugin();
357             plugin.setId(id);
358             requiredPlugins[i++] = plugin;
359         }
360         product.addPlugins(requiredPlugins);
361     }
362     
363     private void handleAddWorkingSet() {
364         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
365         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), true);
366         if (dialog.open() == Window.OK) {
367             IWorkingSet[] workingSets = dialog.getSelection();
368             IProduct product = getProduct();
369             IProductModelFactory factory = product.getModel().getFactory();
370             ArrayList JavaDoc pluginList = new ArrayList JavaDoc();
371             for (int i = 0; i < workingSets.length; i++) {
372                 IAdaptable[] elements = workingSets[i].getElements();
373                 for (int j = 0; j < elements.length; j++) {
374                     IPluginModelBase model = findModel(elements[j]);
375                     if (model != null) {
376                         IProductPlugin plugin = factory.createPlugin();
377                         IPluginBase base = model.getPluginBase();
378                         plugin.setId(base.getId());
379                         pluginList.add(plugin);
380                     }
381                 }
382             }
383             product.addPlugins((IProductPlugin[]) pluginList.toArray(new IProductPlugin[pluginList.size()]));
384         }
385     }
386     
387     private void handleRemoveAll() {
388         IProduct product = getProduct();
389         product.removePlugins(product.getPlugins());
390     }
391     
392     private void handleDelete() {
393         IStructuredSelection ssel = (IStructuredSelection)fPluginTable.getSelection();
394         if (ssel.size() > 0) {
395             Object JavaDoc[] objects = ssel.toArray();
396             IProductPlugin[] plugins = new IProductPlugin[objects.length];
397             System.arraycopy(objects, 0, plugins, 0, objects.length);
398             getProduct().removePlugins(plugins);
399         }
400     }
401
402     private void handleAdd() {
403         ElementListSelectionDialog dialog = new ElementListSelectionDialog(
404                 PDEPlugin.getActiveWorkbenchShell(),
405                 PDEPlugin.getDefault().getLabelProvider());
406         dialog.setElements(getBundles());
407         dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
408         dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
409         dialog.setMultipleSelection(true);
410         if (dialog.open() == Window.OK) {
411             Object JavaDoc[] bundles = dialog.getResult();
412             for (int i = 0; i < bundles.length; i++) {
413                 addPlugin(((BundleDescription)bundles[i]).getSymbolicName());
414             }
415         }
416     }
417     
418     private BundleDescription[] getBundles() {
419         TreeMap JavaDoc map = new TreeMap JavaDoc();
420         IProduct product = getProduct();
421         BundleDescription[] bundles = TargetPlatformHelper.getState().getBundles();
422         for (int i = 0; i < bundles.length; i++) {
423             String JavaDoc id = bundles[i].getSymbolicName();
424             if (!product.containsPlugin(id)) {
425                 map.put(id, bundles[i]);
426             }
427         }
428         return (BundleDescription[])map.values().toArray(new BundleDescription[map.size()]);
429     }
430     
431     private void addPlugin(String JavaDoc id) {
432         IProduct product = getProduct();
433         IProductModelFactory factory = product.getModel().getFactory();
434         IProductPlugin plugin = factory.createPlugin();
435         plugin.setId(id);
436         product.addPlugins(new IProductPlugin[] {plugin});
437         fPluginTable.setSelection(new StructuredSelection(plugin));
438     }
439     
440     private IProduct getProduct() {
441         return getModel().getProduct();
442     }
443     
444     private IProductModel getModel() {
445         return (IProductModel) getPage().getPDEEditor().getAggregateModel();
446     }
447
448     /* (non-Javadoc)
449      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
450      */

451     public void modelChanged(IModelChangedEvent e) {
452         // No need to call super, handling world changed event here
453
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
454             handleModelEventWorldChanged(e);
455             return;
456         }
457         Object JavaDoc[] objects = e.getChangedObjects();
458         if (e.getChangeType() == IModelChangedEvent.INSERT) {
459             for (int i = 0; i < objects.length; i++) {
460                 if (objects[i] instanceof IProductPlugin)
461                     fPluginTable.add(objects[i]);
462             }
463         } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
464             
465             Table table = fPluginTable.getTable();
466             int index = table.getSelectionIndex();
467             
468             for (int i = 0; i < objects.length; i++) {
469                 if (objects[i] instanceof IProductPlugin)
470                     fPluginTable.remove(objects[i]);
471             }
472             
473             // Update Selection
474

475             int count = table.getItemCount();
476                 
477             if ( count == 0 ) {
478                 // Nothing to select
479
} else if ( index < count ) {
480                 table.setSelection( index );
481             } else {
482                 table.setSelection( count - 1 );
483             }
484             
485         }
486         updateRemoveButtons(false, true);
487     }
488
489     /**
490      * @param event
491      */

492     private void handleModelEventWorldChanged(IModelChangedEvent event) {
493         // This section can get disposed if the configuration is changed from
494
// plugins to features or vice versa. Subsequently, the configuration
495
// page is removed and readded. In this circumstance, abort the
496
// refresh
497
if (fPluginTable.getTable().isDisposed()) {
498             return;
499         }
500         // Reload the input
501
fPluginTable.setInput(getProduct());
502         // Perform the refresh
503
refresh();
504     }
505         
506     /* (non-Javadoc)
507      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
508      */

509     public void refresh() {
510         fPluginTable.refresh();
511         updateRemoveButtons(true, true);
512         super.refresh();
513     }
514
515     /* (non-Javadoc)
516      * @see org.eclipse.pde.internal.core.IPluginModelListener#modelsChanged(org.eclipse.pde.internal.core.PluginModelDelta)
517      */

518     public void modelsChanged(PluginModelDelta delta) {
519         final Control control = fPluginTable.getControl();
520         if (!control.isDisposed()) {
521             control.getDisplay().asyncExec(new Runnable JavaDoc() {
522                 public void run() {
523                     if (!control.isDisposed()) {
524                         fPluginTable.refresh();
525                         updateRemoveButtons(true, true);
526                     }
527                 }
528             });
529         }
530     }
531     
532     private IPluginModelBase findModel(IAdaptable object) {
533         if (object instanceof IJavaProject)
534             object = ((IJavaProject)object).getProject();
535         if (object instanceof IProject)
536             return PluginRegistry.findModel((IProject)object);
537         if (object instanceof PersistablePluginObject) {
538             return PluginRegistry.findModel(((PersistablePluginObject)object).getPluginID());
539         }
540         return null;
541     }
542     
543     protected void selectionChanged(IStructuredSelection selection) {
544         getPage().getPDEEditor().setSelection(selection);
545         updateRemoveButtons(true, false);
546     }
547     
548     public boolean setFormInput(Object JavaDoc input) {
549         if (input instanceof IProductPlugin) {
550             fPluginTable.setSelection(new StructuredSelection(input), true);
551             return true;
552         }
553         return super.setFormInput(input);
554     }
555     
556     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
557         IProductPlugin[] plugins;
558         if (objects instanceof IProductPlugin[])
559             plugins = (IProductPlugin[])objects;
560         else {
561             plugins = new IProductPlugin[objects.length];
562             for (int i = 0; i < objects.length; i++)
563                 if (objects[i] instanceof IProductPlugin)
564                     plugins[i] = (IProductPlugin)objects[i];
565         }
566         getProduct().addPlugins(plugins);
567     }
568     
569     private void updateRemoveButtons(boolean updateRemove, boolean updateRemoveAll) {
570         TablePart tablePart = getTablePart();
571         if (updateRemove) {
572             ISelection selection = getViewerSelection();
573             tablePart.setButtonEnabled(3,
574                     isEditable() && !selection.isEmpty() && selection instanceof IStructuredSelection &&
575                     ((IStructuredSelection)selection).getFirstElement() instanceof IProductPlugin);
576         }
577         int count = fPluginTable.getTable().getItemCount();
578         if (updateRemoveAll)
579             tablePart.setButtonEnabled(4, isEditable() && count > 0);
580         tablePart.setButtonEnabled(2, isEditable() && count > 0);
581     }
582     
583     protected boolean createCount() { return true; }
584     
585     public boolean includeOptionalDependencies() {
586         return fIncludeOptionalButton.getSelection();
587     }
588 }
589
Popular Tags