1 11 12 package org.eclipse.jface.action; 13 14 import java.text.MessageFormat ; import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.ResourceBundle ; 20 import java.util.Set ; 21 22 import org.eclipse.core.commands.Command; 23 import org.eclipse.core.commands.CommandEvent; 24 import org.eclipse.core.commands.CommandManager; 25 import org.eclipse.core.commands.ICommandListener; 26 import org.eclipse.core.commands.ParameterizedCommand; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.jface.bindings.BindingManager; 30 import org.eclipse.jface.bindings.BindingManagerEvent; 31 import org.eclipse.jface.bindings.IBindingManagerListener; 32 import org.eclipse.jface.bindings.Trigger; 33 import org.eclipse.jface.bindings.TriggerSequence; 34 import org.eclipse.jface.bindings.keys.KeySequence; 35 import org.eclipse.jface.bindings.keys.KeyStroke; 36 import org.eclipse.jface.bindings.keys.SWTKeySupport; 37 import org.eclipse.jface.util.IPropertyChangeListener; 38 import org.eclipse.jface.util.Policy; 39 import org.eclipse.jface.util.PropertyChangeEvent; 40 import org.eclipse.jface.util.Util; 41 42 63 public final class ExternalActionManager { 64 65 72 public static final class CommandCallback implements 73 IBindingManagerListener, IBindingManagerCallback { 74 75 78 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle 79 .getBundle(ExternalActionManager.class.getName()); 80 81 84 private final IActiveChecker activeChecker; 85 86 90 private final BindingManager bindingManager; 91 92 95 private boolean bindingManagerListenerAttached = false; 96 97 101 private final CommandManager commandManager; 102 103 110 private final Set loggedCommandIds = new HashSet (); 111 112 117 private final Map registeredListeners = new HashMap (); 118 119 132 public CommandCallback(final BindingManager bindingManager, 133 final CommandManager commandManager) { 134 this(bindingManager, commandManager, new IActiveChecker() { 135 public boolean isActive(String commandId) { 136 return true; 137 } 138 139 }); 140 } 141 142 158 public CommandCallback(final BindingManager bindingManager, 159 final CommandManager commandManager, 160 final IActiveChecker activeChecker) { 161 if (bindingManager == null) { 162 throw new NullPointerException ( 163 "The callback needs a binding manager"); } 165 166 if (commandManager == null) { 167 throw new NullPointerException ( 168 "The callback needs a command manager"); } 170 171 if (activeChecker == null) { 172 throw new NullPointerException ( 173 "The callback needs an active callback"); } 175 176 this.activeChecker = activeChecker; 177 this.bindingManager = bindingManager; 178 this.commandManager = commandManager; 179 } 180 181 185 public final void addPropertyChangeListener(final String commandId, 186 final IPropertyChangeListener listener) { 187 registeredListeners.put(commandId, listener); 188 if (!bindingManagerListenerAttached) { 189 bindingManager.addBindingManagerListener(this); 190 bindingManagerListenerAttached = true; 191 } 192 } 193 194 public final void bindingManagerChanged(final BindingManagerEvent event) { 195 if (event.isActiveBindingsChanged()) { 196 final Iterator listenerItr = registeredListeners.entrySet() 197 .iterator(); 198 while (listenerItr.hasNext()) { 199 final Map.Entry entry = (Map.Entry ) listenerItr.next(); 200 final String commandId = (String ) entry.getKey(); 201 final Command command = commandManager 202 .getCommand(commandId); 203 final ParameterizedCommand parameterizedCommand = new ParameterizedCommand( 204 command, null); 205 if (event.isActiveBindingsChangedFor(parameterizedCommand)) { 206 final IPropertyChangeListener listener = (IPropertyChangeListener) entry 207 .getValue(); 208 listener.propertyChange(new PropertyChangeEvent(event 209 .getManager(), IAction.TEXT, null, null)); 210 } 211 } 212 } 213 } 214 215 218 public final Integer getAccelerator(final String commandId) { 219 final TriggerSequence triggerSequence = bindingManager 220 .getBestActiveBindingFor(commandId); 221 if (triggerSequence != null) { 222 final Trigger[] triggers = triggerSequence.getTriggers(); 223 if (triggers.length == 1) { 224 final Trigger trigger = triggers[0]; 225 if (trigger instanceof KeyStroke) { 226 final KeyStroke keyStroke = (KeyStroke) trigger; 227 final int accelerator = SWTKeySupport 228 .convertKeyStrokeToAccelerator(keyStroke); 229 return new Integer (accelerator); 230 } 231 } 232 } 233 234 return null; 235 } 236 237 240 public final String getAcceleratorText(final String commandId) { 241 final TriggerSequence triggerSequence = bindingManager 242 .getBestActiveBindingFor(commandId); 243 if (triggerSequence == null) { 244 return null; 245 } 246 247 return triggerSequence.format(); 248 } 249 250 262 public final TriggerSequence[] getActiveBindingsFor( 263 final String commandId) { 264 return bindingManager.getActiveBindingsFor(commandId); 265 } 266 267 270 public final boolean isAcceleratorInUse(final int accelerator) { 271 final KeySequence keySequence = KeySequence 272 .getInstance(SWTKeySupport 273 .convertAcceleratorToKeyStroke(accelerator)); 274 return bindingManager.isPerfectMatch(keySequence) 275 || bindingManager.isPartialMatch(keySequence); 276 } 277 278 284 public final boolean isActive(final String commandId) { 285 if (commandId != null) { 286 final Command command = commandManager.getCommand(commandId); 287 288 if (!command.isDefined() 289 && (!loggedCommandIds.contains(commandId))) { 290 final String message = MessageFormat.format(Util 292 .translateString(RESOURCE_BUNDLE, 293 "undefinedCommand.WarningMessage", null), new String [] { command.getId() }); 295 IStatus status = new Status(IStatus.ERROR, 296 "org.eclipse.jface", 0, message, new Exception ()); 298 Policy.getLog().log(status); 299 300 loggedCommandIds.add(commandId); 302 command.addCommandListener(new ICommandListener() { 303 308 public final void commandChanged( 309 final CommandEvent commandEvent) { 310 if (command.isDefined()) { 311 command.removeCommandListener(this); 312 loggedCommandIds.remove(commandId); 313 } 314 } 315 }); 316 317 return true; 318 } 319 320 return activeChecker.isActive(commandId); 321 } 322 323 return true; 324 } 325 326 330 public final void removePropertyChangeListener(final String commandId, 331 final IPropertyChangeListener listener) { 332 final IPropertyChangeListener existingListener = (IPropertyChangeListener) registeredListeners 333 .get(commandId); 334 if (existingListener == listener) { 335 registeredListeners.remove(commandId); 336 if (registeredListeners.isEmpty()) { 337 bindingManager.removeBindingManagerListener(this); 338 bindingManagerListenerAttached = false; 339 } 340 } 341 } 342 } 343 344 350 public static interface IActiveChecker { 351 362 public boolean isActive(String commandId); 363 } 364 365 378 public static interface IBindingManagerCallback extends ICallback { 379 380 393 public TriggerSequence[] getActiveBindingsFor(String commandId); 394 } 395 396 402 public static interface ICallback { 403 404 423 public void addPropertyChangeListener(String identifier, 424 IPropertyChangeListener listener); 425 426 437 public Integer getAccelerator(String identifier); 438 439 451 public String getAcceleratorText(String identifier); 452 453 465 public boolean isAcceleratorInUse(int accelerator); 466 467 478 public boolean isActive(String identifier); 479 480 492 public void removePropertyChangeListener(String identifier, 493 IPropertyChangeListener listener); 494 } 495 496 500 private static ExternalActionManager instance; 501 502 507 public static ExternalActionManager getInstance() { 508 if (instance == null) { 509 instance = new ExternalActionManager(); 510 } 511 512 return instance; 513 } 514 515 518 private ICallback callback; 519 520 523 private ExternalActionManager() { 524 } 526 527 535 public ICallback getCallback() { 536 return callback; 537 } 538 539 547 public void setCallback(ICallback callbackToUse) { 548 callback = callbackToUse; 549 } 550 } 551 | Popular Tags |