KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > SelectionUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.ui.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.IWorkbenchPartReference;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.part.ISetSelectionTarget;
29
30 public class SelectionUtil {
31
32     public static List JavaDoc toList(ISelection selection) {
33         if (selection instanceof IStructuredSelection)
34             return ((IStructuredSelection) selection).toList();
35         return null;
36     }
37
38     /**
39      * Returns the selected element if the selection consists of a single
40      * element only.
41      *
42      * @param s the selection
43      * @return the selected first element or null
44      */

45     public static Object JavaDoc getSingleElement(ISelection s) {
46         if (! (s instanceof IStructuredSelection))
47             return null;
48         IStructuredSelection selection= (IStructuredSelection) s;
49         if (selection.size() != 1)
50             return null;
51
52         return selection.getFirstElement();
53     }
54
55
56     /**
57      * Attempts to select and reveal the specified resources in all parts within
58      * the supplied workbench window's active page.
59      * <p>
60      * Checks all parts in the active page to see if they implement
61      * <code>ISetSelectionTarget</code>, either directly or as an adapter. If
62      * so, tells the part to select and reveal the specified resources.
63      * </p>
64      *
65      * @param resources the resources to be selected and revealed
66      * @param window the workbench window to select and reveal the resource
67      *
68      * @see ISetSelectionTarget
69      *
70      * @see org.eclipse.ui.wizards.newresource.BasicNewResourceWizard#selectAndReveal(IResource,
71      * IWorkbenchWindow)
72      */

73     public static void selectAndReveal(IResource[] resources, IWorkbenchWindow window) {
74         // validate the input
75
if (window == null || resources == null || Arrays.asList(resources).contains(null)) {
76             return;
77         }
78         IWorkbenchPage page= window.getActivePage();
79         if (page == null) {
80             return;
81         }
82
83         // get all the view and editor parts
84
List JavaDoc parts= new ArrayList JavaDoc();
85         IWorkbenchPartReference refs[]= page.getViewReferences();
86         for (int i= 0; i < refs.length; i++) {
87             IWorkbenchPart part= refs[i].getPart(false);
88             if (part != null) {
89                 parts.add(part);
90             }
91         }
92         refs= page.getEditorReferences();
93         for (int i= 0; i < refs.length; i++) {
94             if (refs[i].getPart(false) != null) {
95                 parts.add(refs[i].getPart(false));
96             }
97         }
98
99         final ISelection selection= new StructuredSelection(resources);
100         Iterator JavaDoc itr= parts.iterator();
101         while (itr.hasNext()) {
102             IWorkbenchPart part= (IWorkbenchPart) itr.next();
103
104             // get the part's ISetSelectionTarget implementation
105
ISetSelectionTarget target= null;
106             if (part instanceof ISetSelectionTarget) {
107                 target= (ISetSelectionTarget) part;
108             } else {
109                 target= (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class);
110             }
111
112             if (target != null) {
113                 // select and reveal resource
114
final ISetSelectionTarget finalTarget= target;
115                 window.getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
116                     public void run() {
117                         finalTarget.selectReveal(selection);
118                     }
119                 });
120             }
121         }
122     }
123
124 }
125
Popular Tags