001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.ide.model;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.mapping.ModelProvider;
020: import org.eclipse.core.resources.mapping.ResourceMapping;
021: import org.eclipse.core.resources.mapping.ResourceMappingContext;
022: import org.eclipse.core.resources.mapping.ResourceTraversal;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IAdaptable;
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.NullProgressMonitor;
027: import org.eclipse.core.runtime.SubProgressMonitor;
028: import org.eclipse.ui.IWorkingSet;
029:
030: /**
031: * A resource mapping for working sets
032: */
033: public class WorkingSetResourceMapping extends ResourceMapping {
034:
035: private IWorkingSet set;
036:
037: /**
038: * Create the resource mapping
039: * @param workingSet the working set
040: */
041: public WorkingSetResourceMapping(IWorkingSet workingSet) {
042: set = workingSet;
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelObject()
047: */
048: public Object getModelObject() {
049: return set;
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelProviderId()
054: */
055: public String getModelProviderId() {
056: return ModelProvider.RESOURCE_MODEL_PROVIDER_ID;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.core.resources.mapping.ResourceMapping#getProjects()
061: */
062: public IProject[] getProjects() {
063: Set result = new HashSet();
064: ResourceMapping[] mappings = getMappings();
065: for (int i = 0; i < mappings.length; i++) {
066: ResourceMapping mapping = mappings[i];
067: IProject[] projects = mapping.getProjects();
068: for (int j = 0; j < projects.length; j++) {
069: IProject project = projects[j];
070: result.add(project);
071: }
072: }
073: return (IProject[]) result.toArray(new IProject[result.size()]);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.core.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor)
078: */
079: public ResourceTraversal[] getTraversals(
080: ResourceMappingContext context, IProgressMonitor monitor)
081: throws CoreException {
082: if (monitor == null)
083: monitor = new NullProgressMonitor();
084: try {
085: ResourceMapping[] mappings = getMappings();
086: monitor.beginTask("", 100 * mappings.length); //$NON-NLS-1$
087: List result = new ArrayList();
088: for (int i = 0; i < mappings.length; i++) {
089: ResourceMapping mapping = mappings[i];
090: result
091: .addAll(Arrays.asList(mapping.getTraversals(
092: context, new SubProgressMonitor(
093: monitor, 100))));
094: }
095: return (ResourceTraversal[]) result
096: .toArray(new ResourceTraversal[result.size()]);
097: } finally {
098: monitor.done();
099: }
100: }
101:
102: /**
103: * Return the mappings contained in the set.
104: * @return the mappings contained in the set
105: */
106: private ResourceMapping[] getMappings() {
107: IAdaptable[] elements = set.getElements();
108: List result = new ArrayList();
109: for (int i = 0; i < elements.length; i++) {
110: IAdaptable element = elements[i];
111: ResourceMapping mapping = WorkingSetAdapterFactory
112: .getContributedResourceMapping(element);
113: if (mapping == null) {
114: mapping = WorkingSetAdapterFactory
115: .getResourceMapping(element);
116: }
117: if (mapping != null) {
118: result.add(mapping);
119: }
120: }
121: return (ResourceMapping[]) result
122: .toArray(new ResourceMapping[result.size()]);
123: }
124:
125: /* (non-Javadoc)
126: * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping)
127: */
128: public boolean contains(ResourceMapping mapping) {
129: ResourceMapping[] mappings = getMappings();
130: for (int i = 0; i < mappings.length; i++) {
131: ResourceMapping childMapping = mappings[i];
132: if (childMapping.contains(mapping)) {
133: return true;
134: }
135: }
136: return false;
137: }
138:
139: }
|