01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.ide;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14: import java.util.List;
15:
16: import org.eclipse.core.resources.IResource;
17: import org.eclipse.core.resources.mapping.ResourceMapping;
18: import org.eclipse.core.resources.mapping.ResourceMappingContext;
19: import org.eclipse.core.resources.mapping.ResourceTraversal;
20: import org.eclipse.core.runtime.CoreException;
21: import org.eclipse.core.runtime.NullProgressMonitor;
22: import org.eclipse.jface.viewers.IStructuredSelection;
23: import org.eclipse.jface.viewers.StructuredSelection;
24: import org.eclipse.ui.ide.ResourceUtil;
25: import org.eclipse.ui.internal.ISelectionConversionService;
26:
27: /**
28: * The IDESelectionConversionService is the selection service that uses the
29: * resource support available to the IDE.
30: *
31: * @since 3.2
32: */
33: public class IDESelectionConversionService implements
34: ISelectionConversionService {
35:
36: /*
37: * (non-Javadoc)
38: *
39: * @see org.eclipse.ui.internal.SelectionConversionService#convertToResources(org.eclipse.jface.viewers.IStructuredSelection)
40: */
41: public IStructuredSelection convertToResources(
42: IStructuredSelection originalSelection) {
43:
44: List result = new ArrayList();
45: Iterator elements = originalSelection.iterator();
46:
47: while (elements.hasNext()) {
48: Object currentElement = elements.next();
49:
50: IResource resource = ResourceUtil
51: .getResource(currentElement);
52:
53: if (resource == null) {
54:
55: ResourceMapping mapping = ResourceUtil
56: .getResourceMapping(currentElement);
57: if (mapping == null)
58: continue;
59:
60: ResourceTraversal[] traversals = null;
61: try {
62: traversals = mapping.getTraversals(
63: ResourceMappingContext.LOCAL_CONTEXT,
64: new NullProgressMonitor());
65: } catch (CoreException e) {
66: IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e
67: .getStatus());
68: }
69: if (traversals != null) {
70: ResourceTraversal traversal = null;
71: IResource[] resources = null;
72: for (int i = 0; i < traversals.length; i++) {
73: traversal = traversals[i];
74: resources = traversal.getResources();
75: if (resources != null) {
76: for (int j = 0; j < resources.length; j++) {
77: result.add(resources[j]);
78: }
79: }
80: }
81: }
82:
83: } else
84: result.add(resource);
85: }
86:
87: // all that can be converted are done, answer new selection
88: if (result.isEmpty()) {
89: return StructuredSelection.EMPTY;
90: }
91: return new StructuredSelection(result.toArray());
92: }
93: }
|