KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > runtime > registry > RegistryBrowser


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.runtime.registry;
12
13 import org.eclipse.core.runtime.IExtension;
14 import org.eclipse.core.runtime.IExtensionDelta;
15 import org.eclipse.core.runtime.IExtensionPoint;
16 import org.eclipse.core.runtime.IRegistryChangeEvent;
17 import org.eclipse.core.runtime.IRegistryChangeListener;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.viewers.ViewerComparator;
28 import org.eclipse.pde.internal.runtime.IHelpContextIds;
29 import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
30 import org.eclipse.pde.internal.runtime.PDERuntimePlugin;
31 import org.eclipse.pde.internal.runtime.PDERuntimePluginImages;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.custom.BusyIndicator;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Menu;
38 import org.eclipse.swt.widgets.Tree;
39 import org.eclipse.swt.widgets.TreeItem;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.IViewSite;
43 import org.eclipse.ui.PartInitException;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.XMLMemento;
46 import org.eclipse.ui.dialogs.FilteredTree;
47 import org.eclipse.ui.dialogs.PatternFilter;
48 import org.eclipse.ui.part.DrillDownAdapter;
49 import org.eclipse.ui.part.ViewPart;
50 import org.osgi.framework.Bundle;
51 import org.osgi.framework.BundleEvent;
52 import org.osgi.framework.BundleListener;
53 public class RegistryBrowser extends ViewPart implements BundleListener, IRegistryChangeListener {
54     
55     public static final String JavaDoc SHOW_RUNNING_PLUGINS = "RegistryView.showRunning.label"; //$NON-NLS-1$
56

57     private FilteredTree fFilteredTree;
58     private TreeViewer fTreeViewer;
59     private IMemento fMemento;
60     
61     // menus and action items
62
private Action fRefreshAction;
63     private Action fShowPluginsAction;
64     private Action fCollapseAllAction;
65     private Action fRemoveAction;
66     private Action fAddAction;
67     private DrillDownAdapter drillDownAdapter;
68     
69     /*
70      * customized DrillDownAdapter which modifies enabled state of showing active/inactive
71      * plug-ins action - see Bug 58467
72      */

73     class RegistryDrillDownAdapter extends DrillDownAdapter{
74         public RegistryDrillDownAdapter(TreeViewer tree){
75             super(tree);
76         }
77
78         public void goInto() {
79             super.goInto();
80             fShowPluginsAction.setEnabled(!canGoHome());
81         }
82
83         public void goBack() {
84             super.goBack();
85             fShowPluginsAction.setEnabled(!canGoHome());
86         }
87
88         public void goHome() {
89             super.goHome();
90             fShowPluginsAction.setEnabled(!canGoHome());
91         }
92
93         public void goInto(Object JavaDoc newInput) {
94             super.goInto(newInput);
95             fShowPluginsAction.setEnabled(!canGoHome());
96         }
97     }
98     public RegistryBrowser() {
99         super();
100     }
101     
102     public void init(IViewSite site, IMemento memento) throws PartInitException {
103         super.init(site, memento);
104         if (memento == null)
105             this.fMemento = XMLMemento.createWriteRoot("REGISTRYVIEW"); //$NON-NLS-1$
106
else
107             this.fMemento = memento;
108         initializeMemento();
109     }
110     
111     private void initializeMemento() {
112         // show all plug-ins by default (i.e. not just activated ones)
113
if (fMemento.getString(SHOW_RUNNING_PLUGINS) == null)
114             fMemento.putString(SHOW_RUNNING_PLUGINS, "false"); //$NON-NLS-1$
115
}
116     
117     public void dispose() {
118         Platform.getExtensionRegistry().removeRegistryChangeListener(this);
119         PDERuntimePlugin.getDefault().getBundleContext().removeBundleListener(
120                 this);
121         super.dispose();
122     }
123     
124     public void createPartControl(Composite parent) {
125         // create the sash form that will contain the tree viewer & text viewer
126
Composite composite = new Composite(parent, SWT.NONE);
127         GridLayout layout = new GridLayout();
128         layout.marginHeight = layout.marginWidth = 0;
129         composite.setLayout(layout);
130         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
131         makeActions();
132         createTreeViewer(composite);
133         fillToolBar();
134         setContentDescription(((RegistryBrowserContentProvider)fTreeViewer.getContentProvider()).getTitleSummary());
135         
136         Platform.getExtensionRegistry().addRegistryChangeListener(this);
137         PDERuntimePlugin.getDefault().getBundleContext().addBundleListener(this);
138     }
139     private void createTreeViewer(Composite parent) {
140         Composite composite = new Composite(parent, SWT.NONE);
141         GridLayout layout = new GridLayout();
142         layout.marginHeight = layout.marginWidth = 0;
143         composite.setLayout(layout);
144         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
145         
146         fFilteredTree = new RegistryFilteredTree(composite, SWT.MULTI, new PatternFilter());
147         fFilteredTree.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
148         Tree tree = fFilteredTree.getViewer().getTree();
149         GridData gd = new GridData(GridData.FILL_BOTH);
150         fFilteredTree.setLayoutData(gd);
151         fTreeViewer = fFilteredTree.getViewer();
152         boolean showRunning = fMemento.getString(SHOW_RUNNING_PLUGINS).equals("true") ? true : false; //$NON-NLS-1$
153
fTreeViewer.setContentProvider(new RegistryBrowserContentProvider(fTreeViewer, showRunning));
154         fTreeViewer.setLabelProvider(new RegistryBrowserLabelProvider(fTreeViewer));
155         fTreeViewer.setUseHashlookup(true);
156         fTreeViewer.setComparator(new ViewerComparator() {
157             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
158                 if (e1 instanceof PluginObjectAdapter)
159                     e1 = ((PluginObjectAdapter)e1).getObject();
160                 if (e2 instanceof PluginObjectAdapter)
161                     e2 = ((PluginObjectAdapter)e2).getObject();
162                 if (e1 instanceof IBundleFolder && e2 instanceof IBundleFolder)
163                     return ((IBundleFolder)e1).getFolderId() - ((IBundleFolder)e2).getFolderId();
164                 if (e1 instanceof Bundle && e2 instanceof Bundle) {
165                     e1 = ((Bundle)e1).getSymbolicName();
166                     e2 = ((Bundle)e2).getSymbolicName();
167                 }
168                 return super.compare(viewer, e1, e2);
169             }
170         });
171         
172         Bundle[] bundles = PDERuntimePlugin.getDefault().getBundleContext().getBundles();
173         fTreeViewer.setInput(new PluginObjectAdapter(bundles));
174         
175         PlatformUI.getWorkbench().getHelpSystem().setHelp(fTreeViewer.getControl(),
176                 IHelpContextIds.REGISTRY_VIEW);
177         
178         getViewSite().setSelectionProvider(fTreeViewer);
179         
180         MenuManager popupMenuManager = new MenuManager();
181         IMenuListener listener = new IMenuListener() {
182             public void menuAboutToShow(IMenuManager mng) {
183                 fillContextMenu(mng);
184             }
185         };
186         popupMenuManager.setRemoveAllWhenShown(true);
187         popupMenuManager.addMenuListener(listener);
188         Menu menu = popupMenuManager.createContextMenu(tree);
189         tree.setMenu(menu);
190     }
191         
192     private void fillToolBar(){
193         drillDownAdapter = new RegistryDrillDownAdapter(fTreeViewer);
194         IActionBars bars = getViewSite().getActionBars();
195         IToolBarManager mng = bars.getToolBarManager();
196         drillDownAdapter.addNavigationActions(mng);
197         mng.add(fRefreshAction);
198         mng.add(new Separator());
199         mng.add(fCollapseAllAction);
200         IMenuManager mgr = bars.getMenuManager();
201         mgr.add(new Separator());
202         mgr.add(fShowPluginsAction);
203     }
204     public void fillContextMenu(IMenuManager manager) {
205         manager.add(fRefreshAction);
206         Tree tree = getUndisposedTree();
207         // TODO remove testing actions from code after delta is fixed
208
// bug 130655
209
if (tree != null && false) {
210             TreeItem[] selection = tree.getSelection();
211             boolean allRemoveable = true;
212             boolean canAdd = true;
213             for (int i = 0; i < selection.length; i++) {
214                 Object JavaDoc obj = selection[i].getData();
215                 if (obj instanceof PluginObjectAdapter)
216                     obj = ((PluginObjectAdapter)obj).getObject();
217                 if (!(obj instanceof Bundle) || ((Bundle)obj).getState() < Bundle.RESOLVED)
218                     canAdd = false;
219                 if (!(obj instanceof IExtensionPoint) && !(obj instanceof IExtension)) {
220                     allRemoveable = false;
221                     break;
222                 }
223             }
224             if (allRemoveable)
225                 manager.add(fRemoveAction);
226             if (canAdd && selection.length == 1)
227                 manager.add(fAddAction);
228         }
229         manager.add(new Separator());
230         drillDownAdapter.addNavigationActions(manager);
231         manager.add(new Separator());
232         manager.add(fShowPluginsAction);
233     }
234     public TreeViewer getTreeViewer() {
235         return fTreeViewer;
236     }
237     
238     public void saveState(IMemento memento) {
239         if (memento == null || this.fMemento == null || fTreeViewer == null)
240             return;
241         boolean showRunning = ((RegistryBrowserContentProvider) fTreeViewer
242                 .getContentProvider()).isShowRunning();
243         if (showRunning)
244             this.fMemento.putString(SHOW_RUNNING_PLUGINS, Boolean.toString(true));
245         else
246             this.fMemento.putString(SHOW_RUNNING_PLUGINS, Boolean.toString(false));
247         memento.putMemento(this.fMemento);
248     }
249     
250     public void setFocus() {
251         fFilteredTree.getFilterControl().setFocus();
252     }
253     
254     /*
255      * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
256      */

257     public void bundleChanged(BundleEvent event) {
258         Tree tree = getUndisposedTree();
259         if (tree == null)
260             return;
261         
262         final RegistryBrowserContentProvider provider = ((RegistryBrowserContentProvider) fTreeViewer.getContentProvider());
263         final Bundle eventBundle = Platform.getBundle(event.getBundle().getSymbolicName());
264         if (eventBundle == null)
265             return;
266         final PluginObjectAdapter adapter = new PluginObjectAdapter(eventBundle);
267         tree.getDisplay().asyncExec(new Runnable JavaDoc() {
268             public void run() {
269                 Tree tree = getUndisposedTree();
270                 if (tree == null)
271                     return;
272                 TreeItem[] items = fTreeViewer.getTree().getItems();
273                 if (items != null) {
274                     for (int i = 0; i < items.length; i++) {
275                         Object JavaDoc object = items[i].getData();
276                         if (object instanceof PluginObjectAdapter)
277                             object = ((PluginObjectAdapter)object).getObject();
278                         if (object != null && object instanceof Bundle) {
279                             Bundle bundle = (Bundle) object;
280                             if (bundle.equals(eventBundle)) {
281                                 fTreeViewer.update(items[i].getData(), null);
282                                 updateTitle();
283                                 return;
284                             }
285                         }
286                     }
287                 }
288                 if (provider.isShowRunning() && eventBundle.getState() != Bundle.ACTIVE)
289                     return;
290                 fTreeViewer.add(fTreeViewer.getInput(), adapter);
291                 updateTitle();
292             }
293         });
294     }
295     
296     /*
297      * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
298      */

299     public void registryChanged(IRegistryChangeEvent event) {
300         Tree tree = getUndisposedTree();
301         if (tree == null)
302             return;
303         final IExtensionDelta[] deltas = event.getExtensionDeltas();
304         tree.getDisplay().syncExec(new Runnable JavaDoc() {
305             public void run() {
306                 if (getUndisposedTree() == null)
307                     return;
308                 for (int i = 0; i < deltas.length; i++)
309                     handleDelta(deltas[i]);
310             }
311         });
312     }
313     
314     private void handleDelta(IExtensionDelta delta) {
315         IExtension ext = delta.getExtension();
316         IExtensionPoint extPoint = delta.getExtensionPoint();
317         if (delta.getKind() == IExtensionDelta.ADDED) {
318             addToTree(ext);
319             addToTree(extPoint);
320         } else if (delta.getKind() == IExtensionDelta.REMOVED) {
321             removeFromTree(ext);
322             removeFromTree(extPoint);
323         }
324     }
325     
326     private void addToTree(Object JavaDoc object) {
327         String JavaDoc namespace = getNamespaceIdentifier(object);
328         if (namespace == null)
329             return;
330         TreeItem[] bundles = fTreeViewer.getTree().getItems();
331         for (int i = 0; i < bundles.length; i++) {
332             Object JavaDoc data = bundles[i].getData();
333             Object JavaDoc adapted = null;
334             if (data instanceof PluginObjectAdapter)
335                 adapted = ((PluginObjectAdapter)data).getObject();
336             if (adapted instanceof Bundle && ((Bundle)adapted).getSymbolicName().equals(namespace)) {
337                 // TODO fix this method
338
if (true) {
339                     fTreeViewer.refresh(data);
340                     return;
341                 }
342                 TreeItem[] folders = bundles[i].getItems();
343                 for (int j = 0; j < folders.length; j++) {
344                     IBundleFolder folder = (IBundleFolder)folders[j].getData();
345                     if (correctFolder(folder, object)) {
346                         fTreeViewer.add(folder, object);
347                         return;
348                     }
349                 }
350                 // folder not found - 1st extension - refresh bundle item
351
fTreeViewer.refresh(data);
352             }
353         }
354     }
355     
356     private String JavaDoc getNamespaceIdentifier(Object JavaDoc object) {
357         if (object instanceof IExtensionPoint)
358             return ((IExtensionPoint)object).getNamespaceIdentifier();
359         if (object instanceof IExtension)
360             return ((IExtension)object).getContributor().getName();
361         return null;
362     }
363     
364     private boolean correctFolder(IBundleFolder folder, Object JavaDoc child) {
365         if (folder == null)
366             return false;
367         if (child instanceof IExtensionPoint)
368             return folder.getFolderId() == IBundleFolder.F_EXTENSION_POINTS;
369         if (child instanceof IExtension)
370             return folder.getFolderId() == IBundleFolder.F_EXTENSIONS;
371         return false;
372     }
373     
374     private void removeFromTree(Object JavaDoc object) {
375         String JavaDoc namespace = getNamespaceIdentifier(object);
376         if (namespace == null)
377             return;
378         TreeItem[] bundles = fTreeViewer.getTree().getItems();
379         for (int i = 0; i < bundles.length; i++) {
380             Object JavaDoc data = bundles[i].getData();
381             Object JavaDoc adapted = null;
382             if (data instanceof PluginObjectAdapter)
383                 adapted = ((PluginObjectAdapter)data).getObject();
384             if (adapted instanceof Bundle && ((Bundle)adapted).getSymbolicName().equals(namespace)) {
385                 TreeItem[] folders = bundles[i].getItems();
386                 // TODO fix this method
387
if (true) {
388                     fTreeViewer.refresh(data);
389                     return;
390                 }
391                 for (int j = 0; j < folders.length; j++) {
392                     IBundleFolder folder = (IBundleFolder)folders[j].getData();
393                     if (correctFolder(folder, object)) {
394                         fTreeViewer.remove(object);
395                         return;
396                     }
397                 }
398                 // folder not found - 1st extension - refresh bundle item
399
fTreeViewer.refresh(data);
400             }
401         }
402     }
403     
404     /*
405      * toolbar and context menu actions
406      */

407     public void makeActions() {
408         fRefreshAction = new Action("refresh") { //$NON-NLS-1$
409
public void run() {
410                 BusyIndicator.showWhile(fTreeViewer.getTree().getDisplay(),
411                         new Runnable JavaDoc() {
412                     public void run() {
413                         fTreeViewer.refresh();
414                     }
415                 });
416             }
417         };
418         fRefreshAction.setText(PDERuntimeMessages.RegistryView_refresh_label);
419         fRefreshAction.setToolTipText(PDERuntimeMessages.RegistryView_refresh_tooltip);
420         fRefreshAction.setImageDescriptor(PDERuntimePluginImages.DESC_REFRESH);
421         fRefreshAction.setDisabledImageDescriptor(PDERuntimePluginImages.DESC_REFRESH_DISABLED);
422         
423         fShowPluginsAction = new Action(PDERuntimeMessages.RegistryView_showRunning_label){
424             public void run() {
425                 RegistryBrowserContentProvider cp = (RegistryBrowserContentProvider) fTreeViewer.getContentProvider();
426                 cp.setShowRunning(fShowPluginsAction.isChecked());
427                 fTreeViewer.refresh();
428                 updateTitle();
429             }
430         };
431         fShowPluginsAction.setChecked(fMemento.getString(SHOW_RUNNING_PLUGINS).equals("true")); //$NON-NLS-1$
432

433         fRemoveAction = new Action("Remove") { //$NON-NLS-1$
434
public void run() {
435 // Tree tree = getUndisposedTree();
436
// if (tree == null)
437
// return;
438
// IExtensionRegistry registry = Platform.getExtensionRegistry();
439
// Object token = ((ExtensionRegistry)registry).getTemporaryUserToken();
440
// TreeItem[] selection = tree.getSelection();
441
// for (int i = 0; i < selection.length; i++) {
442
// Object obj = selection[i].getData();
443
// if (obj instanceof ParentAdapter)
444
// obj = ((ParentAdapter)obj).getObject();
445
// if (obj instanceof IExtensionPoint)
446
// registry.removeExtensionPoint((IExtensionPoint)obj, token);
447
// else if (obj instanceof IExtension)
448
// registry.removeExtension((IExtension)obj, token);
449
// }
450

451             }
452         };
453         
454         fAddAction = new Action("Add...") { //$NON-NLS-1$
455
public void run() {
456 // Tree tree = getUndisposedTree();
457
// if (tree == null)
458
// return;
459
// FileDialog dialog = new FileDialog(getSite().getShell(), SWT.OPEN);
460
// String input = dialog.open();
461
// if (input == null)
462
// return;
463
// Object selection = tree.getSelection()[0].getData();
464
// if (selection instanceof PluginObjectAdapter)
465
// selection = ((PluginObjectAdapter)selection).getObject();
466
// if (!(selection instanceof Bundle))
467
// return;
468
// IContributor contributor = ContributorFactoryOSGi.createContributor((Bundle)selection);
469
// IExtensionRegistry registry = Platform.getExtensionRegistry();
470
// Object token = ((ExtensionRegistry)registry).getTemporaryUserToken();
471
// try {
472
// registry.addContribution(new FileInputStream(input), contributor, false, null, null, token);
473
// } catch (FileNotFoundException e) {
474
// }
475
}
476         };
477         
478         fCollapseAllAction = new Action("collapseAll"){ //$NON-NLS-1$
479
public void run(){
480                 fTreeViewer.collapseAll();
481             }
482         };
483         fCollapseAllAction.setText(PDERuntimeMessages.RegistryView_collapseAll_label);
484         fCollapseAllAction.setImageDescriptor(PDERuntimePluginImages.DESC_COLLAPSE_ALL);
485         fCollapseAllAction.setToolTipText(PDERuntimeMessages.RegistryView_collapseAll_tooltip);
486     }
487     
488     public void updateTitle(){
489         if (fTreeViewer == null || fTreeViewer.getContentProvider() == null)
490             return;
491         setContentDescription(((RegistryBrowserContentProvider)fTreeViewer.getContentProvider()).getTitleSummary());
492     }
493     
494     private Tree getUndisposedTree() {
495         if (fTreeViewer == null || fTreeViewer.getTree() == null || fTreeViewer.getTree().isDisposed())
496             return null;
497         return fTreeViewer.getTree();
498     }
499 }
500
Popular Tags