KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > WorkspaceModelManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.core;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IFolder;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IResourceChangeEvent;
26 import org.eclipse.core.resources.IResourceChangeListener;
27 import org.eclipse.core.resources.IResourceDelta;
28 import org.eclipse.core.resources.IResourceDeltaVisitor;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.pde.core.IModel;
31 import org.eclipse.pde.core.IModelProviderEvent;
32 import org.eclipse.team.core.RepositoryProvider;
33
34 public abstract class WorkspaceModelManager extends AbstractModelManager
35         implements IResourceChangeListener, IResourceDeltaVisitor {
36     
37     public static boolean isPluginProject(IProject project) {
38         if (project.isOpen())
39             return project.exists(ICoreConstants.MANIFEST_PATH)
40                    || project.exists(ICoreConstants.PLUGIN_PATH)
41                    || project.exists(ICoreConstants.FRAGMENT_PATH);
42         return false;
43     }
44
45     public static boolean isFeatureProject(IProject project) {
46         return project.isOpen() && project.exists(ICoreConstants.FEATURE_PATH);
47     }
48
49     public static boolean isBinaryProject(IProject project) {
50         try {
51             if (project.isOpen()) {
52                 String JavaDoc binary = project.getPersistentProperty(PDECore.EXTERNAL_PROJECT_PROPERTY);
53                 if (binary != null) {
54                     RepositoryProvider provider = RepositoryProvider.getProvider(project);
55                     return provider == null || provider instanceof BinaryRepositoryProvider;
56                 }
57             }
58         } catch (CoreException e) {
59             PDECore.logException(e);
60         }
61         return false;
62     }
63     
64     public static boolean isUnsharedProject(IProject project) {
65         return RepositoryProvider.getProvider(project) == null || isBinaryProject(project);
66     }
67
68     class ModelChange {
69         IModel model;
70         int type;
71         public ModelChange(IModel model, int type) {
72             this.model = model;
73             this.type = type;
74         }
75         
76         public boolean equals(Object JavaDoc obj) {
77             if (obj instanceof ModelChange) {
78                 ModelChange change = (ModelChange)obj;
79                 IProject project = change.model.getUnderlyingResource().getProject();
80                 int type = change.type;
81                 return model.getUnderlyingResource().getProject().equals(project) && this.type == type;
82             }
83             return false;
84         }
85     }
86     
87     protected Map JavaDoc fModels = null;
88     private ArrayList JavaDoc fChangedModels;
89     
90     protected synchronized void initialize() {
91         if (fModels != null)
92             return;
93         
94         fModels = Collections.synchronizedMap(new HashMap JavaDoc());
95         IProject[] projects = PDECore.getWorkspace().getRoot().getProjects();
96         for (int i = 0; i < projects.length; i++) {
97             if (isInterestingProject(projects[i]))
98                 createModel(projects[i], false);
99         }
100         addListeners();
101     }
102     
103     protected abstract boolean isInterestingProject(IProject project);
104     
105     protected abstract void createModel(IProject project, boolean notify);
106     
107     protected abstract void addListeners();
108     
109     protected Object JavaDoc getModel(IProject project) {
110         initialize();
111         return fModels.get(project);
112     }
113     
114     protected Object JavaDoc[] getModels() {
115         initialize();
116         return fModels.values().toArray();
117     }
118     
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
123      */

124     public void resourceChanged(IResourceChangeEvent event) {
125         switch (event.getType()) {
126         case IResourceChangeEvent.POST_CHANGE:
127             handleResourceDelta(event.getDelta());
128             processModelChanges();
129             break;
130         case IResourceChangeEvent.PRE_CLOSE:
131             removeModel((IProject)event.getResource());
132             processModelChanges();
133             break;
134         }
135     }
136
137     private void handleResourceDelta(IResourceDelta delta) {
138         try {
139             delta.accept(this);
140         } catch (CoreException e) {
141             PDECore.logException(e);
142         }
143     }
144     
145     /*
146      * (non-Javadoc)
147      * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
148      */

149     public boolean visit(IResourceDelta delta) throws CoreException {
150         if (delta != null) {
151             IResource resource = delta.getResource();
152             if (resource instanceof IProject) {
153                 IProject project = (IProject) resource;
154                 if (isInterestingProject(project)
155                         && (delta.getKind() == IResourceDelta.ADDED || (delta.getFlags() & IResourceDelta.OPEN) != 0)) {
156                     createModel(project, true);
157                     return false;
158                 } else if (delta.getKind() == IResourceDelta.REMOVED) {
159                     removeModel(project);
160                     return false;
161                 }
162                 return true;
163             } else if (resource instanceof IFile) {
164                 handleFileDelta(delta);
165             } else if (resource instanceof IFolder) {
166                 return isInterestingFolder((IFolder)resource);
167             }
168         }
169         return true;
170     }
171     
172     protected boolean isInterestingFolder(IFolder folder) {
173         return false;
174     }
175     
176     protected abstract void handleFileDelta(IResourceDelta delta);
177     
178     protected Object JavaDoc removeModel(IProject project) {
179         Object JavaDoc model = fModels != null ? fModels.remove(project) : null;
180         addChange(model, IModelProviderEvent.MODELS_REMOVED);
181         return model;
182     }
183     
184     protected void addChange(Object JavaDoc model, int eventType) {
185         if (model instanceof IModel) {
186             if (fChangedModels == null)
187                 fChangedModels = new ArrayList JavaDoc();
188             ModelChange change = new ModelChange((IModel)model, eventType);
189             if (!fChangedModels.contains(change))
190                 fChangedModels.add(change);
191         }
192     }
193     
194     private void processModelChanges() {
195         if (fChangedModels == null)
196             return;
197         
198         if (fChangedModels.size() == 0) {
199             fChangedModels = null;
200             return;
201         }
202
203         ArrayList JavaDoc added = new ArrayList JavaDoc();
204         ArrayList JavaDoc removed = new ArrayList JavaDoc();
205         ArrayList JavaDoc changed = new ArrayList JavaDoc();
206         for (int i = 0; i < fChangedModels.size(); i++) {
207             ModelChange change = (ModelChange) fChangedModels.get(i);
208             switch (change.type) {
209                 case IModelProviderEvent.MODELS_ADDED:
210                     added.add(change.model);
211                     break;
212                 case IModelProviderEvent.MODELS_REMOVED:
213                     removed.add(change.model);
214                     break;
215                 case IModelProviderEvent.MODELS_CHANGED:
216                     changed.add(change.model);
217             }
218         }
219
220         int type = 0;
221         if (added.size() > 0)
222             type |= IModelProviderEvent.MODELS_ADDED;
223         if (removed.size() > 0)
224             type |= IModelProviderEvent.MODELS_REMOVED;
225         if (changed.size() > 0)
226             type |= IModelProviderEvent.MODELS_CHANGED;
227
228         fChangedModels = null;
229         if (type != 0) {
230             final ModelProviderEvent event =
231                 new ModelProviderEvent(
232                     this,
233                     type,
234                     (IModel[])added.toArray(new IModel[added.size()]),
235                     (IModel[])removed.toArray(new IModel[removed.size()]),
236                     (IModel[])changed.toArray(new IModel[changed.size()]));
237             fireModelProviderEvent(event);
238         }
239     }
240     
241     protected void loadModel(IModel model, boolean reload) {
242         IFile file = (IFile) model.getUnderlyingResource();
243         InputStream JavaDoc stream = null;
244         try {
245             stream = new BufferedInputStream JavaDoc(file.getContents(true));
246             if (reload)
247                 model.reload(stream, false);
248             else
249                 model.load(stream, false);
250         } catch (CoreException e) {
251             PDECore.logException(e);
252         } finally {
253             try {
254                 if (stream != null)
255                     stream.close();
256             } catch (IOException JavaDoc e) {
257                 PDECore.log(e);
258             }
259         }
260     }
261 }
262
Popular Tags