| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 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 | |
| 12 | package org.eclipse.ui.internal.ide.model; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Arrays; |
| 16 | import java.util.HashSet; |
| 17 | import java.util.List; |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.eclipse.core.resources.IProject; |
| 21 | import org.eclipse.core.resources.mapping.ModelProvider; |
| 22 | import org.eclipse.core.resources.mapping.ResourceMapping; |
| 23 | import org.eclipse.core.resources.mapping.ResourceMappingContext; |
| 24 | import org.eclipse.core.resources.mapping.ResourceTraversal; |
| 25 | import org.eclipse.core.runtime.CoreException; |
| 26 | import org.eclipse.core.runtime.IAdaptable; |
| 27 | import org.eclipse.core.runtime.IProgressMonitor; |
| 28 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 29 | import org.eclipse.core.runtime.SubProgressMonitor; |
| 30 | import org.eclipse.ui.IWorkingSet; |
| 31 | |
| 32 | /** |
| 33 | * A resource mapping for working sets |
| 34 | */ |
| 35 | public class WorkingSetResourceMapping extends ResourceMapping { |
| 36 | |
| 37 | private IWorkingSet set; |
| 38 | |
| 39 | /** |
| 40 | * Create the resource mapping |
| 41 | * @param workingSet the working set |
| 42 | */ |
| 43 | public WorkingSetResourceMapping(IWorkingSet workingSet) { |
| 44 | set = workingSet; |
| 45 | } |
| 46 | |
| 47 | /* (non-Javadoc) |
| 48 | * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelObject() |
| 49 | */ |
| 50 | public Object getModelObject() { |
| 51 | return set; |
| 52 | } |
| 53 | |
| 54 | /* (non-Javadoc) |
| 55 | * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelProviderId() |
| 56 | */ |
| 57 | public String getModelProviderId() { |
| 58 | return ModelProvider.RESOURCE_MODEL_PROVIDER_ID; |
| 59 | } |
| 60 | |
| 61 | /* (non-Javadoc) |
| 62 | * @see org.eclipse.core.resources.mapping.ResourceMapping#getProjects() |
| 63 | */ |
| 64 | public IProject[] getProjects() { |
| 65 | Set result = new HashSet(); |
| 66 | ResourceMapping[] mappings = getMappings(); |
| 67 | for (int i = 0; i < mappings.length; i++) { |
| 68 | ResourceMapping mapping = mappings[i]; |
| 69 | IProject[] projects = mapping.getProjects(); |
| 70 | for (int j = 0; j < projects.length; j++) { |
| 71 | IProject project = projects[j]; |
| 72 | result.add(project); |
| 73 | } |
| 74 | } |
| 75 | return (IProject[]) result.toArray(new IProject[result.size()]); |
| 76 | } |
| 77 | |
| 78 | /* (non-Javadoc) |
| 79 | * @see org.eclipse.core.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor) |
| 80 | */ |
| 81 | public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { |
| 82 | if (monitor == null) |
| 83 | monitor = new NullProgressMonitor(); |
| 84 | try { |
| 85 | ResourceMapping[] mappings = getMappings(); |
| 86 | monitor.beginTask("", 100 * mappings.length); //$NON-NLS-1$ |
| 87 | List result = new ArrayList(); |
| 88 | for (int i = 0; i < mappings.length; i++) { |
| 89 | ResourceMapping mapping = mappings[i]; |
| 90 | result.addAll(Arrays.asList(mapping.getTraversals(context, new SubProgressMonitor(monitor, 100)))); |
| 91 | } |
| 92 | return (ResourceTraversal[]) result.toArray(new ResourceTraversal[result.size()]); |
| 93 | } finally { |
| 94 | monitor.done(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Return the mappings contained in the set. |
| 100 | * @return the mappings contained in the set |
| 101 | */ |
| 102 | private ResourceMapping[] getMappings() { |
| 103 | IAdaptable[] elements = set.getElements(); |
| 104 | List result = new ArrayList(); |
| 105 | for (int i = 0; i < elements.length; i++) { |
| 106 | IAdaptable element = elements[i]; |
| 107 | ResourceMapping mapping = WorkingSetAdapterFactory.getContributedResourceMapping(element); |
| 108 | if (mapping == null) { |
| 109 | mapping = WorkingSetAdapterFactory.getResourceMapping(element); |
| 110 | } |
| 111 | if (mapping != null) { |
| 112 | result.add(mapping); |
| 113 | } |
| 114 | } |
| 115 | return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]); |
| 116 | } |
| 117 | |
| 118 | /* (non-Javadoc) |
| 119 | * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping) |
| 120 | */ |
| 121 | public boolean contains(ResourceMapping mapping) { |
| 122 | ResourceMapping[] mappings = getMappings(); |
| 123 | for (int i = 0; i < mappings.length; i++) { |
| 124 | ResourceMapping childMapping = mappings[i]; |
| 125 | if (childMapping.contains(mapping)) { |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | } |