KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > model > JavaModelProvider


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.jdt.internal.ui.model;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.Platform;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ModelProvider;
20 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.core.resources.mapping.ResourceMappingContext;
22
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.JavaCore;
25
26 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
27
28 /**
29  * Java-aware model provider.
30  *
31  * @since 3.2
32  */

33 public final class JavaModelProvider extends ModelProvider {
34
35     /** The model provider id */
36     public static final String JavaDoc JAVA_MODEL_PROVIDER_ID= "org.eclipse.jdt.ui.modelProvider"; //$NON-NLS-1$
37

38     /**
39      * Returns the resource associated with the corresponding model element.
40      *
41      * @param element
42      * the model element
43      * @return the associated resource, or <code>null</code>
44      */

45     public static IResource getResource(final Object JavaDoc element) {
46         IResource resource= null;
47         if (element instanceof IJavaElement) {
48             resource= ((IJavaElement) element).getResource();
49         } else if (element instanceof IResource) {
50             resource= (IResource) element;
51         } else if (element instanceof IAdaptable) {
52             final IAdaptable adaptable= (IAdaptable) element;
53             final Object JavaDoc adapted= adaptable.getAdapter(IResource.class);
54             if (adapted instanceof IResource)
55                 resource= (IResource) adapted;
56         } else {
57             final Object JavaDoc adapted= Platform.getAdapterManager().getAdapter(element, IResource.class);
58             if (adapted instanceof IResource)
59                 resource= (IResource) adapted;
60         }
61         return resource;
62     }
63
64     /**
65      * Creates a new java model provider.
66      */

67     public JavaModelProvider() {
68         // Used by the runtime
69
}
70
71     /**
72      * {@inheritDoc}
73      */

74     public ResourceMapping[] getMappings(final IResource resource, final ResourceMappingContext context, final IProgressMonitor monitor) throws CoreException {
75         final IJavaElement element= JavaCore.create(resource);
76         if (element != null)
77             return new ResourceMapping[] { JavaElementResourceMapping.create(element)};
78         final Object JavaDoc adapted= resource.getAdapter(ResourceMapping.class);
79         if (adapted instanceof ResourceMapping)
80             return new ResourceMapping[] { ((ResourceMapping) adapted)};
81         return new ResourceMapping[] { new JavaResourceMapping(resource)};
82     }
83 }
84
Popular Tags