1 11 package org.eclipse.pde.internal.ui.util; 12 13 import org.eclipse.core.resources.IProject; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.PlatformObject; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.JavaCore; 20 import org.eclipse.pde.core.plugin.IPluginModelBase; 21 import org.eclipse.pde.core.plugin.PluginRegistry; 22 import org.eclipse.ui.IContainmentAdapter; 23 import org.eclipse.ui.IElementFactory; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.IPersistableElement; 26 27 28 public class PersistablePluginObject extends PlatformObject implements 29 IPersistableElement, IElementFactory { 30 31 public static final String FACTORY_ID = "org.eclipse.pde.ui.elementFactory"; public static final String KEY = "org.eclipse.pde.workingSetKey"; private static PluginContainmentAdapter fgContainmentAdapter; 34 35 private String fPluginID; 36 37 public PersistablePluginObject() { 38 } 39 40 public PersistablePluginObject(String pluginID) { 41 fPluginID = pluginID; 42 } 43 44 47 public String getFactoryId() { 48 return FACTORY_ID; 49 } 50 51 54 public void saveState(IMemento memento) { 55 memento.putString(KEY, fPluginID); 56 } 57 58 61 public IAdaptable createElement(IMemento memento) { 62 return new PersistablePluginObject(memento.getString(KEY)); 63 } 64 65 68 public Object getAdapter(Class adapter) { 69 if (adapter.equals(IPersistableElement.class)) 70 return this; 71 if (adapter.equals(IResource.class)) 72 return getResource(); 73 if (adapter.equals(IContainmentAdapter.class)) 74 return getPluginContainmentAdapter(); 75 if (adapter.equals(IJavaElement.class)) { 76 IResource res = getResource(); 77 if (res instanceof IProject) { 78 IProject project = (IProject) res; 79 try { 80 if (project.hasNature(JavaCore.NATURE_ID)) 81 return JavaCore.create(project); 82 } catch (CoreException e) { 83 } 84 } 85 } 86 return super.getAdapter(adapter); 87 } 88 89 public IResource getResource() { 90 IPluginModelBase model = PluginRegistry.findModel(fPluginID); 91 IResource resource = (model != null) ? model.getUnderlyingResource() : null; 92 return resource == null ? null : resource.getProject(); 93 } 94 95 public String getPluginID() { 96 return fPluginID; 97 } 98 99 private static IContainmentAdapter getPluginContainmentAdapter() { 100 if (fgContainmentAdapter == null) 101 fgContainmentAdapter = new PluginContainmentAdapter(); 102 return fgContainmentAdapter; 103 } 104 105 public boolean equals(Object arg0) { 106 if (arg0 instanceof PersistablePluginObject) { 107 String id = ((PersistablePluginObject)arg0).fPluginID; 108 return (fPluginID != null) ? fPluginID.equals(id) : id == null; 109 } 110 return false; 111 } 112 113 public int hashCode() { 114 return fPluginID.hashCode(); 115 } 116 117 } 118 | Popular Tags |