KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > WorkingSetAwareContentProvider


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.jdt.internal.ui.packageview;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.IAdaptable;
24
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.ResourcesPlugin;
28
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.jface.viewers.TreePath;
32
33 import org.eclipse.ui.IWorkingSet;
34 import org.eclipse.ui.IWorkingSetManager;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IJavaModel;
38 import org.eclipse.jdt.core.IJavaProject;
39 import org.eclipse.jdt.core.JavaCore;
40
41 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetModel;
42
43 public class WorkingSetAwareContentProvider extends PackageExplorerContentProvider implements IMultiElementTreeContentProvider {
44
45     private WorkingSetModel fWorkingSetModel;
46     private IPropertyChangeListener fListener;
47     
48     public WorkingSetAwareContentProvider(boolean provideMembers, WorkingSetModel model) {
49         super(provideMembers);
50         fWorkingSetModel= model;
51         fListener= new IPropertyChangeListener() {
52                     public void propertyChange(PropertyChangeEvent event) {
53                         workingSetModelChanged(event);
54                     }
55                 };
56         fWorkingSetModel.addPropertyChangeListener(fListener);
57     }
58     
59     /**
60      * {@inheritDoc}
61      */

62     public void dispose() {
63         fWorkingSetModel.removePropertyChangeListener(fListener);
64         super.dispose();
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public boolean hasChildren(Object JavaDoc element) {
71         if (element instanceof IWorkingSet)
72             return true;
73         return super.hasChildren(element);
74     }
75     
76     /**
77      * {@inheritDoc}
78      */

79     public Object JavaDoc[] getChildren(Object JavaDoc element) {
80         Object JavaDoc[] children;
81         if (element instanceof WorkingSetModel) {
82             Assert.isTrue(fWorkingSetModel == element);
83             return fWorkingSetModel.getActiveWorkingSets();
84         } else if (element instanceof IWorkingSet) {
85             children= getWorkingSetChildren((IWorkingSet)element);
86         } else {
87             children= super.getChildren(element);
88         }
89         return children;
90     }
91
92     private Object JavaDoc[] getWorkingSetChildren(IWorkingSet set) {
93         IAdaptable[] elements= fWorkingSetModel.getChildren(set);
94         Set JavaDoc result= new HashSet JavaDoc(elements.length);
95         for (int i= 0; i < elements.length; i++) {
96             IAdaptable element= elements[i];
97             if (element instanceof IProject) {
98                 processResource((IProject) element, result); // also add closed projects
99
} else if (element instanceof IResource) {
100                 IProject project= ((IResource) element).getProject();
101                 if (project.isOpen()) {
102                     processResource((IResource) element, result);
103                 }
104             } else if (element instanceof IJavaProject) {
105                 result.add(element); // also add closed projects
106
} else if (element instanceof IJavaElement) {
107                 IJavaElement elem= (IJavaElement) element;
108                 IProject project= getProject(elem);
109                 if (project != null && project.isOpen()) {
110                     result.add(elem);
111                 }
112             } else {
113                 IProject project= (IProject) element.getAdapter(IProject.class);
114                 if (project != null) {
115                     processResource(project, result);
116                 }
117             }
118         }
119         return result.toArray();
120     }
121     
122     private void processResource(IResource resource, Collection JavaDoc result) {
123         IJavaElement elem= JavaCore.create(resource);
124         if (elem != null && elem.exists()) {
125             result.add(elem);
126         } else {
127             result.add(resource);
128         }
129     }
130     
131     private IProject getProject(IJavaElement element) {
132         IJavaProject project= element.getJavaProject();
133         if (project == null)
134             return null;
135         return project.getProject();
136     }
137  
138     /**
139      * {@inheritDoc}
140      */

141     public TreePath[] getTreePaths(Object JavaDoc element) {
142         if (element instanceof IWorkingSet) {
143             TreePath path= new TreePath(new Object JavaDoc[] {element});
144             return new TreePath[] {path};
145         }
146         List JavaDoc modelParents= getModelPath(element);
147         List JavaDoc result= new ArrayList JavaDoc();
148         for (int i= 0; i < modelParents.size(); i++) {
149             result.addAll(getTreePaths(modelParents, i));
150         }
151         return (TreePath[])result.toArray(new TreePath[result.size()]);
152     }
153     
154     private List JavaDoc getModelPath(Object JavaDoc element) {
155         List JavaDoc result= new ArrayList JavaDoc();
156         result.add(element);
157         Object JavaDoc parent= super.getParent(element);
158         Object JavaDoc input= getViewerInput();
159         // stop at input or on JavaModel. We never visualize it anyway.
160
while (parent != null && !parent.equals(input) && !(parent instanceof IJavaModel)) {
161             result.add(parent);
162             parent= super.getParent(parent);
163         }
164         Collections.reverse(result);
165         return result;
166     }
167     
168     private List JavaDoc/*<TreePath>*/ getTreePaths(List JavaDoc modelParents, int index) {
169         List JavaDoc result= new ArrayList JavaDoc();
170         Object JavaDoc input= getViewerInput();
171         Object JavaDoc element= modelParents.get(index);
172         Object JavaDoc[] parents= fWorkingSetModel.getAllParents(element);
173         for (int i= 0; i < parents.length; i++) {
174             List JavaDoc chain= new ArrayList JavaDoc();
175             if (!parents[i].equals(input))
176                 chain.add(parents[i]);
177             for (int m= index; m < modelParents.size(); m++) {
178                 chain.add(modelParents.get(m));
179             }
180             result.add(new TreePath(chain.toArray()));
181         }
182         return result;
183     }
184     
185     /**
186      * {@inheritDoc}
187      */

188     public Object JavaDoc getParent(Object JavaDoc child) {
189         Object JavaDoc[] parents= fWorkingSetModel.getAllParents(child);
190         if(parents.length == 0)
191             return super.getParent(child);
192         Object JavaDoc first= parents[0];
193         return first;
194     }
195     
196     protected void augmentElementToRefresh(List JavaDoc toRefresh, int relation, Object JavaDoc affectedElement) {
197         // we are refreshing the JavaModel and are in working set mode.
198
if (JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).equals(affectedElement)) {
199             toRefresh.remove(affectedElement);
200             toRefresh.add(fWorkingSetModel);
201         } else if (relation == GRANT_PARENT) {
202             Object JavaDoc parent= internalGetParent(affectedElement);
203             if (parent != null) {
204                 toRefresh.addAll(Arrays.asList(fWorkingSetModel.getAllParents(parent)));
205             }
206         }
207         List JavaDoc nonProjetTopLevelElemens= fWorkingSetModel.getNonProjectTopLevelElements();
208         if (nonProjetTopLevelElemens.isEmpty())
209             return;
210         List JavaDoc toAdd= new ArrayList JavaDoc();
211         for (Iterator JavaDoc iter= nonProjetTopLevelElemens.iterator(); iter.hasNext();) {
212             Object JavaDoc element= iter.next();
213             if (isChildOf(element, toRefresh))
214                 toAdd.add(element);
215         }
216         toRefresh.addAll(toAdd);
217     }
218     
219     private void workingSetModelChanged(PropertyChangeEvent event) {
220         String JavaDoc property= event.getProperty();
221         Object JavaDoc newValue= event.getNewValue();
222         List JavaDoc toRefresh= new ArrayList JavaDoc(1);
223         if (WorkingSetModel.CHANGE_WORKING_SET_MODEL_CONTENT.equals(property)) {
224             toRefresh.add(fWorkingSetModel);
225         } else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property)) {
226             toRefresh.add(newValue);
227         } else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property)) {
228             toRefresh.add(newValue);
229         }
230         ArrayList JavaDoc runnables= new ArrayList JavaDoc();
231         postRefresh(toRefresh, true, runnables);
232         executeRunnables(runnables);
233     }
234     
235     private boolean isChildOf(Object JavaDoc element, List JavaDoc potentialParents) {
236         // Calling super get parent to bypass working set mapping
237
Object JavaDoc parent= super.getParent(element);
238         if (parent == null)
239             return false;
240         for (Iterator JavaDoc iter= potentialParents.iterator(); iter.hasNext();) {
241             Object JavaDoc potentialParent= iter.next();
242             while(parent != null) {
243                 if (parent.equals(potentialParent))
244                     return true;
245                 parent= super.getParent(parent);
246             }
247             
248         }
249         return false;
250     }
251 }
Popular Tags