1 /*******************************************************************************
2  * Copyright (c) 2004, 2015 IBM Corporation and others.
3  *
4  * 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  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jface.bindings.keys;
15 
16 import org.eclipse.core.commands.ParameterizedCommand;
17 import org.eclipse.jface.bindings.Binding;
18 import org.eclipse.jface.bindings.TriggerSequence;
19 
20 /**
21  * <p>
22  * A keyboard shortcut. This is a binding between some keyboard input and the
23  * triggering of a command. This object is immutable.
24  * </p>
25  *
26  * @since 3.1
27  */
28 public final class KeyBinding extends Binding {
29 
30 	/**
31 	 * The key sequence which triggers this binding. This sequence is never
32 	 * <code>null</code>.
33 	 */
34 	private final KeySequence keySequence;
35 
36 	/**
37 	 * Constructs a new instance of <code>KeyBinding</code>.
38 	 *
39 	 * @param keySequence
40 	 *            The key sequence which should trigger this binding. This value
41 	 *            must not be <code>null</code>. It also must be a complete,
42 	 *            non-empty key sequence.
43 	 * @param command
44 	 *            The parameterized command to which this binding applies; this
45 	 *            value may be <code>null</code> if the binding is meant to
46 	 *            "unbind" a previously defined binding.
47 	 * @param schemeId
48 	 *            The scheme to which this binding belongs; this value must not
49 	 *            be <code>null</code>.
50 	 * @param contextId
51 	 *            The context to which this binding applies; this value must not
52 	 *            be <code>null</code>.
53 	 * @param locale
54 	 *            The locale to which this binding applies; this value may be
55 	 *            <code>null</code> if it applies to all locales.
56 	 * @param platform
57 	 *            The platform to which this binding applies; this value may be
58 	 *            <code>null</code> if it applies to all platforms.
59 	 * @param windowManager
60 	 *            The window manager to which this binding applies; this value
61 	 *            may be <code>null</code> if it applies to all window
62 	 *            managers. This value is currently ignored.
63 	 * @param type
64 	 *            The type of binding. This should be either <code>SYSTEM</code>
65 	 *            or <code>USER</code>.
66 	 */
KeyBinding(final KeySequence keySequence, final ParameterizedCommand command, final String schemeId, final String contextId, final String locale, final String platform, final String windowManager, final int type)67 	public KeyBinding(final KeySequence keySequence,
68 			final ParameterizedCommand command, final String schemeId,
69 			final String contextId, final String locale, final String platform,
70 			final String windowManager, final int type) {
71 		super(command, schemeId, contextId, locale, platform, windowManager,
72 				type);
73 
74 		if (keySequence == null) {
75 			throw new NullPointerException("The key sequence cannot be null"); //$NON-NLS-1$
76 		}
77 
78 		if (!keySequence.isComplete()) {
79 			throw new IllegalArgumentException(
80 					"Cannot bind to an incomplete key sequence"); //$NON-NLS-1$
81 		}
82 
83 		if (keySequence.isEmpty()) {
84 			throw new IllegalArgumentException(
85 					"Cannot bind to an empty key sequence"); //$NON-NLS-1$
86 		}
87 
88 		this.keySequence = keySequence;
89 	}
90 
91 	/**
92 	 * Returns the key sequence which triggers this binding. The key sequence
93 	 * will not be <code>null</code>, empty or incomplete.
94 	 *
95 	 * @return The key sequence; never <code>null</code>.
96 	 */
getKeySequence()97 	public final KeySequence getKeySequence() {
98 		return keySequence;
99 	}
100 
101 	@Override
getTriggerSequence()102 	public TriggerSequence getTriggerSequence() {
103 		return getKeySequence();
104 	}
105 }
106