1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.jivalo.editor.key;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import javax.swing.KeyStroke;
29
30 /**
31 * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
32 *
33 */
34 public final class CustomKeyBinding
35 {
36
37 /**
38 * Name of key bindings
39 */
40 private String name;
41
42 private String keyBindingFileName;
43
44 private Map keyBindings;
45
46 /**
47 * @param name
48 * @param keyBindingFileName
49 * @throws NullPointerException if name or keyBindingFileName is null.
50 */
51 public CustomKeyBinding( String name, String keyBindingFileName )
52 {
53 super();
54 if ( keyBindingFileName == null )
55 {
56 throw new NullPointerException( "keyBindingFileName" );
57 }
58 if ( name == null )
59 {
60 throw new NullPointerException( "name" );
61 }
62
63 this.keyBindingFileName = keyBindingFileName;
64 this.name = name;
65 }
66
67 public Map getKeyBindings()
68 {
69 if ( this.keyBindings == null )
70 {
71 this.keyBindings = getBindings();
72 }
73
74 return this.keyBindings;
75 }
76
77 /**
78 * @return
79 */
80 private Map getBindings()
81 {
82
83 HashMap map = new HashMap();
84
85 Properties properties = getProperties();
86
87 for ( Iterator iter = properties.entrySet().iterator(); iter.hasNext(); )
88 {
89 Map.Entry element = ( Map.Entry ) iter.next();
90
91 String actionName = ( ( String )element.getKey() ).trim();
92
93 String keyStroke = ( ( String )element.getValue() ).trim();
94
95 KeyAction keyAction =
96 DefaultEditorKitActionFactory.getAction( actionName );
97
98 KeyStroke key = getKeyStroke( keyStroke );
99
100 if ( keyAction != null && key != null )
101 {
102 map.put( key, keyAction.getAction() );
103 }
104 }
105
106 return map;
107 }
108
109 /**
110 * @param keyStroke
111 * @return
112 */
113 private KeyStroke getKeyStroke( String keyStroke )
114 {
115
116 KeyStroke key = null;
117
118 String[] strings = keyStroke.split( "," );
119
120 KeyCode keyCode = KeyEventKeyCodeFactory.getKeyCode( strings[0].trim() );
121
122 if ( keyCode != null )
123 {
124 int modifiers = 0;
125
126 if ( strings.length == 2 )
127 {
128 String[] masks = strings[1].trim().split( "[|]" );
129
130 InputEventKeyModifier[] keyModifiers =
131 new InputEventKeyModifier[masks.length];
132
133 for ( int i = 0; i < masks.length; i++ )
134 {
135 keyModifiers[i] = InputEventKeyModifier.valueOf( masks[i].trim() );
136 modifiers = modifiers | keyModifiers[i].getModifier();
137 }
138 }
139
140 key = KeyStroke.getKeyStroke( keyCode.getKeyCode(), modifiers );
141 }
142
143 return key;
144 }
145
146 private Properties getProperties()
147 {
148
149 InputStream is = null;
150
151 if ( this.keyBindingFileName.startsWith( "/" ) )
152 {
153 is = this.getClass().getResourceAsStream( this.keyBindingFileName );
154 }
155 else
156 {
157 File file = new File( this.keyBindingFileName );
158
159 try
160 {
161 is = new FileInputStream( file );
162 }
163 catch ( FileNotFoundException e )
164 {
165
166 e.printStackTrace();
167 }
168 }
169
170 Properties props = new Properties();
171
172 if ( is != null )
173 {
174 try
175 {
176 props.load( is );
177 }
178 catch ( IOException e )
179 {
180
181 e.printStackTrace();
182 }
183 }
184
185 return props;
186 }
187
188 /**
189 * @see java.lang.Object#toString()
190 */
191 public String toString()
192 {
193 return this.name;
194 }
195
196 }