1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.ui.IKeyBindingService; 25 import org.eclipse.ui.INestableKeyBindingService; 26 import org.eclipse.ui.IWorkbenchPartSite; 27 import org.eclipse.ui.IWorkbenchSite; 28 import org.eclipse.ui.commands.ActionHandler; 29 import org.eclipse.ui.commands.HandlerSubmission; 30 import org.eclipse.ui.commands.IHandler; 31 import org.eclipse.ui.commands.Priority; 32 import org.eclipse.ui.contexts.EnabledSubmission; 33 import org.eclipse.ui.internal.actions.CommandAction; 34 import org.eclipse.ui.internal.handlers.CommandLegacyActionWrapper; 35 36 44 public final class KeyBindingService implements INestableKeyBindingService { 45 46 50 private IKeyBindingService activeService = null; 51 52 56 private boolean disposed; 57 58 63 private Set enabledContextIds = Collections.EMPTY_SET; 64 65 70 private List enabledSubmissions = new ArrayList (); 71 72 77 private Map handlerSubmissionsByCommandId = new HashMap (); 78 79 84 private List nestedEnabledSubmissions = null; 85 86 91 private List nestedHandlerSubmissions = null; 92 93 97 private final Map nestedServices = new HashMap (); 98 99 105 private final KeyBindingService parent; 106 107 111 private IWorkbenchPartSite workbenchPartSite; 112 113 121 public KeyBindingService(IWorkbenchPartSite workbenchPartSite) { 122 this(workbenchPartSite, null); 123 } 124 125 136 KeyBindingService(IWorkbenchPartSite workbenchPartSite, 137 KeyBindingService parent) { 138 this.workbenchPartSite = workbenchPartSite; 139 this.parent = parent; 140 } 141 142 147 public boolean activateKeyBindingService(IWorkbenchSite nestedSite) { 148 if (disposed) { 149 return false; 150 } 151 152 if (nestedSite == null) { 154 if (activeService == null) { 156 return false; 158 } else { 159 deactivateNestedService(); 161 return true; 162 } 163 } 164 165 final IKeyBindingService service = (IKeyBindingService) nestedServices 167 .get(nestedSite); 168 if (service == null) { 169 return false; 170 } 171 172 if (service == activeService) { 173 return false; 175 } 176 177 deactivateNestedService(); 178 activateNestedService(service); 179 return true; 180 } 181 182 192 private final void activateNestedService(final IKeyBindingService service) { 193 if (disposed) { 194 return; 195 } 196 197 201 boolean active = false; 202 boolean haveParent = (parent != null); 203 if (haveParent) { 204 active = (parent.activeService == this); 205 if (active) { 206 parent.deactivateNestedService(); 207 } 208 } 209 210 activeService = service; 212 213 if (service == null) { 215 return; 216 } 217 218 if (haveParent) { 219 if (active) { 220 parent.activateNestedService(this); 221 } 222 223 } else if (activeService instanceof KeyBindingService) { 224 final KeyBindingService nestedService = (KeyBindingService) activeService; 226 227 nestedEnabledSubmissions = nestedService.getEnabledSubmissions(); 229 normalizeSites(nestedEnabledSubmissions); 230 Workbench.getInstance().getContextSupport().addEnabledSubmissions( 231 nestedEnabledSubmissions); 232 233 nestedHandlerSubmissions = nestedService.getHandlerSubmissions(); 235 normalizeSites(nestedHandlerSubmissions); 236 Workbench.getInstance().getCommandSupport().addHandlerSubmissions( 237 nestedHandlerSubmissions); 238 } 239 } 240 241 245 private final void deactivateNestedService() { 246 if (disposed) { 247 return; 248 } 249 250 if (activeService == null) { 252 return; 253 } 254 255 boolean active = false; 257 if (parent != null) { 258 if (parent.activeService == this) { 260 active = true; 261 parent.deactivateNestedService(); 263 } 264 265 } else if (activeService instanceof KeyBindingService) { 266 Workbench.getInstance().getContextSupport() 268 .removeEnabledSubmissions(nestedEnabledSubmissions); 269 270 275 Workbench.getInstance().getCommandSupport() 276 .removeHandlerSubmissions(nestedHandlerSubmissions); 277 278 } 279 280 activeService = null; 282 283 if (active) { 285 parent.activateNestedService(this); 286 } 287 } 288 289 293 public void dispose() { 294 if (!disposed) { 295 deactivateNestedService(); 296 disposed = true; 297 298 Workbench 299 .getInstance() 300 .getContextSupport() 301 .removeEnabledSubmissions(new ArrayList (enabledSubmissions)); 302 enabledSubmissions.clear(); 303 304 308 final List submissions = new ArrayList ( 309 handlerSubmissionsByCommandId.values()); 310 final Iterator submissionItr = submissions.iterator(); 311 while (submissionItr.hasNext()) { 312 ((HandlerSubmission) submissionItr.next()).getHandler() 313 .dispose(); 314 } 315 Workbench.getInstance().getCommandSupport() 316 .removeHandlerSubmissions(submissions); 317 handlerSubmissionsByCommandId.clear(); 318 319 for (Iterator iterator = nestedServices.values().iterator(); iterator 320 .hasNext();) { 321 KeyBindingService keyBindingService = (KeyBindingService) iterator 322 .next(); 323 keyBindingService.dispose(); 324 } 325 326 nestedEnabledSubmissions = null; 327 nestedHandlerSubmissions = null; 328 nestedServices.clear(); 329 } 330 } 331 332 339 private final List getEnabledSubmissions() { 340 if (disposed) { 341 return null; 342 } 343 344 final List submissions = new ArrayList (enabledSubmissions); 345 if (activeService instanceof KeyBindingService) { 346 final KeyBindingService nestedService = (KeyBindingService) activeService; 347 submissions.addAll(nestedService.getEnabledSubmissions()); 348 } 349 return submissions; 350 } 351 352 359 private final List getHandlerSubmissions() { 360 if (disposed) { 361 return null; 362 } 363 364 final List submissions = new ArrayList (handlerSubmissionsByCommandId 365 .values()); 366 if (activeService instanceof KeyBindingService) { 367 final KeyBindingService nestedService = (KeyBindingService) activeService; 368 submissions.addAll(nestedService.getHandlerSubmissions()); 369 } 370 return submissions; 371 } 372 373 378 public IKeyBindingService getKeyBindingService(IWorkbenchSite nestedSite) { 379 if (disposed) { 380 return null; 381 } 382 383 if (nestedSite == null) { 384 return null; 385 } 386 387 IKeyBindingService service = (IKeyBindingService) nestedServices 388 .get(nestedSite); 389 if (service == null) { 390 if (nestedSite instanceof IWorkbenchPartSite) { 393 service = new KeyBindingService( 394 (IWorkbenchPartSite) nestedSite, this); 395 } else { 396 service = new KeyBindingService(null, this); 397 } 398 399 nestedServices.put(nestedSite, service); 400 } 401 402 return service; 403 } 404 405 public String [] getScopes() { 406 if (disposed) { 407 return null; 408 } 409 410 final String [] nestedScopes; 412 if (activeService == null) { 413 nestedScopes = null; 414 } else { 415 nestedScopes = activeService.getScopes(); 416 } 417 418 final Set activeScopes = new HashSet (); 420 activeScopes.addAll(enabledContextIds); 421 if (nestedScopes != null) { 422 for (int i = 0; i < nestedScopes.length; i++) { 423 activeScopes.add(nestedScopes[i]); 424 } 425 } 426 427 return (String []) activeScopes.toArray(new String [activeScopes.size()]); 428 } 429 430 440 private final void normalizeSites(final List submissionsToModify) { 441 if (disposed) { 442 return; 443 } 444 445 final int size = submissionsToModify.size(); 446 for (int i = 0; i < size; i++) { 447 final Object submission = submissionsToModify.get(i); 448 final Object replacementSubmission; 449 450 if (submission instanceof EnabledSubmission) { 451 final EnabledSubmission enabledSubmission = (EnabledSubmission) submission; 452 if (!workbenchPartSite.equals(enabledSubmission 453 .getActiveWorkbenchPartSite())) { 454 replacementSubmission = new EnabledSubmission(null, 455 enabledSubmission.getActiveShell(), 456 workbenchPartSite, enabledSubmission.getContextId()); 457 } else { 458 replacementSubmission = enabledSubmission; 459 } 460 461 } else if (submission instanceof HandlerSubmission) { 462 final HandlerSubmission handlerSubmission = (HandlerSubmission) submission; 463 if (!workbenchPartSite.equals(handlerSubmission 464 .getActiveWorkbenchPartSite())) { 465 replacementSubmission = new HandlerSubmission(null, 466 handlerSubmission.getActiveShell(), 467 workbenchPartSite, 468 handlerSubmission.getCommandId(), handlerSubmission 469 .getHandler(), handlerSubmission 470 .getPriority()); 471 } else { 472 replacementSubmission = handlerSubmission; 473 } 474 475 } else { 476 replacementSubmission = submission; 477 } 478 479 submissionsToModify.set(i, replacementSubmission); 480 } 481 482 } 483 484 public void registerAction(IAction action) { 485 if (disposed) { 486 return; 487 } 488 489 if (action instanceof CommandLegacyActionWrapper) { 490 WorkbenchPlugin 493 .log("Cannot register a CommandLegacyActionWrapper back into the system"); return; 495 } 496 497 if (action instanceof CommandAction) { 498 return; 501 } 502 503 unregisterAction(action); 504 String commandId = action.getActionDefinitionId(); 505 if (commandId != null) { 506 510 boolean active = false; 511 if ((parent != null) && (parent.activeService == this)) { 512 active = true; 513 parent.deactivateNestedService(); 514 } 515 516 IHandler handler = new ActionHandler(action); 518 HandlerSubmission handlerSubmission = new HandlerSubmission(null, 519 workbenchPartSite.getShell(), workbenchPartSite, commandId, 520 handler, Priority.MEDIUM); 521 handlerSubmissionsByCommandId.put(commandId, handlerSubmission); 522 523 if (parent != null) { 525 if (active) { 526 parent.activateNestedService(this); 527 } 528 } else { 529 Workbench.getInstance().getCommandSupport() 530 .addHandlerSubmission(handlerSubmission); 531 } 532 } 533 } 534 535 540 public boolean removeKeyBindingService(IWorkbenchSite nestedSite) { 541 if (disposed) { 542 return false; 543 } 544 545 final IKeyBindingService service = (IKeyBindingService) nestedServices 546 .remove(nestedSite); 547 if (service == null) { 548 return false; 549 } 550 551 if (service.equals(activeService)) { 552 deactivateNestedService(); 553 } 554 555 return true; 556 } 557 558 public void setScopes(String [] scopes) { 559 if (disposed) { 560 return; 561 } 562 563 boolean active = false; 565 if ((parent != null) && (parent.activeService == this)) { 566 active = true; 567 parent.deactivateNestedService(); 568 } else { 569 Workbench.getInstance().getContextSupport() 570 .removeEnabledSubmissions(enabledSubmissions); 571 } 572 enabledSubmissions.clear(); 573 574 enabledContextIds = new HashSet (Arrays.asList(scopes)); 576 for (Iterator iterator = enabledContextIds.iterator(); iterator 577 .hasNext();) { 578 String contextId = (String ) iterator.next(); 579 enabledSubmissions.add(new EnabledSubmission(null, null, 580 workbenchPartSite, contextId)); 581 } 582 583 if (parent != null) { 585 if (active) { 586 parent.activateNestedService(this); 587 } 588 } else { 589 Workbench.getInstance().getContextSupport().addEnabledSubmissions( 590 enabledSubmissions); 591 } 592 } 593 594 public void unregisterAction(IAction action) { 595 if (disposed) { 596 return; 597 } 598 599 if (action instanceof CommandLegacyActionWrapper) { 600 WorkbenchPlugin 603 .log("Cannot unregister a CommandLegacyActionWrapper out of the system"); return; 605 } 606 607 String commandId = action.getActionDefinitionId(); 608 609 if (commandId != null) { 610 boolean active = false; 612 if ((parent != null) && (parent.activeService == this)) { 613 active = true; 614 parent.deactivateNestedService(); 615 } 616 617 HandlerSubmission handlerSubmission = (HandlerSubmission) handlerSubmissionsByCommandId 619 .remove(commandId); 620 621 625 if (parent != null) { 626 if (active) { 627 parent.activateNestedService(this); 628 } 629 } else { 630 if (handlerSubmission != null) { 631 Workbench.getInstance().getCommandSupport() 632 .removeHandlerSubmission(handlerSubmission); 633 handlerSubmission.getHandler().dispose(); 634 } 635 } 636 } 637 } 638 } 639 | Popular Tags |