KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.ResourceMapping;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IAdapterFactory;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.IContributorResourceAdapter;
23 import org.eclipse.ui.IWorkingSet;
24 import org.eclipse.ui.ide.IContributorResourceAdapter2;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
26
27 /**
28  * Adapter factory which provides a ResourceMapping for a working set
29  */

30 public class WorkingSetAdapterFactory implements IAdapterFactory {
31
32     /*
33      * Adapter for converting a working set to a resource mapping for use by
34      * object contributions.
35      */

36     class ContributorResourceAdapter implements IContributorResourceAdapter2 {
37
38         public ResourceMapping getAdaptedResourceMapping(IAdaptable adaptable) {
39             if (adaptable instanceof IWorkingSet) {
40                 IWorkingSet workingSet = (IWorkingSet) adaptable;
41                 IAdaptable[] elements = workingSet.getElements();
42                 List JavaDoc result = new ArrayList JavaDoc();
43                 for (int i = 0; i < elements.length; i++) {
44                     IAdaptable element = elements[i];
45                     ResourceMapping mapping = getContributedResourceMapping(element);
46                     if (mapping == null) {
47                         mapping = getResourceMapping(element);
48                     }
49                     if (mapping != null) {
50                         result.add(mapping);
51                     }
52                 }
53                 if (!result.isEmpty()) {
54                     return new WorkingSetResourceMapping(workingSet);
55                 }
56             }
57             return null;
58         }
59
60         public IResource getAdaptedResource(IAdaptable adaptable) {
61             // Working sets don't adapt to IResource
62
return null;
63         }
64
65     }
66
67     class WorkbenchAdapter implements IWorkbenchAdapter {
68
69         public Object JavaDoc[] getChildren(Object JavaDoc o) {
70             if (o instanceof IWorkingSet) {
71                 IWorkingSet set = (IWorkingSet) o;
72                 return set.getElements();
73             }
74             return null;
75         }
76
77         public ImageDescriptor getImageDescriptor(Object JavaDoc o) {
78             if (o instanceof IWorkingSet) {
79                 IWorkingSet set = (IWorkingSet) o;
80                 return set.getImageDescriptor();
81             }
82             return null;
83         }
84
85         public String JavaDoc getLabel(Object JavaDoc o) {
86             if (o instanceof IWorkingSet) {
87                 IWorkingSet set = (IWorkingSet) o;
88                 return set.getLabel();
89             }
90             return null;
91         }
92
93         public Object JavaDoc getParent(Object JavaDoc o) {
94             return null;
95         }
96
97     }
98
99     private IContributorResourceAdapter2 contributorResourceAdapter = new ContributorResourceAdapter();
100
101     private IWorkbenchAdapter workbenchAdapter = new WorkbenchAdapter();
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object,
107      * java.lang.Class)
108      */

109     public Object JavaDoc getAdapter(Object JavaDoc adaptableObject, Class JavaDoc adapterType) {
110         if (adaptableObject instanceof IWorkingSet) {
111             if (adapterType == IContributorResourceAdapter.class) {
112                 return contributorResourceAdapter;
113             }
114             if (adapterType == IWorkbenchAdapter.class) {
115                 return workbenchAdapter;
116             }
117             if (adapterType == ResourceMapping.class) {
118                 IWorkingSet workingSet = (IWorkingSet) adaptableObject;
119                 IAdaptable[] elements = workingSet.getElements();
120                 List JavaDoc result = new ArrayList JavaDoc();
121                 for (int i = 0; i < elements.length; i++) {
122                     IAdaptable element = elements[i];
123                     ResourceMapping mapping = getResourceMapping(element);
124                     if (mapping != null) {
125                         result.add(mapping);
126                     }
127                 }
128                 if (!result.isEmpty()) {
129                     return new WorkingSetResourceMapping(workingSet);
130                 }
131             }
132         }
133         return null;
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
140      */

141     public Class JavaDoc[] getAdapterList() {
142         return new Class JavaDoc[] { IContributorResourceAdapter2.class,
143                 IWorkbenchAdapter.class, ResourceMapping.class };
144     }
145
146     static ResourceMapping getResourceMapping(Object JavaDoc o) {
147         // First, ask the object directly for a resource mapping
148
Object JavaDoc mapping = internalGetAdapter(o, ResourceMapping.class);
149         if (mapping instanceof ResourceMapping) {
150             return (ResourceMapping) mapping;
151         }
152         // If this fails, ask for a resource and convert to a resource mapping
153
Object JavaDoc resource = internalGetAdapter(o, IResource.class);
154         if (resource != null) {
155             mapping = internalGetAdapter(resource, ResourceMapping.class);
156             if (mapping instanceof ResourceMapping) {
157                 return (ResourceMapping) mapping;
158             }
159         }
160         return null;
161     }
162
163     static ResourceMapping getContributedResourceMapping(
164             IAdaptable element) {
165         Object JavaDoc resourceAdapter = internalGetAdapter(element,
166                 IContributorResourceAdapter.class);
167         if (resourceAdapter != null) {
168             if (resourceAdapter instanceof IContributorResourceAdapter2) {
169                 // First, use the mapping contributor adapter to get the mapping
170
IContributorResourceAdapter2 mappingAdapter = (IContributorResourceAdapter2) resourceAdapter;
171                 ResourceMapping mapping = mappingAdapter
172                         .getAdaptedResourceMapping(element);
173                 if (mapping != null) {
174                     return mapping;
175                 }
176             }
177             if (resourceAdapter instanceof IContributorResourceAdapter) {
178                 // Next, use the resource adapter to get a resource and then get
179
// the mapping for that resource
180
IResource resource = ((IContributorResourceAdapter) resourceAdapter)
181                         .getAdaptedResource(element);
182                 if (resource != null) {
183                     Object JavaDoc mapping = internalGetAdapter(resource,
184                             ResourceMapping.class);
185                     if (mapping instanceof ResourceMapping) {
186                         return (ResourceMapping) mapping;
187                     }
188                 }
189             }
190         }
191         return null;
192     }
193
194     static Object JavaDoc internalGetAdapter(Object JavaDoc o, Class JavaDoc adapter) {
195         if (o instanceof IAdaptable) {
196             IAdaptable element = (IAdaptable) o;
197             Object JavaDoc adapted = element.getAdapter(adapter);
198             if (adapted != null) {
199                 return adapted;
200             }
201         }
202         // Fallback to the adapter manager in case the object doesn't
203
// implement getAdapter or in the case where the implementation
204
// doesn't consult the manager
205
return Platform.getAdapterManager().getAdapter(o, adapter);
206     }
207
208 }
209
Popular Tags