KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > stringsubstitution > SelectedResourceManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.internal.ui.stringsubstitution;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.debug.internal.ui.DebugUIPlugin;
16 import org.eclipse.jface.text.ITextSelection;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IEditorSite;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.IWorkbenchPartSite;
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.IWorkbenchWindow;
28
29 /**
30  * Maintains the context used to expand variables. The context is based on
31  * the selected resource.
32  */

33 public class SelectedResourceManager {
34
35     // singleton
36
private static SelectedResourceManager fgDefault;
37     
38     /**
39      * Returns the singleton resource selection manager
40      *
41      * @return VariableContextManager
42      */

43     public static SelectedResourceManager getDefault() {
44         if (fgDefault == null) {
45             fgDefault = new SelectedResourceManager();
46         }
47         return fgDefault;
48     }
49     
50     /**
51      * Returns the selection from the currently active part. If the active part is an
52      * editor a new selection of the editor input is made, otherwise the selection
53      * from the parts' selection provider is returned if it is a structured selection. Otherwise
54      * and empty selection is returned, never <code>null</code>
55      *
56      * This method is intended to be called from the UI thread.
57      *
58      * @return the <code>IStructuredSelection</code> from the current parts' selection provider, or
59      * a new <code>IStructuredSelection</code> of the current editor input, depending on what the current part
60      * is.
61      *
62      * @since 3.3
63      */

64     public IStructuredSelection getCurrentSelection() {
65         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
66         if(window != null) {
67             IWorkbenchPage page = window.getActivePage();
68             if(page != null) {
69                 IWorkbenchPart part = page.getActivePart();
70                 if(part instanceof IEditorPart) {
71                     return new StructuredSelection(((IEditorPart)part).getEditorInput());
72                 }
73                 else if(part != null) {
74                     IWorkbenchSite site = part.getSite();
75                     if(site != null) {
76                         ISelectionProvider provider = site.getSelectionProvider();
77                         if(provider != null) {
78                             ISelection selection = provider.getSelection();
79                             if(selection instanceof IStructuredSelection) {
80                                 return (IStructuredSelection) provider.getSelection();
81                             }
82                         }
83                     }
84                 }
85             }
86         }
87         return StructuredSelection.EMPTY;
88     }
89     
90     /**
91      * Returns the currently selected resource in the active workbench window,
92      * or <code>null</code> if none. If an editor is active, the resource adapter
93      * associated with the editor is returned.
94      *
95      * @return selected resource or <code>null</code>
96      */

97     public IResource getSelectedResource() {
98         if(DebugUIPlugin.getStandardDisplay().getThread().equals(Thread.currentThread())) {
99             return getSelectedResource0();
100         }
101         else {
102             final IResource[] resource = new IResource[1];
103             DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
104                 public void run() {
105                     resource[0] = getSelectedResource0();
106                 }
107             });
108             return resource[0];
109         }
110     }
111     
112     /**
113      * Returns the currently selected resource from the active part, or <code>null</code> if one cannot be
114      * resolved.
115      * @return the currently selected <code>IResource</code>, or <code>null</code> if none.
116      * @since 3.3
117      */

118     protected IResource getSelectedResource0() {
119         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
120         IResource resource = null;
121         if(window != null) {
122             IWorkbenchPage page = window.getActivePage();
123             if(page != null) {
124                 IWorkbenchPart part = page.getActivePart();
125                 if(part instanceof IEditorPart) {
126                     IEditorPart epart = (IEditorPart) part;
127                     resource = (IResource) epart.getEditorInput().getAdapter(IResource.class);
128                 }
129                 else if(part != null) {
130                     IWorkbenchPartSite site = part.getSite();
131                     if(site != null) {
132                         ISelectionProvider provider = site.getSelectionProvider();
133                         if(provider != null) {
134                             ISelection selection = provider.getSelection();
135                             if(selection instanceof IStructuredSelection) {
136                                 IStructuredSelection ss = (IStructuredSelection) selection;
137                                 if(!ss.isEmpty()) {
138                                     Object JavaDoc o = ss.getFirstElement();
139                                     if(o instanceof IAdaptable) {
140                                         resource = (IResource) ((IAdaptable)o).getAdapter(IResource.class);
141                                     }
142                                 }
143                             }
144                         }
145                     }
146                 }
147             }
148         }
149         return resource;
150     }
151     
152     /**
153      * Returns the current text selection as a <code>String</code>, or <code>null</code> if
154      * none.
155      *
156      * @return the current text selection as a <code>String</code> or <code>null</code>
157      */

158     public String JavaDoc getSelectedText() {
159         if(DebugUIPlugin.getStandardDisplay().getThread().equals(Thread.currentThread())) {
160             return getSelectedText0();
161         }
162         else {
163             final String JavaDoc[] text = new String JavaDoc[1];
164             DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
165                 public void run() {
166                     text[0] = getSelectedText0();
167                 }
168             });
169             return text[0];
170         }
171     }
172     
173     /**
174      * Returns the selected text from the most currently active editor. The editor does not have to
175      * have focus at the time this method is called.
176      * @return the currently selected text in the most recent active editor.
177      *
178      * @since 3.3
179      */

180     protected String JavaDoc getSelectedText0() {
181         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
182         if(window != null) {
183             IWorkbenchPage page = window.getActivePage();
184             if(page != null) {
185                 IEditorPart epart = page.getActiveEditor();
186                 if(epart != null) {
187                     IEditorSite esite = epart.getEditorSite();
188                     if(esite != null) {
189                         ISelectionProvider sprovider = esite.getSelectionProvider();
190                         if(sprovider != null) {
191                             ISelection selection = sprovider.getSelection();
192                             if(selection instanceof ITextSelection) {
193                                 return ((ITextSelection)selection).getText();
194                             }
195                         }
196                     }
197                 }
198             }
199         }
200         return null;
201     }
202     
203     /**
204      * Returns the currently active workbench window
205      * @return the currently active workbench window
206      * @since 3.3
207      */

208     public IWorkbenchWindow getActiveWindow0() {
209         return DebugUIPlugin.getActiveWorkbenchWindow();
210     }
211     
212     /**
213      * Returns the active workbench window, or <code>null</code> if none.
214      *
215      * @return the active workbench window, or <code>null</code> if none
216      * @since 3.2
217      */

218     public IWorkbenchWindow getActiveWindow() {
219         if(DebugUIPlugin.getStandardDisplay().getThread().equals(Thread.currentThread())) {
220             return getActiveWindow0();
221         }
222         else {
223             final IWorkbenchWindow[] window = new IWorkbenchWindow[1];
224             DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
225                 public void run() {
226                     window[0] = getActiveWindow0();
227                 }
228             });
229             return window[0];
230         }
231     }
232
233 }
234
Popular Tags