1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.BufferedInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.Map ; 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 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 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 fModels = null; 88 private ArrayList fChangedModels; 89 90 protected synchronized void initialize() { 91 if (fModels != null) 92 return; 93 94 fModels = Collections.synchronizedMap(new HashMap ()); 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 getModel(IProject project) { 110 initialize(); 111 return fModels.get(project); 112 } 113 114 protected Object [] getModels() { 115 initialize(); 116 return fModels.values().toArray(); 117 } 118 119 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 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 removeModel(IProject project) { 179 Object model = fModels != null ? fModels.remove(project) : null; 180 addChange(model, IModelProviderEvent.MODELS_REMOVED); 181 return model; 182 } 183 184 protected void addChange(Object model, int eventType) { 185 if (model instanceof IModel) { 186 if (fChangedModels == null) 187 fChangedModels = new ArrayList (); 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 added = new ArrayList (); 204 ArrayList removed = new ArrayList (); 205 ArrayList changed = new ArrayList (); 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 stream = null; 244 try { 245 stream = new BufferedInputStream (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 e) { 257 PDECore.log(e); 258 } 259 } 260 } 261 } 262 | Popular Tags |