KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IPackageFragment;
17 import org.eclipse.jdt.core.IPackageFragmentRoot;
18 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerFilter;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.EntryFileAdapter;
26 import org.eclipse.pde.internal.core.FileAdapter;
27 import org.eclipse.pde.internal.core.IPluginModelListener;
28 import org.eclipse.pde.internal.core.ModelEntry;
29 import org.eclipse.pde.internal.core.PluginModelDelta;
30 import org.eclipse.pde.internal.core.PluginModelManager;
31 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
32
33 public class PluginsContentProvider
34     extends DefaultContentProvider
35     implements ITreeContentProvider, IStructuredContentProvider, IPluginModelListener {
36     private PluginModelManager manager;
37     private TreeViewer viewer;
38     private PluginsView view;
39     private StandardJavaElementContentProvider javaProvider;
40
41     /**
42      * Constructor for PluginsContentProvider.
43      */

44     public PluginsContentProvider(PluginsView view, PluginModelManager manager) {
45         this.manager = manager;
46         manager.addPluginModelListener(this);
47         this.view = view;
48         javaProvider = new StandardJavaElementContentProvider();
49     }
50
51     public void dispose() {
52         manager.removePluginModelListener(this);
53     }
54     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
55         this.viewer = (TreeViewer) viewer;
56         if (newInput==null) return;
57         view.updateTitle(newInput);
58     }
59
60     /**
61      * @see ITreeContentProvider#getChildren(Object)
62      */

63     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
64         if (parentElement instanceof PluginModelManager) {
65             return ((PluginModelManager) parentElement).getEntries();
66         }
67         if (parentElement instanceof ModelEntry) {
68             ModelEntry entry = (ModelEntry) parentElement;
69             return entry.getChildren();
70         }
71         if (parentElement instanceof FileAdapter) {
72             return ((FileAdapter) parentElement).getChildren();
73         }
74         if (parentElement instanceof IPackageFragmentRoot ||
75             parentElement instanceof IPackageFragment ||
76             parentElement instanceof ICompilationUnit)
77             return javaProvider.getChildren(parentElement);
78         return new Object JavaDoc[0];
79     }
80
81     /**
82      * @see ITreeContentProvider#getParent(Object)
83      */

84     public Object JavaDoc getParent(Object JavaDoc element) {
85         if (element instanceof PluginModelManager) {
86             return null;
87         }
88         if (element instanceof ModelEntry) {
89             return manager;
90         }
91         if (element instanceof EntryFileAdapter) {
92             return ((EntryFileAdapter) element).getEntry();
93         }
94         if (element instanceof FileAdapter) {
95             return ((FileAdapter) element).getParent();
96         }
97         return null;
98     }
99
100     /**
101      * @see ITreeContentProvider#hasChildren(Object)
102      */

103     public boolean hasChildren(Object JavaDoc element) {
104         if (element instanceof PluginModelManager) {
105             return ((PluginModelManager) element).isEmpty() == false;
106         }
107         if (element instanceof ModelEntry) {
108             ModelEntry entry = (ModelEntry) element;
109             IPluginModelBase model = entry.getActiveModel();
110             File JavaDoc file = new File JavaDoc(model.getInstallLocation());
111             return !file.isFile() && model.getUnderlyingResource() == null;
112         }
113         if (element instanceof FileAdapter) {
114             FileAdapter fileAdapter = (FileAdapter) element;
115             return fileAdapter.hasChildren();
116         }
117         if (element instanceof IPackageFragmentRoot ||
118             element instanceof IPackageFragment ||
119             element instanceof ICompilationUnit)
120             return javaProvider.hasChildren(element);
121         return false;
122     }
123
124     /**
125      * @see IStructuredContentProvider#getElements(Object)
126      */

127     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
128         return getChildren(inputElement);
129     }
130
131     public void modelsChanged(final PluginModelDelta delta) {
132         if (viewer == null || viewer.getTree().isDisposed())
133             return;
134
135         viewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
136             public void run() {
137                 int kind = delta.getKind();
138                 if (viewer.getTree().isDisposed())
139                     return;
140                 if ((kind & PluginModelDelta.CHANGED) !=0) {
141                     // Don't know exactly what change -
142
// the safest way out is to refresh
143
viewer.refresh();
144                     return;
145                 }
146                 if ((kind & PluginModelDelta.REMOVED) != 0) {
147                     ModelEntry[] removed = delta.getRemovedEntries();
148                     viewer.remove(removed);
149                 }
150                 if ((kind & PluginModelDelta.ADDED) != 0) {
151                     ModelEntry[] added = delta.getAddedEntries();
152                     for (int i = 0; i < added.length; i++) {
153                         if (isVisible(added[i]))
154                             viewer.add(manager, added[i]);
155                     }
156                 }
157             }
158         });
159     }
160     private boolean isVisible(ModelEntry entry) {
161         ViewerFilter[] filters = viewer.getFilters();
162         for (int i = 0; i < filters.length; i++) {
163             ViewerFilter filter = filters[i];
164             if (!filter.select(viewer, manager, entry))
165                 return false;
166         }
167         return true;
168     }
169 }
170
Popular Tags