KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > KeyStrokeOption


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import edu.rice.cs.drjava.platform.PlatformFactory;
37 import edu.rice.cs.util.UnexpectedException;
38 import java.lang.reflect.Field JavaDoc;
39 import javax.swing.KeyStroke JavaDoc;
40 import java.awt.event.KeyEvent JavaDoc;
41 import java.awt.Event JavaDoc;
42 import java.util.Hashtable JavaDoc;
43
44 /** Class representing all configuration options with values of type KeyStroke. Only runs in the event thread, so no
45   * synchronization is necessary (or advisable).*/

46 public class KeyStrokeOption extends Option<KeyStroke JavaDoc> {
47
48   /** Storage for keystrokes.*/
49   static Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> keys = new Hashtable JavaDoc<Integer JavaDoc, String JavaDoc>();
50   public static final KeyStroke JavaDoc NULL_KEYSTROKE = KeyStroke.getKeyStroke(0, 0);
51   /** Standard constructor
52    * @param key The name of this option.
53    */

54   public KeyStrokeOption(String JavaDoc key, KeyStroke JavaDoc def) {
55     super(key,def); }
56
57   // This sets up the hashtable that has key-value pairs consisting of
58
// ascii codes and Strings that describe the ascii character and are
59
// in the form that KeyStroke.getKeyStroke(String s) requires.
60
static {
61     try {
62       Field JavaDoc[] fields = KeyEvent JavaDoc.class.getFields();
63       for (int i = 0; i < fields.length; i++) {
64         Field JavaDoc currfield = fields[i];
65         String JavaDoc name = currfield.getName();
66         if (name.startsWith("VK_")) {
67           keys.put(new Integer JavaDoc(currfield.getInt(null)), name.substring(3));
68         }
69       }
70     }
71     catch(IllegalAccessException JavaDoc iae) {
72       throw new UnexpectedException(iae);
73     }
74   }
75
76
77   /** @param s The String to be parsed, must be the string representation of
78     * the KeyStroke to be created. Uses the method KeyStroke.getKeyStroke(String s)
79     * which returns a KeyStroke if the string is correctly formatted or null
80     * otherwise.
81     * @return The KeyStroke object corresponding to the input string "s".
82     */

83   public KeyStroke JavaDoc parse(String JavaDoc s) {
84     if (s.equals("<none>")) { return NULL_KEYSTROKE; }
85
86     // Replace "command" with "meta" (OS X)
87
int cIndex = s.indexOf("command");
88     if (cIndex > -1) {
89       final StringBuilder JavaDoc sb = new StringBuilder JavaDoc(s.substring(0, cIndex));
90       sb.append("meta");
91       sb.append(s.substring(cIndex + "command".length(), s.length()));
92       s = sb.toString();
93     }
94
95     // Replace "option" with "alt" (OS X)
96
int oIndex = s.indexOf("option");
97     if (oIndex > -1) {
98       final StringBuilder JavaDoc sb = new StringBuilder JavaDoc(s.substring(0, oIndex));
99       sb.append("alt");
100       sb.append(s.substring(oIndex + "option".length(), s.length()));
101       s = sb.toString();
102     }
103
104     KeyStroke JavaDoc ks = KeyStroke.getKeyStroke(s);
105     if (ks == null) {
106       throw new OptionParseException(name, s,
107                                      "Must be a valid string representation of a Keystroke.");
108     }
109     return ks;
110   }
111
112   /** @param k The instance of class KeyStroke to be formatted.
113     * @return A String representing the KeyStroke "k".
114     */

115   public String JavaDoc format(KeyStroke JavaDoc k) {
116     if (k == NULL_KEYSTROKE) {
117       return "<none>";
118     }
119
120     // This code prints out locale specific text, which is bad!
121
// (KeyStroke.getKeystroke(s) can't parse it.)
122
/*
123     StringBuffer buf = new StringBuffer();
124     String s = KeyEvent.getKeyModifiersText(k.getModifiers()).toLowerCase();
125     s = s.replace('+', ' ');
126     if (!s.equals(""))
127       s += " ";
128     buf.append(s);
129     */

130
131     // Generate modifiers text on our own, since getKeyStroke can't parse locale-specific modifiers.
132
int modifiers = k.getModifiers();
133     boolean isMac = PlatformFactory.ONLY.isMacPlatform();
134     final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
135     if ((modifiers & Event.META_MASK) > 0) {
136       String JavaDoc meta = (! isMac) ? "meta " : "command ";
137       buf.append(meta);
138     }
139     if ((modifiers & Event.CTRL_MASK) > 0) {
140       buf.append("ctrl ");
141     }
142     if ((modifiers & Event.ALT_MASK) > 0) {
143       String JavaDoc alt = (!isMac) ? "alt " : "option ";
144       buf.append(alt);
145     }
146     if ((modifiers & Event.SHIFT_MASK) > 0) {
147       buf.append("shift ");
148     }
149
150     // If the key code is undefined, this is a "typed" unicode character
151
if (k.getKeyCode() == KeyEvent.VK_UNDEFINED) {
152       buf.append("typed ");
153       buf.append(k.getKeyChar());
154     }
155     // else this corresponds to a static KeyEvent constant
156
else {
157       // defaults to pressed
158
if (k.isOnKeyRelease()) {
159         buf.append("released ");
160       }
161       String JavaDoc key = keys.get(new Integer JavaDoc(k.getKeyCode()));
162       if (key == null) {
163         throw new IllegalArgumentException JavaDoc("Invalid keystroke");
164       }
165       if (key.equals("CONTROL") || key.equals("ALT") || key.equals("META") ||
166           key.equals("SHIFT") || key.equals("ALT_GRAPH")) {
167         return buf.toString();
168       }
169       else {
170         buf.append(key);
171         return buf.toString();
172       }
173     }
174     return buf.toString();
175   }
176 }
177
Popular Tags