KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > PersistablePluginObject


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc FACTORY_ID = "org.eclipse.pde.ui.elementFactory"; //$NON-NLS-1$
32
public static final String JavaDoc KEY = "org.eclipse.pde.workingSetKey"; //$NON-NLS-1$
33
private static PluginContainmentAdapter fgContainmentAdapter;
34     
35     private String JavaDoc fPluginID;
36
37     public PersistablePluginObject() {
38     }
39     
40     public PersistablePluginObject(String JavaDoc pluginID) {
41         fPluginID = pluginID;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.IPersistableElement#getFactoryId()
46      */

47     public String JavaDoc getFactoryId() {
48         return FACTORY_ID;
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.ui.IPersistableElement#saveState(org.eclipse.ui.IMemento)
53      */

54     public void saveState(IMemento memento) {
55         memento.putString(KEY, fPluginID);
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
60      */

61     public IAdaptable createElement(IMemento memento) {
62         return new PersistablePluginObject(memento.getString(KEY));
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
67      */

68     public Object JavaDoc getAdapter(Class JavaDoc 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 JavaDoc 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 JavaDoc arg0) {
106         if (arg0 instanceof PersistablePluginObject) {
107             String JavaDoc 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