KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > SelectionConversionService


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;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.ui.internal.util.Util;
21
22 /**
23  * <p>
24  * The SelectionConversionService is the service that converts the selection to
25  * IResources.
26  * </p>
27  * <p>
28  * This interface is only intended for use within the
29  * <code>org.eclipse.ui.workbench</code> and <code>org.eclipse.ui.ide</code>
30  * plug-ins.
31  * </p>
32  *
33  * @since 3.2
34  */

35 public class SelectionConversionService implements ISelectionConversionService {
36
37     /**
38      * Attempt to convert the elements in the passed selection into resources by
39      * asking each for its IResource property (iff it isn't already a resource).
40      * If all elements in the initial selection can be converted to resources
41      * then answer a new selection containing these resources; otherwise answer
42      * an empty selection.
43      *
44      * @param originalSelection
45      * the original selection
46      * @return the converted selection or an empty selection.
47      */

48     public IStructuredSelection convertToResources(
49             IStructuredSelection originalSelection) {
50         // @issue resource-specific code should be pushed into IDE
51
Class JavaDoc resourceClass = LegacyResourceSupport.getResourceClass();
52         if (resourceClass == null) {
53             return originalSelection;
54         }
55
56         List JavaDoc result = new ArrayList JavaDoc();
57         Iterator JavaDoc elements = originalSelection.iterator();
58
59         while (elements.hasNext()) {
60             Object JavaDoc currentElement = elements.next();
61             Object JavaDoc resource = Util.getAdapter(currentElement, 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 }
75
Popular Tags