1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.widgets.Shell; 16 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.ISelectionProvider; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 24 import org.eclipse.jface.text.ITextSelection; 25 26 import org.eclipse.ui.IWorkbenchSite; 27 28 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 29 30 52 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener { 53 54 private IWorkbenchSite fSite; 55 private ISelectionProvider fSpecialSelectionProvider; 56 57 65 protected SelectionDispatchAction(IWorkbenchSite site) { 66 Assert.isNotNull(site); 67 fSite= site; 68 } 69 70 87 protected SelectionDispatchAction(IWorkbenchSite site, ISelectionProvider provider) { 88 this(site); 89 setSpecialSelectionProvider(provider); 90 } 91 92 97 public IWorkbenchSite getSite() { 98 return fSite; 99 } 100 101 106 public ISelection getSelection() { 107 ISelectionProvider selectionProvider= getSelectionProvider(); 108 if (selectionProvider != null) 109 return selectionProvider.getSelection(); 110 else 111 return null; 112 } 113 114 119 public Shell getShell() { 120 return fSite.getShell(); 121 } 122 123 129 public ISelectionProvider getSelectionProvider() { 130 if (fSpecialSelectionProvider != null) { 131 return fSpecialSelectionProvider; 132 } 133 return fSite.getSelectionProvider(); 134 } 135 136 148 public void setSpecialSelectionProvider(ISelectionProvider provider) { 149 fSpecialSelectionProvider= provider; 150 } 151 152 159 public void update(ISelection selection) { 160 dispatchSelectionChanged(selection); 161 } 162 163 169 public void selectionChanged(IStructuredSelection selection) { 170 selectionChanged((ISelection)selection); 171 } 172 173 179 public void run(IStructuredSelection selection) { 180 run((ISelection)selection); 181 } 182 183 188 public void selectionChanged(JavaTextSelection selection) { 189 selectionChanged((ITextSelection)selection); 190 } 191 192 197 public void run(JavaTextSelection selection) { 198 run((ITextSelection)selection); 199 } 200 201 207 public void selectionChanged(ITextSelection selection) { 208 selectionChanged((ISelection)selection); 209 } 210 211 217 public void run(ITextSelection selection) { 218 run((ISelection)selection); 219 } 220 221 227 public void selectionChanged(ISelection selection) { 228 setEnabled(false); 229 } 230 231 237 public void run(ISelection selection) { 238 } 239 240 243 public void run() { 244 dispatchRun(getSelection()); 245 } 246 247 250 public void selectionChanged(SelectionChangedEvent event) { 251 dispatchSelectionChanged(event.getSelection()); 252 } 253 254 private void dispatchSelectionChanged(ISelection selection) { 255 if (selection instanceof IStructuredSelection) { 256 selectionChanged((IStructuredSelection)selection); 257 } else if (selection instanceof JavaTextSelection) { 258 selectionChanged((JavaTextSelection)selection); 259 } else if (selection instanceof ITextSelection) { 260 selectionChanged((ITextSelection)selection); 261 } else { 262 selectionChanged(selection); 263 } 264 } 265 266 private void dispatchRun(ISelection selection) { 267 if (selection instanceof IStructuredSelection) { 268 run((IStructuredSelection)selection); 269 } else if (selection instanceof JavaTextSelection) { 270 run((JavaTextSelection)selection); 271 } else if (selection instanceof ITextSelection) { 272 run((ITextSelection)selection); 273 } else { 274 run(selection); 275 } 276 } 277 } 278 | Popular Tags |