1 11 package org.eclipse.pde.internal.core.bundle; 12 import java.io.InputStream ; 13 import java.io.PrintWriter ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.osgi.service.resolver.BundleDescription; 20 import org.eclipse.pde.core.IEditable; 21 import org.eclipse.pde.core.IEditableModel; 22 import org.eclipse.pde.core.IModelChangedEvent; 23 import org.eclipse.pde.core.build.IBuildModel; 24 import org.eclipse.pde.core.plugin.IExtensions; 25 import org.eclipse.pde.core.plugin.IExtensionsModelFactory; 26 import org.eclipse.pde.core.plugin.IPluginAttribute; 27 import org.eclipse.pde.core.plugin.IPluginBase; 28 import org.eclipse.pde.core.plugin.IPluginElement; 29 import org.eclipse.pde.core.plugin.IPluginExtension; 30 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 31 import org.eclipse.pde.core.plugin.IPluginImport; 32 import org.eclipse.pde.core.plugin.IPluginLibrary; 33 import org.eclipse.pde.core.plugin.IPluginModelFactory; 34 import org.eclipse.pde.core.plugin.IPluginObject; 35 import org.eclipse.pde.core.plugin.ISharedExtensionsModel; 36 import org.eclipse.pde.internal.core.AbstractNLModel; 37 import org.eclipse.pde.internal.core.NLResourceHelper; 38 import org.eclipse.pde.internal.core.PDEManager; 39 import org.eclipse.pde.internal.core.ibundle.IBundle; 40 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 41 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 42 import org.eclipse.pde.internal.core.plugin.PluginImport; 43 import org.eclipse.pde.internal.core.plugin.PluginLibrary; 44 import org.eclipse.pde.internal.core.text.plugin.PluginModelBase; 45 46 public abstract class BundlePluginModelBase extends AbstractNLModel 47 implements IBundlePluginModelBase, IPluginModelFactory { 48 49 private IBundleModel fBundleModel; 50 private ISharedExtensionsModel fExtensionsModel; 51 private BundlePluginBase fBundlePluginBase; 52 private IBuildModel fBuildModel; 53 private BundleDescription fBundleDescription; 54 55 public BundlePluginModelBase() { 56 getPluginBase(); 57 } 58 63 public IBundleModel getBundleModel() { 64 return fBundleModel; 65 } 66 public IResource getUnderlyingResource() { 67 return fBundleModel.getUnderlyingResource(); 68 } 69 74 public ISharedExtensionsModel getExtensionsModel() { 75 return fExtensionsModel; 76 } 77 public void dispose() { 78 if (fBundleModel != null) { 79 if (fBundlePluginBase != null) 80 fBundleModel.removeModelChangedListener(fBundlePluginBase); 81 fBundleModel.dispose(); 82 fBundleModel = null; 83 } 84 if (fExtensionsModel != null) { 85 if (fBundlePluginBase != null) 86 fExtensionsModel.removeModelChangedListener(fBundlePluginBase); 87 fExtensionsModel.dispose(); 88 fExtensionsModel = null; 89 } 90 super.dispose(); 91 } 92 public void save() { 93 if (fBundleModel != null && fBundleModel instanceof IEditableModel) { 94 IEditableModel emodel = (IEditableModel) fBundleModel; 95 if (emodel.isDirty()) 96 emodel.save(); 97 } 98 if (fExtensionsModel != null 99 && fExtensionsModel instanceof IEditableModel) { 100 IEditableModel emodel = (IEditableModel) fExtensionsModel; 101 if (emodel.isDirty()) 102 emodel.save(); 103 } 104 } 105 110 public void setBundleModel(IBundleModel bundleModel) { 111 if (fBundleModel != null && fBundlePluginBase != null) { 112 fBundleModel.removeModelChangedListener(fBundlePluginBase); 113 } 114 fBundleModel = bundleModel; 115 if (fBundleModel != null && fBundlePluginBase != null) 116 bundleModel.addModelChangedListener(fBundlePluginBase); 117 } 118 123 public void setExtensionsModel(ISharedExtensionsModel extensionsModel) { 124 if (fExtensionsModel != null && fBundlePluginBase != null) { 125 fExtensionsModel.removeModelChangedListener(fBundlePluginBase); 126 } 127 fExtensionsModel = extensionsModel; 128 if (fExtensionsModel instanceof PluginModelBase) { 129 ((PluginModelBase)fExtensionsModel).setLocalization(getBundleLocalization()); 130 } 131 if (extensionsModel != null && fBundlePluginBase != null) 132 extensionsModel.addModelChangedListener(fBundlePluginBase); 133 } 134 135 public IBuildModel getBuildModel() { 136 return fBuildModel; 137 } 138 139 public void setBuildModel(IBuildModel buildModel) { 140 fBuildModel = buildModel; 141 } 142 143 public IPluginBase getPluginBase() { 144 return getPluginBase(true); 145 } 146 147 public IExtensions getExtensions() { 148 return getPluginBase(); 149 } 150 151 154 public void fireModelChanged(IModelChangedEvent event) { 155 super.fireModelChanged(event); 156 Object [] objects = event.getChangedObjects(); 157 if (objects!= null && objects.length > 0) { 158 if (objects[0] instanceof IPluginImport) { 159 fBundlePluginBase.updateImport((IPluginImport)objects[0]); 160 } 161 } 162 } 163 164 169 public IPluginBase getPluginBase(boolean createIfMissing) { 170 if (fBundlePluginBase == null && createIfMissing) { 171 fBundlePluginBase = (BundlePluginBase) createPluginBase(); 172 if (fBundleModel != null) 173 fBundleModel.addModelChangedListener(fBundlePluginBase); 174 setLoaded(true); 175 } 176 return fBundlePluginBase; 177 } 178 public IExtensions getExtensions(boolean createIfMissing) { 179 return getPluginBase(createIfMissing); 180 } 181 186 public IPluginModelFactory getPluginFactory() { 187 return this; 188 } 189 194 public IExtensionsModelFactory getFactory() { 195 if (fExtensionsModel != null) 196 return fExtensionsModel.getFactory(); 197 return null; 198 } 199 204 public String getInstallLocation() { 205 if (fBundleModel != null) 206 return fBundleModel.getInstallLocation(); 207 return null; 208 } 209 210 public String getBundleLocalization() { 211 IBundle bundle = fBundleModel != null ? fBundleModel.getBundle() : null; 212 return bundle != null ? bundle.getLocalization() : null; 213 214 } 215 216 protected NLResourceHelper createNLResourceHelper() { 217 String localization = getBundleLocalization(); 218 return localization == null 219 ? null 220 : new NLResourceHelper(localization, PDEManager.getNLLookupLocations(this)); 221 } 222 223 public URL getNLLookupLocation() { 224 try { 225 return new URL ("file:" + getInstallLocation()); } catch (MalformedURLException e) { 227 return null; 228 } 229 } 230 235 public boolean isEditable() { 236 if (fBundleModel != null && fBundleModel.isEditable() == false) 237 return false; 238 if (fExtensionsModel != null && fExtensionsModel.isEditable() == false) 239 return false; 240 return true; 241 } 242 247 public boolean isInSync() { 248 return ((fBundleModel == null || fBundleModel.isInSync()) && (fExtensionsModel == null || fExtensionsModel 249 .isInSync())); 250 } 251 256 public boolean isValid() { 257 return ((fBundleModel == null || fBundleModel.isValid()) && (fExtensionsModel == null || fExtensionsModel 258 .isValid())); 259 } 260 265 public void load() throws CoreException { 266 } 267 272 public void load(InputStream source, boolean outOfSync) 273 throws CoreException { 274 } 275 280 public void reload(InputStream source, boolean outOfSync) 281 throws CoreException { 282 } 283 286 public boolean isEnabled() { 287 return true; 288 } 289 290 public void setEnabled(boolean enabled) { 291 } 292 293 298 protected void updateTimeStamp() { 299 } 300 301 public IPluginImport createImport() { 302 PluginImport iimport = new PluginImport(); 303 iimport.setModel(this); 304 iimport.setParent(getPluginBase()); 305 return iimport; 306 } 307 308 public IPluginImport createImport(String pluginId) { 309 PluginImport iimport = new PluginImport(this, pluginId); 310 iimport.setParent(getPluginBase()); 311 return iimport; 312 } 313 314 public IPluginLibrary createLibrary() { 315 PluginLibrary library = new PluginLibrary(); 316 library.setModel(this); 317 library.setParent(getPluginBase()); 318 return library; 319 } 320 public IPluginAttribute createAttribute(IPluginElement element) { 321 if (fExtensionsModel != null) 322 return fExtensionsModel.getFactory().createAttribute(element); 323 return null; 324 } 325 public IPluginElement createElement(IPluginObject parent) { 326 if (fExtensionsModel != null) 327 return fExtensionsModel.getFactory().createElement(parent); 328 return null; 329 } 330 public IPluginExtension createExtension() { 331 if (fExtensionsModel != null) 332 return fExtensionsModel.getFactory().createExtension(); 333 return null; 334 } 335 public IPluginExtensionPoint createExtensionPoint() { 336 if (fExtensionsModel != null) 337 return fExtensionsModel.getFactory().createExtensionPoint(); 338 return null; 339 } 340 341 public boolean isBundleModel() { 342 return true; 343 } 344 349 public BundleDescription getBundleDescription() { 350 return fBundleDescription; 351 } 352 357 public void setBundleDescription(BundleDescription description) { 358 fBundleDescription = description; 359 } 360 365 public boolean isDirty() { 366 if (fBundleModel != null && (fBundleModel instanceof IEditable) 367 && ((IEditable) fBundleModel).isDirty()) 368 return true; 369 if (fExtensionsModel != null && (fExtensionsModel instanceof IEditable) 370 && ((IEditable) fExtensionsModel).isDirty()) 371 return true; 372 return false; 373 } 374 379 public void save(PrintWriter writer) { 380 } 382 387 public void setDirty(boolean dirty) { 388 } 390 391 public String toString() { 392 IPluginBase base = getPluginBase(); 393 if (base != null) 394 return base.getId(); 395 return super.toString(); 396 } 397 } 398 | Popular Tags |