KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > DependenciesViewPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.view;
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.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.jface.viewers.DoubleClickEvent;
24 import org.eclipse.jface.viewers.IDoubleClickListener;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.jface.viewers.StructuredViewer;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.pde.core.plugin.IPlugin;
31 import org.eclipse.pde.core.plugin.IPluginBase;
32 import org.eclipse.pde.core.plugin.IPluginImport;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.core.plugin.IPluginObject;
35 import org.eclipse.pde.core.plugin.ISharedPluginModel;
36 import org.eclipse.pde.internal.core.PDECore;
37 import org.eclipse.pde.internal.core.plugin.PluginReference;
38 import org.eclipse.pde.internal.ui.IPreferenceConstants;
39 import org.eclipse.pde.internal.ui.PDEPlugin;
40 import org.eclipse.pde.internal.ui.PDEUIMessages;
41 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
42 import org.eclipse.pde.internal.ui.search.dependencies.DependencyExtentAction;
43 import org.eclipse.pde.internal.ui.search.dependencies.UnusedDependenciesAction;
44 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Menu;
48 import org.eclipse.ui.IWorkbenchActionConstants;
49 import org.eclipse.ui.part.Page;
50
51 public abstract class DependenciesViewPage extends Page {
52     class FocusOnSelectionAction extends Action {
53         public void run() {
54             handleFocusOn(getSelectedObject());
55         }
56
57         public void update(Object JavaDoc object) {
58             setEnabled(object != null);
59             String JavaDoc name = ((LabelProvider) fViewer.getLabelProvider())
60                     .getText(object);
61             setText(NLS.bind(PDEUIMessages.DependenciesViewPage_focusOnSelection, name));
62         }
63     }
64
65     private Action fFocusOnAction;
66
67     private FocusOnSelectionAction fFocusOnSelectionAction;
68
69     private Action fOpenAction;
70
71     private IPropertyChangeListener fPropertyListener;
72
73     private DependenciesView fView;
74
75     protected StructuredViewer fViewer;
76
77     /**
78      *
79      */

80     public DependenciesViewPage(DependenciesView view) {
81         this.fView = view;
82         fPropertyListener = new IPropertyChangeListener() {
83             public void propertyChange(PropertyChangeEvent event) {
84                 String JavaDoc property = event.getProperty();
85                 if (property.equals(IPreferenceConstants.PROP_SHOW_OBJECTS)) {
86                     fViewer.refresh();
87                 }
88             }
89         };
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
96      */

97     public void createControl(Composite parent) {
98         fViewer = createViewer(parent);
99         PDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(
100                 fPropertyListener);
101         getSite().setSelectionProvider(fViewer);
102     }
103
104     protected abstract StructuredViewer createViewer(Composite parent);
105
106     public void dispose() {
107         PDEPlugin.getDefault().getPreferenceStore()
108                 .removePropertyChangeListener(fPropertyListener);
109         super.dispose();
110     }
111
112     private void fillContextMenu(IMenuManager manager) {
113         IStructuredSelection selection = (IStructuredSelection) fViewer
114                 .getSelection();
115
116         if (selection.size() == 1) {
117             manager.add(fOpenAction);
118             manager.add(new Separator());
119         }
120         fFocusOnSelectionAction.update(getSelectedObject());
121         if (fFocusOnSelectionAction.isEnabled())
122             manager.add(fFocusOnSelectionAction);
123         manager.add(fFocusOnAction);
124         Object JavaDoc selectionElement = selection.getFirstElement();
125
126         manager.add(new Separator());
127         if (selection.size() == 1) {
128             // Compute dep extent
129
// manager.add(new Separator());
130
// PluginSearchActionGroup actionGroup = new
131
// PluginSearchActionGroup();
132
// actionGroup.setContext(new ActionContext(selection));
133
// actionGroup.fillContextMenu(manager);
134
Object JavaDoc importObj = selectionElement;
135             if (importObj instanceof IPluginImport) {
136                 String JavaDoc id = ((IPluginImport) importObj).getId();
137                 IResource resource = ((IPluginImport) importObj).getModel()
138                         .getUnderlyingResource();
139                 if (resource != null) {
140                     manager.add(new DependencyExtentAction(resource
141                             .getProject(), id));
142                 }
143             }
144
145         }
146         // Unused Dependencies Action, only for worskpace plug-ins
147
ISharedPluginModel model = null;
148         if (selectionElement instanceof PluginReference) {
149             selectionElement = ((PluginReference) selectionElement).getPlugin();
150         }
151         if (selectionElement instanceof IPluginObject) {
152             model = ((IPluginObject) selectionElement).getModel();
153         }
154         if (model != null && model.getUnderlyingResource() != null) {
155             manager.add(new UnusedDependenciesAction(
156                     (IPluginModelBase) model, true));
157         }
158         //
159
manager.add(new Separator());
160         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.ui.part.IPage#getControl()
167      */

168     public Control getControl() {
169         return fViewer.getControl();
170     }
171
172     private Object JavaDoc getSelectedObject() {
173         IStructuredSelection selection = getSelection();
174         if (selection.isEmpty() || selection.size() != 1)
175             return null;
176         return selection.getFirstElement();
177     }
178
179     protected IStructuredSelection getSelection() {
180         return (IStructuredSelection) fViewer.getSelection();
181     }
182     
183     protected void setSelection(IStructuredSelection selection) {
184         if (selection != null && !selection.isEmpty())
185             fViewer.setSelection(selection, true);
186     }
187     
188     /**
189      * @return Returns the view.
190      */

191     public DependenciesView getView() {
192         return fView;
193     }
194
195     private void handleDoubleClick() {
196         Object JavaDoc obj = getSelectedObject();
197         if (obj instanceof IPluginImport) {
198             ManifestEditor.openPluginEditor(((IPluginImport) obj).getId());
199         } else {
200             ManifestEditor.open(obj, false);
201         }
202     }
203
204     private void handleFocusOn() {
205         PluginSelectionDialog dialog = new PluginSelectionDialog(fViewer
206                 .getControl().getShell(), true, false);
207         dialog.create();
208         if (dialog.open() == Window.OK) {
209             handleFocusOn(dialog.getFirstResult());
210         }
211     }
212
213     private void handleFocusOn(Object JavaDoc newFocus) {
214         if (newFocus instanceof IPluginModelBase) {
215             fView.openTo(newFocus);
216         }
217         if (newFocus instanceof IPluginBase) {
218             fView.openTo(((IPluginBase) newFocus).getModel());
219         }
220         if (newFocus instanceof IPluginImport) {
221             IPluginImport pluginImport = ((IPluginImport) newFocus);
222             String JavaDoc id = pluginImport.getId();
223             IPlugin importedPlugin = PDECore.getDefault().findPlugin(id);
224             if (importedPlugin != null) {
225                 fView.openTo(importedPlugin.getModel());
226             } else {
227                 fView.openTo(null);
228             }
229         }
230     }
231
232     private void hookContextMenu() {
233         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
234
menuMgr.setRemoveAllWhenShown(true);
235         menuMgr.addMenuListener(new IMenuListener() {
236             public void menuAboutToShow(IMenuManager manager) {
237                 DependenciesViewPage.this.fillContextMenu(manager);
238             }
239         });
240         Menu menu = menuMgr.createContextMenu(fViewer.getControl());
241         fViewer.getControl().setMenu(menu);
242
243         getSite()
244                 .registerContextMenu(fView.getSite().getId(), menuMgr, fViewer);
245     }
246
247     private void hookDoubleClickAction() {
248         fViewer.addDoubleClickListener(new IDoubleClickListener() {
249             public void doubleClick(DoubleClickEvent event) {
250                 handleDoubleClick();
251             }
252         });
253     }
254
255     private void makeActions() {
256         fOpenAction = new Action() {
257             public void run() {
258                 handleDoubleClick();
259             }
260         };
261         fOpenAction.setText(PDEUIMessages.DependenciesView_open);
262
263         fFocusOnSelectionAction = new FocusOnSelectionAction();
264
265         fFocusOnAction = new Action() {
266             public void run() {
267                 handleFocusOn();
268             }
269         };
270         fFocusOnAction.setText(PDEUIMessages.DependenciesViewPage_focusOn);
271     }
272
273     /*
274      * (non-Javadoc)
275      *
276      * @see org.eclipse.ui.part.Page#makeContributions(org.eclipse.jface.action.IMenuManager,
277      * org.eclipse.jface.action.IToolBarManager,
278      * org.eclipse.jface.action.IStatusLineManager)
279      */

280     public void makeContributions(IMenuManager menuManager,
281             IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
282         super.makeContributions(menuManager, toolBarManager, statusLineManager);
283         makeActions();
284         hookContextMenu();
285         hookDoubleClickAction();
286     }
287
288     /*
289      * (non-Javadoc)
290      *
291      * @see org.eclipse.ui.part.IPage#setFocus()
292      */

293     public void setFocus() {
294         if (fViewer != null) {
295             Control c = fViewer.getControl();
296             if (!c.isFocusControl()) {
297                 c.setFocus();
298             }
299         }
300     }
301
302     public void setInput(Object JavaDoc object) {
303         if (object != fViewer.getInput())
304             fViewer.setInput(object);
305     }
306 }
307
Popular Tags