KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.jface.viewers.*;
14 import org.eclipse.pde.core.plugin.*;
15 import org.eclipse.pde.internal.core.*;
16 import org.eclipse.pde.internal.core.plugin.ImportObject;
17 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
18
19 public class DependenciesContentProvider
20     extends DefaultContentProvider
21     implements ITreeContentProvider, IStructuredContentProvider, IPluginModelListener {
22     private PluginModelManager manager;
23     private TreeViewer viewer;
24     private DependenciesView view;
25
26     /**
27      * Constructor for PluginsContentProvider.
28      */

29     public DependenciesContentProvider(DependenciesView view, PluginModelManager manager) {
30         this.manager = manager;
31         manager.addPluginModelListener(this);
32         this.view = view;
33     }
34
35     public void dispose() {
36         manager.removePluginModelListener(this);
37     }
38     
39     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
40         this.viewer = (TreeViewer) viewer;
41         if (newInput==null) return;
42         view.updateTitle(newInput);
43     }
44
45     /**
46      * @see ITreeContentProvider#getChildren(Object)
47      */

48     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
49         if (parentElement instanceof IPluginModelBase) {
50             return new Object JavaDoc[] {((IPluginModelBase)parentElement).getPluginBase()};
51         }
52         if (parentElement instanceof IPluginBase) {
53             return createImportObjects((IPluginBase)parentElement);
54         }
55         if (parentElement instanceof ImportObject) {
56             ImportObject iobj = (ImportObject)parentElement;
57             IPlugin plugin = iobj.getPlugin();
58             if (plugin==null) return new Object JavaDoc[0];
59             return createImportObjects(plugin);
60         }
61         return new Object JavaDoc[0];
62     }
63     
64     private Object JavaDoc [] createImportObjects(IPluginBase plugin) {
65         IPluginImport [] imports = plugin.getImports();
66         Object JavaDoc [] result = new Object JavaDoc[imports.length];
67         for (int i=0; i<imports.length; i++) {
68             result[i] = new ImportObject(imports[i]);
69         }
70         return result;
71     }
72
73     /**
74      * @see ITreeContentProvider#getParent(Object)
75      */

76     public Object JavaDoc getParent(Object JavaDoc element) {
77         return null;
78     }
79
80     /**
81      * @see ITreeContentProvider#hasChildren(Object)
82      */

83     public boolean hasChildren(Object JavaDoc element) {
84         return getChildren(element).length>0;
85     }
86
87     /**
88      * @see IStructuredContentProvider#getElements(Object)
89      */

90     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
91         return getChildren(inputElement);
92     }
93
94     public void modelsChanged(final PluginModelDelta delta) {
95         if (viewer == null || viewer.getTree().isDisposed())
96             return;
97
98         viewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
99             public void run() {
100                 int kind = delta.getKind();
101                 if (viewer.getTree().isDisposed())
102                     return;
103                 if ((kind & PluginModelDelta.CHANGED) !=0 ||
104                     (kind & PluginModelDelta.ADDED) !=0) {
105                     // Don't know exactly what change -
106
// the safest way out is to refresh
107
viewer.refresh();
108                     return;
109                 }
110                 if ((kind & PluginModelDelta.REMOVED) != 0) {
111                     ModelEntry[] removed = delta.getRemovedEntries();
112                     handleRemoved(removed);
113                 }
114                 if ((kind & PluginModelDelta.ADDED) != 0) {
115                     viewer.refresh();
116                 }
117             }
118         });
119     }
120     private void handleRemoved(ModelEntry [] removed) {
121         for (int i=0; i<removed.length; i++) {
122             ModelEntry entry = removed[i];
123             IPluginModelBase model = entry.getActiveModel();
124             if (model!=null && model.equals(viewer.getInput())) {
125                 viewer.setInput(null);
126                 return;
127             }
128         }
129         viewer.refresh();
130     }
131 }
Popular Tags