1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.keybindings;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import org.osgi.framework.*;
import net.java.sip.communicator.service.fileaccess.*;
import net.java.sip.communicator.service.keybindings.*;
import net.java.sip.communicator.util.*;
/**
* Service that concerns keybinding mappings used by various parts of the UI.
* Persistence is handled as follows when started:
* <ol>
* <li>Load default bindings from relative directory</li>
* <li>Attempt to load custom bindings and resolve any duplicates</li>
* <li>If merged bindings differ from the custom bindings then this attempts to
* save the merged version</li>
* </ol>
* Custom bindings attempt to be written again whenever they're changed if the
* service is running. Each category of keybindings are stored in its own file.
*
* @author Damian Johnson
*/
class KeybindingsServiceImpl
implements KeybindingsService, Observer
{
/**
* The <tt>Logger</tt> instance used by the
* <tt>KeybindingsServiceImpl</tt> class and its instances for logging
* output.
*/
private static final Logger logger =
Logger.getLogger(KeybindingsServiceImpl.class);
/**
* Name of the relative directory that holds default bindings.
*/
private static final String DEFAULT_KEYBINDING_DIR =
"/resources/config/defaultkeybindings";
/**
* Name of the directory that holds custom bindings.
*/
private static final String CUSTOM_KEYBINDING_DIR = "keybindings";
/**
* Flag indicating if service is running.
*/
private boolean isRunning = false;
/**
* Loaded keybinding mappings, maps to null if defaults failed to be loaded.
*/
private final HashMap<KeybindingSet.Category, KeybindingSetImpl> bindings =
new HashMap<KeybindingSet.Category, KeybindingSetImpl>();
/**
* Starts the KeybindingService, for each keybinding category retrieving the
* default bindings then overwriting them with any custom bindings that can
* be retrieved. This writes the merged copy back if it differs from the
* custom bindings. This is a no-op if the service has already been started.
*
* @param bc the currently valid OSGI bundle context.
*/
synchronized void start(BundleContext bc)
{
if (this.isRunning)
return;
for (KeybindingSet.Category category : KeybindingSet.Category.values())
{
// Retrieves default bindings
Persistence format = category.getFormat();
LinkedHashMap<KeyStroke, String> defaultBindings;
try
{
String defaultPath =
DEFAULT_KEYBINDING_DIR + "/" + category.getResource();
InputStream in = getClass().getResourceAsStream(defaultPath);
defaultBindings = format.load(in);
}
catch (IOException exc)
{
logger.error("default bindings set missing: "
+ category.getResource(), exc);
this.bindings.put(category, null);
continue;
}
catch (ParseException exc)
{
logger.error("unable to parse default bindings set: "
+ category.getResource(), exc);
this.bindings.put(category, null);
continue;
}
// Attempts to retrieve custom bindings
String customPath =
CUSTOM_KEYBINDING_DIR + "/" + category.getResource();
File customFile;
try
{
ServiceReference faServiceReference =
bc.getServiceReference(FileAccessService.class.getName());
FileAccessService faService =
(FileAccessService) bc.getService(faServiceReference);
// Makes directory for custom bindings if it doesn't exist
File customDir =
faService
.getPrivatePersistentDirectory(CUSTOM_KEYBINDING_DIR);
if (!customDir.exists())
customDir.mkdir();
// Gets file access service to reference persistent storage
// of the user
customFile = faService.getPrivatePersistentFile(customPath);
}
catch (Exception exc)
{
String msg =
"unable to secure file for custom bindings (" + customPath
+ "), using defaults but won't be able to save changes";
logger.error(msg, exc);
KeybindingSetImpl newSet =
new KeybindingSetImpl(defaultBindings, category, null);
this.bindings.put(category, newSet);
newSet.addObserver(this);
continue;
}
LinkedHashMap<KeyStroke, String> customBindings = null;
if (customFile.exists())
{
try
{
FileInputStream in = new FileInputStream(customFile);
customBindings = format.load(in);
in.close();
}
catch (Exception exc)
{
// If either an IO or ParseException occur then we skip
// loading custom bindings
}
}
// Merges custom bindings
LinkedHashMap<KeyStroke, String> merged =
new LinkedHashMap<KeyStroke, String>();
if (customBindings != null)
{
Map<KeyStroke, String> customTmp =
new LinkedHashMap<KeyStroke, String>(customBindings);
for (Map.Entry<KeyStroke, String> shortcut2action : defaultBindings
.entrySet())
{
String action = shortcut2action.getValue();
if (customTmp.containsValue(action))
{
KeyStroke custom = null;
for (Map.Entry<KeyStroke, String> customShortcut2action : customTmp
.entrySet())
{
if (customShortcut2action.getValue().equals(action))
{
custom = customShortcut2action.getKey();
break;
}
}
assert custom != null;
customTmp.remove(custom);
merged.put(custom, action);
}
else
{
merged.put(shortcut2action.getKey(), action);
}
}
}
else
{
merged = defaultBindings;
}
// Writes merged result
if (!merged.equals(customBindings))
{
try
{
FileOutputStream out = new FileOutputStream(customFile);
format.save(out, merged);
out.close();
}
catch (IOException exc)
{
logger.error("unable to write to: "
+ customFile.getAbsolutePath(), exc);
}
}
KeybindingSetImpl newSet =
new KeybindingSetImpl(merged, category, customFile);
this.bindings.put(category, newSet);
newSet.addObserver(this);
}
this.isRunning = true;
}
/**
* Invalidates references to custom bindings, preventing further writes.
*/
synchronized void stop()
{
if (!this.isRunning)
return;
for (KeybindingSetImpl bindingSet : this.bindings.values())
{
bindingSet.invalidate();
}
this.bindings.clear();
this.isRunning = false;
}
/**
* Provides the bindings associated with a given category. This may be null
* if the default bindings failed to be loaded.
*
* @param category segment of the UI for which bindings should be retrieved
* @return mappings of keystrokes to the string representation of their
* actions
* @throws UnsupportedOperationException if the service isn't running
*/
public synchronized KeybindingSet getBindings(
KeybindingSet.Category category)
{
if (!this.isRunning)
throw new UnsupportedOperationException();
// Started services should have all categories
assert this.bindings.containsKey(category);
return this.bindings.get(category);
}
/**
* Listens for changes in binding sets so changes can be written.
*/
public void update(Observable obs, Object arg)
{
if (obs instanceof KeybindingSetImpl)
{
KeybindingSetImpl changedBindings = (KeybindingSetImpl) obs;
// Attempts to avoid lock if unwritable (this works since bindings
// can't become re-writable)
if (changedBindings.isWritable())
{
synchronized (this)
{
if (changedBindings.isWritable())
{
// Writes new bindings to custom file
File customFile = changedBindings.getCustomFile();
try
{
FileOutputStream out =
new FileOutputStream(customFile);
Persistence format =
changedBindings.getCategory().getFormat();
format.save(out, changedBindings.getBindings());
out.close();
}
catch (IOException exc)
{
logger.error("unable to write to: "
+ customFile.getAbsolutePath(), exc);
}
}
}
}
}
}
}
|