01: /* SwingML
02: * Copyright (C) 2005 Bobby Walters.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the
16: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17: * Boston, MA 02111-1307, USA.
18: *
19: * Authors:
20: * Bobby Walters
21: *
22: */
23:
24: package org.swingml.event;
25:
26: import java.awt.*;
27: import java.awt.event.*;
28:
29: import javax.swing.*;
30:
31: import org.swingml.registry.*;
32:
33: /**
34: * A key adapter used to capture the return (enter) key press and
35: * issue a <code>JButton.doClick()</code> using the name of the
36: * button supplied at construction of this adapter.
37: *
38: * @author Bobby Walters
39: */
40: public final class ReturnKeyAdapter extends KeyAdapter {
41:
42: /**
43: * Name of the button to issue a doClick on.
44: */
45: private String button;
46:
47: /**
48: * Creates a key adapter that will use the supplied button name.
49: *
50: * @param buttonName Button name
51: */
52: public ReturnKeyAdapter(final String buttonName) {
53: button = buttonName;
54: }
55:
56: /**
57: * Checks the key event for the return (enter) key press and issues
58: * a <code>JButton.doClick()</code>.
59: *
60: * @param ke The key event that was triggered
61: */
62: public void keyPressed(final KeyEvent ke) {
63: if (KeyEvent.VK_ENTER == ke.getKeyCode()) {
64: Container possibleButton = SwingMLModelToContainerRegistry
65: .getContainer(button);
66: if (possibleButton != null
67: && possibleButton instanceof JButton) {
68: JButton theButton = (JButton) possibleButton;
69: theButton.doClick();
70: }
71: }
72: }
73: }
|