KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > model > WorkbenchAdapterFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal.ide.model;
12
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspace;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.runtime.IAdapterFactory;
19 import org.eclipse.ui.IActionFilter;
20 import org.eclipse.ui.IElementFactory;
21 import org.eclipse.ui.IPersistableElement;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.model.IWorkbenchAdapter;
24
25 /**
26  * Dispenses adapters for various core objects.
27  * Returns IWorkbenchAdapter adapters, used for displaying,
28  * navigating, and populating menus for core objects.
29  */

30 class WorkbenchAdapterFactory implements IAdapterFactory {
31     private Object JavaDoc workspaceAdapter = new WorkbenchWorkspace();
32
33     private Object JavaDoc rootAdapter = new WorkbenchRootResource();
34
35     private Object JavaDoc projectAdapter = new WorkbenchProject();
36
37     private Object JavaDoc folderAdapter = new WorkbenchFolder();
38
39     private Object JavaDoc fileAdapter = new WorkbenchFile();
40
41     private Object JavaDoc markerAdapter = new WorkbenchMarker();
42
43     private Object JavaDoc resourceFactory = new ResourceFactory();
44
45     private Object JavaDoc workspaceFactory = new WorkspaceFactory();
46
47     /**
48      * Returns the IActionFilter for an object.
49      */

50     protected Object JavaDoc getActionFilter(Object JavaDoc o) {
51         if (o instanceof IResource) {
52             switch (((IResource) o).getType()) {
53             case IResource.FILE:
54                 return fileAdapter;
55             case IResource.FOLDER:
56                 return folderAdapter;
57             case IResource.PROJECT:
58                 return projectAdapter;
59             }
60         }
61         if (o instanceof IMarker) {
62             return markerAdapter;
63         }
64         return null;
65     }
66
67     /**
68      * Returns an object which is an instance of the given class
69      * associated with the given object. Returns <code>null</code> if
70      * no such object can be found.
71      *
72      * @param o the adaptable object being queried
73      * (usually an instance of <code>IAdaptable</code>)
74      * @param adapterType the type of adapter to look up
75      * @return a object castable to the given adapter type,
76      * or <code>null</code> if this adapter provider
77      * does not have an adapter of the given type for the
78      * given object
79      */

80     public Object JavaDoc getAdapter(Object JavaDoc o, Class JavaDoc adapterType) {
81         if (adapterType.isInstance(o)) {
82             return o;
83         }
84         if (adapterType == IWorkbenchAdapter.class) {
85             return getWorkbenchElement(o);
86         }
87         if (adapterType == IPersistableElement.class) {
88             return getPersistableElement(o);
89         }
90         if (adapterType == IElementFactory.class) {
91             return getElementFactory(o);
92         }
93         if (adapterType == IActionFilter.class) {
94             return getActionFilter(o);
95         }
96         if (adapterType == IUndoContext.class) {
97             return getUndoContext(o);
98         }
99         return null;
100     }
101
102     /**
103      * Returns the collection of adapter types handled by this
104      * provider.
105      * <p>
106      * This method is generally used by an adapter manager
107      * to discover which adapter types are supported, in advance
108      * of dispatching any actual <code>getAdapter</code> requests.
109      * </p>
110      *
111      * @return the collection of adapter types
112      */

113     public Class JavaDoc[] getAdapterList() {
114         return new Class JavaDoc[] { IWorkbenchAdapter.class, IElementFactory.class,
115                 IPersistableElement.class, IActionFilter.class, IUndoContext.class };
116     }
117
118     /**
119      * Returns an object which is an instance of IElementFactory
120      * associated with the given object. Returns <code>null</code> if
121      * no such object can be found.
122      */

123     protected Object JavaDoc getElementFactory(Object JavaDoc o) {
124         if (o instanceof IResource) {
125             return resourceFactory;
126         }
127         if (o instanceof IWorkspace) {
128             return workspaceFactory;
129         }
130         return null;
131     }
132
133     /**
134      * Returns an object which is an instance of IPersistableElement
135      * associated with the given object. Returns <code>null</code> if
136      * no such object can be found.
137      */

138     protected Object JavaDoc getPersistableElement(Object JavaDoc o) {
139         if (o instanceof IResource) {
140             return new ResourceFactory((IResource) o);
141         }
142         if (o instanceof IWorkspace) {
143             return workspaceFactory;
144         }
145         return null;
146     }
147
148     /**
149      * Returns an object which is an instance of IWorkbenchAdapter
150      * associated with the given object. Returns <code>null</code> if
151      * no such object can be found.
152      */

153     protected Object JavaDoc getWorkbenchElement(Object JavaDoc o) {
154         if (o instanceof IResource) {
155             switch (((IResource) o).getType()) {
156             case IResource.FILE:
157                 return fileAdapter;
158             case IResource.FOLDER:
159                 return folderAdapter;
160             case IResource.PROJECT:
161                 return projectAdapter;
162             }
163         }
164         if (o instanceof IWorkspaceRoot) {
165             return rootAdapter;
166         }
167         if (o instanceof IWorkspace) {
168             return workspaceAdapter;
169         }
170         if (o instanceof IMarker) {
171             return markerAdapter;
172         }
173         return null;
174     }
175     
176     /**
177      * Returns the IUndoContext for an object.
178      */

179     protected Object JavaDoc getUndoContext(Object JavaDoc o) {
180         if (o instanceof IWorkspace) {
181             return PlatformUI.getWorkbench().getOperationSupport().getUndoContext();
182         }
183         return null;
184     }
185 }
186
Popular Tags