root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / KeyMapping.java @ 38564
History | View | Annotate | Download (6.58 KB)
1 | 1104 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 29593 | jpiera | package org.gvsig.andami.ui.mdiFrame; |
42 | 598 | fernando | |
43 | 38564 | jjdelcerro | import java.awt.Toolkit; |
44 | import java.awt.event.InputEvent; |
||
45 | 598 | fernando | import java.awt.event.KeyEvent; |
46 | import java.util.HashMap; |
||
47 | 38564 | jjdelcerro | import java.util.Map; |
48 | 598 | fernando | |
49 | 38564 | jjdelcerro | import javax.swing.KeyStroke; |
50 | |||
51 | 29593 | jpiera | import org.gvsig.andami.messages.Messages; |
52 | 38564 | jjdelcerro | import org.slf4j.Logger; |
53 | import org.slf4j.LoggerFactory; |
||
54 | 598 | fernando | |
55 | |||
56 | 29593 | jpiera | |
57 | 598 | fernando | /**
|
58 | * Clase que sirve para obtener los codigos de las teclas de la manera que las
|
||
59 | * requieren los JMenu a partir de un caracter que representa dicha tecla
|
||
60 | *
|
||
61 | */
|
||
62 | public class KeyMapping { |
||
63 | 38564 | jjdelcerro | private static Logger logger = LoggerFactory.getLogger(KeyMapping.class); |
64 | |||
65 | 598 | fernando | /** Asocia caracteres con KeyEvents */
|
66 | 38564 | jjdelcerro | private static Map<String,Integer> keys = new HashMap<String, Integer>(); |
67 | 598 | fernando | |
68 | static {
|
||
69 | 38564 | jjdelcerro | keys.put("a", new Integer(KeyEvent.VK_A)); |
70 | keys.put("b", new Integer(KeyEvent.VK_B)); |
||
71 | keys.put("c", new Integer(KeyEvent.VK_C)); |
||
72 | keys.put("d", new Integer(KeyEvent.VK_D)); |
||
73 | keys.put("e", new Integer(KeyEvent.VK_E)); |
||
74 | keys.put("f", new Integer(KeyEvent.VK_F)); |
||
75 | keys.put("g", new Integer(KeyEvent.VK_G)); |
||
76 | keys.put("h", new Integer(KeyEvent.VK_H)); |
||
77 | keys.put("i", new Integer(KeyEvent.VK_I)); |
||
78 | keys.put("j", new Integer(KeyEvent.VK_J)); |
||
79 | keys.put("k", new Integer(KeyEvent.VK_K)); |
||
80 | keys.put("l", new Integer(KeyEvent.VK_L)); |
||
81 | keys.put("m", new Integer(KeyEvent.VK_M)); |
||
82 | keys.put("n", new Integer(KeyEvent.VK_N)); |
||
83 | keys.put("o", new Integer(KeyEvent.VK_O)); |
||
84 | keys.put("p", new Integer(KeyEvent.VK_P)); |
||
85 | keys.put("q", new Integer(KeyEvent.VK_Q)); |
||
86 | keys.put("r", new Integer(KeyEvent.VK_R)); |
||
87 | keys.put("s", new Integer(KeyEvent.VK_S)); |
||
88 | keys.put("t", new Integer(KeyEvent.VK_T)); |
||
89 | keys.put("u", new Integer(KeyEvent.VK_U)); |
||
90 | keys.put("v", new Integer(KeyEvent.VK_V)); |
||
91 | keys.put("w", new Integer(KeyEvent.VK_W)); |
||
92 | keys.put("x", new Integer(KeyEvent.VK_X)); |
||
93 | keys.put("y", new Integer(KeyEvent.VK_Y)); |
||
94 | keys.put("z", new Integer(KeyEvent.VK_Z)); |
||
95 | keys.put("0", new Integer(KeyEvent.VK_0)); |
||
96 | keys.put("1", new Integer(KeyEvent.VK_1)); |
||
97 | keys.put("2", new Integer(KeyEvent.VK_2)); |
||
98 | keys.put("3", new Integer(KeyEvent.VK_3)); |
||
99 | keys.put("4", new Integer(KeyEvent.VK_4)); |
||
100 | keys.put("5", new Integer(KeyEvent.VK_5)); |
||
101 | keys.put("6", new Integer(KeyEvent.VK_6)); |
||
102 | keys.put("7", new Integer(KeyEvent.VK_7)); |
||
103 | keys.put("8", new Integer(KeyEvent.VK_8)); |
||
104 | keys.put("9", new Integer(KeyEvent.VK_9)); |
||
105 | keys.put("+", new Integer(KeyEvent.VK_PLUS)); |
||
106 | keys.put("-", new Integer(KeyEvent.VK_MINUS)); |
||
107 | keys.put("F1", new Integer(KeyEvent.VK_F1)); |
||
108 | keys.put("F2", new Integer(KeyEvent.VK_F2)); |
||
109 | keys.put("F3", new Integer(KeyEvent.VK_F3)); |
||
110 | keys.put("F4", new Integer(KeyEvent.VK_F4)); |
||
111 | keys.put("F5", new Integer(KeyEvent.VK_F5)); |
||
112 | keys.put("F6", new Integer(KeyEvent.VK_F6)); |
||
113 | keys.put("F7", new Integer(KeyEvent.VK_F7)); |
||
114 | keys.put("F8", new Integer(KeyEvent.VK_F8)); |
||
115 | keys.put("F9", new Integer(KeyEvent.VK_F9)); |
||
116 | keys.put("F10", new Integer(KeyEvent.VK_F10)); |
||
117 | keys.put("F11", new Integer(KeyEvent.VK_F11)); |
||
118 | keys.put("F12", new Integer(KeyEvent.VK_F12)); |
||
119 | keys.put("DELETE", new Integer(KeyEvent.VK_DELETE)); |
||
120 | 598 | fernando | } |
121 | |||
122 | /**
|
||
123 | * Obtiene dado un caracter el entero correspondiente al codigo que tiene
|
||
124 | * la tecla que produce dicho caracter
|
||
125 | *
|
||
126 | * @param a caracter
|
||
127 | *
|
||
128 | * @return Codigo de la tecla asociada
|
||
129 | *
|
||
130 | * @throws RuntimeException Si el caracter no tiene una tecla asociada
|
||
131 | */
|
||
132 | public static int getKey(char a) { |
||
133 | 38564 | jjdelcerro | Integer ret = (Integer) keys.get(String.valueOf(a)); |
134 | 598 | fernando | if (ret == null) { |
135 | throw new RuntimeException(Messages.getString( |
||
136 | "KeyMapping.Caracter_no_valido") + a); //$NON-NLS-1$ |
||
137 | } |
||
138 | return ret.intValue();
|
||
139 | } |
||
140 | 38564 | jjdelcerro | |
141 | public static KeyStroke getKeyStroke(String accelerator) { |
||
142 | if( accelerator == null ) { |
||
143 | return null; |
||
144 | } |
||
145 | accelerator = accelerator.trim(); |
||
146 | if( accelerator.length() < 1 ) { |
||
147 | return null; |
||
148 | } |
||
149 | String[] parts = accelerator.toLowerCase().split("[+]"); |
||
150 | int modifiers = 0; |
||
151 | int key = 0; |
||
152 | for( int i=0; i<parts.length ; i++) { |
||
153 | String part = parts[i];
|
||
154 | if( "ctrl".equals(part) ) { |
||
155 | modifiers = modifiers | InputEvent.CTRL_MASK;
|
||
156 | } else if( "shift".equals(part) ) { |
||
157 | modifiers = modifiers | InputEvent.SHIFT_MASK;
|
||
158 | } else if( "meta".equals(part) ) { |
||
159 | modifiers = modifiers | InputEvent.META_MASK;
|
||
160 | } else if( "alt".equals(part) ) { |
||
161 | modifiers = modifiers | InputEvent.ALT_MASK;
|
||
162 | } else if( "".equals(part) ) { // + |
||
163 | key = KeyEvent.VK_PLUS;
|
||
164 | } else {
|
||
165 | Integer x = (Integer) keys.get(part); |
||
166 | if( x != null ) { |
||
167 | key = x.intValue(); |
||
168 | } else {
|
||
169 | try {
|
||
170 | throw new IllegalArgumentException(); |
||
171 | } catch (IllegalArgumentException ex) { |
||
172 | logger.info("getKeyStroke('"+accelerator+"') malformed accelerator.",ex); |
||
173 | } |
||
174 | return null; |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | if( key == 0 ) { |
||
179 | return null; |
||
180 | } |
||
181 | if( modifiers == 0 ) { |
||
182 | modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
|
||
183 | } |
||
184 | return KeyStroke.getKeyStroke(key,modifiers); |
||
185 | } |
||
186 | 598 | fernando | } |