File: ArrowKeyNavigationSupport.java

package info (click to toggle)
libjide-oss-java 3.7.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,264 kB
  • sloc: java: 89,463; xml: 268; makefile: 35
file content (253 lines) | stat: -rw-r--r-- 10,185 bytes parent folder | download | duplicates (6)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.jidesoft.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a util class to support the four arrow keys navigation in any container. To use it, you can call
 * <code><pre>
 * new ArrowKeyNavigationSupport().install(container);
 * </pre></code>
 * The container could be any container. A typical use case is the button panel. By default we used it in {@link
 * com.jidesoft.dialog.ButtonPanel} class to enable left/right/up/down key.
 * <p/>
 * By default, all components will be navigable in the container but you can further define what components are
 * navigable by using the constructor
 * <code><pre>
 * new ArrowKeyNavigationSupport(Class[] componentTypes)
 * </pre></code>
 * where componentTypes is the list of the classes of the components that you would like to be navigable. For example,
 * <code><pre>
 * new ArrowKeyNavigationSupport(new Class[]{ AbstractButton.class }).install(container);
 * </pre></code>
 * to only allow any buttons (JButton, JideButton, JCheckBox, JRadioButton) etc.
 * <p/>
 * You can also allow certain keys to be used. For example.
 * <code><pre>
 * new ArrowKeyNavigationSupport(new int[]{ KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT}).install(container);
 * </pre></code>
 * if only left and right keys are making sense to navigate in your container.
 */
public class ArrowKeyNavigationSupport {
    private int[] _keyCode = new int[]{KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_DOWN};
    private Class[] _componentTypes;
    public static final String CLIENT_PROPERTY_ARROWKEY_NAVIGATION_SUPPORT = "ArrowKeyNavigationSupport.previousAction";

    public ArrowKeyNavigationSupport() {
    }

    public ArrowKeyNavigationSupport(Class[] componentTypes) {
        _componentTypes = componentTypes;
    }

    public ArrowKeyNavigationSupport(int[] keyCodes) {
        _keyCode = keyCodes;
    }

    public ArrowKeyNavigationSupport(Class[] componentTypes, int[] keyCode) {
        _keyCode = keyCode;
        _componentTypes = componentTypes;
    }

    /**
     * Installs the actions for arrow keys to allow user to navigate components using arrow keys.
     *
     * @param container the container such as ButtonPanel, JPanel etc.
     */
    public void install(JComponent container) {
        for (int keyCode : _keyCode) {
            InputMap inputMap = container.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0);
            Object actionName = inputMap.get(keyStroke);
            if (actionName != null) {
                container.putClientProperty(CLIENT_PROPERTY_ARROWKEY_NAVIGATION_SUPPORT, actionName);
            }
            container.registerKeyboardAction(new NavigationAction(container, keyCode), "ArrowKeyNavigation " + keyCode, keyStroke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        }
    }

    /**
     * Uninstalls the actions for arrow keys.
     *
     * @param container the container such as ButtonPanel, JPanel etc.
     */
    public void uninstall(JComponent container) {
        for (int keyCode : _keyCode) {
            Object actionName = container.getClientProperty(CLIENT_PROPERTY_ARROWKEY_NAVIGATION_SUPPORT);
            if (actionName != null) {
                InputMap inputMap = container.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0);
                inputMap.put(keyStroke, actionName);
            }
            else {
                container.unregisterKeyboardAction(KeyStroke.getKeyStroke(keyCode, 0));
            }
        }
    }

    private class NavigationAction implements ActionListener {
        private JComponent _parent;
        private int _keyCode;

        public NavigationAction(JComponent c, int key) {
            _parent = c;
            _keyCode = key;
        }

        public void actionPerformed(ActionEvent e) {
            final List<Rectangle> rects = new ArrayList();
            final List<Component> components = new ArrayList();
            JideSwingUtilities.setRecursively(_parent, new JideSwingUtilities.Handler() {
                public void postAction(Component c) {
                }

                public void action(Component c) {
                    if (_componentTypes != null) {
                        boolean allowed = false;
                        for (Class allowedType : _componentTypes) {
                            if (allowedType.isAssignableFrom(c.getClass())) {
                                allowed = true;
                                break;
                            }
                        }
                        if (!allowed) return;
                    }
                    Rectangle bounds = c.getBounds();
                    rects.add(SwingUtilities.convertRectangle(c, bounds, _parent));
                    components.add(c);
                }

                public boolean condition(Component c) {
                    return (c.isVisible() && c.isDisplayable() && c.isFocusable() && c.isEnabled());
                }
            });
            Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            Component c = null;
            switch (_keyCode) {
                case KeyEvent.VK_RIGHT:
                    c = findComponentToRight(owner, rects, components);
                    break;
                case KeyEvent.VK_LEFT:
                    c = findComponentToLeft(owner, rects, components);
                    break;
                case KeyEvent.VK_UP:
                    c = findComponentToAbove(owner, rects, components);
                    break;
                case KeyEvent.VK_DOWN:
                    c = findComponentToBelow(owner, rects, components);
                    break;
            }
            if (c != null) c.requestFocusInWindow();
        }

        private Component findComponentToRight(Component c, List<Rectangle> rects, List<Component> components) {
            int max = Integer.MAX_VALUE;
            Component found = null;
            Rectangle src = SwingUtilities.convertRectangle(c, c.getBounds(), _parent);
            for (int i = 0; i < rects.size(); i++) {
                Rectangle dst = rects.get(i);
                if (dst.x <= src.x + src.width) { // not on the left
                    continue;
                }
                else if (dst.y + dst.height < src.y) { // on top
                    continue;
                }
                else if (dst.y > src.y + src.height) { // on bottom
                    continue;
                }

                int dist = dst.x - src.x - src.width;
                if (dist < max) {
                    max = dist;
                    found = components.get(i);
                }
            }

            return found;
        }

        private Component findComponentToBelow(Component c, List<Rectangle> rects, List<Component> components) {
            int max = Integer.MAX_VALUE;
            Component found = null;
            Rectangle src = SwingUtilities.convertRectangle(c, c.getBounds(), _parent);
            for (int i = 0; i < rects.size(); i++) {
                Rectangle dst = rects.get(i);
                if (dst.y <= src.y + src.height) { // not on the left
                    continue;
                }
                else if (dst.x + dst.width < src.x) { // on top
                    continue;
                }
                else if (dst.x > src.x + src.width) { // on bottom
                    continue;
                }

                int dist = dst.y - src.y - src.height;
                if (dist < max) {
                    max = dist;
                    found = components.get(i);
                }
            }

            return found;
        }

        private Component findComponentToLeft(Component c, List<Rectangle> rects, List<Component> components) {
            int max = Integer.MAX_VALUE;
            Component found = null;
            Rectangle src = SwingUtilities.convertRectangle(c, c.getBounds(), _parent);
            for (int i = 0; i < rects.size(); i++) {
                Rectangle dst = rects.get(i);
                if (dst.x + dst.width >= src.x) { // not on the right
                    continue;
                }
                else if (dst.y + dst.height < src.y) { // on top
                    continue;
                }
                else if (dst.y > src.y + src.height) { // on bottom
                    continue;
                }

                int dist = src.x - dst.x - dst.width;
                if (dist < max) {
                    max = dist;
                    found = components.get(i);
                }
            }

            return found;
        }

        private Component findComponentToAbove(Component c, List<Rectangle> rects, List<Component> components) {
            int max = Integer.MAX_VALUE;
            Component found = null;
            Rectangle src = SwingUtilities.convertRectangle(c, c.getBounds(), _parent);
            for (int i = 0; i < rects.size(); i++) {
                Rectangle dst = rects.get(i);
                if (dst.y + dst.height >= src.y) { // not on the above
                    continue;
                }
                else if (dst.x + dst.width < src.x) { // on left
                    continue;
                }
                else if (dst.x > src.x + src.width) { // on right
                    continue;
                }

                int dist = src.y - dst.y - dst.height;
                if (dist < max) {
                    max = dist;
                    found = components.get(i);
                }
            }

            return found;
        }

    }
}