Skip to content

Package: KeyBindingManager$1

KeyBindingManager$1

nameinstructionbranchcomplexitylinemethod
keyPressed(KeyEvent)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Mat Hansen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.swt.table.action;
15:
16: import java.util.Collection;
17: import java.util.LinkedHashMap;
18: import java.util.LinkedList;
19: import java.util.Map;
20:
21: import org.eclipse.emfforms.spi.swt.table.action.Action;
22: import org.eclipse.emfforms.spi.swt.table.action.ActionConfiguration;
23: import org.eclipse.jface.bindings.keys.KeyStroke;
24: import org.eclipse.jface.viewers.Viewer;
25: import org.eclipse.swt.events.KeyAdapter;
26: import org.eclipse.swt.events.KeyEvent;
27: import org.eclipse.swt.events.KeyListener;
28:
29: /**
30: * Key binding management class for viewer actions.
31: *
32: * @author Mat Hansen <mhansen@eclipsesource.com>
33: * @since 1.18
34: *
35: */
36: public class KeyBindingManager {
37:
38:         /**
39:          * Internal key binding adapter.
40:          */
41:         private class ActionKeyBinding {
42:
43:                 private final KeyStroke keyStroke;
44:                 private final Action action;
45:
46:                 ActionKeyBinding(Action action, KeyStroke keyStroke) {
47:                         this.action = action;
48:                         this.keyStroke = keyStroke;
49:                 }
50:
51:                 public void keyPressed(KeyEvent e) {
52:                         final KeyStroke current = KeyStroke.getInstance(e.stateMask, e.keyCode);
53:                         if (keyStroke.equals(current)) {
54:                                 invokeAction();
55:                         }
56:                 }
57:
58:                 private void invokeAction() {
59:                         if (action.canExecute()) {
60:                                 action.execute();
61:                         }
62:                 }
63:
64:         }
65:
66:         private final Collection<ActionKeyBinding> keyBindings = new LinkedList<ActionKeyBinding>();
67:         private final Map<Viewer, KeyListener> viewerBindings = new LinkedHashMap<Viewer, KeyListener>();
68:
69:         /**
70:          * Bind a new key binding to an {@link Action}.
71:          *
72:          * @param action the action to bind
73:          * @param keyStroke the key stroke to bind to
74:          */
75:         public void bindAction(Action action, KeyStroke keyStroke) {
76:                 keyBindings.add(new ActionKeyBinding(action, keyStroke));
77:         }
78:
79:         /**
80:          * Apply action bindings stored within an {@link ActionConfiguration}.
81:          *
82:          * @param configuration the {@link ActionConfiguration} to apply
83:          */
84:         public void applyActionConfiguration(ActionConfiguration configuration) {
85:                 for (final Action action : configuration.getActions()) {
86:                         if (configuration.hasKeyStrokesFor(action)) {
87:                                 for (final KeyStroke keyStroke : configuration.getKeyStrokesFor(action)) {
88:                                         bindAction(action, keyStroke);
89:                                 }
90:                         }
91:                 }
92:         }
93:
94:         /**
95:          * Bind all known actions to a viewer.
96:          *
97:          * @param viewer the viewer to bind to
98:          */
99:         public void bindToViewer(Viewer viewer) {
100:                 if (viewerBindings.containsKey(viewer)) {
101:                         throw new IllegalArgumentException("Already bound to the given viewer"); //$NON-NLS-1$
102:                 }
103:
104:                 final KeyAdapter listener = new KeyAdapter() {
105:                         @Override
106:                         public void keyPressed(KeyEvent e) {
107:•                                for (final ActionKeyBinding binding : keyBindings) {
108:                                         binding.keyPressed(e);
109:                                 }
110:                         }
111:                 };
112:                 viewer.getControl().addKeyListener(listener);
113:                 viewerBindings.put(viewer, listener);
114:         }
115:
116:         /**
117:          * Unbind from viewer.
118:          *
119:          * @param viewer the viewer to unbind from
120:          */
121:         public void unbindFromViewer(Viewer viewer) {
122:                 if (!viewerBindings.containsKey(viewer)) {
123:                         return; // silently ignore this
124:                 }
125:                 final KeyListener listener = viewerBindings.get(viewer);
126:                 viewer.getControl().removeKeyListener(listener);
127:                 viewerBindings.remove(viewer);
128:         }
129:
130: }