1 11 package org.eclipse.ui.internal.commands; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.commands.CommandManager; 19 import org.eclipse.core.commands.contexts.ContextManager; 20 import org.eclipse.jface.bindings.BindingManager; 21 import org.eclipse.ui.ISources; 22 import org.eclipse.ui.LegacyHandlerSubmissionExpression; 23 import org.eclipse.ui.commands.HandlerSubmission; 24 import org.eclipse.ui.commands.ICommandManager; 25 import org.eclipse.ui.commands.IWorkbenchCommandSupport; 26 import org.eclipse.ui.commands.Priority; 27 import org.eclipse.ui.handlers.IHandlerActivation; 28 import org.eclipse.ui.handlers.IHandlerService; 29 import org.eclipse.ui.internal.handlers.LegacyHandlerWrapper; 30 31 36 public class WorkbenchCommandSupport implements IWorkbenchCommandSupport { 37 38 43 private Map activationsBySubmission = null; 44 45 49 private final CommandManagerLegacyWrapper commandManagerWrapper; 50 51 55 private final IHandlerService handlerService; 56 57 73 public WorkbenchCommandSupport(final BindingManager bindingManager, 74 final CommandManager commandManager, 75 final ContextManager contextManager, 76 final IHandlerService handlerService) { 77 if (handlerService == null) { 78 throw new NullPointerException ("The handler service cannot be null"); } 80 81 this.handlerService = handlerService; 82 83 commandManagerWrapper = CommandManagerFactory.getCommandManagerWrapper( 84 bindingManager, commandManager, contextManager); 85 86 org.eclipse.ui.keys.KeyFormatterFactory 88 .setDefault(org.eclipse.ui.keys.SWTKeySupport 89 .getKeyFormatterForPlatform()); 90 } 91 92 public final void addHandlerSubmission( 93 final HandlerSubmission handlerSubmission) { 94 98 int sourcePriorities = 0; 99 if (handlerSubmission.getActivePartId() != null) { 100 sourcePriorities |= ISources.ACTIVE_PART_ID; 101 } 102 if (handlerSubmission.getActiveShell() != null) { 103 sourcePriorities |= (ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW); 104 } 105 if (handlerSubmission.getActiveWorkbenchPartSite() != null) { 106 sourcePriorities |= ISources.ACTIVE_SITE; 107 } 108 if (handlerSubmission.getPriority() == Priority.LEGACY) { 109 sourcePriorities |= ISources.LEGACY_LEGACY; 110 } else if (handlerSubmission.getPriority() == Priority.LOW) { 111 sourcePriorities |= ISources.LEGACY_LOW; 112 } else if (handlerSubmission.getPriority() == Priority.MEDIUM) { 113 sourcePriorities |= ISources.LEGACY_MEDIUM; 114 } 115 116 final IHandlerActivation activation = handlerService.activateHandler( 117 handlerSubmission.getCommandId(), new LegacyHandlerWrapper( 118 handlerSubmission.getHandler()), 119 new LegacyHandlerSubmissionExpression(handlerSubmission 120 .getActivePartId(), handlerSubmission.getActiveShell(), 121 handlerSubmission.getActiveWorkbenchPartSite())); 122 if (activationsBySubmission == null) { 123 activationsBySubmission = new HashMap (); 124 } 125 activationsBySubmission.put(handlerSubmission, activation); 126 } 127 128 public final void addHandlerSubmissions(final Collection handlerSubmissions) { 129 final Iterator submissionItr = handlerSubmissions.iterator(); 130 while (submissionItr.hasNext()) { 131 addHandlerSubmission((HandlerSubmission) submissionItr.next()); 132 } 133 } 134 135 public ICommandManager getCommandManager() { 136 return commandManagerWrapper; 137 } 138 139 public final void removeHandlerSubmission( 140 final HandlerSubmission handlerSubmission) { 141 if (activationsBySubmission == null) { 142 return; 143 } 144 145 final Object value = activationsBySubmission.remove(handlerSubmission); 146 if (value instanceof IHandlerActivation) { 147 final IHandlerActivation activation = (IHandlerActivation) value; 148 handlerService.deactivateHandler(activation); 149 } 150 } 151 152 public final void removeHandlerSubmissions( 153 final Collection handlerSubmissions) { 154 final Iterator submissionItr = handlerSubmissions.iterator(); 155 while (submissionItr.hasNext()) { 156 removeHandlerSubmission((HandlerSubmission) submissionItr.next()); 157 } 158 } 159 } 160 | Popular Tags |