KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.core.FeatureModelManager;
30 import org.eclipse.pde.internal.core.IFeatureModelDelta;
31 import org.eclipse.pde.internal.core.IFeatureModelListener;
32 import org.eclipse.pde.internal.core.PDECore;
33 import org.eclipse.pde.internal.core.feature.FeatureChild;
34 import org.eclipse.pde.internal.core.ifeature.IFeature;
35 import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
36 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
37 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
38 import org.eclipse.pde.internal.ui.PDEPlugin;
39 import org.eclipse.pde.internal.ui.PDEUIMessages;
40 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
41 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
42 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
43 import org.eclipse.pde.internal.ui.editor.TableSection;
44 import org.eclipse.pde.internal.ui.editor.actions.SortAction;
45 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
46 import org.eclipse.pde.internal.ui.parts.TablePart;
47 import org.eclipse.pde.internal.ui.wizards.FeatureSelectionDialog;
48 import org.eclipse.pde.internal.ui.wizards.ListUtil;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.custom.BusyIndicator;
51 import org.eclipse.swt.dnd.Clipboard;
52 import org.eclipse.swt.events.DisposeEvent;
53 import org.eclipse.swt.events.DisposeListener;
54 import org.eclipse.swt.graphics.Cursor;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.widgets.Composite;
57 import org.eclipse.swt.widgets.Display;
58 import org.eclipse.swt.widgets.ToolBar;
59 import org.eclipse.ui.actions.ActionFactory;
60 import org.eclipse.ui.forms.widgets.FormToolkit;
61 import org.eclipse.ui.forms.widgets.Section;
62
63 public class IncludedFeaturesSection extends TableSection implements
64         IFeatureModelListener {
65     private TableViewer fIncludesViewer;
66
67     private Action fNewAction;
68
69     private Action fOpenAction;
70
71     private Action fDeleteAction;
72
73     private SortAction fSortAction;
74     
75     class IncludedFeaturesContentProvider extends DefaultContentProvider
76             implements IStructuredContentProvider {
77         public Object JavaDoc[] getElements(Object JavaDoc parent) {
78             if (parent instanceof IFeature) {
79                 return ((IFeature) parent).getIncludedFeatures();
80             }
81             return new Object JavaDoc[0];
82         }
83     }
84
85     public IncludedFeaturesSection(PDEFormPage page, Composite parent) {
86         super(page, parent, Section.DESCRIPTION, new String JavaDoc[] { PDEUIMessages.FeatureEditor_IncludedFeatures_new });
87         getSection().setText(PDEUIMessages.FeatureEditor_IncludedFeatures_title);
88         getSection().setDescription(PDEUIMessages.FeatureEditor_IncludedFeatures_desc);
89         getTablePart().setEditable(false);
90         getSection().setLayoutData(new GridData(GridData.FILL_BOTH));
91     }
92
93     public void commit(boolean onSave) {
94         super.commit(onSave);
95     }
96
97     public void createClient(Section section, FormToolkit toolkit) {
98         
99         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
100         GridData data = new GridData(GridData.FILL_BOTH);
101         section.setLayoutData(data);
102         
103         Composite container = createClientContainer(section, 2, toolkit);
104
105         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
106         TablePart tablePart = getTablePart();
107         fIncludesViewer = tablePart.getTableViewer();
108         fIncludesViewer
109                 .setContentProvider(new IncludedFeaturesContentProvider());
110         fIncludesViewer.setLabelProvider(PDEPlugin.getDefault()
111                 .getLabelProvider());
112         fIncludesViewer.setComparator(ListUtil.NAME_COMPARATOR);
113         toolkit.paintBordersFor(container);
114         makeActions();
115         section.setClient(container);
116         initialize();
117         createSectionToolbar(section, toolkit);
118     }
119
120     /**
121      * @param section
122      * @param toolkit
123      */

124     private void createSectionToolbar(Section section, FormToolkit toolkit) {
125         
126         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
127         ToolBar toolbar = toolBarManager.createControl(section);
128         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
129         toolbar.setCursor(handCursor);
130         // Cursor needs to be explicitly disposed
131
toolbar.addDisposeListener(new DisposeListener() {
132             public void widgetDisposed(DisposeEvent e) {
133                 if ((handCursor != null) &&
134                         (handCursor.isDisposed() == false)) {
135                     handCursor.dispose();
136                 }
137             }
138         });
139         // Add sort action to the tool bar
140
fSortAction = new SortAction(
141                 getStructuredViewerPart().getViewer(),
142                 PDEUIMessages.FeatureEditor_IncludedFeatures_sortAlpha,
143                 ListUtil.NAME_COMPARATOR,
144                 null,
145                 null);
146         
147         toolBarManager.add(fSortAction);
148
149         toolBarManager.update(true);
150
151         section.setTextClient(toolbar);
152     }
153     
154     protected void handleDoubleClick(IStructuredSelection selection) {
155         fOpenAction.run();
156     }
157
158     protected void buttonSelected(int index) {
159         if (index == 0)
160             handleNew();
161     }
162
163     public void dispose() {
164         IFeatureModel model = (IFeatureModel) getPage().getModel();
165         if (model != null)
166             model.removeModelChangedListener(this);
167         FeatureModelManager mng = PDECore.getDefault()
168         .getFeatureModelManager();
169         mng.removeFeatureModelListener(this);
170         super.dispose();
171     }
172
173     public boolean setFormInput(Object JavaDoc object) {
174         if (object instanceof IFeatureChild) {
175             fIncludesViewer.setSelection(new StructuredSelection(object), true);
176             return true;
177         }
178         return false;
179     }
180
181     protected void fillContextMenu(IMenuManager manager) {
182         manager.add(fOpenAction);
183         manager.add(new Separator());
184         manager.add(fNewAction);
185         manager.add(fDeleteAction);
186         manager.add(new Separator());
187         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(
188                 manager);
189     }
190
191     private void handleNew() {
192         BusyIndicator.showWhile(fIncludesViewer.getTable().getDisplay(),
193                 new Runnable JavaDoc() {
194                     public void run() {
195                         IFeatureModel[] allModels = PDECore.getDefault()
196                                 .getFeatureModelManager().getModels();
197                         ArrayList JavaDoc newModels = new ArrayList JavaDoc();
198                         for (int i = 0; i < allModels.length; i++) {
199                             if (canAdd(allModels[i]))
200                                 newModels.add(allModels[i]);
201                         }
202                         IFeatureModel[] candidateModels = (IFeatureModel[]) newModels
203                                 .toArray(new IFeatureModel[newModels.size()]);
204                         FeatureSelectionDialog dialog = new FeatureSelectionDialog(
205                                 fIncludesViewer.getTable().getShell(),
206                                 candidateModels, true);
207                         if (dialog.open() == Window.OK) {
208                             Object JavaDoc[] models = dialog.getResult();
209                             try {
210                                 doAdd(models);
211                             } catch (CoreException e) {
212                                 PDEPlugin.log(e);
213                             }
214                         }
215                     }
216                 });
217     }
218
219     private void doAdd(Object JavaDoc[] candidates) throws CoreException {
220         IFeatureModel model = (IFeatureModel) getPage().getModel();
221         IFeature feature = model.getFeature();
222         IFeatureChild[] added = new IFeatureChild[candidates.length];
223         for (int i = 0; i < candidates.length; i++) {
224             IFeatureModel candidate = (IFeatureModel) candidates[i];
225             FeatureChild child = (FeatureChild) model.getFactory()
226                     .createChild();
227             child.loadFrom(candidate.getFeature());
228             child.setVersion("0.0.0"); //$NON-NLS-1$
229
added[i] = child;
230         }
231         feature.addIncludedFeatures(added);
232     }
233
234     private boolean canAdd(IFeatureModel candidate) {
235         IFeature cfeature = candidate.getFeature();
236
237         IFeatureModel model = (IFeatureModel) getPage().getModel();
238         IFeature feature = model.getFeature();
239
240         if (cfeature.getId().equals(feature.getId())
241                 && cfeature.getVersion().equals(feature.getVersion())) {
242             return false;
243         }
244
245         boolean isPatchEditor = ((FeatureEditor) getPage().getEditor())
246         .isPatchEditor();
247         if (isPatchEditor && !isFeaturePatch(candidate.getFeature())) {
248             return false;
249         }
250
251         IFeatureChild[] features = feature.getIncludedFeatures();
252
253         for (int i = 0; i < features.length; i++) {
254             if (features[i].getId().equals(cfeature.getId())
255                     && features[i].getVersion().equals(cfeature.getVersion()))
256                 return false;
257         }
258         return true;
259     }
260
261     private static boolean isFeaturePatch(IFeature feature) {
262         IFeatureImport[] imports = feature.getImports();
263         for (int i = 0; i < imports.length; i++) {
264             if (imports[i].isPatch())
265                 return true;
266         }
267         return false;
268     }
269
270     private void handleSelectAll() {
271         IStructuredContentProvider provider = (IStructuredContentProvider) fIncludesViewer
272                 .getContentProvider();
273         Object JavaDoc[] elements = provider.getElements(fIncludesViewer.getInput());
274         StructuredSelection ssel = new StructuredSelection(elements);
275         fIncludesViewer.setSelection(ssel);
276     }
277
278     private void handleDelete() {
279         IStructuredSelection ssel = (IStructuredSelection) fIncludesViewer
280                 .getSelection();
281
282         if (ssel.isEmpty())
283             return;
284         IFeatureModel model = (IFeatureModel) getPage().getModel();
285         if (!model.isEditable()) {
286             return;
287         }
288         IFeature feature = model.getFeature();
289
290         try {
291             IFeatureChild[] removed = new IFeatureChild[ssel.size()];
292             int i = 0;
293             for (Iterator JavaDoc iter = ssel.iterator(); iter.hasNext();) {
294                 IFeatureChild iobj = (IFeatureChild) iter.next();
295                 removed[i++] = iobj;
296             }
297             feature.removeIncludedFeatures(removed);
298         } catch (CoreException e) {
299             PDEPlugin.logException(e);
300         }
301     }
302
303     public boolean doGlobalAction(String JavaDoc actionId) {
304         if (actionId.equals(ActionFactory.DELETE.getId())) {
305             BusyIndicator.showWhile(fIncludesViewer.getTable().getDisplay(),
306                     new Runnable JavaDoc() {
307                         public void run() {
308                             handleDelete();
309                         }
310                     });
311             return true;
312         }
313         if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
314             BusyIndicator.showWhile(fIncludesViewer.getTable().getDisplay(),
315                     new Runnable JavaDoc() {
316                         public void run() {
317                             handleSelectAll();
318                         }
319                     });
320             return true;
321         }
322         if (actionId.equals(ActionFactory.CUT.getId())) {
323             // delete here and let the editor transfer
324
// the selection to the clipboard
325
handleDelete();
326             return false;
327         }
328         if (actionId.equals(ActionFactory.PASTE.getId())) {
329             doPaste();
330             return true;
331         }
332         return false;
333     }
334
335     protected void selectionChanged(IStructuredSelection selection) {
336         getPage().getPDEEditor().setSelection(selection);
337     }
338
339     public void initialize() {
340         IFeatureModel model = (IFeatureModel) getPage().getModel();
341         refresh();
342         getTablePart().setButtonEnabled(0, model.isEditable());
343         model.addModelChangedListener(this);
344         FeatureModelManager mng = PDECore.getDefault()
345                 .getFeatureModelManager();
346         mng.addFeatureModelListener(this);
347     }
348
349     public void modelChanged(IModelChangedEvent e) {
350         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
351             markStale();
352             return;
353         }
354         Object JavaDoc obj = e.getChangedObjects()[0];
355         if (obj instanceof IFeatureChild) {
356             if (e.getChangeType() == IModelChangedEvent.CHANGE) {
357                 fIncludesViewer.update(obj, null);
358             } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
359                 fIncludesViewer.add(e.getChangedObjects());
360                 if (e.getChangedObjects().length > 0) {
361                     fIncludesViewer.setSelection(new StructuredSelection(e
362                             .getChangedObjects()[0]));
363                 }
364             } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
365                 fIncludesViewer.remove(e.getChangedObjects());
366             }
367         }
368     }
369
370     private void makeActions() {
371         IModel model = (IModel) getPage().getModel();
372         fNewAction = new Action() {
373             public void run() {
374                 handleNew();
375             }
376         };
377         fNewAction.setText(PDEUIMessages.Menus_new_label);
378         fNewAction.setEnabled(model.isEditable());
379
380         fDeleteAction = new Action() {
381             public void run() {
382                 BusyIndicator.showWhile(
383                         fIncludesViewer.getTable().getDisplay(),
384                         new Runnable JavaDoc() {
385                             public void run() {
386                                 handleDelete();
387                             }
388                         });
389             }
390         };
391         fDeleteAction.setEnabled(model.isEditable());
392         fDeleteAction.setText(PDEUIMessages.Actions_delete_label);
393         fOpenAction = new OpenReferenceAction(fIncludesViewer);
394     }
395
396     public void modelsChanged(final IFeatureModelDelta delta) {
397         getSection().getDisplay().asyncExec(new Runnable JavaDoc() {
398             public void run() {
399                 if (getSection().isDisposed()) {
400                     return;
401                 }
402                 IFeatureModel[] added = delta.getAdded();
403                 IFeatureModel[] removed = delta.getRemoved();
404                 IFeatureModel[] changed = delta.getChanged();
405                 if (hasModels(added) || hasModels(removed)
406                         || hasModels(changed))
407                     markStale();
408             }
409         });
410     }
411
412     private boolean hasModels(IFeatureModel[] models) {
413         if (models == null)
414             return false;
415         IFeatureModel thisModel = (IFeatureModel) getPage().getModel();
416         if (thisModel == null)
417             return false;
418         for (int i = 0; i < models.length; i++) {
419             if (models[i] != thisModel) {
420                 return true;
421             }
422         }
423         return false;
424     }
425
426     public void setFocus() {
427         if (fIncludesViewer != null)
428             fIncludesViewer.getTable().setFocus();
429     }
430
431     public void refresh() {
432         IFeatureModel model = (IFeatureModel) getPage().getModel();
433         IFeature feature = model.getFeature();
434         fIncludesViewer.setInput(feature);
435         super.refresh();
436     }
437
438     /**
439      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Clipboard)
440      */

441     public boolean canPaste(Clipboard clipboard) {
442         Object JavaDoc[] objects = (Object JavaDoc[]) clipboard.getContents(ModelDataTransfer
443                 .getInstance());
444         if (objects != null && objects.length > 0) {
445             return canPaste(null, objects);
446         }
447         return false;
448     }
449
450     /**
451      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(Object,
452      * Object[])
453      */

454     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
455         for (int i = 0; i < objects.length; i++) {
456             if (!(objects[i] instanceof FeatureChild))
457                 return false;
458         }
459         return true;
460     }
461
462     /**
463      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste()
464      */

465     protected void doPaste() {
466         Clipboard clipboard = getPage().getPDEEditor().getClipboard();
467         ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance();
468         Object JavaDoc[] objects = (Object JavaDoc[]) clipboard.getContents(modelTransfer);
469         if (objects != null) {
470             doPaste(null, objects);
471         }
472     }
473
474     /**
475      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(Object,
476      * Object[])
477      */

478     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
479         IFeatureModel model = (IFeatureModel) getPage().getModel();
480         IFeature feature = model.getFeature();
481         if (!model.isEditable()) {
482             return;
483         }
484
485         FeatureChild[] fChildren = new FeatureChild[objects.length];
486         try {
487             for (int i = 0; i < objects.length; i++) {
488                 if (objects[i] instanceof FeatureChild) {
489                     FeatureChild fChild = (FeatureChild) objects[i];
490                     fChild.setModel(model);
491                     fChild.setParent(feature);
492                     fChildren[i] = fChild;
493                 }
494             }
495             feature.addIncludedFeatures(fChildren);
496         } catch (CoreException e) {
497             PDEPlugin.logException(e);
498         }
499     }
500
501     void fireSelection() {
502         ISelection sel = fIncludesViewer.getSelection();
503         if (!sel.isEmpty()) {
504             fIncludesViewer.setSelection(fIncludesViewer.getSelection());
505         } else if (fIncludesViewer.getElementAt(0) != null) {
506             fIncludesViewer.setSelection(new StructuredSelection(
507                     fIncludesViewer.getElementAt(0)));
508         }
509     }
510     
511     protected boolean createCount() { return true; }
512     
513 }
514
Popular Tags