KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > IDESelectionConversionService


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;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20 import org.eclipse.core.resources.mapping.ResourceMappingContext;
21 import org.eclipse.core.resources.mapping.ResourceTraversal;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.ui.ide.ResourceUtil;
27 import org.eclipse.ui.internal.ISelectionConversionService;
28
29 /**
30  * The IDESelectionConversionService is the selection service that uses the
31  * resource support available to the IDE.
32  *
33  * @since 3.2
34  */

35 public class IDESelectionConversionService implements
36         ISelectionConversionService {
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.eclipse.ui.internal.SelectionConversionService#convertToResources(org.eclipse.jface.viewers.IStructuredSelection)
42      */

43     public IStructuredSelection convertToResources(
44             IStructuredSelection originalSelection) {
45
46         List JavaDoc result = new ArrayList JavaDoc();
47         Iterator JavaDoc elements = originalSelection.iterator();
48
49         while (elements.hasNext()) {
50             Object JavaDoc currentElement = elements.next();
51
52             IResource resource = ResourceUtil.getResource(currentElement);
53
54             if (resource == null) {
55
56                 ResourceMapping mapping = ResourceUtil
57                         .getResourceMapping(currentElement);
58                 if (mapping == null)
59                     continue;
60
61                 ResourceTraversal[] traversals = null;
62                 try {
63                     traversals = mapping.getTraversals(
64                             ResourceMappingContext.LOCAL_CONTEXT,
65                             new NullProgressMonitor());
66                 } catch (CoreException e) {
67                     IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e
68                             .getStatus());
69                 }
70                 if (traversals != null) {
71                     ResourceTraversal traversal = null;
72                     IResource[] resources = null;
73                     for (int i = 0; i < traversals.length; i++) {
74                         traversal = traversals[i];
75                         resources = traversal.getResources();
76                         if (resources != null) {
77                             for (int j = 0; j < resources.length; j++) {
78                                 result.add(resources[j]);
79                             }
80                         }
81                     }
82                 }
83
84             } else
85                 result.add(resource);
86         }
87
88         // all that can be converted are done, answer new selection
89
if (result.isEmpty()) {
90             return StructuredSelection.EMPTY;
91         }
92         return new StructuredSelection(result.toArray());
93     }
94 }
95
Popular Tags