1 11 package org.eclipse.ui.internal.keys; 12 13 import java.io.IOException ; 14 import java.util.Map ; 15 16 import org.eclipse.core.commands.ParameterizedCommand; 17 import org.eclipse.core.commands.common.NotDefinedException; 18 import org.eclipse.jface.bindings.Binding; 19 import org.eclipse.jface.bindings.BindingManager; 20 import org.eclipse.jface.bindings.Scheme; 21 import org.eclipse.jface.bindings.TriggerSequence; 22 import org.eclipse.jface.bindings.keys.SWTKeySupport; 23 import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.swt.widgets.Listener; 27 import org.eclipse.ui.commands.ICommandService; 28 import org.eclipse.ui.internal.Workbench; 29 import org.eclipse.ui.internal.WorkbenchPlugin; 30 import org.eclipse.ui.keys.IBindingService; 31 32 42 public final class BindingService implements IBindingService { 43 44 48 private final BindingManager bindingManager; 49 50 53 private final BindingPersistence bindingPersistence; 54 55 59 private WorkbenchKeyboard keyboard; 60 61 74 public BindingService(final BindingManager bindingManager, 75 final ICommandService commandService, final Workbench workbench) { 76 if (bindingManager == null) { 77 throw new NullPointerException ( 78 "Cannot create a binding service with a null manager"); } 80 if (commandService == null) { 81 throw new NullPointerException ( 82 "Cannot create a binding service with a null command service"); } 84 this.bindingManager = bindingManager; 85 this.bindingPersistence = new BindingPersistence(bindingManager, 86 commandService); 87 88 keyboard = new WorkbenchKeyboard(workbench); 90 final Display display = workbench.getDisplay(); 91 final Listener listener = keyboard.getKeyDownFilter(); 92 display.addFilter(SWT.KeyDown, listener); 93 display.addFilter(SWT.Traverse, listener); 94 95 KeyFormatterFactory.setDefault(SWTKeySupport 97 .getKeyFormatterForPlatform()); 98 } 99 100 114 public final void addBinding(final Binding binding) { 115 bindingManager.addBinding(binding); 116 } 117 118 public final void dispose() { 119 bindingPersistence.dispose(); 120 } 121 122 public final TriggerSequence[] getActiveBindingsFor( 123 final ParameterizedCommand parameterizedCommand) { 124 return bindingManager.getActiveBindingsFor(parameterizedCommand); 125 } 126 127 public final TriggerSequence[] getActiveBindingsFor(final String commandId) { 128 return bindingManager.getActiveBindingsFor(commandId); 129 } 130 131 public final Scheme getActiveScheme() { 132 return bindingManager.getActiveScheme(); 133 } 134 135 public final TriggerSequence getBestActiveBindingFor(final String commandId) { 136 return bindingManager.getBestActiveBindingFor(commandId); 137 } 138 139 public final String getBestActiveBindingFormattedFor(final String commandId) { 140 return bindingManager.getBestActiveBindingFormattedFor(commandId); 141 } 142 143 public final Binding[] getBindings() { 144 return bindingManager.getBindings(); 145 } 146 147 public final TriggerSequence getBuffer() { 148 return keyboard.getBuffer(); 149 } 150 151 public final String getDefaultSchemeId() { 152 return BindingPersistence.getDefaultSchemeId(); 153 } 154 155 public final Scheme[] getDefinedSchemes() { 156 return bindingManager.getDefinedSchemes(); 157 } 158 159 166 public final WorkbenchKeyboard getKeyboard() { 167 return keyboard; 168 } 169 170 public final String getLocale() { 171 return bindingManager.getLocale(); 172 } 173 174 public final Map getPartialMatches(final TriggerSequence trigger) { 175 return bindingManager.getPartialMatches(trigger); 176 } 177 178 public final Binding getPerfectMatch(final TriggerSequence trigger) { 179 return bindingManager.getPerfectMatch(trigger); 180 } 181 182 public final String getPlatform() { 183 return bindingManager.getPlatform(); 184 } 185 186 public final Scheme getScheme(final String schemeId) { 187 return bindingManager.getScheme(schemeId); 188 } 189 190 public final boolean isKeyFilterEnabled() { 191 return keyboard.getKeyDownFilter().isEnabled(); 192 } 193 194 public final boolean isPartialMatch(final TriggerSequence sequence) { 195 return bindingManager.isPartialMatch(sequence); 196 } 197 198 public final boolean isPerfectMatch(final TriggerSequence sequence) { 199 return bindingManager.isPerfectMatch(sequence); 200 } 201 202 public final void openKeyAssistDialog() { 203 keyboard.openMultiKeyAssistShell(); 204 } 205 206 public final void readRegistryAndPreferences( 207 final ICommandService commandService) { 208 bindingPersistence.read(); 209 } 210 211 218 public final void removeBinding(final Binding binding) { 219 bindingManager.removeBinding(binding); 220 } 221 222 public final void savePreferences(final Scheme activeScheme, 223 final Binding[] bindings) throws IOException { 224 BindingPersistence.write(activeScheme, bindings); 225 try { 226 bindingManager.setActiveScheme(activeScheme); 227 } catch (final NotDefinedException e) { 228 WorkbenchPlugin.log("The active scheme is not currently defined.", WorkbenchPlugin.getStatus(e)); 230 } 231 bindingManager.setBindings(bindings); 232 } 233 234 public final void setKeyFilterEnabled(final boolean enabled) { 235 keyboard.getKeyDownFilter().setEnabled(enabled); 236 } 237 238 } 239 | Popular Tags |