KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.Separator;
20 import org.eclipse.jface.action.ToolBarManager;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.pde.core.IModel;
28 import org.eclipse.pde.core.IModelChangedEvent;
29 import org.eclipse.pde.core.plugin.IPluginBase;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.core.plugin.ModelEntry;
32 import org.eclipse.pde.core.plugin.PluginRegistry;
33 import org.eclipse.pde.internal.core.IPluginModelListener;
34 import org.eclipse.pde.internal.core.PDECore;
35 import org.eclipse.pde.internal.core.PluginModelDelta;
36 import org.eclipse.pde.internal.core.feature.FeaturePlugin;
37 import org.eclipse.pde.internal.core.ifeature.IFeature;
38 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
39 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
40 import org.eclipse.pde.internal.core.util.CoreUtility;
41 import org.eclipse.pde.internal.ui.PDEPlugin;
42 import org.eclipse.pde.internal.ui.PDEUIMessages;
43 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
44 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
45 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
46 import org.eclipse.pde.internal.ui.editor.TableSection;
47 import org.eclipse.pde.internal.ui.editor.actions.SortAction;
48 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
49 import org.eclipse.pde.internal.ui.parts.TablePart;
50 import org.eclipse.pde.internal.ui.wizards.ListUtil;
51 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.custom.BusyIndicator;
54 import org.eclipse.swt.dnd.Clipboard;
55 import org.eclipse.swt.events.DisposeEvent;
56 import org.eclipse.swt.events.DisposeListener;
57 import org.eclipse.swt.graphics.Cursor;
58 import org.eclipse.swt.layout.GridData;
59 import org.eclipse.swt.widgets.Composite;
60 import org.eclipse.swt.widgets.Display;
61 import org.eclipse.swt.widgets.ToolBar;
62 import org.eclipse.ui.actions.ActionFactory;
63 import org.eclipse.ui.forms.widgets.FormToolkit;
64 import org.eclipse.ui.forms.widgets.Section;
65
66 public class PluginSection extends TableSection implements
67         IPluginModelListener {
68     private OpenReferenceAction fOpenAction;
69
70     private TableViewer fPluginViewer;
71
72     private Action fNewAction;
73
74     private Action fDeleteAction;
75
76     private SortAction fSortAction;
77     
78     class PluginContentProvider extends DefaultContentProvider implements
79             IStructuredContentProvider {
80         public Object JavaDoc[] getElements(Object JavaDoc parent) {
81             if (parent instanceof IFeature) {
82                 return ((IFeature) parent).getPlugins();
83             }
84             return new Object JavaDoc[0];
85         }
86     }
87
88     public PluginSection(PDEFormPage page, Composite parent) {
89         super(page, parent, Section.DESCRIPTION, new String JavaDoc[] {
90                 PDEUIMessages.FeatureEditor_PluginSection_new, null,
91                 PDEUIMessages.FeatureEditor_SpecSection_synchronize });
92         getSection().setText(PDEUIMessages.FeatureEditor_PluginSection_pluginTitle);
93         getSection().setDescription(PDEUIMessages.FeatureEditor_PluginSection_pluginDesc);
94         getTablePart().setEditable(false);
95     }
96
97     public void commit(boolean onSave) {
98         super.commit(onSave);
99     }
100
101     public void createClient(Section section, FormToolkit toolkit) {
102         
103         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
104         GridData data = new GridData(GridData.FILL_BOTH);
105         section.setLayoutData(data);
106         
107         Composite container = createClientContainer(section, 2, toolkit);
108
109         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
110         TablePart tablePart = getTablePart();
111         fPluginViewer = tablePart.getTableViewer();
112         fPluginViewer.setContentProvider(new PluginContentProvider());
113         fPluginViewer.setLabelProvider(PDEPlugin.getDefault()
114                 .getLabelProvider());
115         fPluginViewer.setComparator(ListUtil.NAME_COMPARATOR);
116         toolkit.paintBordersFor(container);
117         makeActions();
118         section.setClient(container);
119         initialize();
120         createSectionToolbar(section, toolkit);
121     }
122
123     /**
124      * @param section
125      * @param toolkit
126      */

127     private void createSectionToolbar(Section section, FormToolkit toolkit) {
128         
129         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
130         ToolBar toolbar = toolBarManager.createControl(section);
131         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
132         toolbar.setCursor(handCursor);
133         // Cursor needs to be explicitly disposed
134
toolbar.addDisposeListener(new DisposeListener() {
135             public void widgetDisposed(DisposeEvent e) {
136                 if ((handCursor != null) &&
137                         (handCursor.isDisposed() == false)) {
138                     handCursor.dispose();
139                 }
140             }
141         });
142         // Add sort action to the tool bar
143
fSortAction = new SortAction(
144                 getStructuredViewerPart().getViewer(),
145                 PDEUIMessages.FeatureEditor_PluginSection_sortAlpha,
146                 ListUtil.NAME_COMPARATOR,
147                 null,
148                 null);
149         
150         toolBarManager.add(fSortAction);
151
152         toolBarManager.update(true);
153
154         section.setTextClient(toolbar);
155     }
156     
157     protected void handleDoubleClick(IStructuredSelection selection) {
158         fOpenAction.run();
159     }
160
161     protected void buttonSelected(int index) {
162         if (index == 0)
163             handleNew();
164         if (index == 2)
165             handleSynchronize();
166     }
167
168     public void dispose() {
169         IFeatureModel model = (IFeatureModel) getPage().getModel();
170         if (model != null)
171         PDECore.getDefault().getModelManager().removePluginModelListener(this);
172         super.dispose();
173     }
174
175     public boolean setFormInput(Object JavaDoc object) {
176         if (object instanceof IFeaturePlugin) {
177             fPluginViewer.setSelection(new StructuredSelection(object), true);
178             return true;
179         }
180         return false;
181     }
182
183     protected void fillContextMenu(IMenuManager manager) {
184         manager.add(fOpenAction);
185         // add new
186
manager.add(new Separator());
187         manager.add(fNewAction);
188         manager.add(fDeleteAction);
189         // add delete
190

191         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(
192                 manager);
193     }
194
195     private void handleNew() {
196         BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(),
197                 new Runnable JavaDoc() {
198                     public void run() {
199                         IPluginModelBase[] allModels = PluginRegistry.getActiveModels();
200                         ArrayList JavaDoc newModels = new ArrayList JavaDoc();
201                         for (int i = 0; i < allModels.length; i++) {
202                             if (canAdd(allModels[i]))
203                                 newModels.add(allModels[i]);
204                         }
205                         IPluginModelBase[] candidateModels = (IPluginModelBase[]) newModels
206                                 .toArray(new IPluginModelBase[newModels.size()]);
207                         PluginSelectionDialog dialog = new PluginSelectionDialog(
208                                 fPluginViewer.getTable().getShell(),
209                                 candidateModels, true);
210                         if (dialog.open() == Window.OK) {
211                             Object JavaDoc[] models = dialog.getResult();
212                             try {
213                                 doAdd(models);
214                             } catch (CoreException e) {
215                                 PDEPlugin.log(e);
216                             }
217                         }
218                     }
219                 });
220     }
221
222     private void doAdd(Object JavaDoc[] candidates) throws CoreException {
223         IFeatureModel model = (IFeatureModel) getPage().getModel();
224         IFeature feature = model.getFeature();
225         IFeaturePlugin[] added = new IFeaturePlugin[candidates.length];
226         for (int i = 0; i < candidates.length; i++) {
227             IPluginModelBase candidate = (IPluginModelBase) candidates[i];
228             FeaturePlugin fplugin = (FeaturePlugin) model.getFactory()
229                     .createPlugin();
230             fplugin.loadFrom(candidate.getPluginBase());
231             fplugin.setVersion("0.0.0"); //$NON-NLS-1$
232
fplugin.setUnpack(CoreUtility.guessUnpack(candidate
233                     .getBundleDescription()));
234             added[i] = fplugin;
235         }
236         feature.addPlugins(added);
237     }
238     
239     private boolean canAdd(IPluginModelBase candidate) {
240         IPluginBase plugin = candidate.getPluginBase();
241
242         IFeatureModel model = (IFeatureModel) getPage().getModel();
243         IFeaturePlugin[] fplugins = model.getFeature().getPlugins();
244
245         for (int i = 0; i < fplugins.length; i++) {
246             if (fplugins[i].getId().equals(plugin.getId()))
247                 return false;
248         }
249         return true;
250     }
251     
252     private void handleSelectAll() {
253         IStructuredContentProvider provider = (IStructuredContentProvider) fPluginViewer
254                 .getContentProvider();
255         Object JavaDoc[] elements = provider.getElements(fPluginViewer.getInput());
256         StructuredSelection ssel = new StructuredSelection(elements);
257         fPluginViewer.setSelection(ssel);
258     }
259
260     private void handleDelete() {
261         IStructuredSelection ssel = (IStructuredSelection) fPluginViewer
262                 .getSelection();
263
264         if (ssel.isEmpty())
265             return;
266         IFeatureModel model = (IFeatureModel) getPage().getModel();
267         if (!model.isEditable()) {
268             return;
269         }
270         IFeature feature = model.getFeature();
271
272         try {
273             IFeaturePlugin[] removed = new IFeaturePlugin[ssel.size()];
274             int i = 0;
275             for (Iterator JavaDoc iter = ssel.iterator(); iter.hasNext();) {
276                 IFeaturePlugin iobj = (IFeaturePlugin) iter.next();
277                 removed[i++] = iobj;
278             }
279             feature.removePlugins(removed);
280         } catch (CoreException e) {
281             PDEPlugin.logException(e);
282         }
283     }
284
285     private void handleSynchronize() {
286         final FeatureEditorContributor contributor = (FeatureEditorContributor) getPage()
287                 .getPDEEditor().getContributor();
288         BusyIndicator.showWhile(fPluginViewer.getControl().getDisplay(),
289                 new Runnable JavaDoc() {
290                     public void run() {
291                         contributor.getSynchronizeAction().run();
292                     }
293                 });
294     }
295
296     public boolean doGlobalAction(String JavaDoc actionId) {
297         if (actionId.equals(ActionFactory.DELETE.getId())) {
298             BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(),
299                     new Runnable JavaDoc() {
300                         public void run() {
301                             handleDelete();
302                         }
303                     });
304             return true;
305         }
306         if (actionId.equals(ActionFactory.CUT.getId())) {
307             // delete here and let the editor transfer
308
// the selection to the clipboard
309
handleDelete();
310             return false;
311         }
312         if (actionId.equals(ActionFactory.PASTE.getId())) {
313             doPaste();
314             return true;
315         }
316         if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
317             BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(),
318                     new Runnable JavaDoc() {
319                         public void run() {
320                             handleSelectAll();
321                         }
322                     });
323             return true;
324         }
325         return false;
326     }
327
328     protected void selectionChanged(IStructuredSelection selection) {
329         getPage().getPDEEditor().setSelection(selection);
330     }
331
332     public void initialize() {
333         IFeatureModel model = (IFeatureModel) getPage().getModel();
334         refresh();
335         getTablePart().setButtonEnabled(0, model.isEditable());
336         getTablePart().setButtonEnabled(2, model.isEditable());
337         model.addModelChangedListener(this);
338         PDECore.getDefault().getModelManager().addPluginModelListener(this);
339     }
340
341     public void modelChanged(IModelChangedEvent e) {
342         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
343             markStale();
344             return;
345         }
346         Object JavaDoc obj = e.getChangedObjects()[0];
347         if (obj instanceof IFeaturePlugin) {
348             if (e.getChangeType() == IModelChangedEvent.CHANGE) {
349                 fPluginViewer.update(obj, null);
350             } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
351                 fPluginViewer.add(e.getChangedObjects());
352                 if (e.getChangedObjects().length > 0) {
353                     fPluginViewer.setSelection(new StructuredSelection(e
354                             .getChangedObjects()[0]));
355                 }
356             } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
357                 fPluginViewer.remove(e.getChangedObjects());
358             }
359         }
360     }
361
362     private void makeActions() {
363         IModel model = (IModel) getPage().getModel();
364         fNewAction = new Action() {
365             public void run() {
366                 handleNew();
367             }
368         };
369         fNewAction.setText(PDEUIMessages.Menus_new_label);
370         fNewAction.setEnabled(model.isEditable());
371
372         fDeleteAction = new Action() {
373             public void run() {
374                 BusyIndicator.showWhile(fPluginViewer.getTable().getDisplay(),
375                         new Runnable JavaDoc() {
376                             public void run() {
377                                 handleDelete();
378                             }
379                         });
380             }
381         };
382         fDeleteAction.setText(PDEUIMessages.Actions_delete_label);
383         fDeleteAction.setEnabled(model.isEditable());
384         fOpenAction = new OpenReferenceAction(fPluginViewer);
385     }
386
387     public void modelsChanged(final PluginModelDelta delta) {
388         getSection().getDisplay().asyncExec(new Runnable JavaDoc() {
389             public void run() {
390                 if (getSection().isDisposed()) {
391                     return;
392                 }
393                 ModelEntry[] added = delta.getAddedEntries();
394                 ModelEntry[] removed = delta.getRemovedEntries();
395                 ModelEntry[] changed = delta.getChangedEntries();
396                 if (hasPluginModels(added) || hasPluginModels(removed)
397                         || hasPluginModels(changed))
398                     markStale();
399             }
400         });
401     }
402
403     private boolean hasPluginModels(ModelEntry[] entries) {
404         if (entries == null)
405             return false;
406         return true;
407     }
408
409     public void setFocus() {
410         if (fPluginViewer != null)
411             fPluginViewer.getTable().setFocus();
412     }
413
414     public void refresh() {
415         IFeatureModel model = (IFeatureModel) getPage().getModel();
416         IFeature feature = model.getFeature();
417         fPluginViewer.setInput(feature);
418         super.refresh();
419     }
420
421     /**
422      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Clipboard)
423      */

424     public boolean canPaste(Clipboard clipboard) {
425         Object JavaDoc[] objects = (Object JavaDoc[]) clipboard.getContents(ModelDataTransfer
426                 .getInstance());
427         if (objects != null && objects.length > 0) {
428             return canPaste(null, objects);
429         }
430         return false;
431     }
432
433     /**
434      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Object,
435      * Object[])
436      */

437     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
438         for (int i = 0; i < objects.length; i++) {
439             if (!(objects[i] instanceof FeaturePlugin))
440                 return false;
441         }
442         return true;
443     }
444
445     /**
446      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste()
447      */

448     protected void doPaste() {
449         Clipboard clipboard = getPage().getPDEEditor().getClipboard();
450         Object JavaDoc[] objects = (Object JavaDoc[]) clipboard.getContents(ModelDataTransfer
451                 .getInstance());
452         if (objects != null && canPaste(null, objects))
453             doPaste(null, objects);
454     }
455
456     /**
457      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(Object,
458      * Object[])
459      */

460     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
461         IFeatureModel model = (IFeatureModel) getPage().getModel();
462         if (!model.isEditable()) {
463             return;
464         }
465         IFeature feature = model.getFeature();
466         FeaturePlugin[] fPlugins = new FeaturePlugin[objects.length];
467         try {
468             for (int i = 0; i < objects.length; i++) {
469                 FeaturePlugin fPlugin = (FeaturePlugin) objects[i];
470                 fPlugin.setModel(model);
471                 fPlugin.setParent(feature);
472                 fPlugins[i] = fPlugin;
473             }
474             feature.addPlugins(fPlugins);
475         } catch (CoreException e) {
476             PDEPlugin.logException(e);
477         }
478     }
479
480     void fireSelection() {
481         ISelection sel = fPluginViewer.getSelection();
482         if (!sel.isEmpty()) {
483             fPluginViewer.setSelection(fPluginViewer.getSelection());
484         } else if (fPluginViewer.getElementAt(0) != null) {
485             fPluginViewer.setSelection(new StructuredSelection(fPluginViewer
486                     .getElementAt(0)));
487         }
488     }
489     
490     protected boolean createCount() { return true; }
491
492 }
493
Popular Tags