KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import org.eclipse.core.internal.resources.mapping.ResourceMapping;
19 import org.eclipse.core.internal.resources.mapping.ResourceMappingContext;
20 import org.eclipse.core.internal.resources.mapping.ResourceTraversal;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26
27 /**
28  * A resource mapping that contains child mappings which are used to obtain the traversals
29  */

30 public class CompositeResourceMapping extends ResourceMapping {
31
32     private final Object JavaDoc modelObject;
33     private final ResourceMapping[] mappings;
34     private final IProject[] projects;
35
36     /**
37      * Create a composite mapping that obtains it's traversals from a set of sub-mappings.
38      * @param modelObject the model object for this mapping
39      * @param mappings the sub-mappings from which the traversals are obtained
40      */

41     public CompositeResourceMapping(Object JavaDoc modelObject, ResourceMapping[] mappings) {
42         this.modelObject = modelObject;
43         this.mappings = mappings;
44         this.projects = getProjects(mappings);
45     }
46
47     /*
48      * Return an array of all the projects of the given mappings.
49      */

50     private IProject[] getProjects(ResourceMapping[] mappings) {
51         Set JavaDoc result = new HashSet JavaDoc();
52         for (int i = 0; i < mappings.length; i++) {
53             ResourceMapping mapping = mappings[i];
54             result.addAll(Arrays.asList(mapping.getProjects()));
55         }
56         return (IProject[]) result.toArray(new IProject[result.size()]);
57     }
58
59     /* (non-Javadoc)
60      * @see org.eclipse.core.internal.resources.mapping.ResourceMapping#getModelObject()
61      */

62     public Object JavaDoc getModelObject() {
63         return modelObject;
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.core.internal.resources.mapping.ResourceMapping#getProjects()
68      */

69     public IProject[] getProjects() {
70         return projects;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.core.internal.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.internal.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor)
75      */

76     public ResourceTraversal[] getTraversals(ResourceMappingContext context,
77             IProgressMonitor monitor) throws CoreException {
78         if (monitor == null)
79             monitor = new NullProgressMonitor();
80         try {
81             monitor.beginTask(null, 100 * mappings.length);
82             List JavaDoc result = new ArrayList JavaDoc();
83             for (int i = 0; i < mappings.length; i++) {
84                 ResourceMapping mapping = mappings[i];
85                 result.addAll(Arrays.asList(mapping.getTraversals(context, new SubProgressMonitor(monitor, 100))));
86             }
87             return (ResourceTraversal[]) result.toArray(new ResourceTraversal[result.size()]);
88         } finally {
89             monitor.done();
90         }
91     }
92
93 }
94
Popular Tags