1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.PrintWriter ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IFolder; 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.IResourceChangeEvent; 28 import org.eclipse.core.resources.IResourceDelta; 29 import org.eclipse.core.resources.IWorkspace; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IPath; 32 import org.eclipse.core.runtime.Path; 33 import org.eclipse.jdt.core.JavaCore; 34 import org.eclipse.pde.core.IModelProviderEvent; 35 import org.eclipse.pde.core.plugin.IPluginModelBase; 36 import org.eclipse.pde.core.plugin.ISharedExtensionsModel; 37 import org.eclipse.pde.internal.core.builders.SchemaTransformer; 38 import org.eclipse.pde.internal.core.bundle.BundleFragmentModel; 39 import org.eclipse.pde.internal.core.bundle.BundlePluginModel; 40 import org.eclipse.pde.internal.core.bundle.WorkspaceBundleModel; 41 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 42 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 43 import org.eclipse.pde.internal.core.ischema.ISchema; 44 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor; 45 import org.eclipse.pde.internal.core.plugin.WorkspaceExtensionsModel; 46 import org.eclipse.pde.internal.core.plugin.WorkspaceFragmentModel; 47 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModel; 48 import org.eclipse.pde.internal.core.schema.SchemaDescriptor; 49 50 public class WorkspacePluginModelManager extends WorkspaceModelManager { 51 52 56 protected boolean isInterestingProject(IProject project) { 57 return isPluginProject(project); 58 } 59 60 71 protected void createModel(IProject project, boolean notify) { 72 IPluginModelBase model = null; 73 if (project.exists(ICoreConstants.MANIFEST_PATH)) { 74 WorkspaceBundleModel bmodel = new WorkspaceBundleModel(project.getFile(ICoreConstants.MANIFEST_PATH)); 75 loadModel(bmodel, false); 76 if (bmodel.isFragmentModel()) 77 model = new BundleFragmentModel(); 78 else 79 model = new BundlePluginModel(); 80 model.setEnabled(true); 81 ((IBundlePluginModelBase)model).setBundleModel(bmodel); 82 83 IFile efile = project.getFile(bmodel.isFragmentModel() 84 ? ICoreConstants.FRAGMENT_PATH : ICoreConstants.PLUGIN_PATH); 85 if (efile.exists()) { 86 WorkspaceExtensionsModel extModel = new WorkspaceExtensionsModel(efile); 87 loadModel(extModel, false); 88 ((IBundlePluginModelBase)model).setExtensionsModel(extModel); 89 extModel.setBundleModel((IBundlePluginModelBase)model); 90 } 91 92 } else if (project.exists(ICoreConstants.PLUGIN_PATH)) { 93 model = new WorkspacePluginModel(project.getFile(ICoreConstants.PLUGIN_PATH), true); 94 loadModel(model, false); 95 } else if (project.exists(ICoreConstants.FRAGMENT_PATH)) { 96 model = new WorkspaceFragmentModel(project.getFile(ICoreConstants.FRAGMENT_PATH), true); 97 loadModel(model, false); 98 } 99 100 if (project.getFile(".options").exists()) PDECore.getDefault().getTracingOptionsManager().reset(); 102 103 if (model != null) { 104 if (fModels == null) 105 fModels = new HashMap (); 106 fModels.put(project, model); 107 if (notify) 108 addChange(model, IModelProviderEvent.MODELS_ADDED); 109 } 110 } 111 112 115 protected void handleFileDelta(IResourceDelta delta) { 116 IFile file = (IFile)delta.getResource(); 117 String filename = file.getName(); 118 if (filename.equals(".options")) { PDECore.getDefault().getTracingOptionsManager().reset(); 120 } else if (filename.endsWith(".properties")) { if (filename.equals("build.properties")) { Object model = getModel(file.getProject()); 126 if (model != null) 127 addChange(model, IModelProviderEvent.MODELS_CHANGED); 128 } else { 129 IPluginModelBase model = getPluginModel(file.getProject()); 131 String localization = null; 132 if (model instanceof IBundlePluginModelBase) { 133 localization = ((IBundlePluginModelBase)model).getBundleLocalization(); 134 } else if (model != null) { 135 localization = "plugin"; } 137 if (localization != null && filename.startsWith(localization)) { 138 ((AbstractNLModel)model).resetNLResourceHelper(); 139 } 140 } 141 } else if (filename.endsWith(".exsd")) { handleEclipseSchemaDelta(file, delta); 143 } else { 144 IPath path = file.getProjectRelativePath(); 145 if (path.equals(ICoreConstants.PLUGIN_PATH) 146 || path.equals(ICoreConstants.FRAGMENT_PATH)){ 147 handleExtensionFileDelta(file, delta); 148 } else if (path.equals(ICoreConstants.MANIFEST_PATH)) { 149 handleBundleManifestDelta(file, delta); 150 } 151 } 152 } 153 154 158 private void handleEclipseSchemaDelta(IFile schemaFile, IResourceDelta delta) { 159 int kind = delta.getKind(); 161 if (kind != IResourceDelta.CHANGED) { 163 return; 164 } else if ((IResourceDelta.CONTENT & delta.getFlags()) == 0) { 165 return; 166 } 167 Object property = null; 169 try { 170 property = 171 schemaFile.getSessionProperty(PDECore.SCHEMA_PREVIEW_FILE); 172 } catch (CoreException e) { 173 return; 175 } 176 if (property == null) { 181 return; 182 } else if ((property instanceof File ) == false) { 183 return; 184 } 185 File schemaPreviewFile = (File )property; 186 if (schemaPreviewFile.exists() == false) { 188 return; 189 } else if (schemaPreviewFile.isFile() == false) { 190 return; 191 } else if (schemaPreviewFile.canWrite() == false) { 192 return; 193 } 194 ISchemaDescriptor descriptor = new SchemaDescriptor(schemaFile, false); 196 ISchema schema = descriptor.getSchema(false); 197 198 try { 199 recreateSchemaPreviewFileContents(schemaPreviewFile, schema); 202 } catch (IOException e) { 203 } 205 } 206 207 212 private void recreateSchemaPreviewFileContents(File schemaPreviewFile, 213 ISchema schema) throws IOException { 214 SchemaTransformer transformer = new SchemaTransformer(); 215 OutputStream os = new FileOutputStream (schemaPreviewFile); 216 PrintWriter printWriter = new PrintWriter (os, true); 217 transformer.transform(schema, printWriter); 218 os.flush(); 219 os.close(); 220 } 221 222 243 private void handleExtensionFileDelta(IFile file, IResourceDelta delta) { 244 int kind = delta.getKind(); 245 IPluginModelBase model = (IPluginModelBase)getModel(file.getProject()); 246 if (kind == IResourceDelta.REMOVED) { 247 if (model instanceof IBundlePluginModelBase) { 248 ((IBundlePluginModelBase)model).setExtensionsModel(null); 249 } else { 250 removeModel(file.getProject()); 251 } 252 } else if (kind == IResourceDelta.ADDED) { 253 if (model instanceof IBundlePluginModelBase){ 254 WorkspaceExtensionsModel extensions = new WorkspaceExtensionsModel(file); 255 ((IBundlePluginModelBase)model).setExtensionsModel(extensions); 256 extensions.setBundleModel((IBundlePluginModelBase)model); 257 loadModel(extensions, false); 258 } else { 259 createModel(file.getProject(), true); 260 } 261 } else if (kind == IResourceDelta.CHANGED 262 && (IResourceDelta.CONTENT & delta.getFlags()) != 0) { 263 if (model instanceof IBundlePluginModelBase) { 264 ISharedExtensionsModel extensions = ((IBundlePluginModelBase)model).getExtensionsModel(); 265 boolean reload = extensions != null; 266 if (extensions == null) { 267 extensions = new WorkspaceExtensionsModel(file); 268 ((IBundlePluginModelBase)model).setExtensionsModel(extensions); 269 ((WorkspaceExtensionsModel)extensions).setBundleModel((IBundlePluginModelBase)model); 270 } 271 loadModel(extensions, reload); 272 } else if (model != null) { 273 loadModel(model, true); 274 addChange(model, IModelProviderEvent.MODELS_CHANGED); 275 } 276 } 277 } 278 279 291 private void handleBundleManifestDelta(IFile file, IResourceDelta delta) { 292 int kind = delta.getKind(); 293 IProject project = file.getProject(); 294 Object model = getModel(project); 295 if (kind == IResourceDelta.REMOVED && model != null) { 296 removeModel(project); 297 createModel(project, true); 299 } else if (kind == IResourceDelta.ADDED || model == null) { 300 createModel(project, true); 301 } else if (kind == IResourceDelta.CHANGED 302 && (IResourceDelta.CONTENT & delta.getFlags()) != 0) { 303 if (model instanceof IBundlePluginModelBase) { 304 String oldLocalization = ((IBundlePluginModelBase)model).getBundleLocalization(); 306 IBundleModel bmodel = ((IBundlePluginModelBase)model).getBundleModel(); 307 boolean wasFragment = bmodel.isFragmentModel(); 308 loadModel(bmodel, true); 309 String newLocalization = ((IBundlePluginModelBase)model).getBundleLocalization(); 310 311 if (wasFragment != bmodel.isFragmentModel()) { 313 removeModel(project); 314 createModel(project, true); 315 } else { 316 if (model instanceof AbstractNLModel && 317 (oldLocalization != null && (newLocalization == null || !oldLocalization.equals(newLocalization))) || 318 (newLocalization != null && (oldLocalization == null || !newLocalization.equals(oldLocalization)))) 319 ((AbstractNLModel)model).resetNLResourceHelper(); 320 addChange(model, IModelProviderEvent.MODELS_CHANGED); 321 } 322 } 323 } 324 } 325 326 330 protected Object removeModel(IProject project) { 331 Object model = super.removeModel(project); 332 if (model != null && project.exists(new Path(".options"))) PDECore.getDefault().getTracingOptionsManager().reset(); 334 return model; 335 } 336 337 347 protected IPluginModelBase getPluginModel(IProject project) { 348 return (IPluginModelBase)getModel(project); 349 } 350 351 356 protected IPluginModelBase[] getPluginModels() { 357 initialize(); 358 return (IPluginModelBase[])fModels.values().toArray(new IPluginModelBase[fModels.size()]); 359 } 360 361 365 protected void addListeners() { 366 IWorkspace workspace = PDECore.getWorkspace(); 367 workspace.addResourceChangeListener(this, IResourceChangeEvent.PRE_CLOSE); 368 JavaCore.addPreProcessingResourceChangedListener(this, IResourceChangeEvent.POST_CHANGE); 371 } 372 373 377 protected void removeListeners() { 378 PDECore.getWorkspace().removeResourceChangeListener(this); 379 JavaCore.removePreProcessingResourceChangedListener(this); 380 super.removeListeners(); 381 } 382 383 392 protected boolean isInterestingFolder(IFolder folder) { 393 if (folder.getName().equals("META-INF") && folder.getParent() instanceof IProject) { return true; 395 } 396 397 if (folder.getName().equals("schema") && folder.getParent() instanceof IProject) { return true; 399 } 400 401 return false; 402 } 403 404 410 protected void initializeModels(IPluginModelBase[] models) { 411 fModels = Collections.synchronizedMap(new HashMap ()); 412 for (int i = 0; i < models.length; i++) { 413 IProject project = models[i].getUnderlyingResource().getProject(); 414 fModels.put(project, models[i]); 415 } 416 addListeners(); 417 } 418 419 425 protected URL [] getPluginPaths() { 426 ArrayList list = new ArrayList (); 427 IProject[] projects = PDECore.getWorkspace().getRoot().getProjects(); 428 for (int i = 0; i < projects.length; i++) { 429 if (isPluginProject(projects[i])) { 430 try { 431 IPath path = projects[i].getLocation(); 432 if (path != null) { 433 list.add(path.toFile().toURL()); 434 } 435 } catch (MalformedURLException e) { 436 } 437 } 438 } 439 return (URL [])list.toArray(new URL [list.size()]); 440 } 441 442 } 443 | Popular Tags |