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;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14: import java.util.List;
15:
16: import org.eclipse.jface.viewers.IStructuredSelection;
17: import org.eclipse.jface.viewers.StructuredSelection;
18: import org.eclipse.ui.internal.util.Util;
19:
20: /**
21: * <p>
22: * The SelectionConversionService is the service that converts the selection to
23: * IResources.
24: * </p>
25: * <p>
26: * This interface is only intended for use within the
27: * <code>org.eclipse.ui.workbench</code> and <code>org.eclipse.ui.ide</code>
28: * plug-ins.
29: * </p>
30: *
31: * @since 3.2
32: */
33: public class SelectionConversionService implements
34: ISelectionConversionService {
35:
36: /**
37: * Attempt to convert the elements in the passed selection into resources by
38: * asking each for its IResource property (iff it isn't already a resource).
39: * If all elements in the initial selection can be converted to resources
40: * then answer a new selection containing these resources; otherwise answer
41: * an empty selection.
42: *
43: * @param originalSelection
44: * the original selection
45: * @return the converted selection or an empty selection.
46: */
47: public IStructuredSelection convertToResources(
48: IStructuredSelection originalSelection) {
49: // @issue resource-specific code should be pushed into IDE
50: Class resourceClass = LegacyResourceSupport.getResourceClass();
51: if (resourceClass == null) {
52: return originalSelection;
53: }
54:
55: List result = new ArrayList();
56: Iterator elements = originalSelection.iterator();
57:
58: while (elements.hasNext()) {
59: Object currentElement = elements.next();
60: Object resource = Util.getAdapter(currentElement,
61: resourceClass);
62: if (resource != null) {
63: result.add(resource);
64: }
65: }
66:
67: // all that can be converted are done, answer new selection
68: if (result.isEmpty()) {
69: return StructuredSelection.EMPTY;
70: }
71: return new StructuredSelection(result.toArray());
72: }
73:
74: }
|