-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathAccelerator.java
More file actions
171 lines (145 loc) · 5.13 KB
/
Copy pathAccelerator.java
File metadata and controls
171 lines (145 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.input;
import java.util.regex.Pattern;
import org.scijava.util.PlatformUtils;
/**
* A keyboard shortcut, consisting of a {@link KeyCode} plus
* {@link InputModifiers}.
*
* @author Curtis Rueden
* @author Barry DeZonia
*/
public class Accelerator {
/** Actual key pressed. */
private final KeyCode keyCode;
/** Key modifiers used. */
private final InputModifiers modifiers;
public Accelerator(final KeyCode keyCode, final InputModifiers modifiers) {
this.keyCode = keyCode;
this.modifiers = modifiers;
}
// -- Accelerator methods --
public KeyCode getKeyCode() {
return keyCode;
}
public InputModifiers getModifiers() {
return modifiers;
}
// -- Object methods --
@Override
public boolean equals(final Object o) {
if (!(o instanceof Accelerator)) return false;
final Accelerator acc = (Accelerator) o;
if (!getModifiers().equals(acc.getModifiers())) return false;
if (!getKeyCode().equals(acc.getKeyCode())) return false;
return true;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public String toString() {
final String modString = modifiers.toString();
final String keyString = keyCode.name();
if (modString.length() == 0) return keyString;
return modString + " " + keyString;
}
// -- Utility methods --
/**
* Creates an accelerator from the given string representation. The string
* must have the following syntax:
*
* <pre>
* <modifiers>* <keyCode>
*
* modifiers := alt | altGraph | control | meta | shift
* keyCode := {@link KeyCode} field (e.g., COMMA or UP)
* </pre>
*
* For convenience:
* <ul>
* <li>"control" may be shortened to "ctrl"</li>
* <li>"altGraph" may be shortened to "altGr"</li>
* <li>The caret character ('^') is expanded to "control " on non-Mac
* platforms, and "meta " (i.e., the Mac command key) on Mac platforms.</li>
* </ul>
* Here are some example strings:
* <ul>
* <li>"INSERT"</li>
* <li>"control DELETE"</li>
* <li>"alt shift X"</li>
* <li>"^C"</li>
* </ul>
*
* @see javax.swing.KeyStroke#getKeyStroke(String) for the syntax from which
* this one is derived.
* @see KeyCode for the complete list of special character codes.
*/
public static Accelerator create(final String acc) {
if (acc == null || acc.isEmpty()) return null;
// allow use of caret for control/command
// (e.g., "^X" to mean "control X" or "meta X")
final String a = acc.replaceAll(Pattern.quote("^"), expandedCaret());
final String[] components = a.split(" ");
// determine which modifiers are used
boolean alt = false, altGr = false;
boolean ctrl = false, meta = false, shift = false;
for (int i = 0; i < components.length - 1; i++) {
if (components[i].equalsIgnoreCase("alt")) alt = true;
else if (components[i].equalsIgnoreCase("altGr") ||
components[i].equalsIgnoreCase("altGraph"))
{
altGr = true;
}
else if (components[i].equalsIgnoreCase("control") ||
components[i].equalsIgnoreCase("ctrl"))
{
ctrl = true;
}
else if (components[i].equalsIgnoreCase("meta")) meta = true;
else if (components[i].equalsIgnoreCase("shift")) shift = true;
}
final InputModifiers modifiers =
new InputModifiers(alt, altGr, ctrl, meta, shift, false, false, false);
// upper case the key code
final String code = components[components.length - 1].toUpperCase();
final KeyCode keyCode = KeyCode.get(code);
return new Accelerator(keyCode, modifiers);
}
public static boolean isCtrlReplacedWithMeta() {
// FIXME: Relocate this platform-specific logic.
return PlatformUtils.isMac();
}
// -- Helper methods --
private static String expandedCaret() {
return isCtrlReplacedWithMeta() ? "meta " : "control ";
}
}