Lines 18-23
Link Here
|
18 |
import java.io.File; |
18 |
import java.io.File; |
19 |
import java.io.IOException; |
19 |
import java.io.IOException; |
20 |
import java.lang.ref.WeakReference; |
20 |
import java.lang.ref.WeakReference; |
|
|
21 |
import java.lang.reflect.Field; |
22 |
import java.lang.reflect.Method; |
21 |
import java.util.Arrays; |
23 |
import java.util.Arrays; |
22 |
import java.util.Collection; |
24 |
import java.util.Collection; |
23 |
import java.util.Collections; |
25 |
import java.util.Collections; |
Lines 30-39
Link Here
|
30 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
33 |
import javax.swing.Action; |
32 |
import javax.swing.KeyStroke; |
34 |
import javax.swing.KeyStroke; |
|
|
35 |
import javax.swing.text.Keymap; |
33 |
import junit.framework.*; |
36 |
import junit.framework.*; |
34 |
import org.netbeans.junit.*; |
37 |
import org.netbeans.junit.*; |
35 |
import org.openide.ErrorManager; |
38 |
import org.openide.ErrorManager; |
36 |
import org.openide.cookies.InstanceCookie; |
39 |
import org.openide.cookies.InstanceCookie; |
|
|
40 |
import org.openide.filesystems.FileLock; |
37 |
import org.openide.filesystems.FileObject; |
41 |
import org.openide.filesystems.FileObject; |
38 |
import org.openide.filesystems.FileSystem; |
42 |
import org.openide.filesystems.FileSystem; |
39 |
import org.openide.filesystems.LocalFileSystem; |
43 |
import org.openide.filesystems.LocalFileSystem; |
Lines 63-77
Link Here
|
63 |
super(s); |
67 |
super(s); |
64 |
} |
68 |
} |
65 |
|
69 |
|
|
|
70 |
ShortcutsFolder sf = null; |
71 |
FileSystem fs = null; |
72 |
FileObject fld = null; |
73 |
FileObject fo = null; |
74 |
|
75 |
static Repository repository = null; |
76 |
protected void setUp () { |
77 |
kmap = new NbKeymap(); |
78 |
fs = createTestingFilesystem(); |
79 |
fo = getFolderForShortcuts(fs); |
80 |
sf = createShortcutsFolder(fs); |
81 |
sf.waitShortcutsFinished(); |
82 |
} |
83 |
|
84 |
protected void tearDown() { |
85 |
try { |
86 |
FileLock lock = fo.lock(); |
87 |
fo.delete(lock); |
88 |
lock.releaseLock(); |
89 |
} catch (Exception ioe) { |
90 |
} |
91 |
|
92 |
ShortcutsFolder.shortcutsFolder = null; |
93 |
sf = null; |
94 |
fs = null; |
95 |
fld = null; |
96 |
fo = null; |
97 |
try { |
98 |
if (dir.exists()) { |
99 |
File[] f = dir.listFiles(); |
100 |
for (int i=0; i < f.length; i++) { |
101 |
f[i].delete(); |
102 |
} |
103 |
dir.delete(); |
104 |
} |
105 |
dir = null; |
106 |
folderName = null; |
107 |
repository = null; |
108 |
|
109 |
} catch (Exception e) { |
110 |
throw new RuntimeException(e); |
111 |
} |
112 |
} |
113 |
|
66 |
public void testHyphenation (String s) { |
114 |
public void testHyphenation (String s) { |
|
|
115 |
System.out.println("testHyphenation"); |
67 |
HashSet set = new HashSet(); |
116 |
HashSet set = new HashSet(); |
68 |
char[] c = new String("ABCD").toCharArray(); |
117 |
char[] c = new String("ABCD").toCharArray(); |
69 |
ShortcutsFolder.createHyphenatedPermutation (c, set, "-F5"); |
118 |
ShortcutsFolder.createHyphenatedPermutation (c, set, "-F5"); |
70 |
|
119 |
|
71 |
assertTrue (set.contains("A-B-C-D-F5")); |
120 |
assertTrue (set.contains("A-B-C-D-F5")); |
72 |
} |
121 |
} |
73 |
|
122 |
|
74 |
public void testPermutations () { |
123 |
public void testPermutations () { |
|
|
124 |
System.out.println("testPermutations"); |
75 |
HashSet set = new HashSet(); |
125 |
HashSet set = new HashSet(); |
76 |
|
126 |
|
77 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
127 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
Lines 89-94
Link Here
|
89 |
} |
139 |
} |
90 |
|
140 |
|
91 |
public void testPermutationsIncludeHyphenatedVariants() { |
141 |
public void testPermutationsIncludeHyphenatedVariants() { |
|
|
142 |
System.out.println("testPermutationsIncludeHyphenatedVariants"); |
92 |
HashSet set = new HashSet(); |
143 |
HashSet set = new HashSet(); |
93 |
|
144 |
|
94 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
145 |
ShortcutsFolder.getAllPossibleOrderings("BANG", "-F5", set); |
Lines 106-111
Link Here
|
106 |
} |
157 |
} |
107 |
|
158 |
|
108 |
public void testPermutationsContainConvertedWildcard () { |
159 |
public void testPermutationsContainConvertedWildcard () { |
|
|
160 |
System.out.println("testPermutationsContainConvertedWildcard "); |
109 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
161 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
110 |
? "M" : "C"; |
162 |
? "M" : "C"; |
111 |
|
163 |
|
Lines 127-132
Link Here
|
127 |
} |
179 |
} |
128 |
|
180 |
|
129 |
public void testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator () { |
181 |
public void testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator () { |
|
|
182 |
System.out.println("testPermutationsIncludeWildcardIfSpecifiedKeyIsToolkitAccelerator "); |
130 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
183 |
String targetChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
131 |
? "M" : "C"; |
184 |
? "M" : "C"; |
132 |
|
185 |
|
Lines 147-160
Link Here
|
147 |
} |
200 |
} |
148 |
} |
201 |
} |
149 |
|
202 |
|
|
|
203 |
public void testPermutationsContainConvertedAltWildcard () { |
204 |
System.out.println("testPermutationsContainConvertedWildcard"); |
205 |
String cmdChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
206 |
? "M" : "C"; |
207 |
|
208 |
String altKey = Utilities.getOperatingSystem() == Utilities.OS_MAC ? |
209 |
"C" : "A"; //NOI18N |
210 |
|
211 |
String[] s = ShortcutsFolder.getPermutations("DO-F5"); |
212 |
HashSet set = new HashSet (Arrays.asList(s)); |
213 |
set.add ("DO-F5"); //Permutations will not contain the passed value |
214 |
|
215 |
String[] permutations = new String[] { |
216 |
"OD-F5", |
217 |
"O" + cmdChar + "-F5", |
218 |
"DO-F5", |
219 |
"D-O-F5", |
220 |
"O-D-F5", |
221 |
altKey + "-D-F5", |
222 |
altKey + "-" + cmdChar + "-F5", |
223 |
altKey + "D-F5", |
224 |
}; |
225 |
|
226 |
for (int i=0; i < permutations.length; i++) { |
227 |
assertTrue ("Permutation of D"+ altKey + "-F5 not generated:" |
228 |
+ permutations[i] + "; (generated:" + set + ")", |
229 |
set.contains(permutations[i])); |
230 |
} |
231 |
} |
232 |
|
233 |
public void testDualWildcardPermutations() { |
234 |
System.out.println("testDualWildcardPermutations"); |
235 |
String cmdChar = (Utilities.getOperatingSystem() & Utilities.OS_MAC) != 0 |
236 |
? "M" : "C"; |
237 |
|
238 |
String altKey = Utilities.getOperatingSystem() == Utilities.OS_MAC ? |
239 |
"C" : "A"; //NOI18N |
240 |
|
241 |
String[] s = ShortcutsFolder.getPermutations("OD-F5"); |
242 |
HashSet set = new HashSet (Arrays.asList(s)); |
243 |
set.add ("OD-F5"); //Permutations will not contain the passed value |
244 |
|
245 |
String[] permutations = new String[] { |
246 |
"OD-F5", "O" + cmdChar + "-F5", "DO-F5", "D-O-F5", |
247 |
"O-D-F5", altKey + "-D-F5", altKey + "-" + cmdChar + "-F5", altKey + "D-F5", |
248 |
altKey + cmdChar + "-F5" |
249 |
}; |
250 |
|
251 |
for (int i=0; i < permutations.length; i++) { |
252 |
assertTrue ("Permutation of OD-F5 not generated:" |
253 |
+ permutations[i] + "; (generated:" + set + ")", |
254 |
set.contains(permutations[i])); |
255 |
} |
256 |
} |
257 |
|
258 |
public void testOPermutationOfAlt () throws Exception { |
259 |
System.out.println("testOPermutationOfAlt"); |
260 |
// FileSystem fs = createTestingFilesystem(); |
261 |
// FileObject fo = getFolderForShortcuts(fs); |
262 |
|
263 |
// assertEquals (lastDir, repository.getDefaultFileSystem().getRoot().getPath()); |
264 |
|
265 |
FileObject data1 = fo.createData("OD-F6.instance"); |
266 |
assertNotNull(data1); |
267 |
data1.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
268 |
|
269 |
FileObject data2 = fo.createData("OS-F6.instance"); |
270 |
assertNotNull(data2); |
271 |
data2.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
272 |
|
273 |
File file = new File (lastDir + folderName + File.separator + "OD-F6.instance"); |
274 |
assertTrue ("Actual file not created: " + file.getPath(), file.exists()); |
275 |
|
276 |
sf.refreshGlobalMap(); |
277 |
|
278 |
DataObject ob = DataObject.find (data1); |
279 |
assertNotNull("Data object not found: " + data1.getPath(), ob); |
280 |
|
281 |
InstanceCookie ck = (InstanceCookie) ob.getCookie(InstanceCookie.class); |
282 |
Object obj = ck.instanceCreate(); |
283 |
|
284 |
assertTrue ("InstanceCookie was not an instanceof TestAction - " + obj, obj instanceof TestAction); |
285 |
|
286 |
int mask = System.getProperty("mrj.version") == null ? KeyEvent.ALT_MASK : |
287 |
KeyEvent.CTRL_MASK; |
288 |
|
289 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F6, mask | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); |
290 |
Action action = (Action) obj; |
291 |
|
292 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
293 |
|
294 |
ShortcutsFolder.refreshGlobalMap(); |
295 |
|
296 |
FileObject now = fo.getFileObject ("OD-F6.instance"); |
297 |
//XXX WTF?? |
298 |
assertNull ("File object should be deleted - but is " + (now == null ? " null " : now.getPath()), now); |
299 |
|
300 |
assertFalse ("File still exists: " + lastDir + "OD-F6.instance", file.exists()); |
301 |
|
302 |
file = new File (lastDir + "Shortcuts" + File.separator + "OS-F6.instance"); |
303 |
assertTrue ("File should not have been deleted: " + file.getPath(), file.exists()); |
304 |
|
305 |
} |
306 |
|
307 |
|
308 |
|
309 |
private static String lastDir = null; |
310 |
private static File dir = null; |
311 |
private File getTempDir() { |
312 |
String outdir = System.getProperty("java.io.tmpdir"); //NOI18N |
313 |
if (!outdir.endsWith(File.separator)) { |
314 |
outdir += File.separator; |
315 |
} |
316 |
String dirname = Long.toHexString(System.currentTimeMillis()); |
317 |
lastDir = outdir + dirname + File.separator; |
318 |
dir = new File (outdir + dirname); |
319 |
try { |
320 |
dir.mkdir(); |
321 |
} catch (Exception ioe) { |
322 |
ioe.printStackTrace(); |
323 |
fail ("Exception creating temporary dir for tests " + dirname + " - " + ioe.getMessage()); |
324 |
} |
325 |
dir.deleteOnExit(); |
326 |
|
327 |
return dir; |
328 |
} |
329 |
|
150 |
private FileSystem createTestingFilesystem () { |
330 |
private FileSystem createTestingFilesystem () { |
151 |
FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); |
331 |
FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); |
152 |
try { |
332 |
try { |
|
|
333 |
LocalFileSystem result = new LocalFileSystem(); |
334 |
result.setRootDirectory(getTempDir()); |
335 |
repository = new Repository (result); |
336 |
fixDefaultRepository(); |
337 |
System.setProperty ("org.openide.util.Lookup", "org.netbeans.core.ShortcutsFolderTest$LKP"); |
153 |
FileObject[] arr = root.getChildren (); |
338 |
FileObject[] arr = root.getChildren (); |
154 |
for (int i = 0; i < arr.length; i++) { |
339 |
for (int i = 0; i < arr.length; i++) { |
155 |
arr[i].delete (); |
340 |
arr[i].delete (); |
156 |
} |
341 |
} |
157 |
return root.getFileSystem (); |
342 |
return result; |
158 |
} catch (Exception e) { |
343 |
} catch (Exception e) { |
159 |
e.printStackTrace(); |
344 |
e.printStackTrace(); |
160 |
fail (e.getMessage()); |
345 |
fail (e.getMessage()); |
Lines 162-173
Link Here
|
162 |
return null; |
347 |
return null; |
163 |
} |
348 |
} |
164 |
|
349 |
|
|
|
350 |
private void fixDefaultRepository () { |
351 |
try { |
352 |
Class c = ClassLoader.getSystemClassLoader().loadClass("org.openide.filesystems.ExternalUtil"); |
353 |
Method m = c.getDeclaredMethod ("setRepository", new Class[] {Repository.class}); |
354 |
m.setAccessible(true); |
355 |
m.invoke (null, new Object[] { repository }); |
356 |
} catch (Exception e) { |
357 |
throw new RuntimeException (e); |
358 |
} |
359 |
} |
360 |
|
165 |
private FileObject getFolderForShortcuts(FileSystem fs) { |
361 |
private FileObject getFolderForShortcuts(FileSystem fs) { |
166 |
FileObject result = null; |
362 |
FileObject result = null; |
167 |
try { |
363 |
try { |
168 |
result = fs.getRoot().getFileObject("Shortcuts"); |
364 |
folderName = "Shortcuts"; |
|
|
365 |
result = fs.getRoot().getFileObject(folderName); |
169 |
if (result == null) { |
366 |
if (result == null) { |
170 |
result = fs.getRoot().createFolder("Shortcuts"); |
367 |
result = fs.getRoot().createFolder(folderName); |
171 |
} |
368 |
} |
172 |
} catch (Exception e) { |
369 |
} catch (Exception e) { |
173 |
e.printStackTrace(); |
370 |
e.printStackTrace(); |
Lines 176-181
Link Here
|
176 |
return result; |
373 |
return result; |
177 |
} |
374 |
} |
178 |
|
375 |
|
|
|
376 |
private String folderName = null; |
179 |
private ShortcutsFolder createShortcutsFolder(FileSystem fs) { |
377 |
private ShortcutsFolder createShortcutsFolder(FileSystem fs) { |
180 |
try { |
378 |
try { |
181 |
DataObject dob = DataObject.find(getFolderForShortcuts(fs)); |
379 |
DataObject dob = DataObject.find(getFolderForShortcuts(fs)); |
Lines 202-221
Link Here
|
202 |
data2.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
400 |
data2.setAttribute("instanceClass", "org.netbeans.core.ShortcutsFolderTest$TestAction"); |
203 |
|
401 |
|
204 |
ShortcutsFolder sf = createShortcutsFolder(fs); |
402 |
ShortcutsFolder sf = createShortcutsFolder(fs); |
205 |
System.err.println("Created shortcuts folder " + sf); |
|
|
206 |
|
403 |
|
207 |
sf.waitShortcutsFinished(); |
404 |
sf.waitShortcutsFinished(); |
208 |
|
405 |
|
209 |
DataObject ob = DataObject.find (data1); |
406 |
DataObject ob = DataObject.find (data1); |
210 |
assertNotNull("Data object not found: " + data1.getPath(), ob); |
407 |
assertNotNull("Data object not found: " + data1.getPath(), ob); |
211 |
System.err.println("Found data object " + data1); |
|
|
212 |
|
408 |
|
213 |
InstanceCookie ck = (InstanceCookie) ob.getCookie(InstanceCookie.class); |
409 |
InstanceCookie ck = (InstanceCookie) ob.getCookie(InstanceCookie.class); |
214 |
Object obj = ck.instanceCreate(); |
410 |
Object obj = ck.instanceCreate(); |
215 |
|
411 |
|
216 |
assertTrue ("InstanceCookie was not an instanceof TestAction - " + obj, obj instanceof TestAction); |
412 |
assertTrue ("InstanceCookie was not an instanceof TestAction - " + obj, obj instanceof TestAction); |
217 |
|
413 |
|
218 |
System.err.println("Got an instance: " + obj); |
|
|
219 |
|
414 |
|
220 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.ALT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); |
415 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.ALT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); |
221 |
Action action = (Action) obj; |
416 |
Action action = (Action) obj; |
Lines 223-229
Link Here
|
223 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
418 |
ShortcutsFolder.applyChanges(Arrays.asList(new Object[] {new ShortcutsFolder.ChangeRequest (stroke, action, false)})); |
224 |
|
419 |
|
225 |
ShortcutsFolder.refreshGlobalMap(); |
420 |
ShortcutsFolder.refreshGlobalMap(); |
226 |
|
|
|
227 |
FileObject now = fo.getFileObject ("AD-F5.instance"); |
421 |
FileObject now = fo.getFileObject ("AD-F5.instance"); |
228 |
assertNull ("File object should be deleted - ", now); |
422 |
assertNull ("File object should be deleted - ", now); |
229 |
|
423 |
|
Lines 234-239
Link Here
|
234 |
public void actionPerformed (ActionEvent ae) {} |
428 |
public void actionPerformed (ActionEvent ae) {} |
235 |
} |
429 |
} |
236 |
|
430 |
|
|
|
431 |
static NbKeymap kmap = null; |
237 |
public static class LKP extends Lookup { |
432 |
public static class LKP extends Lookup { |
238 |
private javax.swing.text.Keymap keymap = new NbKeymap (); |
433 |
private javax.swing.text.Keymap keymap = new NbKeymap (); |
239 |
|
434 |
|
Lines 241-248
Link Here
|
241 |
if (ErrorManager.class == clazz) { |
436 |
if (ErrorManager.class == clazz) { |
242 |
return new EM(); |
437 |
return new EM(); |
243 |
} |
438 |
} |
244 |
if (javax.swing.text.Keymap.class == clazz) { |
439 |
if (Keymap.class == clazz) { |
245 |
return keymap; |
440 |
return kmap; |
246 |
} |
441 |
} |
247 |
return null; |
442 |
return null; |
248 |
} |
443 |
} |
Lines 252-257
Link Here
|
252 |
public Collection allInstances() { |
447 |
public Collection allInstances() { |
253 |
if (tpl.getType() == ErrorManager.class) { |
448 |
if (tpl.getType() == ErrorManager.class) { |
254 |
return Arrays.asList(new Object[] { new EM()}); |
449 |
return Arrays.asList(new Object[] { new EM()}); |
|
|
450 |
} else if (tpl.getType() == Keymap.class) { |
451 |
return Arrays.asList(new Object[] {kmap}); |
255 |
} else { |
452 |
} else { |
256 |
return Collections.EMPTY_LIST; |
453 |
return Collections.EMPTY_LIST; |
257 |
} |
454 |
} |