KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.keymap;
21 import java.awt.event.InputEvent JavaDoc;
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import javax.swing.KeyStroke JavaDoc;
27 import org.openide.ErrorManager;
28 import org.openide.util.Utilities;
29
30
31 /**
32  *
33  * @author Jan Jancura
34  */

35 class Utils {
36     
37     static KeyStroke JavaDoc[] stringToKeyStrokes2 (String JavaDoc key) {
38         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (key, " ");
39         List JavaDoc result = new ArrayList JavaDoc ();
40         key = null;
41         while (st.hasMoreTokens ()) {
42             String JavaDoc ks = st.nextToken ().trim ();
43             KeyStroke JavaDoc keyStroke = Utilities.stringToKey (ks);
44             //S ystem.out.println("1 " + ks + ">" + keyStroke);
45
if (keyStroke == null) {
46                 if (System.getProperty ("org.netbeans.optionsDialog") != null)
47                     System.out.println("no key stroke for:" + key);
48                 return null;
49             }
50             result.add (keyStroke);
51         }
52         return (KeyStroke JavaDoc[]) result.toArray
53             (new KeyStroke JavaDoc [result.size ()]);
54     }
55     
56     static String JavaDoc getKeyStrokesAsText (KeyStroke JavaDoc[] keyStrokes, String JavaDoc delim) {
57         if (keyStrokes == null) return "";
58         if (keyStrokes.length == 0) return "";
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (getKeyStrokeAsText (keyStrokes [0]));
60         int i, k = keyStrokes.length;
61         for (i = 1; i < k; i++)
62             sb.append (delim).append (getKeyStrokeAsText (keyStrokes [i]));
63         return new String JavaDoc (sb);
64     }
65
66     static KeyStroke JavaDoc getKeyStroke (String JavaDoc keyStroke) {
67         int modifiers = 0;
68         if (keyStroke.startsWith ("Ctrl+")) {
69             modifiers |= InputEvent.CTRL_DOWN_MASK;
70             keyStroke = keyStroke.substring (5);
71         }
72         if (keyStroke.startsWith ("Alt+")) {
73             modifiers |= InputEvent.ALT_DOWN_MASK;
74             keyStroke = keyStroke.substring (4);
75         }
76         if (keyStroke.startsWith ("Shift+")) {
77             modifiers |= InputEvent.SHIFT_DOWN_MASK;
78             keyStroke = keyStroke.substring (6);
79         }
80         if (keyStroke.startsWith ("Meta+")) {
81             modifiers |= InputEvent.META_DOWN_MASK;
82             keyStroke = keyStroke.substring (5);
83         }
84         KeyStroke JavaDoc ks = Utilities.stringToKey (keyStroke);
85         if (ks == null)
86             ErrorManager.getDefault ().notify (
87                 new IllegalArgumentException JavaDoc (keyStroke)
88             );
89         KeyStroke JavaDoc result = KeyStroke.getKeyStroke (ks.getKeyCode (), modifiers);
90         return result;
91     }
92     
93     static String JavaDoc getKeyStrokeAsText (KeyStroke JavaDoc keyStroke) {
94         int modifiers = keyStroke.getModifiers ();
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
96         if ((modifiers & InputEvent.CTRL_DOWN_MASK) > 0)
97             sb.append ("Ctrl+");
98         if ((modifiers & InputEvent.ALT_DOWN_MASK) > 0)
99             sb.append ("Alt+");
100         if ((modifiers & InputEvent.SHIFT_DOWN_MASK) > 0)
101             sb.append ("Shift+");
102         if ((modifiers & InputEvent.META_DOWN_MASK) > 0)
103             sb.append ("Meta+");
104         if (keyStroke.getKeyCode () != KeyEvent.VK_SHIFT &&
105             keyStroke.getKeyCode () != KeyEvent.VK_CONTROL &&
106             keyStroke.getKeyCode () != KeyEvent.VK_META &&
107             keyStroke.getKeyCode () != KeyEvent.VK_ALT &&
108             keyStroke.getKeyCode () != KeyEvent.VK_ALT_GRAPH
109         )
110             sb.append (Utilities.keyToString (
111                 KeyStroke.getKeyStroke (keyStroke.getKeyCode (), 0)
112             ));
113         return sb.toString ();
114     }
115 }
116
Popular Tags