1 11 12 package org.eclipse.ui.handlers; 13 14 import java.util.Collection ; 15 16 import org.eclipse.core.commands.ExecutionEvent; 17 import org.eclipse.core.commands.ExecutionException; 18 import org.eclipse.core.expressions.IEvaluationContext; 19 import org.eclipse.jface.viewers.ISelection; 20 import org.eclipse.swt.widgets.Shell; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.ISources; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchSite; 25 import org.eclipse.ui.IWorkbenchWindow; 26 27 35 public class HandlerUtil { 36 private static void noVariableFound(ExecutionEvent event, String name) 37 throws ExecutionException { 38 throw new ExecutionException("No " + name + " found while executing " + event.getCommand().getId()); } 41 42 private static void incorrectTypeFound(ExecutionEvent event, String name, 43 Class expectedType, Class wrongType) throws ExecutionException { 44 throw new ExecutionException("Incorrect type for " + name 46 + " found while executing " + event.getCommand().getId() 48 + ", expected " + expectedType.getName() + " found " + wrongType.getName()); } 51 52 62 public static Object getVariable(ExecutionEvent event, String name) { 63 if (event.getApplicationContext() instanceof IEvaluationContext) { 64 return ((IEvaluationContext) event.getApplicationContext()) 65 .getVariable(name); 66 } 67 return null; 68 } 69 70 82 public static Object getVariableChecked(ExecutionEvent event, String name) 83 throws ExecutionException { 84 Object o = getVariable(event, name); 85 if (o == null) { 86 noVariableFound(event, name); 87 } 88 return o; 89 } 90 91 98 public static Collection getActiveContexts(ExecutionEvent event) { 99 Object o = getVariable(event, ISources.ACTIVE_CONTEXT_NAME); 100 if (o instanceof Collection ) { 101 return (Collection ) o; 102 } 103 return null; 104 } 105 106 116 public static Collection getActiveContextsChecked(ExecutionEvent event) 117 throws ExecutionException { 118 Object o = getVariableChecked(event, ISources.ACTIVE_CONTEXT_NAME); 119 if (!(o instanceof Collection )) { 120 incorrectTypeFound(event, ISources.ACTIVE_CONTEXT_NAME, 121 Collection .class, o.getClass()); 122 } 123 return (Collection ) o; 124 } 125 126 134 public static Shell getActiveShell(ExecutionEvent event) { 135 Object o = getVariable(event, ISources.ACTIVE_SHELL_NAME); 136 if (o instanceof Shell) { 137 return (Shell) o; 138 } 139 return null; 140 } 141 142 152 public static Shell getActiveShellChecked(ExecutionEvent event) 153 throws ExecutionException { 154 Object o = getVariableChecked(event, ISources.ACTIVE_SHELL_NAME); 155 if (!(o instanceof Shell)) { 156 incorrectTypeFound(event, ISources.ACTIVE_SHELL_NAME, Shell.class, 157 o.getClass()); 158 } 159 return (Shell) o; 160 } 161 162 169 public static IWorkbenchWindow getActiveWorkbenchWindow(ExecutionEvent event) { 170 Object o = getVariable(event, ISources.ACTIVE_WORKBENCH_WINDOW_NAME); 171 if (o instanceof IWorkbenchWindow) { 172 return (IWorkbenchWindow) o; 173 } 174 return null; 175 } 176 177 186 public static IWorkbenchWindow getActiveWorkbenchWindowChecked( 187 ExecutionEvent event) throws ExecutionException { 188 Object o = getVariableChecked(event, 189 ISources.ACTIVE_WORKBENCH_WINDOW_NAME); 190 if (!(o instanceof IWorkbenchWindow)) { 191 incorrectTypeFound(event, ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 192 IWorkbenchWindow.class, o.getClass()); 193 } 194 return (IWorkbenchWindow) o; 195 } 196 197 204 public static IEditorPart getActiveEditor(ExecutionEvent event) { 205 Object o = getVariable(event, ISources.ACTIVE_EDITOR_NAME); 206 if (o instanceof IEditorPart) { 207 return (IEditorPart) o; 208 } 209 return null; 210 } 211 212 221 public static IEditorPart getActiveEditorChecked(ExecutionEvent event) 222 throws ExecutionException { 223 Object o = getVariableChecked(event, ISources.ACTIVE_EDITOR_NAME); 224 if (!(o instanceof IEditorPart)) { 225 incorrectTypeFound(event, ISources.ACTIVE_EDITOR_NAME, 226 IEditorPart.class, o.getClass()); 227 } 228 return (IEditorPart) o; 229 } 230 231 238 public static String getActiveEditorId(ExecutionEvent event) { 239 Object o = getVariable(event, ISources.ACTIVE_EDITOR_ID_NAME); 240 if (o instanceof String ) { 241 return (String ) o; 242 } 243 return null; 244 } 245 246 256 public static String getActiveEditorIdChecked(ExecutionEvent event) 257 throws ExecutionException { 258 Object o = getVariableChecked(event, ISources.ACTIVE_EDITOR_ID_NAME); 259 if (!(o instanceof String )) { 260 incorrectTypeFound(event, ISources.ACTIVE_EDITOR_ID_NAME, 261 String .class, o.getClass()); 262 } 263 return (String ) o; 264 } 265 266 273 public static IWorkbenchPart getActivePart(ExecutionEvent event) { 274 Object o = getVariable(event, ISources.ACTIVE_PART_NAME); 275 if (o instanceof IWorkbenchPart) { 276 return (IWorkbenchPart) o; 277 } 278 return null; 279 } 280 281 290 public static IWorkbenchPart getActivePartChecked(ExecutionEvent event) 291 throws ExecutionException { 292 Object o = getVariableChecked(event, ISources.ACTIVE_PART_NAME); 293 if (!(o instanceof IWorkbenchPart)) { 294 incorrectTypeFound(event, ISources.ACTIVE_PART_NAME, 295 IWorkbenchPart.class, o.getClass()); 296 } 297 return (IWorkbenchPart) o; 298 } 299 300 307 public static String getActivePartId(ExecutionEvent event) { 308 Object o = getVariable(event, ISources.ACTIVE_PART_ID_NAME); 309 if (o instanceof String ) { 310 return (String ) o; 311 } 312 return null; 313 } 314 315 324 public static String getActivePartIdChecked(ExecutionEvent event) 325 throws ExecutionException { 326 Object o = getVariableChecked(event, ISources.ACTIVE_PART_ID_NAME); 327 if (!(o instanceof String )) { 328 incorrectTypeFound(event, ISources.ACTIVE_PART_ID_NAME, 329 String .class, o.getClass()); 330 } 331 return (String ) o; 332 } 333 334 341 public static IWorkbenchSite getActiveSite(ExecutionEvent event) { 342 Object o = getVariable(event, ISources.ACTIVE_SITE_NAME); 343 if (o instanceof IWorkbenchSite) { 344 return (IWorkbenchSite) o; 345 } 346 return null; 347 } 348 349 358 public static IWorkbenchSite getActiveSiteChecked(ExecutionEvent event) 359 throws ExecutionException { 360 Object o = getVariableChecked(event, ISources.ACTIVE_SITE_NAME); 361 if (!(o instanceof IWorkbenchSite)) { 362 incorrectTypeFound(event, ISources.ACTIVE_SITE_NAME, 363 IWorkbenchSite.class, o.getClass()); 364 } 365 return (IWorkbenchSite) o; 366 } 367 368 375 public static ISelection getCurrentSelection(ExecutionEvent event) { 376 Object o = getVariable(event, ISources.ACTIVE_CURRENT_SELECTION_NAME); 377 if (o instanceof ISelection) { 378 return (ISelection) o; 379 } 380 return null; 381 } 382 383 392 public static ISelection getCurrentSelectionChecked(ExecutionEvent event) 393 throws ExecutionException { 394 Object o = getVariableChecked(event, 395 ISources.ACTIVE_CURRENT_SELECTION_NAME); 396 if (!(o instanceof ISelection)) { 397 incorrectTypeFound(event, ISources.ACTIVE_CURRENT_SELECTION_NAME, 398 ISelection.class, o.getClass()); 399 } 400 return (ISelection) o; 401 } 402 403 411 public static Collection getActiveMenus(ExecutionEvent event) { 412 Object o = getVariable(event, ISources.ACTIVE_MENU_NAME); 413 if (o instanceof Collection ) { 414 return (Collection ) o; 415 } 416 return null; 417 } 418 419 429 public static Collection getActiveMenusChecked(ExecutionEvent event) 430 throws ExecutionException { 431 Object o = getVariableChecked(event, ISources.ACTIVE_MENU_NAME); 432 if (!(o instanceof Collection )) { 433 incorrectTypeFound(event, ISources.ACTIVE_MENU_NAME, 434 Collection .class, o.getClass()); 435 } 436 return (Collection ) o; 437 } 438 439 447 public static ISelection getActiveMenuSelection(ExecutionEvent event) { 448 Object o = getVariable(event, ISources.ACTIVE_MENU_SELECTION_NAME); 449 if (o instanceof ISelection) { 450 return (ISelection) o; 451 } 452 return null; 453 } 454 455 465 public static ISelection getActiveMenuSelectionChecked(ExecutionEvent event) 466 throws ExecutionException { 467 Object o = getVariableChecked(event, 468 ISources.ACTIVE_MENU_SELECTION_NAME); 469 if (!(o instanceof ISelection)) { 470 incorrectTypeFound(event, ISources.ACTIVE_MENU_SELECTION_NAME, 471 ISelection.class, o.getClass()); 472 } 473 return (ISelection) o; 474 } 475 476 484 public static ISelection getActiveMenuEditorInput(ExecutionEvent event) { 485 Object o = getVariable(event, ISources.ACTIVE_MENU_EDITOR_INPUT_NAME); 486 if (o instanceof ISelection) { 487 return (ISelection) o; 488 } 489 return null; 490 } 491 492 503 public static ISelection getActiveMenuEditorInputChecked( 504 ExecutionEvent event) throws ExecutionException { 505 Object o = getVariableChecked(event, 506 ISources.ACTIVE_MENU_EDITOR_INPUT_NAME); 507 if (!(o instanceof ISelection)) { 508 incorrectTypeFound(event, ISources.ACTIVE_MENU_EDITOR_INPUT_NAME, 509 ISelection.class, o.getClass()); 510 } 511 return (ISelection) o; 512 } 513 } 514 | Popular Tags |