KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > KeyMapping


1 /*
2  * KeyMapping.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: KeyMapping.java,v 1.3 2003/07/18 15:19:35 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.KeyStroke JavaDoc;
25
26 public class KeyMapping implements Constants
27 {
28     private final char keyChar;
29     private final int keyCode;
30     private final int modifiers;
31     private final Object JavaDoc command;
32
33     public KeyMapping(int keyCode, int modifiers, Object JavaDoc command)
34     {
35         this.keyChar = 0;
36         this.keyCode = keyCode;
37         this.modifiers = modifiers;
38         if (command instanceof String JavaDoc)
39             this.command = ((String JavaDoc)command).intern();
40         else
41             this.command = command;
42     }
43
44     public KeyMapping(char keyChar, Object JavaDoc command)
45     {
46         this.keyChar = keyChar;
47         this.keyCode = 0;
48         this.modifiers = 0;
49         if (command instanceof String JavaDoc)
50             this.command = ((String JavaDoc)command).intern();
51         else
52             this.command = command;
53     }
54
55     private KeyMapping(KeyStroke JavaDoc keyStroke, String JavaDoc command)
56     {
57         char c = keyStroke.getKeyChar();
58         keyChar = c == 0xffff ? 0 : c;
59         keyCode = keyStroke.getKeyCode();
60         // Mask off the bits we don't care about (Java 1.4).
61
modifiers = keyStroke.getModifiers() & 0x0f;
62         if (command != null)
63             this.command = command.intern();
64         else
65             this.command = null;
66     }
67
68     // Returns null if string can't be parsed.
69
public static KeyMapping createKeyMapping(String JavaDoc s)
70     {
71         s = s.trim();
72         String JavaDoc parameters = null;
73         int index = s.indexOf('(');
74         if (index >= 0) {
75             parameters = s.substring(index);
76             s = s.substring(0, index).trim();
77         }
78         index = s.lastIndexOf(' ');
79         if (index < 0)
80             return null;
81         String JavaDoc keyText = s.substring(0, index).trim();
82         String JavaDoc command = s.substring(index + 1).trim();
83         if (parameters != null)
84             command += parameters;
85         return createKeyMapping(keyText, command);
86     }
87
88     private static KeyMapping createKeyMapping(String JavaDoc keyText, String JavaDoc command)
89     {
90         KeyStroke JavaDoc keyStroke = Utilities.getKeyStroke(keyText);
91         if (keyStroke == null)
92             return null;
93         if (command != null)
94             command = command.trim();
95         return new KeyMapping(keyStroke, command);
96     }
97
98     public final char getKeyChar()
99     {
100         return keyChar;
101     }
102
103     public final int getKeyCode()
104     {
105         return keyCode;
106     }
107
108     public final int getModifiers()
109     {
110         return modifiers;
111     }
112
113     public final Object JavaDoc getCommand()
114     {
115         return command;
116     }
117
118     public String JavaDoc toString()
119     {
120         FastStringBuffer sb = new FastStringBuffer(64);
121         sb.append(Utilities.getKeyText(keyChar, keyCode, modifiers));
122         if (command != null) {
123             while (sb.length() < 32)
124                 sb.append(' ');
125             sb.append(command);
126         }
127         return sb.toString();
128     }
129
130     public final String JavaDoc getKeyText()
131     {
132         return Utilities.getKeyText(keyChar, keyCode, modifiers);
133     }
134 }
135
Popular Tags