001: /*******************************************************************************
002: * Copyright (c) 2005, 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 java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.mapping.ResourceMapping;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.core.runtime.IAdapterFactory;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.jface.resource.ImageDescriptor;
021: import org.eclipse.ui.IContributorResourceAdapter;
022: import org.eclipse.ui.IWorkingSet;
023: import org.eclipse.ui.ide.IContributorResourceAdapter2;
024: import org.eclipse.ui.model.IWorkbenchAdapter;
025:
026: /**
027: * Adapter factory which provides a ResourceMapping for a working set
028: */
029: public class WorkingSetAdapterFactory implements IAdapterFactory {
030:
031: /*
032: * Adapter for converting a working set to a resource mapping for use by
033: * object contributions.
034: */
035: class ContributorResourceAdapter implements
036: IContributorResourceAdapter2 {
037:
038: public ResourceMapping getAdaptedResourceMapping(
039: IAdaptable adaptable) {
040: if (adaptable instanceof IWorkingSet) {
041: IWorkingSet workingSet = (IWorkingSet) adaptable;
042: IAdaptable[] elements = workingSet.getElements();
043: List result = new ArrayList();
044: for (int i = 0; i < elements.length; i++) {
045: IAdaptable element = elements[i];
046: ResourceMapping mapping = getContributedResourceMapping(element);
047: if (mapping == null) {
048: mapping = getResourceMapping(element);
049: }
050: if (mapping != null) {
051: result.add(mapping);
052: }
053: }
054: if (!result.isEmpty()) {
055: return new WorkingSetResourceMapping(workingSet);
056: }
057: }
058: return null;
059: }
060:
061: public IResource getAdaptedResource(IAdaptable adaptable) {
062: // Working sets don't adapt to IResource
063: return null;
064: }
065:
066: }
067:
068: class WorkbenchAdapter implements IWorkbenchAdapter {
069:
070: public Object[] getChildren(Object o) {
071: if (o instanceof IWorkingSet) {
072: IWorkingSet set = (IWorkingSet) o;
073: return set.getElements();
074: }
075: return null;
076: }
077:
078: public ImageDescriptor getImageDescriptor(Object o) {
079: if (o instanceof IWorkingSet) {
080: IWorkingSet set = (IWorkingSet) o;
081: return set.getImageDescriptor();
082: }
083: return null;
084: }
085:
086: public String getLabel(Object o) {
087: if (o instanceof IWorkingSet) {
088: IWorkingSet set = (IWorkingSet) o;
089: return set.getLabel();
090: }
091: return null;
092: }
093:
094: public Object getParent(Object o) {
095: return null;
096: }
097:
098: }
099:
100: private IContributorResourceAdapter2 contributorResourceAdapter = new ContributorResourceAdapter();
101:
102: private IWorkbenchAdapter workbenchAdapter = new WorkbenchAdapter();
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object,
108: * java.lang.Class)
109: */
110: public Object getAdapter(Object adaptableObject, Class adapterType) {
111: if (adaptableObject instanceof IWorkingSet) {
112: if (adapterType == IContributorResourceAdapter.class) {
113: return contributorResourceAdapter;
114: }
115: if (adapterType == IWorkbenchAdapter.class) {
116: return workbenchAdapter;
117: }
118: if (adapterType == ResourceMapping.class) {
119: IWorkingSet workingSet = (IWorkingSet) adaptableObject;
120: IAdaptable[] elements = workingSet.getElements();
121: List result = new ArrayList();
122: for (int i = 0; i < elements.length; i++) {
123: IAdaptable element = elements[i];
124: ResourceMapping mapping = getResourceMapping(element);
125: if (mapping != null) {
126: result.add(mapping);
127: }
128: }
129: if (!result.isEmpty()) {
130: return new WorkingSetResourceMapping(workingSet);
131: }
132: }
133: }
134: return null;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
141: */
142: public Class[] getAdapterList() {
143: return new Class[] { IContributorResourceAdapter2.class,
144: IWorkbenchAdapter.class, ResourceMapping.class };
145: }
146:
147: static ResourceMapping getResourceMapping(Object o) {
148: // First, ask the object directly for a resource mapping
149: Object mapping = internalGetAdapter(o, ResourceMapping.class);
150: if (mapping instanceof ResourceMapping) {
151: return (ResourceMapping) mapping;
152: }
153: // If this fails, ask for a resource and convert to a resource mapping
154: Object resource = internalGetAdapter(o, IResource.class);
155: if (resource != null) {
156: mapping = internalGetAdapter(resource,
157: ResourceMapping.class);
158: if (mapping instanceof ResourceMapping) {
159: return (ResourceMapping) mapping;
160: }
161: }
162: return null;
163: }
164:
165: static ResourceMapping getContributedResourceMapping(
166: IAdaptable element) {
167: Object resourceAdapter = internalGetAdapter(element,
168: IContributorResourceAdapter.class);
169: if (resourceAdapter != null) {
170: if (resourceAdapter instanceof IContributorResourceAdapter2) {
171: // First, use the mapping contributor adapter to get the mapping
172: IContributorResourceAdapter2 mappingAdapter = (IContributorResourceAdapter2) resourceAdapter;
173: ResourceMapping mapping = mappingAdapter
174: .getAdaptedResourceMapping(element);
175: if (mapping != null) {
176: return mapping;
177: }
178: }
179: if (resourceAdapter instanceof IContributorResourceAdapter) {
180: // Next, use the resource adapter to get a resource and then get
181: // the mapping for that resource
182: IResource resource = ((IContributorResourceAdapter) resourceAdapter)
183: .getAdaptedResource(element);
184: if (resource != null) {
185: Object mapping = internalGetAdapter(resource,
186: ResourceMapping.class);
187: if (mapping instanceof ResourceMapping) {
188: return (ResourceMapping) mapping;
189: }
190: }
191: }
192: }
193: return null;
194: }
195:
196: static Object internalGetAdapter(Object o, Class adapter) {
197: if (o instanceof IAdaptable) {
198: IAdaptable element = (IAdaptable) o;
199: Object adapted = element.getAdapter(adapter);
200: if (adapted != null) {
201: return adapted;
202: }
203: }
204: // Fallback to the adapter manager in case the object doesn't
205: // implement getAdapter or in the case where the implementation
206: // doesn't consult the manager
207: return Platform.getAdapterManager().getAdapter(o, adapter);
208: }
209:
210: }
|