KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > target > ContentSection


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.target;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.TreeMap JavaDoc;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.QualifiedName;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.jface.viewers.TableViewer;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerComparator;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.osgi.service.resolver.BundleDescription;
37 import org.eclipse.pde.core.IModelChangedEvent;
38 import org.eclipse.pde.core.plugin.IPluginModelBase;
39 import org.eclipse.pde.core.plugin.PluginRegistry;
40 import org.eclipse.pde.internal.core.PDECore;
41 import org.eclipse.pde.internal.core.TargetPlatformHelper;
42 import org.eclipse.pde.internal.core.ifeature.IFeature;
43 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
44 import org.eclipse.pde.internal.core.itarget.ITarget;
45 import org.eclipse.pde.internal.core.itarget.ITargetFeature;
46 import org.eclipse.pde.internal.core.itarget.ITargetModel;
47 import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
48 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
49 import org.eclipse.pde.internal.core.plugin.ExternalPluginModelBase;
50 import org.eclipse.pde.internal.ui.IPDEUIConstants;
51 import org.eclipse.pde.internal.ui.PDEPlugin;
52 import org.eclipse.pde.internal.ui.PDEPluginImages;
53 import org.eclipse.pde.internal.ui.PDEUIMessages;
54 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
55 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
56 import org.eclipse.pde.internal.ui.editor.TableSection;
57 import org.eclipse.pde.internal.ui.editor.feature.FeatureEditor;
58 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
59 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
60 import org.eclipse.pde.internal.ui.parts.ConditionalListSelectionDialog;
61 import org.eclipse.pde.internal.ui.parts.TablePart;
62 import org.eclipse.pde.internal.ui.search.dependencies.DependencyCalculator;
63 import org.eclipse.pde.internal.ui.util.PersistablePluginObject;
64 import org.eclipse.pde.internal.ui.wizards.FeatureSelectionDialog;
65 import org.eclipse.swt.SWT;
66 import org.eclipse.swt.custom.CTabFolder;
67 import org.eclipse.swt.custom.CTabItem;
68 import org.eclipse.swt.events.SelectionAdapter;
69 import org.eclipse.swt.events.SelectionEvent;
70 import org.eclipse.swt.graphics.Color;
71 import org.eclipse.swt.graphics.Image;
72 import org.eclipse.swt.layout.GridData;
73 import org.eclipse.swt.widgets.Button;
74 import org.eclipse.swt.widgets.Composite;
75 import org.eclipse.swt.widgets.Table;
76 import org.eclipse.swt.widgets.TableItem;
77 import org.eclipse.ui.IEditorInput;
78 import org.eclipse.ui.IFileEditorInput;
79 import org.eclipse.ui.IWorkingSet;
80 import org.eclipse.ui.IWorkingSetManager;
81 import org.eclipse.ui.PlatformUI;
82 import org.eclipse.ui.actions.ActionFactory;
83 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
84 import org.eclipse.ui.forms.IFormColors;
85 import org.eclipse.ui.forms.widgets.FormToolkit;
86 import org.eclipse.ui.forms.widgets.Section;
87
88 public class ContentSection extends TableSection {
89     
90     class ContentProvider extends DefaultTableProvider {
91         public Object JavaDoc[] getElements(Object JavaDoc parent) {
92             ITarget target = getTarget();
93             if (target.useAllPlugins())
94                 return new Object JavaDoc[0];
95             if (fLastTab == 0)
96                 return target.getPlugins();
97             return target.getFeatures();
98         }
99     }
100     
101     private static final String JavaDoc[] TAB_LABELS = new String JavaDoc[2];
102     static {
103         TAB_LABELS[0] = PDEUIMessages.ContentSection_plugins;
104         TAB_LABELS[1] = PDEUIMessages.ContentSection_features;
105     }
106     
107     private static final String JavaDoc[] BUTTONS = new String JavaDoc[5];
108     static {
109         BUTTONS[0] = PDEUIMessages.ContentSection_add;
110         BUTTONS[1] = PDEUIMessages.ContentSection_remove;
111         BUTTONS[2] = PDEUIMessages.ContentSection_removeAll;
112         BUTTONS[3] = PDEUIMessages.ContentSection_workingSet;
113         BUTTONS[4] = PDEUIMessages.ContentSection_required;
114     }
115     
116     private TableViewer fContentViewer;
117     private CTabFolder fTabFolder;
118     private int fLastTab;
119     private Button fUseAllPlugins;
120     private Image[] fTabImages;
121     private Button fIncludeOptionalButton;
122     public static final QualifiedName OPTIONAL_PROPERTY = new QualifiedName(IPDEUIConstants.PLUGIN_ID, "target.includeOptional"); //$NON-NLS-1$
123

124     public ContentSection(PDEFormPage page, Composite parent) {
125         super(page, parent, Section.DESCRIPTION, BUTTONS);
126     }
127
128     /* (non-Javadoc)
129      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
130      */

131     protected void createClient(Section section, FormToolkit toolkit) {
132         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
133         Composite client = toolkit.createComposite(section);
134         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
135         client.setLayoutData(new GridData(GridData.FILL_BOTH));
136         
137         fUseAllPlugins = toolkit.createButton(client, PDEUIMessages.ContentSection_allTarget, SWT.CHECK);
138         fUseAllPlugins.addSelectionListener(new SelectionAdapter() {
139             public void widgetSelected(SelectionEvent e) {
140                 getTarget().setUseAllPlugins(fUseAllPlugins.getSelection());
141             }
142         });
143         GridData gd = new GridData();
144         gd.horizontalSpan = 2;
145         fUseAllPlugins.setLayoutData(gd);
146         
147         fTabFolder = new CTabFolder(client, SWT.FLAT|SWT.TOP);
148         gd = new GridData(GridData.FILL_HORIZONTAL);
149         gd.heightHint = 2;
150         gd.horizontalSpan = 2;
151         fTabFolder.setLayoutData(gd);
152         toolkit.adapt(fTabFolder, true, true);
153         toolkit.getColors().initializeSectionToolBarColors();
154         Color selectedColor = toolkit.getColors().getColor(IFormColors.TB_BG);
155         fTabFolder.setSelectionBackground(new Color[] { selectedColor,
156                 toolkit.getColors().getBackground() },
157                 new int[] { 100 }, true);
158         fTabFolder.addSelectionListener(new SelectionAdapter() {
159             public void widgetSelected(SelectionEvent e) {
160                 refresh();
161             }
162         });
163         
164         createTabs();
165         
166         createViewerPartControl(client, SWT.MULTI, 2, toolkit);
167         
168         TablePart tablePart = getTablePart();
169         GridData data = (GridData) tablePart.getControl().getLayoutData();
170         data.grabExcessVerticalSpace = true;
171         data.grabExcessHorizontalSpace = true;
172         fContentViewer = tablePart.getTableViewer();
173         fContentViewer.setContentProvider(new ContentProvider());
174         fContentViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
175         fContentViewer.setComparator(new ViewerComparator() {
176             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
177                 if (e1 instanceof ITargetPlugin) {
178                     ITargetPlugin p1 = (ITargetPlugin) e1;
179                     ITargetPlugin p2 = (ITargetPlugin) e2;
180                     return super.compare(viewer, p1.getId(), p2.getId());
181                 } // else
182
ITargetFeature f1 = (ITargetFeature)e1;
183                 ITargetFeature f2 = (ITargetFeature)e2;
184                 return super.compare(viewer, f1.getId(), f2.getId());
185             }
186         });
187         fContentViewer.setInput(PDECore.getDefault().getModelManager());
188         fContentViewer.addSelectionChangedListener(new ISelectionChangedListener() {
189             public void selectionChanged(SelectionChangedEvent event) {
190                 updateButtons();
191             }
192         });
193         
194         createOptionalDependenciesButton(client);
195         
196         toolkit.paintBordersFor(client);
197         section.setClient(client);
198         section.setText(PDEUIMessages.ContentSection_targetContent);
199         section.setDescription(PDEUIMessages.ContentSection_targetContentDesc);
200         section.setLayoutData(new GridData(GridData.FILL_BOTH));
201         updateButtons();
202         getModel().addModelChangedListener(this);
203     }
204     
205     private void createOptionalDependenciesButton(Composite client) {
206         if (isEditable()) {
207             fIncludeOptionalButton = new Button(client, SWT.CHECK);
208             fIncludeOptionalButton.setText(PDEUIMessages.ContentSection_includeOptional);
209             // initialize value
210
IEditorInput input = getPage().getEditorInput();
211             if (input instanceof IFileEditorInput) {
212                 IFile file = ((IFileEditorInput)input).getFile();
213                 try {
214                     fIncludeOptionalButton.setSelection("true".equals(file.getPersistentProperty(OPTIONAL_PROPERTY))); //$NON-NLS-1$
215
} catch (CoreException e) {
216                 }
217             }
218             fIncludeOptionalButton.setEnabled(!getTarget().useAllPlugins());
219             // create listener to save value when the checkbox is changed
220
fIncludeOptionalButton.addSelectionListener(new SelectionAdapter() {
221                 public void widgetSelected(SelectionEvent e) {
222                     IEditorInput input = getPage().getEditorInput();
223                     if (input instanceof IFileEditorInput) {
224                         IFile file = ((IFileEditorInput)input).getFile();
225                         try {
226                             file.setPersistentProperty(OPTIONAL_PROPERTY, fIncludeOptionalButton.getSelection() ? "true" : null); //$NON-NLS-1$
227
} catch (CoreException e1) {
228                         }
229                     }
230                 }
231             });
232         }
233     }
234     
235     /* (non-Javadoc)
236      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int)
237      */

238     protected void buttonSelected(int index) {
239         switch (index) {
240         case 0:
241             handleAdd();
242             break;
243         case 1:
244             handleDelete();
245             break;
246         case 2:
247             handleRemoveAll();
248             break;
249         case 3:
250             handleAddWorkingSet();
251             break;
252         case 4:
253             handleAddRequired(getTarget().getPlugins(), fIncludeOptionalButton.getSelection());
254         }
255     }
256     
257     private void createTabs() {
258         fTabImages = new Image[] {PDEPluginImages.DESC_PLUGIN_OBJ.createImage(),
259                 PDEPluginImages.DESC_FEATURE_OBJ.createImage()
260         };
261         for (int i = 0; i < TAB_LABELS.length; i++) {
262             CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
263             item.setText(TAB_LABELS[i]);
264             item.setImage(fTabImages[i]);
265         }
266         fLastTab = 0;
267         fTabFolder.setSelection(fLastTab);
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
272      */

273     public void refresh() {
274         fLastTab = fTabFolder.getSelectionIndex();
275         fContentViewer.refresh();
276         updateButtons();
277         super.refresh();
278     }
279     
280     protected void updateButtons(){
281         boolean useAllPlugins = getTarget().useAllPlugins();
282         fUseAllPlugins.setSelection(useAllPlugins);
283         fTabFolder.setEnabled(!useAllPlugins);
284         TablePart table = getTablePart();
285         boolean itemsSelected = !fContentViewer.getSelection().isEmpty();
286         boolean hasItems = fContentViewer.getTable().getItemCount() > 0;
287         table.setButtonEnabled(0, isEditable() && !useAllPlugins);
288         table.setButtonEnabled(1, isEditable() && !useAllPlugins && itemsSelected);
289         table.setButtonEnabled(2, isEditable() && !useAllPlugins && hasItems);
290         boolean pluginTab = (fLastTab == 0);
291         table.setButtonEnabled(3, isEditable() && pluginTab && !useAllPlugins);
292         table.setButtonEnabled(4, isEditable() && pluginTab && !useAllPlugins && hasItems);
293     }
294     
295     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
296         for (int i = 0; i < objects.length; i++) {
297             if (objects[i] instanceof ITargetPlugin && fLastTab == 0 ||
298                 objects[i] instanceof ITargetFeature && fLastTab == 1)
299                 return true;
300         }
301         return false;
302     }
303     
304     private ITarget getTarget() {
305         return getModel().getTarget();
306     }
307     
308     private ITargetModel getModel() {
309         return (ITargetModel)getPage().getPDEEditor().getAggregateModel();
310     }
311     
312     private void handleAdd() {
313         if (fLastTab == 0)
314             handleAddPlugin();
315         else
316             handleAddFeature();
317         updateButtons();
318     }
319     
320     private void handleAddPlugin() {
321         ConditionalListSelectionDialog dialog = new ConditionalListSelectionDialog(
322                 PDEPlugin.getActiveWorkbenchShell(),
323                 PDEPlugin.getDefault().getLabelProvider(),
324                 PDEUIMessages.ContentSection_addDialogButtonLabel);
325         
326         TreeMap JavaDoc map = getBundles();
327         dialog.setElements(map.values().toArray());
328         dialog.setConditionalElements(getWorkspaceBundles(map).values().toArray());
329         dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
330         dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
331         dialog.setMultipleSelection(true);
332         if (dialog.open() == Window.OK) {
333             Object JavaDoc[] bundles = dialog.getResult();
334             ITarget target = getTarget();
335             ITargetModelFactory factory = getModel().getFactory();
336             ITargetPlugin[] plugins = new ITargetPlugin[bundles.length];
337             for (int i = 0; i < bundles.length; i++) {
338                 String JavaDoc id = ((BundleDescription)bundles[i]).getSymbolicName();
339                 ITargetPlugin plugin = factory.createPlugin();
340                 plugin.setId(id);
341                 plugins[i] = plugin;
342             }
343             target.addPlugins(plugins);
344             fContentViewer.setSelection(new StructuredSelection(plugins[plugins.length - 1]));
345         }
346     }
347     
348     private TreeMap JavaDoc getBundles() {
349         TreeMap JavaDoc map = new TreeMap JavaDoc();
350         ITarget target = getTarget();
351         IPluginModelBase[] models = PluginRegistry.getExternalModels();
352         for (int i = 0; i < models.length; i++) {
353             BundleDescription desc = ((ExternalPluginModelBase)models[i]).getBundleDescription();
354             String JavaDoc id = desc.getSymbolicName();
355             if (!target.containsPlugin(id))
356                 map.put(id, desc);
357         }
358         return map;
359     }
360     
361     protected TreeMap JavaDoc getWorkspaceBundles(TreeMap JavaDoc used) {
362         TreeMap JavaDoc map = new TreeMap JavaDoc();
363         ITarget target = getTarget();
364         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
365         for (int i = 0; i < models.length; i++) {
366             BundleDescription desc = models[i].getBundleDescription();
367             String JavaDoc id = desc.getSymbolicName();
368             if (id != null && !target.containsPlugin(id) && !used.containsKey(id))
369                 map.put(id, desc);
370         }
371         return map;
372     }
373     
374     private void handleAddFeature() {
375         IFeatureModel[] allModels = PDECore.getDefault()
376             .getFeatureModelManager().getModels();
377         ArrayList JavaDoc newModels = new ArrayList JavaDoc();
378         ITarget target = getTarget();
379         for (int i = 0; i < allModels.length; i++) {
380             if (!target.containsFeature(allModels[i].getFeature().getId()))
381                 newModels.add(allModels[i]);
382         }
383         IFeatureModel[] candidateModels = (IFeatureModel[]) newModels
384         .toArray(new IFeatureModel[newModels.size()]);
385         FeatureSelectionDialog dialog = new FeatureSelectionDialog(
386                 getSection().getShell(),
387                 candidateModels, true);
388         if (dialog.open() == Window.OK) {
389             Object JavaDoc[] models = dialog.getResult();
390             ITargetModelFactory factory = getModel().getFactory();
391             ITargetFeature [] features = new ITargetFeature[models.length];
392             for (int i = 0; i < models.length; ++i) {
393                 IFeature feature = ((IFeatureModel)models[i]).getFeature();
394                 String JavaDoc id = feature.getId();
395                 ITargetFeature tfeature = factory.createFeature();
396                 tfeature.setId(id);
397                 features[i] = tfeature;
398             }
399             target.addFeatures(features);
400             fContentViewer.setSelection(new StructuredSelection(features[features.length - 1]));
401         }
402     }
403     
404     private void handleDelete() {
405         IStructuredSelection ssel = (IStructuredSelection)fContentViewer.getSelection();
406         if (ssel.size() > 0) {
407             Object JavaDoc[] objects = ssel.toArray();
408             ITarget target = getTarget();
409             if (fLastTab == 0) {
410                 ITargetPlugin[] plugins = new ITargetPlugin[objects.length];
411                 System.arraycopy(objects, 0, plugins, 0, objects.length);
412                 target.removePlugins(plugins);
413             } else {
414                 ITargetFeature[] features = new ITargetFeature[objects.length];
415                 System.arraycopy(objects, 0, features, 0, objects.length);
416                 target.removeFeatures(features);
417             }
418         }
419         updateButtons();
420     }
421     
422     private void handleRemoveAll() {
423         TableItem[] items = fContentViewer.getTable().getItems();
424         ITarget target = getTarget();
425         if (fLastTab == 0) {
426             ITargetPlugin[] plugins = new ITargetPlugin[items.length];
427             for (int i = 0; i < plugins.length; i++)
428                 plugins[i] = (ITargetPlugin)items[i].getData();
429             target.removePlugins(plugins);
430         } else {
431             ITargetFeature[] features = new ITargetFeature[items.length];
432             for (int i = 0; i < features.length; i++)
433                 features[i] = (ITargetFeature)items[i].getData();
434             target.removeFeatures(features);
435         }
436         updateButtons();
437     }
438     
439     /* (non-Javadoc)
440      * @see org.eclipse.pde.internal.ui.editor.TableSection#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection)
441      */

442     protected void handleDoubleClick(IStructuredSelection selection) {
443         handleOpen(selection);
444     }
445     
446     private void handleOpen(IStructuredSelection selection) {
447         Object JavaDoc object = selection.getFirstElement();
448         if (object instanceof ITargetPlugin) {
449             ManifestEditor.openPluginEditor(((ITargetPlugin)object).getId());
450         } else if (object instanceof ITargetFeature) {
451             handleOpenFeature((ITargetFeature)object);
452         }
453     }
454     
455     private void handleOpenFeature(ITargetFeature feature) {
456         IFeatureModel model = PDECore.getDefault()
457             .getFeatureModelManager().findFeatureModel(feature.getId());
458         FeatureEditor.openFeatureEditor(model);
459     }
460     
461     private void handleAddWorkingSet() {
462         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
463         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), true);
464         if (dialog.open() == Window.OK) {
465             IWorkingSet[] workingSets = dialog.getSelection();
466             ITarget target = getTarget();
467             ITargetModelFactory factory = target.getModel().getFactory();
468             HashSet JavaDoc plugins = new HashSet JavaDoc();
469             for (int i = 0; i < workingSets.length; i++) {
470                 IAdaptable[] elements = workingSets[i].getElements();
471                 for (int j = 0; j < elements.length; j++) {
472                     IPluginModelBase model = findModel(elements[j]);
473                     if (model != null) {
474                         ITargetPlugin plugin = factory.createPlugin();
475                         plugin.setId(model.getPluginBase().getId());
476                         plugins.add(plugin);
477                     }
478                 }
479             }
480             target.addPlugins((ITargetPlugin[]) plugins.toArray(new ITargetPlugin[plugins.size()]));
481         }
482         updateButtons();
483     }
484     
485     private IPluginModelBase findModel(IAdaptable object) {
486         if (object instanceof IJavaProject)
487             object = ((IJavaProject)object).getProject();
488         if (object instanceof IProject)
489             return PluginRegistry.findModel((IProject)object);
490         if (object instanceof PersistablePluginObject) {
491             return PluginRegistry.findModel(((PersistablePluginObject)object).getPluginID());
492         }
493         return null;
494     }
495     
496     public static void handleAddRequired(ITargetPlugin[] plugins, boolean includeOptional) {
497         if (plugins.length == 0)
498             return;
499         
500         ArrayList JavaDoc list = new ArrayList JavaDoc(plugins.length);
501         for (int i = 0; i < plugins.length; i++) {
502             list.add(TargetPlatformHelper.getState().getBundle(plugins[i].getId(), null));
503         }
504         DependencyCalculator calculator = new DependencyCalculator(includeOptional);
505         calculator.findDependencies(list.toArray());
506         Collection JavaDoc dependencies = calculator.getBundleIDs();
507         
508         ITarget target = plugins[0].getTarget();
509         ITargetModelFactory factory = target.getModel().getFactory();
510         ITargetPlugin[] pluginsToAdd = new ITargetPlugin[dependencies.size()];
511         int i = 0;
512         Iterator JavaDoc iter = dependencies.iterator();
513         while (iter.hasNext()) {
514             String JavaDoc id = iter.next().toString();
515             ITargetPlugin plugin = factory.createPlugin();
516             plugin.setId(id);
517             pluginsToAdd[i++] = plugin;
518         }
519         target.addPlugins(pluginsToAdd);
520     }
521     
522     /* (non-Javadoc)
523      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
524      */

525     public void modelChanged(IModelChangedEvent e) {
526         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
527             handleModelEventWorldChanged(e);
528             return;
529         }
530         Object JavaDoc[] objects = e.getChangedObjects();
531         if (e.getChangeType() == IModelChangedEvent.INSERT) {
532             for (int i = 0; i < objects.length; i++) {
533                 if ((objects[i] instanceof ITargetPlugin && fLastTab == 0) ||
534                         (objects[i] instanceof ITargetFeature && fLastTab == 1)) {
535                     fContentViewer.add(objects[i]);
536                 }
537             }
538         } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
539             
540             Table table = fContentViewer.getTable();
541             int index = table.getSelectionIndex();
542             
543             for (int i = 0; i < objects.length; i++) {
544                 if ((objects[i] instanceof ITargetPlugin && fLastTab == 0) ||
545                         (objects[i] instanceof ITargetFeature && fLastTab == 1)) {
546                     fContentViewer.remove(objects[i]);
547                 }
548             }
549             
550             // Update Selection
551

552             int count = table.getItemCount();
553                 
554             if ( count == 0 ) {
555                 // Nothing to select
556
} else if ( index < count ) {
557                 table.setSelection( index );
558             } else {
559                 table.setSelection( count - 1 );
560             }
561             
562         }
563         if (e.getChangedProperty() == ITarget.P_ALL_PLUGINS) {
564             refresh();
565             fIncludeOptionalButton.setEnabled(!((Boolean JavaDoc)e.getNewValue()).booleanValue());
566         }
567     }
568     
569     /**
570      * @param event
571      */

572     private void handleModelEventWorldChanged(IModelChangedEvent event) {
573         // Reload input
574
fContentViewer.setInput(PDECore.getDefault().getModelManager());
575         // Perform the refresh
576
refresh();
577     }
578     
579     public boolean doGlobalAction(String JavaDoc actionId) {
580         if (actionId.equals(ActionFactory.DELETE.getId())) {
581             handleDelete();
582             return true;
583         }
584         if (actionId.equals(ActionFactory.CUT.getId())) {
585             handleDelete();
586             return false;
587         }
588         if (actionId.equals(ActionFactory.PASTE.getId())) {
589             doPaste();
590             return true;
591         }
592         return false;
593     }
594     
595     /* (non-Javadoc)
596      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#fillContextMenu(org.eclipse.jface.action.IMenuManager)
597      */

598     protected void fillContextMenu(IMenuManager manager) {
599         IStructuredSelection ssel = (IStructuredSelection)fContentViewer.getSelection();
600         if (ssel == null)
601             return;
602         
603         Action openAction = new Action(PDEUIMessages.ContentSection_open) {
604             public void run() {
605                 handleDoubleClick((IStructuredSelection)fContentViewer.getSelection());
606             }
607         };
608         openAction.setEnabled(isEditable() && ssel.size() == 1);
609         manager.add(openAction);
610         
611         manager.add(new Separator());
612         
613         Action removeAction = new Action(PDEUIMessages.ContentSection_remove) {
614             public void run() {
615                 handleDelete();
616             }
617         };
618         removeAction.setEnabled(isEditable() && ssel.size() > 0);
619         manager.add(removeAction);
620         
621         Action removeAll = new Action(PDEUIMessages.ContentSection_removeAll) {
622             public void run() {
623                 handleRemoveAll();
624             }
625         };
626         removeAll.setEnabled(isEditable());
627         manager.add(removeAll);
628
629         manager.add(new Separator());
630         
631         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
632     }
633
634     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
635         for (int i = 0; i < objects.length; i++) {
636             if (objects[i] instanceof ITargetPlugin && fLastTab == 0)
637                 getTarget().addPlugin((ITargetPlugin)objects[i]);
638             else if (objects[i] instanceof ITargetFeature && fLastTab == 1)
639                 getTarget().addFeature((ITargetFeature)objects[i]);
640         }
641     }
642     
643     protected void selectionChanged(IStructuredSelection selection) {
644         getPage().getPDEEditor().setSelection(selection);
645     }
646     
647     public boolean setFormInput(Object JavaDoc input) {
648         if (input instanceof ITargetPlugin) {
649             if (fTabFolder.getSelectionIndex() != 0) {
650                 fTabFolder.setSelection(0);
651                 refresh();
652             }
653             fContentViewer.setSelection(new StructuredSelection(input), true);
654             return true;
655         } else if (input instanceof ITargetFeature) {
656             if (fTabFolder.getSelectionIndex() != 1) {
657                 fTabFolder.setSelection(1);
658                 refresh();
659             }
660             fContentViewer.setSelection(new StructuredSelection(input), true);
661             return true;
662         }
663         return super.setFormInput(input);
664     }
665
666     
667     public void dispose() {
668         ITargetModel model = getModel();
669         if (model != null)
670             model.removeModelChangedListener(this);
671         if (fTabImages != null)
672             for (int i = 0; i < fTabImages.length; i++)
673                 fTabImages[i].dispose();
674         super.dispose();
675     }
676     
677     protected boolean createCount() { return true; }
678 }
679
Popular Tags