KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > Persistence


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.commands;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.TreeMap JavaDoc;
20
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.commands.IHandler;
25 import org.eclipse.ui.internal.commands.ws.HandlerProxy;
26 import org.eclipse.ui.internal.util.Util;
27 import org.eclipse.ui.keys.KeySequence;
28 import org.eclipse.ui.keys.KeyStroke;
29 import org.eclipse.ui.keys.ParseException;
30 import org.eclipse.ui.keys.SWTKeySupport;
31
32 final class Persistence {
33
34     private static Map JavaDoc stringToValueMap = new TreeMap JavaDoc();
35
36     static {
37         stringToValueMap.put("BACKSPACE", new Integer JavaDoc(8)); //$NON-NLS-1$
38
stringToValueMap.put("TAB", new Integer JavaDoc(9)); //$NON-NLS-1$
39
stringToValueMap.put("RETURN", new Integer JavaDoc(13)); //$NON-NLS-1$
40
stringToValueMap.put("ENTER", new Integer JavaDoc(13)); //$NON-NLS-1$
41
stringToValueMap.put("ESCAPE", new Integer JavaDoc(27)); //$NON-NLS-1$
42
stringToValueMap.put("ESC", new Integer JavaDoc(27)); //$NON-NLS-1$
43
stringToValueMap.put("DELETE", new Integer JavaDoc(127)); //$NON-NLS-1$
44
stringToValueMap.put("SPACE", new Integer JavaDoc(' ')); //$NON-NLS-1$
45
stringToValueMap.put("ARROW_UP", new Integer JavaDoc(SWT.ARROW_UP)); //$NON-NLS-1$
46
stringToValueMap.put("ARROW_DOWN", new Integer JavaDoc(SWT.ARROW_DOWN)); //$NON-NLS-1$
47
stringToValueMap.put("ARROW_LEFT", new Integer JavaDoc(SWT.ARROW_LEFT)); //$NON-NLS-1$
48
stringToValueMap.put("ARROW_RIGHT", new Integer JavaDoc(SWT.ARROW_RIGHT)); //$NON-NLS-1$
49
stringToValueMap.put("PAGE_UP", new Integer JavaDoc(SWT.PAGE_UP)); //$NON-NLS-1$
50
stringToValueMap.put("PAGE_DOWN", new Integer JavaDoc(SWT.PAGE_DOWN)); //$NON-NLS-1$
51
stringToValueMap.put("HOME", new Integer JavaDoc(SWT.HOME)); //$NON-NLS-1$
52
stringToValueMap.put("END", new Integer JavaDoc(SWT.END)); //$NON-NLS-1$
53
stringToValueMap.put("INSERT", new Integer JavaDoc(SWT.INSERT)); //$NON-NLS-1$
54
stringToValueMap.put("F1", new Integer JavaDoc(SWT.F1)); //$NON-NLS-1$
55
stringToValueMap.put("F2", new Integer JavaDoc(SWT.F2)); //$NON-NLS-1$
56
stringToValueMap.put("F3", new Integer JavaDoc(SWT.F3)); //$NON-NLS-1$
57
stringToValueMap.put("F4", new Integer JavaDoc(SWT.F4)); //$NON-NLS-1$
58
stringToValueMap.put("F5", new Integer JavaDoc(SWT.F5)); //$NON-NLS-1$
59
stringToValueMap.put("F6", new Integer JavaDoc(SWT.F6)); //$NON-NLS-1$
60
stringToValueMap.put("F7", new Integer JavaDoc(SWT.F7)); //$NON-NLS-1$
61
stringToValueMap.put("F8", new Integer JavaDoc(SWT.F8)); //$NON-NLS-1$
62
stringToValueMap.put("F9", new Integer JavaDoc(SWT.F9)); //$NON-NLS-1$
63
stringToValueMap.put("F10", new Integer JavaDoc(SWT.F10)); //$NON-NLS-1$
64
stringToValueMap.put("F11", new Integer JavaDoc(SWT.F11)); //$NON-NLS-1$
65
stringToValueMap.put("F12", new Integer JavaDoc(SWT.F12)); //$NON-NLS-1$
66
}
67
68     private final static String JavaDoc ALT = "Alt"; //$NON-NLS-1$
69
private final static String JavaDoc COMMAND = "Command"; //$NON-NLS-1$
70
private final static String JavaDoc CTRL = "Ctrl"; //$NON-NLS-1$
71
private final static String JavaDoc MODIFIER_SEPARATOR = "+"; //$NON-NLS-1$
72

73     final static String JavaDoc PACKAGE_BASE = "commands"; //$NON-NLS-1$
74
final static String JavaDoc PACKAGE_PREFIX = "org.eclipse.ui"; //$NON-NLS-1$
75
final static String JavaDoc PACKAGE_FULL = PACKAGE_PREFIX + '.' + PACKAGE_BASE;
76     private final static String JavaDoc SHIFT = "Shift"; //$NON-NLS-1$
77

78     final static String JavaDoc TAG_ACTIVE_KEY_CONFIGURATION = "activeKeyConfiguration"; //$NON-NLS-1$
79
final static String JavaDoc TAG_CATEGORY = "category"; //$NON-NLS-1$
80
final static String JavaDoc TAG_CATEGORY_ID = "categoryId"; //$NON-NLS-1$
81
final static String JavaDoc TAG_COMMAND = "command"; //$NON-NLS-1$
82
final static String JavaDoc TAG_COMMAND_ID = "commandId"; //$NON-NLS-1$
83
final static String JavaDoc TAG_CONTEXT_ID = "contextId"; //$NON-NLS-1$
84
final static String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
85
final static String JavaDoc TAG_HANDLER = "handlerSubmission"; //$NON-NLS-1$
86
final static String JavaDoc TAG_ID = "id"; //$NON-NLS-1$
87
final static String JavaDoc TAG_KEY_CONFIGURATION = "keyConfiguration"; //$NON-NLS-1$
88
final static String JavaDoc TAG_KEY_CONFIGURATION_ID = "keyConfigurationId"; //$NON-NLS-1$
89
final static String JavaDoc TAG_KEY_SEQUENCE = "keySequence"; //$NON-NLS-1$
90
// TODO keyBinding -> keySequenceBinding
91
final static String JavaDoc TAG_KEY_SEQUENCE_BINDING = "keyBinding"; //$NON-NLS-1$
92
final static String JavaDoc TAG_LOCALE = "locale"; //$NON-NLS-1$
93
final static String JavaDoc TAG_NAME = "name"; //$NON-NLS-1$
94
final static String JavaDoc TAG_PARENT_ID = "parentId"; //$NON-NLS-1$
95
final static String JavaDoc TAG_PLATFORM = "platform"; //$NON-NLS-1$
96
final static String JavaDoc TAG_SOURCE_ID = "sourceId"; //$NON-NLS-1$
97

98     private static KeySequence deprecatedSequenceToKeySequence(int[] sequence) {
99         List JavaDoc keyStrokes = new ArrayList JavaDoc();
100
101         for (int i = 0; i < sequence.length; i++)
102             keyStrokes.add(deprecatedStrokeToKeyStroke(sequence[i]));
103
104         return KeySequence.getInstance(keyStrokes);
105     }
106
107     private static KeyStroke deprecatedStrokeToKeyStroke(int stroke) {
108         return SWTKeySupport.convertAcceleratorToKeyStroke(stroke);
109     }
110
111     private static int[] parseDeprecatedSequence(String JavaDoc string) {
112         if (string == null)
113             throw new NullPointerException JavaDoc();
114
115         StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(string);
116         int length = stringTokenizer.countTokens();
117         int[] strokes = new int[length];
118
119         for (int i = 0; i < length; i++)
120             strokes[i] = parseDeprecatedStroke(stringTokenizer.nextToken());
121
122         return strokes;
123     }
124
125     private static int parseDeprecatedStroke(String JavaDoc string) {
126         if (string == null)
127             throw new NullPointerException JavaDoc();
128
129         List JavaDoc list = new ArrayList JavaDoc();
130         StringTokenizer JavaDoc stringTokenizer =
131             new StringTokenizer JavaDoc(string, MODIFIER_SEPARATOR, true);
132
133         while (stringTokenizer.hasMoreTokens())
134             list.add(stringTokenizer.nextToken());
135
136         int size = list.size();
137         int value = 0;
138
139         if (size % 2 == 1) {
140             String JavaDoc token = (String JavaDoc) list.get(size - 1);
141             Integer JavaDoc integer =
142                 (Integer JavaDoc) stringToValueMap.get(token.toUpperCase());
143
144             if (integer != null)
145                 value = integer.intValue();
146             else if (token.length() == 1)
147                 value = token.toUpperCase().charAt(0);
148
149             if (value != 0) {
150                 for (int i = 0; i < size - 1; i++) {
151                     token = (String JavaDoc) list.get(i);
152
153                     if (i % 2 == 0) {
154                         if (token.equalsIgnoreCase(CTRL)) {
155                             if ((value & SWT.CTRL) != 0)
156                                 return 0;
157
158                             value |= SWT.CTRL;
159                         } else if (token.equalsIgnoreCase(ALT)) {
160                             if ((value & SWT.ALT) != 0)
161                                 return 0;
162
163                             value |= SWT.ALT;
164                         } else if (token.equalsIgnoreCase(SHIFT)) {
165                             if ((value & SWT.SHIFT) != 0)
166                                 return 0;
167
168                             value |= SWT.SHIFT;
169                         } else if (token.equalsIgnoreCase(COMMAND)) {
170                             if ((value & SWT.COMMAND) != 0)
171                                 return 0;
172
173                             value |= SWT.COMMAND;
174                         } else
175                             return 0;
176                     } else if (!MODIFIER_SEPARATOR.equals(token))
177                         return 0;
178                 }
179             }
180         }
181
182         return value;
183     }
184
185     static ActiveKeyConfigurationDefinition readActiveKeyConfigurationDefinition(
186         IMemento memento,
187         String JavaDoc sourceIdOverride) {
188         if (memento == null)
189             throw new NullPointerException JavaDoc();
190
191         String JavaDoc keyConfigurationId = memento.getString(TAG_KEY_CONFIGURATION_ID);
192
193         // TODO deprecated start
194
if (keyConfigurationId == null)
195             keyConfigurationId = memento.getString("value"); //$NON-NLS-1$
196
// TODO deprecated end
197

198         String JavaDoc sourceId =
199             sourceIdOverride != null
200                 ? sourceIdOverride
201                 : memento.getString(TAG_SOURCE_ID);
202
203         // TODO deprecated start
204
if (sourceIdOverride == null && sourceId == null)
205             sourceId = memento.getString("plugin"); //$NON-NLS-1$
206
// TODO deprecated end
207

208         return new ActiveKeyConfigurationDefinition(
209             keyConfigurationId,
210             sourceId);
211     }
212
213     static List JavaDoc readActiveKeyConfigurationDefinitions(
214         IMemento memento,
215         String JavaDoc name,
216         String JavaDoc sourceIdOverride) {
217         if (memento == null || name == null)
218             throw new NullPointerException JavaDoc();
219
220         IMemento[] mementos = memento.getChildren(name);
221
222         if (mementos == null)
223             throw new NullPointerException JavaDoc();
224
225         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
226
227         for (int i = 0; i < mementos.length; i++)
228             list.add(
229                 readActiveKeyConfigurationDefinition(
230                     mementos[i],
231                     sourceIdOverride));
232
233         return list;
234     }
235
236     static CategoryDefinition readCategoryDefinition(
237         IMemento memento,
238         String JavaDoc sourceIdOverride) {
239         if (memento == null)
240             throw new NullPointerException JavaDoc();
241
242         String JavaDoc description = memento.getString(TAG_DESCRIPTION);
243         String JavaDoc id = memento.getString(TAG_ID);
244         String JavaDoc name = memento.getString(TAG_NAME);
245         String JavaDoc sourceId =
246             sourceIdOverride != null
247                 ? sourceIdOverride
248                 : memento.getString(TAG_SOURCE_ID);
249
250         // TODO deprecated start
251
if (sourceIdOverride == null && sourceId == null)
252             sourceId = memento.getString("plugin"); //$NON-NLS-1$
253
// TODO deprecated end
254

255         return new CategoryDefinition(description, id, name, sourceId);
256     }
257
258     static List JavaDoc readCategoryDefinitions(
259         IMemento memento,
260         String JavaDoc name,
261         String JavaDoc sourceIdOverride) {
262         if (memento == null || name == null)
263             throw new NullPointerException JavaDoc();
264
265         IMemento[] mementos = memento.getChildren(name);
266
267         if (mementos == null)
268             throw new NullPointerException JavaDoc();
269
270         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
271
272         for (int i = 0; i < mementos.length; i++)
273             list.add(readCategoryDefinition(mementos[i], sourceIdOverride));
274
275         return list;
276     }
277
278     static CommandDefinition readCommandDefinition(
279         IMemento memento,
280         String JavaDoc sourceIdOverride) {
281         if (memento == null)
282             throw new NullPointerException JavaDoc();
283
284         String JavaDoc categoryId = memento.getString(TAG_CATEGORY_ID);
285
286         // TODO deprecated start
287
if (categoryId == null)
288             categoryId = memento.getString("category"); //$NON-NLS-1$
289
// TODO deprecated end
290

291         String JavaDoc description = memento.getString(TAG_DESCRIPTION);
292         String JavaDoc id = memento.getString(TAG_ID);
293         String JavaDoc name = memento.getString(TAG_NAME);
294         String JavaDoc sourceId =
295             sourceIdOverride != null
296                 ? sourceIdOverride
297                 : memento.getString(TAG_SOURCE_ID);
298
299         // TODO deprecated start
300
if (sourceIdOverride == null && sourceId == null)
301             sourceId = memento.getString("plugin"); //$NON-NLS-1$
302
// TODO deprecated end
303

304         return new CommandDefinition(
305             categoryId,
306             description,
307             id,
308             name,
309             sourceId);
310     }
311
312     static List JavaDoc readCommandDefinitions(
313         IMemento memento,
314         String JavaDoc name,
315         String JavaDoc sourceIdOverride) {
316         if (memento == null || name == null)
317             throw new NullPointerException JavaDoc();
318
319         IMemento[] mementos = memento.getChildren(name);
320
321         if (mementos == null)
322             throw new NullPointerException JavaDoc();
323
324         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
325
326         for (int i = 0; i < mementos.length; i++)
327             list.add(readCommandDefinition(mementos[i], sourceIdOverride));
328
329         return list;
330     }
331
332     private static int[] readDeprecatedSequence(IMemento memento) {
333         if (memento == null)
334             throw new NullPointerException JavaDoc();
335
336         IMemento[] mementos = memento.getChildren("stroke"); //$NON-NLS-1$
337

338         if (mementos == null)
339             throw new NullPointerException JavaDoc();
340
341         int[] strokes = new int[mementos.length];
342
343         for (int i = 0; i < mementos.length; i++)
344             strokes[i] = readDeprecatedStroke(mementos[i]);
345
346         return strokes;
347     }
348
349     private static int readDeprecatedStroke(IMemento memento) {
350         if (memento == null)
351             throw new NullPointerException JavaDoc();
352
353         Integer JavaDoc value = memento.getInteger("value"); //$NON-NLS-1$
354
return value != null ? value.intValue() : 0;
355     }
356
357     /**
358      * Reads the handler from XML, and creates a proxy to contain it. The proxy
359      * will only instantiate the handler when the handler is first asked for
360      * information.
361      *
362      * @param configurationElement
363      * The configuration element to read; must not be
364      * <code>null</code>.
365      * @return The handler proxy for the given definition; never
366      * <code>null</code>.
367      */

368     static IHandler readHandlerSubmissionDefinition(
369             IConfigurationElement configurationElement) {
370         final String JavaDoc commandId = configurationElement
371                 .getAttribute(TAG_COMMAND_ID);
372
373         return new HandlerProxy(commandId, configurationElement);
374     }
375
376     static KeyConfigurationDefinition readKeyConfigurationDefinition(
377         IMemento memento,
378         String JavaDoc sourceIdOverride) {
379         if (memento == null)
380             throw new NullPointerException JavaDoc();
381
382         String JavaDoc description = memento.getString(TAG_DESCRIPTION);
383         String JavaDoc id = memento.getString(TAG_ID);
384         String JavaDoc name = memento.getString(TAG_NAME);
385         String JavaDoc parentId = memento.getString(TAG_PARENT_ID);
386
387         // TODO deprecated start
388
if (parentId == null)
389             parentId = memento.getString("parent"); //$NON-NLS-1$
390
// TODO deprecated end
391

392         String JavaDoc sourceId =
393             sourceIdOverride != null
394                 ? sourceIdOverride
395                 : memento.getString(TAG_SOURCE_ID);
396
397         // TODO deprecated start
398
if (sourceIdOverride == null && sourceId == null)
399             sourceId = memento.getString("plugin"); //$NON-NLS-1$
400
// TODO deprecated end
401

402         return new KeyConfigurationDefinition(
403             description,
404             id,
405             name,
406             parentId,
407             sourceId);
408     }
409
410     static List JavaDoc readKeyConfigurationDefinitions(
411         IMemento memento,
412         String JavaDoc name,
413         String JavaDoc sourceIdOverride) {
414         if (memento == null || name == null)
415             throw new NullPointerException JavaDoc();
416
417         IMemento[] mementos = memento.getChildren(name);
418
419         if (mementos == null)
420             throw new NullPointerException JavaDoc();
421
422         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
423
424         for (int i = 0; i < mementos.length; i++)
425             list.add(
426                 readKeyConfigurationDefinition(mementos[i], sourceIdOverride));
427
428         return list;
429     }
430
431     static KeySequenceBindingDefinition readKeySequenceBindingDefinition(
432         IMemento memento,
433         String JavaDoc sourceIdOverride) {
434         if (memento == null)
435             throw new NullPointerException JavaDoc();
436
437         String JavaDoc contextId = memento.getString(TAG_CONTEXT_ID);
438
439         // TODO deprecated start
440
if (contextId == null)
441             contextId = memento.getString("scope"); //$NON-NLS-1$
442

443         if ("org.eclipse.ui.globalScope".equals(contextId)) //$NON-NLS-1$
444
contextId = null;
445         // TODO deprecated end
446

447         String JavaDoc commandId = memento.getString(TAG_COMMAND_ID);
448
449         // TODO deprecated start
450
if (commandId == null)
451             commandId = memento.getString("command"); //$NON-NLS-1$
452

453         if (commandId == null)
454             commandId = memento.getString("id"); //$NON-NLS-1$
455
// TODO deprecated end
456

457         String JavaDoc keyConfigurationId = memento.getString(TAG_KEY_CONFIGURATION_ID);
458
459         // TODO deprecated start
460
if (keyConfigurationId == null)
461             keyConfigurationId = memento.getString("configuration"); //$NON-NLS-1$
462
// TODO deprecated end
463

464         KeySequence keySequence = null;
465         String JavaDoc keySequenceAsString = memento.getString(TAG_KEY_SEQUENCE);
466
467         if (keySequenceAsString != null)
468             try {
469                 keySequence = KeySequence.getInstance(keySequenceAsString);
470             } catch (ParseException eParse) {
471             }
472         // TODO deprecated start
473
else {
474             IMemento mementoSequence = memento.getChild("sequence"); //$NON-NLS-1$
475

476             if (mementoSequence != null)
477                 keySequence =
478                     deprecatedSequenceToKeySequence(
479                         readDeprecatedSequence(mementoSequence));
480             else {
481                 String JavaDoc string = memento.getString("string"); //$NON-NLS-1$
482

483                 if (string != null)
484                     keySequence =
485                         deprecatedSequenceToKeySequence(
486                             parseDeprecatedSequence(string));
487             }
488             // TODO deprecated end
489
}
490
491         String JavaDoc locale = memento.getString(TAG_LOCALE);
492         String JavaDoc platform = memento.getString(TAG_PLATFORM);
493         String JavaDoc sourceId =
494             sourceIdOverride != null
495                 ? sourceIdOverride
496                 : memento.getString(TAG_SOURCE_ID);
497
498         // TODO deprecated start
499
if (sourceIdOverride == null && sourceId == null)
500             sourceId = memento.getString("plugin"); //$NON-NLS-1$
501
// TODO deprecated end
502

503         // We treat null context identifiers as the window context.
504
if (contextId == null) {
505             contextId = KeySequenceBinding.DEFAULT_CONTEXT_ID;
506         }
507
508         return new KeySequenceBindingDefinition(
509             contextId,
510             commandId,
511             keyConfigurationId,
512             keySequence,
513             locale,
514             platform,
515             sourceId);
516     }
517
518     static List JavaDoc readKeySequenceBindingDefinitions(
519         IMemento memento,
520         String JavaDoc name,
521         String JavaDoc sourceIdOverride) {
522         if (memento == null || name == null)
523             throw new NullPointerException JavaDoc();
524
525         IMemento[] mementos = memento.getChildren(name);
526
527         if (mementos == null)
528             throw new NullPointerException JavaDoc();
529
530         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
531
532         for (int i = 0; i < mementos.length; i++)
533             list.add(
534                 readKeySequenceBindingDefinition(
535                     mementos[i],
536                     sourceIdOverride));
537
538         return list;
539     }
540
541     static void writeActiveKeyConfigurationDefinition(
542         IMemento memento,
543         ActiveKeyConfigurationDefinition activeKeyConfigurationDefinition) {
544         if (memento == null || activeKeyConfigurationDefinition == null)
545             throw new NullPointerException JavaDoc();
546
547         memento.putString(
548             TAG_KEY_CONFIGURATION_ID,
549             activeKeyConfigurationDefinition.getKeyConfigurationId());
550         memento.putString(
551             TAG_SOURCE_ID,
552             activeKeyConfigurationDefinition.getSourceId());
553     }
554
555     static void writeActiveKeyConfigurationDefinitions(
556         IMemento memento,
557         String JavaDoc name,
558         List JavaDoc activeKeyConfigurationDefinitions) {
559         if (memento == null
560             || name == null
561             || activeKeyConfigurationDefinitions == null)
562             throw new NullPointerException JavaDoc();
563
564         activeKeyConfigurationDefinitions =
565             new ArrayList JavaDoc(activeKeyConfigurationDefinitions);
566         Iterator JavaDoc iterator = activeKeyConfigurationDefinitions.iterator();
567
568         while (iterator.hasNext())
569             Util.assertInstance(
570                 iterator.next(),
571                 ActiveKeyConfigurationDefinition.class);
572
573         iterator = activeKeyConfigurationDefinitions.iterator();
574
575         while (iterator.hasNext())
576             writeActiveKeyConfigurationDefinition(
577                 memento.createChild(name),
578                 (ActiveKeyConfigurationDefinition) iterator.next());
579     }
580
581     static void writeCategoryDefinition(
582         IMemento memento,
583         CategoryDefinition categoryDefinition) {
584         if (memento == null || categoryDefinition == null)
585             throw new NullPointerException JavaDoc();
586
587         memento.putString(TAG_DESCRIPTION, categoryDefinition.getDescription());
588         memento.putString(TAG_ID, categoryDefinition.getId());
589         memento.putString(TAG_NAME, categoryDefinition.getName());
590         memento.putString(TAG_SOURCE_ID, categoryDefinition.getSourceId());
591     }
592
593     static void writeCategoryDefinitions(
594         IMemento memento,
595         String JavaDoc name,
596         List JavaDoc categoryDefinitions) {
597         if (memento == null || name == null || categoryDefinitions == null)
598             throw new NullPointerException JavaDoc();
599
600         categoryDefinitions = new ArrayList JavaDoc(categoryDefinitions);
601         Iterator JavaDoc iterator = categoryDefinitions.iterator();
602
603         while (iterator.hasNext())
604             Util.assertInstance(iterator.next(), CategoryDefinition.class);
605
606         iterator = categoryDefinitions.iterator();
607
608         while (iterator.hasNext())
609             writeCategoryDefinition(
610                 memento.createChild(name),
611                 (CategoryDefinition) iterator.next());
612     }
613
614     static void writeCommandDefinition(
615         IMemento memento,
616         CommandDefinition commandDefinition) {
617         if (memento == null || commandDefinition == null)
618             throw new NullPointerException JavaDoc();
619
620         memento.putString(TAG_CATEGORY_ID, commandDefinition.getCategoryId());
621         memento.putString(TAG_DESCRIPTION, commandDefinition.getDescription());
622         memento.putString(TAG_ID, commandDefinition.getId());
623         memento.putString(TAG_NAME, commandDefinition.getName());
624         memento.putString(TAG_SOURCE_ID, commandDefinition.getSourceId());
625     }
626
627     static void writeCommandDefinitions(
628         IMemento memento,
629         String JavaDoc name,
630         List JavaDoc commandDefinitions) {
631         if (memento == null || name == null || commandDefinitions == null)
632             throw new NullPointerException JavaDoc();
633
634         commandDefinitions = new ArrayList JavaDoc(commandDefinitions);
635         Iterator JavaDoc iterator = commandDefinitions.iterator();
636
637         while (iterator.hasNext())
638             Util.assertInstance(iterator.next(), CommandDefinition.class);
639
640         iterator = commandDefinitions.iterator();
641
642         while (iterator.hasNext())
643             writeCommandDefinition(
644                 memento.createChild(name),
645                 (CommandDefinition) iterator.next());
646     }
647
648     static void writeKeyConfigurationDefinition(
649         IMemento memento,
650         KeyConfigurationDefinition keyConfigurationDefinition) {
651         if (memento == null || keyConfigurationDefinition == null)
652             throw new NullPointerException JavaDoc();
653
654         memento.putString(
655             TAG_DESCRIPTION,
656             keyConfigurationDefinition.getDescription());
657         memento.putString(TAG_ID, keyConfigurationDefinition.getId());
658         memento.putString(TAG_NAME, keyConfigurationDefinition.getName());
659         memento.putString(
660             TAG_PARENT_ID,
661             keyConfigurationDefinition.getParentId());
662         memento.putString(
663             TAG_SOURCE_ID,
664             keyConfigurationDefinition.getSourceId());
665     }
666
667     static void writeKeyConfigurationDefinitions(
668         IMemento memento,
669         String JavaDoc name,
670         List JavaDoc keyConfigurationDefinitions) {
671         if (memento == null
672             || name == null
673             || keyConfigurationDefinitions == null)
674             throw new NullPointerException JavaDoc();
675
676         keyConfigurationDefinitions =
677             new ArrayList JavaDoc(keyConfigurationDefinitions);
678         Iterator JavaDoc iterator = keyConfigurationDefinitions.iterator();
679
680         while (iterator.hasNext())
681             Util.assertInstance(
682                 iterator.next(),
683                 KeyConfigurationDefinition.class);
684
685         iterator = keyConfigurationDefinitions.iterator();
686
687         while (iterator.hasNext())
688             writeKeyConfigurationDefinition(
689                 memento.createChild(name),
690                 (KeyConfigurationDefinition) iterator.next());
691     }
692
693     static void writeKeySequenceBindingDefinition(
694         IMemento memento,
695         KeySequenceBindingDefinition keySequenceBindingDefinition) {
696         if (memento == null || keySequenceBindingDefinition == null)
697             throw new NullPointerException JavaDoc();
698
699         memento.putString(
700             TAG_CONTEXT_ID,
701             keySequenceBindingDefinition.getContextId());
702         memento.putString(
703             TAG_COMMAND_ID,
704             keySequenceBindingDefinition.getCommandId());
705         memento.putString(
706             TAG_KEY_CONFIGURATION_ID,
707             keySequenceBindingDefinition.getKeyConfigurationId());
708         memento.putString(
709             TAG_KEY_SEQUENCE,
710             keySequenceBindingDefinition.getKeySequence() != null
711                 ? keySequenceBindingDefinition.getKeySequence().toString()
712                 : null);
713         memento.putString(TAG_LOCALE, keySequenceBindingDefinition.getLocale());
714         memento.putString(
715             TAG_PLATFORM,
716             keySequenceBindingDefinition.getPlatform());
717         memento.putString(
718             TAG_SOURCE_ID,
719             keySequenceBindingDefinition.getSourceId());
720     }
721
722     static void writeKeySequenceBindingDefinitions(
723         IMemento memento,
724         String JavaDoc name,
725         List JavaDoc keySequenceBindingDefinitions) {
726         if (memento == null
727             || name == null
728             || keySequenceBindingDefinitions == null)
729             throw new NullPointerException JavaDoc();
730
731         keySequenceBindingDefinitions =
732             new ArrayList JavaDoc(keySequenceBindingDefinitions);
733         Iterator JavaDoc iterator = keySequenceBindingDefinitions.iterator();
734
735         while (iterator.hasNext())
736             Util.assertInstance(
737                 iterator.next(),
738                 KeySequenceBindingDefinition.class);
739
740         iterator = keySequenceBindingDefinitions.iterator();
741
742         while (iterator.hasNext())
743             writeKeySequenceBindingDefinition(
744                 memento.createChild(name),
745                 (KeySequenceBindingDefinition) iterator.next());
746     }
747
748     private Persistence() {
749     }
750 }
751
Popular Tags