001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.keys;
011:
012: import java.io.IOException;
013: import java.util.Map;
014:
015: import org.eclipse.core.commands.ParameterizedCommand;
016: import org.eclipse.core.commands.common.NotDefinedException;
017: import org.eclipse.jface.bindings.Binding;
018: import org.eclipse.jface.bindings.BindingManager;
019: import org.eclipse.jface.bindings.Scheme;
020: import org.eclipse.jface.bindings.TriggerSequence;
021: import org.eclipse.jface.bindings.keys.SWTKeySupport;
022: import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.swt.widgets.Listener;
026: import org.eclipse.ui.commands.ICommandService;
027: import org.eclipse.ui.internal.Workbench;
028: import org.eclipse.ui.internal.WorkbenchPlugin;
029: import org.eclipse.ui.keys.IBindingService;
030:
031: /**
032: * <p>
033: * Provides services related to the binding architecture (e.g., keyboard
034: * shortcuts) within the workbench. This service can be used to access the
035: * currently active bindings, as well as the current state of the binding
036: * architecture.
037: * </p>
038: *
039: * @since 3.1
040: */
041: public final class BindingService implements IBindingService {
042:
043: /**
044: * The binding manager that supports this service. This value is never
045: * <code>null</code>.
046: */
047: private final BindingManager bindingManager;
048:
049: /**
050: * The persistence class responsible for bindings.
051: */
052: private final BindingPersistence bindingPersistence;
053:
054: /**
055: * The key binding support for the contexts. In the workbench, key bindings
056: * are intimately tied to the context mechanism.
057: */
058: private WorkbenchKeyboard keyboard;
059:
060: /**
061: * Constructs a new instance of <code>BindingService</code> using a JFace
062: * binding manager.
063: *
064: * @param bindingManager
065: * The bind ing manager to use; must not be <code>null</code>.
066: * @param commandService
067: * The command service providing support for this service; must
068: * not be <code>null</code>;
069: * @param workbench
070: * The workbench on which this context service will act; must not
071: * be <code>null</code>.
072: */
073: public BindingService(final BindingManager bindingManager,
074: final ICommandService commandService,
075: final Workbench workbench) {
076: if (bindingManager == null) {
077: throw new NullPointerException(
078: "Cannot create a binding service with a null manager"); //$NON-NLS-1$
079: }
080: if (commandService == null) {
081: throw new NullPointerException(
082: "Cannot create a binding service with a null command service"); //$NON-NLS-1$
083: }
084: this .bindingManager = bindingManager;
085: this .bindingPersistence = new BindingPersistence(
086: bindingManager, commandService);
087:
088: // Hook up the key binding support.
089: keyboard = new WorkbenchKeyboard(workbench);
090: final Display display = workbench.getDisplay();
091: final Listener listener = keyboard.getKeyDownFilter();
092: display.addFilter(SWT.KeyDown, listener);
093: display.addFilter(SWT.Traverse, listener);
094:
095: // Initialize the key formatter.
096: KeyFormatterFactory.setDefault(SWTKeySupport
097: .getKeyFormatterForPlatform());
098: }
099:
100: /**
101: * TODO Promote this method to API.
102: * <p>
103: * Adds a single new binding to the existing array of bindings. If the array
104: * is currently <code>null</code>, then a new array is created and this
105: * binding is added to it. This method does not detect duplicates.
106: * </p>
107: * <p>
108: * This method completes in amortized <code>O(1)</code>.
109: * </p>
110: *
111: * @param binding
112: * The binding to be added; must not be <code>null</code>.
113: */
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
125: .getActiveBindingsFor(parameterizedCommand);
126: }
127:
128: public final TriggerSequence[] getActiveBindingsFor(
129: final String commandId) {
130: return bindingManager.getActiveBindingsFor(commandId);
131: }
132:
133: public final Scheme getActiveScheme() {
134: return bindingManager.getActiveScheme();
135: }
136:
137: public final TriggerSequence getBestActiveBindingFor(
138: final String commandId) {
139: return bindingManager.getBestActiveBindingFor(commandId);
140: }
141:
142: public final String getBestActiveBindingFormattedFor(
143: final String commandId) {
144: return bindingManager
145: .getBestActiveBindingFormattedFor(commandId);
146: }
147:
148: public final Binding[] getBindings() {
149: return bindingManager.getBindings();
150: }
151:
152: public final TriggerSequence getBuffer() {
153: return keyboard.getBuffer();
154: }
155:
156: public final String getDefaultSchemeId() {
157: return BindingPersistence.getDefaultSchemeId();
158: }
159:
160: public final Scheme[] getDefinedSchemes() {
161: return bindingManager.getDefinedSchemes();
162: }
163:
164: /**
165: * Returns the key binding architecture for the workbench. This method is
166: * internal, and is only intended for testing. This must not be used by
167: * clients.
168: *
169: * @return The key binding support; never <code>null</code>.
170: */
171: public final WorkbenchKeyboard getKeyboard() {
172: return keyboard;
173: }
174:
175: public final String getLocale() {
176: return bindingManager.getLocale();
177: }
178:
179: public final Map getPartialMatches(final TriggerSequence trigger) {
180: return bindingManager.getPartialMatches(trigger);
181: }
182:
183: public final Binding getPerfectMatch(final TriggerSequence trigger) {
184: return bindingManager.getPerfectMatch(trigger);
185: }
186:
187: public final String getPlatform() {
188: return bindingManager.getPlatform();
189: }
190:
191: public final Scheme getScheme(final String schemeId) {
192: return bindingManager.getScheme(schemeId);
193: }
194:
195: public final boolean isKeyFilterEnabled() {
196: return keyboard.getKeyDownFilter().isEnabled();
197: }
198:
199: public final boolean isPartialMatch(final TriggerSequence sequence) {
200: return bindingManager.isPartialMatch(sequence);
201: }
202:
203: public final boolean isPerfectMatch(final TriggerSequence sequence) {
204: return bindingManager.isPerfectMatch(sequence);
205: }
206:
207: public final void openKeyAssistDialog() {
208: keyboard.openMultiKeyAssistShell();
209: }
210:
211: public final void readRegistryAndPreferences(
212: final ICommandService commandService) {
213: bindingPersistence.read();
214: }
215:
216: /**
217: * Remove the specific binding by identity. Does nothing if the binding is
218: * not in the manager.
219: *
220: * @param binding
221: * The binding to be removed; must not be <code>null</code>.
222: */
223: public final void removeBinding(final Binding binding) {
224: bindingManager.removeBinding(binding);
225: }
226:
227: public final void savePreferences(final Scheme activeScheme,
228: final Binding[] bindings) throws IOException {
229: BindingPersistence.write(activeScheme, bindings);
230: try {
231: bindingManager.setActiveScheme(activeScheme);
232: } catch (final NotDefinedException e) {
233: WorkbenchPlugin.log(
234: "The active scheme is not currently defined.", //$NON-NLS-1$
235: WorkbenchPlugin.getStatus(e));
236: }
237: bindingManager.setBindings(bindings);
238: }
239:
240: public final void setKeyFilterEnabled(final boolean enabled) {
241: keyboard.getKeyDownFilter().setEnabled(enabled);
242: }
243:
244: }
|