001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.PlatformObject;
017: import org.eclipse.jdt.core.IJavaElement;
018: import org.eclipse.jdt.core.JavaCore;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.core.plugin.PluginRegistry;
021: import org.eclipse.ui.IContainmentAdapter;
022: import org.eclipse.ui.IElementFactory;
023: import org.eclipse.ui.IMemento;
024: import org.eclipse.ui.IPersistableElement;
025:
026: public class PersistablePluginObject extends PlatformObject implements
027: IPersistableElement, IElementFactory {
028:
029: public static final String FACTORY_ID = "org.eclipse.pde.ui.elementFactory"; //$NON-NLS-1$
030: public static final String KEY = "org.eclipse.pde.workingSetKey"; //$NON-NLS-1$
031: private static PluginContainmentAdapter fgContainmentAdapter;
032:
033: private String fPluginID;
034:
035: public PersistablePluginObject() {
036: }
037:
038: public PersistablePluginObject(String pluginID) {
039: fPluginID = pluginID;
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.ui.IPersistableElement#getFactoryId()
044: */
045: public String getFactoryId() {
046: return FACTORY_ID;
047: }
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.ui.IPersistableElement#saveState(org.eclipse.ui.IMemento)
051: */
052: public void saveState(IMemento memento) {
053: memento.putString(KEY, fPluginID);
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
058: */
059: public IAdaptable createElement(IMemento memento) {
060: return new PersistablePluginObject(memento.getString(KEY));
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
065: */
066: public Object getAdapter(Class adapter) {
067: if (adapter.equals(IPersistableElement.class))
068: return this ;
069: if (adapter.equals(IResource.class))
070: return getResource();
071: if (adapter.equals(IContainmentAdapter.class))
072: return getPluginContainmentAdapter();
073: if (adapter.equals(IJavaElement.class)) {
074: IResource res = getResource();
075: if (res instanceof IProject) {
076: IProject project = (IProject) res;
077: try {
078: if (project.hasNature(JavaCore.NATURE_ID))
079: return JavaCore.create(project);
080: } catch (CoreException e) {
081: }
082: }
083: }
084: return super .getAdapter(adapter);
085: }
086:
087: public IResource getResource() {
088: IPluginModelBase model = PluginRegistry.findModel(fPluginID);
089: IResource resource = (model != null) ? model
090: .getUnderlyingResource() : null;
091: return resource == null ? null : resource.getProject();
092: }
093:
094: public String getPluginID() {
095: return fPluginID;
096: }
097:
098: private static IContainmentAdapter getPluginContainmentAdapter() {
099: if (fgContainmentAdapter == null)
100: fgContainmentAdapter = new PluginContainmentAdapter();
101: return fgContainmentAdapter;
102: }
103:
104: public boolean equals(Object arg0) {
105: if (arg0 instanceof PersistablePluginObject) {
106: String id = ((PersistablePluginObject) arg0).fPluginID;
107: return (fPluginID != null) ? fPluginID.equals(id)
108: : id == null;
109: }
110: return false;
111: }
112:
113: public int hashCode() {
114: return fPluginID.hashCode();
115: }
116:
117: }
|