KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > dependencies > DependenciesViewPage


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.views.dependencies;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.IMenuListener;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.IStatusLineManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.viewers.DoubleClickEvent;
25 import org.eclipse.jface.viewers.IContentProvider;
26 import org.eclipse.jface.viewers.IDoubleClickListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.viewers.StructuredViewer;
30 import org.eclipse.jface.viewers.Viewer;
31 import org.eclipse.jface.viewers.ViewerFilter;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.osgi.service.resolver.BaseDescription;
34 import org.eclipse.osgi.service.resolver.BundleDescription;
35 import org.eclipse.osgi.service.resolver.BundleSpecification;
36 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
37 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
38 import org.eclipse.osgi.util.NLS;
39 import org.eclipse.pde.core.plugin.IPluginBase;
40 import org.eclipse.pde.core.plugin.IPluginImport;
41 import org.eclipse.pde.core.plugin.IPluginModelBase;
42 import org.eclipse.pde.core.plugin.IPluginObject;
43 import org.eclipse.pde.core.plugin.ISharedPluginModel;
44 import org.eclipse.pde.core.plugin.PluginRegistry;
45 import org.eclipse.pde.internal.ui.IPreferenceConstants;
46 import org.eclipse.pde.internal.ui.PDEPlugin;
47 import org.eclipse.pde.internal.ui.PDEUIMessages;
48 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
49 import org.eclipse.pde.internal.ui.refactoring.RenamePluginAction;
50 import org.eclipse.pde.internal.ui.search.dependencies.DependencyExtentAction;
51 import org.eclipse.pde.internal.ui.search.dependencies.UnusedDependenciesAction;
52 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Control;
55 import org.eclipse.swt.widgets.Menu;
56 import org.eclipse.ui.IActionBars;
57 import org.eclipse.ui.IWorkbenchActionConstants;
58 import org.eclipse.ui.part.Page;
59
60 public abstract class DependenciesViewPage extends Page {
61     class FocusOnSelectionAction extends Action {
62         public void run() {
63             handleFocusOn(getSelectedObject());
64         }
65
66         public void update(Object JavaDoc object) {
67             setEnabled(object != null);
68             String JavaDoc name = ((LabelProvider) fViewer.getLabelProvider())
69                     .getText(object);
70             setText(NLS.bind(PDEUIMessages.DependenciesViewPage_focusOnSelection, name));
71         }
72     }
73
74     private Action fFocusOnAction;
75
76     private FocusOnSelectionAction fFocusOnSelectionAction;
77
78     private Action fOpenAction;
79     
80     protected RenamePluginAction fRefactorAction;
81
82     private IPropertyChangeListener fPropertyListener;
83
84     private DependenciesView fView;
85
86     protected StructuredViewer fViewer;
87     
88     protected IContentProvider fContentProvider;
89     
90     private Action fHideFragmentFilterAction;
91     
92     protected Action fHideOptionalFilterAction;
93     
94     private FragmentFilter fHideFragmentFilter = new FragmentFilter();
95     
96     private static final String JavaDoc HIDE_FRAGMENTS = "hideFrags"; //$NON-NLS-1$
97

98     private static final String JavaDoc HIDE_OPTIONAL = "hideOptional"; //$NON-NLS-1$
99

100     class FragmentFilter extends ViewerFilter {
101         
102         public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc element) {
103             BundleDescription desc = null;
104             if (element instanceof BundleSpecification) {
105                 BaseDescription supplier = ((BundleSpecification)element).getSupplier();
106                 if (supplier instanceof BundleDescription)
107                     desc = (BundleDescription) supplier;
108             } else if (element instanceof BundleDescription) {
109                 desc = (BundleDescription)element;
110             } else if (element instanceof ImportPackageSpecification) {
111                 BaseDescription export = ((ImportPackageSpecification)element).getSupplier();
112                 desc = ((ExportPackageDescription)export).getExporter();
113             }
114             if (desc != null) {
115                 return desc.getHost() == null;
116             }
117             return true;
118         }
119     }
120
121     /**
122      *
123      */

124     public DependenciesViewPage(DependenciesView view, IContentProvider contentProvider) {
125         this.fView = view;
126         this.fContentProvider = contentProvider;
127         fPropertyListener = new IPropertyChangeListener() {
128             public void propertyChange(PropertyChangeEvent event) {
129                 String JavaDoc property = event.getProperty();
130                 if (property.equals(IPreferenceConstants.PROP_SHOW_OBJECTS)) {
131                     fViewer.refresh();
132                 }
133             }
134         };
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
141      */

142     public void createControl(Composite parent) {
143         fViewer = createViewer(parent);
144         fViewer.setComparator(DependenciesViewComparator.getViewerComparator());
145         PDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(
146                 fPropertyListener);
147         getSite().setSelectionProvider(fViewer);
148     }
149
150     protected abstract StructuredViewer createViewer(Composite parent);
151
152     public void dispose() {
153         PDEPlugin.getDefault().getPreferenceStore()
154                 .removePropertyChangeListener(fPropertyListener);
155         super.dispose();
156     }
157
158     private void fillContextMenu(IMenuManager manager) {
159         IStructuredSelection selection = (IStructuredSelection) fViewer
160                 .getSelection();
161
162         if (selection.size() == 1) {
163             manager.add(fOpenAction);
164             manager.add(new Separator());
165         }
166         fFocusOnSelectionAction.update(getSelectedObject());
167         if (fFocusOnSelectionAction.isEnabled())
168             manager.add(fFocusOnSelectionAction);
169         manager.add(fFocusOnAction);
170         Object JavaDoc selectionElement = selection.getFirstElement();
171
172         manager.add(new Separator());
173         // only show Find Dependency Extent when in Callees view
174
if (selection.size() == 1 && !fView.isShowingCallers()) {
175             String JavaDoc id = null;
176             if (selectionElement instanceof BundleSpecification) {
177                 id = ((BundleSpecification)selectionElement).getName();
178             } else if (selectionElement instanceof BundleDescription) {
179                 id = ((BundleDescription)selectionElement).getSymbolicName();
180             }
181             // don't include find dependency extent for unresolved imports or bundles
182
if (id != null && PluginRegistry.findModel(id) != null) {
183                 Object JavaDoc input = fViewer.getInput();
184                 if (input instanceof IPluginBase)
185                     input = ((IPluginBase)input).getModel();
186                 if (input instanceof IPluginModelBase) {
187                     IPluginModelBase base = (IPluginModelBase)input;
188                     IResource res = (base == null) ? null : base.getUnderlyingResource();
189                     if (res != null)
190                         manager.add(new DependencyExtentAction(res
191                                 .getProject(), id));
192                 }
193             }
194         }
195         // Unused Dependencies Action, only for worskpace plug-ins
196
ISharedPluginModel model = null;
197         if (selectionElement instanceof BundleSpecification) {
198             model = PluginRegistry.findModel(((BundleSpecification)selectionElement).getName());
199         } else if (selectionElement instanceof BundleDescription) {
200             model = PluginRegistry.findModel((BundleDescription)selectionElement);
201         } else if (selectionElement instanceof IPluginBase) {
202             // root
203
model = ((IPluginBase)selectionElement).getModel();
204         }
205         if (model != null && model.getUnderlyingResource() != null) {
206             manager.add(new UnusedDependenciesAction(
207                     (IPluginModelBase) model, true));
208         }
209         if (enableRename(selection)) {
210             manager.add(new Separator());
211             manager.add(fRefactorAction);
212         }
213
214         manager.add(new Separator());
215         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see org.eclipse.ui.part.IPage#getControl()
222      */

223     public Control getControl() {
224         return fViewer.getControl();
225     }
226
227     private Object JavaDoc getSelectedObject() {
228         IStructuredSelection selection = getSelection();
229         if (selection.isEmpty() || selection.size() != 1)
230             return null;
231         return selection.getFirstElement();
232     }
233
234     protected IStructuredSelection getSelection() {
235         return (IStructuredSelection) fViewer.getSelection();
236     }
237     
238     protected void setSelection(IStructuredSelection selection) {
239         if (selection != null && !selection.isEmpty())
240             fViewer.setSelection(selection, true);
241     }
242     
243     /**
244      * @return Returns the view.
245      */

246     public DependenciesView getView() {
247         return fView;
248     }
249
250     private void handleDoubleClick() {
251         Object JavaDoc obj = getSelectedObject();
252         BundleDescription desc = null;
253         if (obj instanceof BundleSpecification) {
254             desc = (BundleDescription)((BundleSpecification)obj).getSupplier();
255         } else if (obj instanceof BundleDescription) {
256             desc = (BundleDescription)obj;
257             
258         } else if (obj instanceof IPluginBase) {
259             // root object
260
desc = ((IPluginModelBase)((IPluginBase)obj).getModel()).getBundleDescription();
261         } else if (obj instanceof ImportPackageSpecification) {
262             BaseDescription export = ((ImportPackageSpecification)obj).getSupplier();
263             desc = ((ExportPackageDescription)export).getExporter();
264         }
265         if (desc != null)
266             ManifestEditor.openPluginEditor(desc.getSymbolicName());
267     }
268
269     private void handleFocusOn() {
270         PluginSelectionDialog dialog = new PluginSelectionDialog(fViewer
271                 .getControl().getShell(), true, false);
272         dialog.create();
273         if (dialog.open() == Window.OK) {
274             handleFocusOn(dialog.getFirstResult());
275         }
276     }
277
278     private void handleFocusOn(Object JavaDoc newFocus) {
279         if (newFocus instanceof IPluginModelBase) {
280             fView.openTo(newFocus);
281         }
282         if (newFocus instanceof IPluginBase) {
283             fView.openTo(((IPluginBase) newFocus).getModel());
284         }
285         if (newFocus instanceof IPluginImport) {
286             IPluginImport pluginImport = ((IPluginImport) newFocus);
287             String JavaDoc id = pluginImport.getId();
288             IPluginModelBase model = PluginRegistry.findModel(id);
289             if (model != null) {
290                 fView.openTo(model);
291             } else {
292                 fView.openTo(null);
293             }
294         }
295         BundleDescription desc = null;
296         if (newFocus instanceof BundleSpecification) {
297             desc = (BundleDescription)((BundleSpecification)newFocus).getSupplier();
298             if (desc == null)
299                 fView.openTo(null);
300         }
301         if (newFocus instanceof BundleDescription) {
302             desc = (BundleDescription)newFocus;
303         }
304         if (desc != null)
305             fView.openTo(PluginRegistry.findModel(desc.getSymbolicName()));
306     }
307
308     private void hookContextMenu() {
309         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
310
menuMgr.setRemoveAllWhenShown(true);
311         menuMgr.addMenuListener(new IMenuListener() {
312             public void menuAboutToShow(IMenuManager manager) {
313                 DependenciesViewPage.this.fillContextMenu(manager);
314             }
315         });
316         Menu menu = menuMgr.createContextMenu(fViewer.getControl());
317         fViewer.getControl().setMenu(menu);
318
319         getSite()
320                 .registerContextMenu(fView.getSite().getId(), menuMgr, fViewer);
321     }
322
323     private void hookDoubleClickAction() {
324         fViewer.addDoubleClickListener(new IDoubleClickListener() {
325             public void doubleClick(DoubleClickEvent event) {
326                 handleDoubleClick();
327             }
328         });
329     }
330
331     private void makeActions() {
332         fOpenAction = new Action() {
333             public void run() {
334                 handleDoubleClick();
335             }
336         };
337         fOpenAction.setText(PDEUIMessages.DependenciesView_open);
338
339         fFocusOnSelectionAction = new FocusOnSelectionAction();
340
341         fFocusOnAction = new Action() {
342             public void run() {
343                 handleFocusOn();
344             }
345         };
346         fFocusOnAction.setText(PDEUIMessages.DependenciesViewPage_focusOn);
347         
348         fRefactorAction = new RenamePluginAction();
349         
350         fHideFragmentFilterAction = new Action() {
351             public void run() {
352                 boolean checked = fHideFragmentFilterAction.isChecked();
353                 if (checked)
354                     fViewer.removeFilter(fHideFragmentFilter);
355                 else
356                     fViewer.addFilter(fHideFragmentFilter);
357                 getSettings().put(HIDE_FRAGMENTS, !checked);
358             }
359         };
360         fHideFragmentFilterAction.setText(PDEUIMessages.DependenciesViewPage_showFragments);
361         
362         fHideOptionalFilterAction = new Action() {
363             public void run() {
364                 boolean checked = isChecked();
365                 handleShowOptional(isChecked(), true);
366                 getSettings().put(HIDE_OPTIONAL, !checked);
367             }
368         };
369         fHideOptionalFilterAction.setText(PDEUIMessages.DependenciesViewPage_showOptional);
370     }
371     
372     protected abstract void handleShowOptional(boolean checked, boolean refreshIfNecessary);
373     
374     protected abstract boolean isShowingOptional();
375
376     /*
377      * (non-Javadoc)
378      *
379      * @see org.eclipse.ui.part.Page#makeContributions(org.eclipse.jface.action.IMenuManager,
380      * org.eclipse.jface.action.IToolBarManager,
381      * org.eclipse.jface.action.IStatusLineManager)
382      */

383     public void makeContributions(IMenuManager menuManager,
384             IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
385         super.makeContributions(menuManager, toolBarManager, statusLineManager);
386         makeActions();
387         hookContextMenu();
388         hookDoubleClickAction();
389         contributeToActionBars(getSite().getActionBars());
390     }
391
392     /*
393      * (non-Javadoc)
394      *
395      * @see org.eclipse.ui.part.IPage#setFocus()
396      */

397     public void setFocus() {
398         if (fViewer != null) {
399             Control c = fViewer.getControl();
400             if (!c.isFocusControl()) {
401                 c.setFocus();
402             }
403         }
404     }
405
406     public void setInput(Object JavaDoc object) {
407         if (object != fViewer.getInput())
408             fViewer.setInput(object);
409     }
410     
411     // returns true if Rename Action is valid.
412
protected boolean enableRename(IStructuredSelection selection) {
413         if (selection.size() == 1) {
414             Object JavaDoc selectionElement = selection.getFirstElement();
415             IPluginModelBase base = null;
416             if (selectionElement instanceof IPluginImport) {
417                 String JavaDoc id = ((IPluginImport)selectionElement).getId();
418                 base = PluginRegistry.findModel(id);
419             } else if (selectionElement instanceof IPluginObject) {
420                 base = (IPluginModelBase)((IPluginObject) selectionElement).getModel();
421             } else if (selectionElement instanceof BundleSpecification) {
422                 BundleDescription desc = (BundleDescription)((BundleSpecification)selectionElement).getSupplier();
423                 if (desc != null)
424                     base = PluginRegistry.findModel(desc);
425             } else if (selectionElement instanceof BundleDescription) {
426                 base = PluginRegistry.findModel((BundleDescription)selectionElement);
427             }
428             if (base != null && base.getUnderlyingResource() != null) {
429                 fRefactorAction.setPlugin(base);
430                 return true;
431             }
432         }
433         return false;
434     }
435     
436     public void setActive(boolean active) {
437         if (active) {
438             // update filter actions before updating filters because the filters depend on the state of the filter actions
439
// update filter actions - both filter actions are specific to each Page instance
440
if (fView.isShowingCallers()) {
441                 // deactive show optional on Callers view.
442
fHideOptionalFilterAction.setChecked(true);
443                 fHideOptionalFilterAction.setEnabled(false);
444             } else {
445                 fHideOptionalFilterAction.setEnabled(true);
446                 fHideOptionalFilterAction.setChecked(!getSettings().getBoolean(HIDE_OPTIONAL));
447             }
448             fHideFragmentFilterAction.setChecked(!getSettings().getBoolean(HIDE_FRAGMENTS));
449
450             // update viewer's fragment filter
451
boolean showFragments = fHideFragmentFilterAction.isChecked();
452             boolean containsFragments = true;
453             ViewerFilter[] filters = fViewer.getFilters();
454             for (int i = 0; i < filters.length; i++) {
455                 if (filters[i].equals(fHideFragmentFilter)) {
456                     containsFragments = false;
457                     break;
458                 }
459             }
460             if (showFragments != containsFragments)
461                 if (showFragments)
462                     fViewer.removeFilter(fHideFragmentFilter);
463                 else
464                     fViewer.addFilter(fHideFragmentFilter);
465             
466             // update viewer's optional filtering
467
if (fHideOptionalFilterAction.isChecked() != isShowingOptional())
468                 handleShowOptional(fHideOptionalFilterAction.isChecked(), false);
469         }
470         
471         if (fContentProvider instanceof DependenciesViewPageContentProvider) {
472             if (active) {
473                 // when a page is activated, we need to have the content provider listen for changes and refresh the view to get current data
474
((DependenciesViewPageContentProvider)fContentProvider).attachModelListener();
475                 fViewer.refresh();
476             } else
477                 // when page is deactivated, we need to remove model listener from content manager. Otherwise model changes will be sent to all
478
// DependenciesViewPageContentProvider (including inactive ones). This will cause problems with the content provider's logic!!
479
((DependenciesViewPageContentProvider)fContentProvider).removeModelListener();
480         }
481     }
482
483     private void contributeToActionBars(IActionBars actionBars) {
484         contributeToDropDownMenu(actionBars.getMenuManager());
485     }
486     
487     private void contributeToDropDownMenu(IMenuManager manager) {
488         manager.add(fHideFragmentFilterAction);
489         manager.add(fHideOptionalFilterAction);
490         IDialogSettings settings = getSettings();
491         boolean hideFragments = settings.getBoolean(HIDE_FRAGMENTS);
492         boolean hideOptional = settings.getBoolean(HIDE_OPTIONAL);
493         fHideFragmentFilterAction.setChecked(!hideFragments);
494         fHideOptionalFilterAction.setChecked(!hideOptional);
495         // The filtering will be executed in the setActive function when the viewer is displayed
496
}
497     
498     private IDialogSettings getSettings() {
499         IDialogSettings master = PDEPlugin.getDefault().getDialogSettings();
500         IDialogSettings section = master.getSection("dependenciesView"); //$NON-NLS-1$
501
if (section == null) {
502             section = master.addNewSection("dependenciesView"); //$NON-NLS-1$
503
}
504         return section;
505     }
506
507 }
508
Popular Tags