KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > mapping > SimpleResourceMapping


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.core.internal.resources.mapping;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.resources.mapping.*;
15 import org.eclipse.core.runtime.IProgressMonitor;
16
17 /**
18  * A simple resource mapping for converting IResource to ResourceMapping.
19  * It uses the resource as the model object and traverses deeply.
20  *
21  * @since 3.1
22  */

23 public class SimpleResourceMapping extends ResourceMapping {
24     private final IResource resource;
25
26     public SimpleResourceMapping(IResource resource) {
27         this.resource = resource;
28     }
29
30     /* (non-Javadoc)
31      * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping)
32      */

33     public boolean contains(ResourceMapping mapping) {
34         if (mapping.getModelProviderId().equals(this.getModelProviderId())) {
35             Object JavaDoc object = mapping.getModelObject();
36             if (object instanceof IResource) {
37                 IResource other = (IResource) object;
38                 return resource.getFullPath().isPrefixOf(other.getFullPath());
39             }
40             if (object instanceof ShallowContainer) {
41                 ShallowContainer sc = (ShallowContainer) object;
42                 IResource other = sc.getResource();
43                 return resource.getFullPath().isPrefixOf(other.getFullPath());
44             }
45         }
46         return false;
47     }
48
49     /* (non-Javadoc)
50      * Method declared on ResourceMapping.
51      */

52     public Object JavaDoc getModelObject() {
53         return resource;
54     }
55
56     public String JavaDoc getModelProviderId() {
57         return ModelProvider.RESOURCE_MODEL_PROVIDER_ID;
58     }
59
60     /* (non-Javadoc)
61      * Method declared on ResourceMapping.
62      */

63     public IProject[] getProjects() {
64         if (resource.getType() == IResource.ROOT)
65             return ((IWorkspaceRoot)resource).getProjects();
66         return new IProject[] {resource.getProject()};
67     }
68     
69     /* (non-Javadoc)
70      * Method declared on ResourceMapping.
71      */

72     public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) {
73         if (resource.getType() == IResource.ROOT) {
74             return new ResourceTraversal[] {new ResourceTraversal(((IWorkspaceRoot)resource).getProjects(), IResource.DEPTH_INFINITE, IResource.NONE)};
75         }
76         return new ResourceTraversal[] {new ResourceTraversal(new IResource[] {resource}, IResource.DEPTH_INFINITE, IResource.NONE)};
77     }
78 }
79
Popular Tags