1 11 package org.eclipse.ui.internal.commands.ws; 12 13 import java.util.HashMap ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.action.ExternalActionManager.ICallback; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.ui.IWorkbench; 22 import org.eclipse.ui.commands.CommandEvent; 23 import org.eclipse.ui.commands.ICommand; 24 import org.eclipse.ui.commands.ICommandListener; 25 import org.eclipse.ui.commands.ICommandManager; 26 import org.eclipse.ui.commands.IKeySequenceBinding; 27 import org.eclipse.ui.commands.NotDefinedException; 28 import org.eclipse.ui.keys.KeySequence; 29 import org.eclipse.ui.keys.KeyStroke; 30 import org.eclipse.ui.keys.SWTKeySupport; 31 32 35 public final class CommandCallback implements ICallback { 36 37 42 private final Map registeredListeners = new HashMap (); 43 44 48 private final IWorkbench workbench; 49 50 58 public CommandCallback(final IWorkbench workbenchToUse) { 59 workbench = workbenchToUse; 60 } 61 62 66 public void addPropertyChangeListener(final String commandId, 67 final IPropertyChangeListener listener) { 68 final ICommand command = workbench.getCommandSupport() 69 .getCommandManager().getCommand(commandId); 70 final ICommandListener commandListener = new ICommandListener() { 71 72 77 public void commandChanged(CommandEvent commandEvent) { 78 if (commandEvent.hasNameChanged() 80 || commandEvent.haveKeySequenceBindingsChanged()) { 81 PropertyChangeEvent event; 82 try { 83 event = new PropertyChangeEvent(command, IAction.TEXT, 84 null , 85 command.getName()); 86 } catch (final NotDefinedException e) { 87 event = new PropertyChangeEvent(command, IAction.TEXT, 88 null , 89 null ); 90 } 91 listener.propertyChange(event); 92 } 93 94 } 96 }; 97 98 command.addCommandListener(commandListener); 99 registeredListeners.put(listener, commandListener); 100 } 101 102 105 public final Integer getAccelerator(final String commandId) { 106 final ICommand command = workbench.getCommandSupport() 107 .getCommandManager().getCommand(commandId); 108 Integer accelerator = null; 109 110 if (command.isDefined()) { 111 List keySequenceBindings = command.getKeySequenceBindings(); 112 final int size = keySequenceBindings.size(); 113 114 for (int i = 0; i < size; i++) { 115 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding) keySequenceBindings 116 .get(i); 117 List keyStrokes = keySequenceBinding.getKeySequence() 118 .getKeyStrokes(); 119 120 if (keyStrokes.size() == 1) { 121 KeyStroke keyStroke = (KeyStroke) keyStrokes.get(0); 122 accelerator = new Integer (SWTKeySupport 123 .convertKeyStrokeToAccelerator(keyStroke)); 124 break; 125 } 126 } 127 } 128 129 return accelerator; 130 } 131 132 135 public final String getAcceleratorText(final String commandId) { 136 final ICommand command = workbench.getCommandSupport() 137 .getCommandManager().getCommand(commandId); 138 String acceleratorText = null; 139 140 if (command.isDefined()) { 141 List keySequenceBindings = command.getKeySequenceBindings(); 142 143 if (!keySequenceBindings.isEmpty()) { 144 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding) keySequenceBindings 145 .get(0); 146 acceleratorText = keySequenceBinding.getKeySequence().format(); 147 } 148 } 149 150 return acceleratorText; 151 } 152 153 156 public boolean isAcceleratorInUse(int accelerator) { 157 final KeySequence keySequence = KeySequence.getInstance(SWTKeySupport 158 .convertAcceleratorToKeyStroke(accelerator)); 159 final ICommandManager commandManager = workbench.getCommandSupport() 160 .getCommandManager(); 161 return commandManager.isPerfectMatch(keySequence) 162 || commandManager.isPartialMatch(keySequence); 163 } 164 165 168 public final boolean isActive(final String commandId) { 169 if (commandId != null) { 170 final ICommand command = workbench.getCommandSupport() 171 .getCommandManager().getCommand(commandId); 172 173 if (command != null) 174 return command.isDefined() 175 && workbench.getActivitySupport() 176 .getActivityManager().getIdentifier( 177 command.getId()).isEnabled(); 178 } 179 180 return true; 181 } 182 183 187 public final void removePropertyChangeListener(final String commandId, 188 final IPropertyChangeListener listener) { 189 final ICommand command = workbench.getCommandSupport() 190 .getCommandManager().getCommand(commandId); 191 final Object associatedListener = registeredListeners.remove(listener); 192 if (associatedListener instanceof ICommandListener) { 193 final ICommandListener commandListener = (ICommandListener) associatedListener; 194 command.removeCommandListener(commandListener); 195 } 196 } 197 } | Popular Tags |