1 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 33 public class SelectedResourceManager { 34 35 private static SelectedResourceManager fgDefault; 37 38 43 public static SelectedResourceManager getDefault() { 44 if (fgDefault == null) { 45 fgDefault = new SelectedResourceManager(); 46 } 47 return fgDefault; 48 } 49 50 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 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 () { 104 public void run() { 105 resource[0] = getSelectedResource0(); 106 } 107 }); 108 return resource[0]; 109 } 110 } 111 112 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 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 158 public String getSelectedText() { 159 if(DebugUIPlugin.getStandardDisplay().getThread().equals(Thread.currentThread())) { 160 return getSelectedText0(); 161 } 162 else { 163 final String [] text = new String [1]; 164 DebugUIPlugin.getStandardDisplay().syncExec(new Runnable () { 165 public void run() { 166 text[0] = getSelectedText0(); 167 } 168 }); 169 return text[0]; 170 } 171 } 172 173 180 protected String 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 208 public IWorkbenchWindow getActiveWindow0() { 209 return DebugUIPlugin.getActiveWorkbenchWindow(); 210 } 211 212 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 () { 225 public void run() { 226 window[0] = getActiveWindow0(); 227 } 228 }); 229 return window[0]; 230 } 231 } 232 233 } 234 | Popular Tags |