KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > BindingService


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.keys;
12
13 import java.io.IOException JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
33  * <p>
34  * Provides services related to the binding architecture (e.g., keyboard
35  * shortcuts) within the workbench. This service can be used to access the
36  * currently active bindings, as well as the current state of the binding
37  * architecture.
38  * </p>
39  *
40  * @since 3.1
41  */

42 public final class BindingService implements IBindingService {
43
44     /**
45      * The binding manager that supports this service. This value is never
46      * <code>null</code>.
47      */

48     private final BindingManager bindingManager;
49
50     /**
51      * The persistence class responsible for bindings.
52      */

53     private final BindingPersistence bindingPersistence;
54
55     /**
56      * The key binding support for the contexts. In the workbench, key bindings
57      * are intimately tied to the context mechanism.
58      */

59     private WorkbenchKeyboard keyboard;
60
61     /**
62      * Constructs a new instance of <code>BindingService</code> using a JFace
63      * binding manager.
64      *
65      * @param bindingManager
66      * The bind ing manager to use; must not be <code>null</code>.
67      * @param commandService
68      * The command service providing support for this service; must
69      * not be <code>null</code>;
70      * @param workbench
71      * The workbench on which this context service will act; must not
72      * be <code>null</code>.
73      */

74     public BindingService(final BindingManager bindingManager,
75             final ICommandService commandService, final Workbench workbench) {
76         if (bindingManager == null) {
77             throw new NullPointerException JavaDoc(
78                     "Cannot create a binding service with a null manager"); //$NON-NLS-1$
79
}
80         if (commandService == null) {
81             throw new NullPointerException JavaDoc(
82                     "Cannot create a binding service with a null command service"); //$NON-NLS-1$
83
}
84         this.bindingManager = bindingManager;
85         this.bindingPersistence = new BindingPersistence(bindingManager,
86                 commandService);
87
88         // Hook up the key binding support.
89
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         // Initialize the key formatter.
96
KeyFormatterFactory.setDefault(SWTKeySupport
97                 .getKeyFormatterForPlatform());
98     }
99
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.getActiveBindingsFor(parameterizedCommand);
125     }
126
127     public final TriggerSequence[] getActiveBindingsFor(final String JavaDoc 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 JavaDoc commandId) {
136         return bindingManager.getBestActiveBindingFor(commandId);
137     }
138
139     public final String JavaDoc getBestActiveBindingFormattedFor(final String JavaDoc 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 JavaDoc getDefaultSchemeId() {
152         return BindingPersistence.getDefaultSchemeId();
153     }
154
155     public final Scheme[] getDefinedSchemes() {
156         return bindingManager.getDefinedSchemes();
157     }
158
159     /**
160      * Returns the key binding architecture for the workbench. This method is
161      * internal, and is only intended for testing. This must not be used by
162      * clients.
163      *
164      * @return The key binding support; never <code>null</code>.
165      */

166     public final WorkbenchKeyboard getKeyboard() {
167         return keyboard;
168     }
169
170     public final String JavaDoc getLocale() {
171         return bindingManager.getLocale();
172     }
173
174     public final Map JavaDoc 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 JavaDoc getPlatform() {
183         return bindingManager.getPlatform();
184     }
185
186     public final Scheme getScheme(final String JavaDoc 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     /**
212      * Remove the specific binding by identity. Does nothing if the binding is
213      * not in the manager.
214      *
215      * @param binding
216      * The binding to be removed; must not be <code>null</code>.
217      */

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 JavaDoc {
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.", //$NON-NLS-1$
229
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