001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.internal.ide.model;
011:
012: import org.eclipse.core.commands.operations.IUndoContext;
013: import org.eclipse.core.resources.IMarker;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.resources.IWorkspace;
016: import org.eclipse.core.resources.IWorkspaceRoot;
017: import org.eclipse.core.runtime.IAdapterFactory;
018: import org.eclipse.ui.IActionFilter;
019: import org.eclipse.ui.IElementFactory;
020: import org.eclipse.ui.IPersistableElement;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.model.IWorkbenchAdapter;
023:
024: /**
025: * Dispenses adapters for various core objects.
026: * Returns IWorkbenchAdapter adapters, used for displaying,
027: * navigating, and populating menus for core objects.
028: */
029: class WorkbenchAdapterFactory implements IAdapterFactory {
030: private Object workspaceAdapter = new WorkbenchWorkspace();
031:
032: private Object rootAdapter = new WorkbenchRootResource();
033:
034: private Object projectAdapter = new WorkbenchProject();
035:
036: private Object folderAdapter = new WorkbenchFolder();
037:
038: private Object fileAdapter = new WorkbenchFile();
039:
040: private Object markerAdapter = new WorkbenchMarker();
041:
042: private Object resourceFactory = new ResourceFactory();
043:
044: private Object workspaceFactory = new WorkspaceFactory();
045:
046: /**
047: * Returns the IActionFilter for an object.
048: */
049: protected Object getActionFilter(Object o) {
050: if (o instanceof IResource) {
051: switch (((IResource) o).getType()) {
052: case IResource.FILE:
053: return fileAdapter;
054: case IResource.FOLDER:
055: return folderAdapter;
056: case IResource.PROJECT:
057: return projectAdapter;
058: }
059: }
060: if (o instanceof IMarker) {
061: return markerAdapter;
062: }
063: return null;
064: }
065:
066: /**
067: * Returns an object which is an instance of the given class
068: * associated with the given object. Returns <code>null</code> if
069: * no such object can be found.
070: *
071: * @param o the adaptable object being queried
072: * (usually an instance of <code>IAdaptable</code>)
073: * @param adapterType the type of adapter to look up
074: * @return a object castable to the given adapter type,
075: * or <code>null</code> if this adapter provider
076: * does not have an adapter of the given type for the
077: * given object
078: */
079: public Object getAdapter(Object o, Class adapterType) {
080: if (adapterType.isInstance(o)) {
081: return o;
082: }
083: if (adapterType == IWorkbenchAdapter.class) {
084: return getWorkbenchElement(o);
085: }
086: if (adapterType == IPersistableElement.class) {
087: return getPersistableElement(o);
088: }
089: if (adapterType == IElementFactory.class) {
090: return getElementFactory(o);
091: }
092: if (adapterType == IActionFilter.class) {
093: return getActionFilter(o);
094: }
095: if (adapterType == IUndoContext.class) {
096: return getUndoContext(o);
097: }
098: return null;
099: }
100:
101: /**
102: * Returns the collection of adapter types handled by this
103: * provider.
104: * <p>
105: * This method is generally used by an adapter manager
106: * to discover which adapter types are supported, in advance
107: * of dispatching any actual <code>getAdapter</code> requests.
108: * </p>
109: *
110: * @return the collection of adapter types
111: */
112: public Class[] getAdapterList() {
113: return new Class[] { IWorkbenchAdapter.class,
114: IElementFactory.class, IPersistableElement.class,
115: 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 getElementFactory(Object 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 getPersistableElement(Object 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 getWorkbenchElement(Object 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 getUndoContext(Object o) {
180: if (o instanceof IWorkspace) {
181: return PlatformUI.getWorkbench().getOperationSupport()
182: .getUndoContext();
183: }
184: return null;
185: }
186: }
|