KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > editor > EditorPersistence


1 package org.antlr.works.editor;
2
3 import org.antlr.works.components.grammar.CEditorGrammar;
4
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 /*
9
10 [The "BSD licence"]
11 Copyright (c) 2005 Jean Bovet
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions
16 are met:
17
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. The name of the author may not be used to endorse or promote products
24 derived from this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 */

38
39 public class EditorPersistence {
40
41     private static final String JavaDoc KEY_RULES = "rules";
42     private static final String JavaDoc KEY_ACTIONS = "actions";
43
44     private Map JavaDoc<String JavaDoc,Map JavaDoc<Object JavaDoc,EditorPersistentObject>> persistence = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<Object JavaDoc,EditorPersistentObject>>();
45     private boolean stored = false;
46     private CEditorGrammar editor;
47
48     public EditorPersistence(CEditorGrammar editor) {
49         this.editor = editor;
50     }
51
52     public void store() {
53         if(stored)
54             return;
55
56         stored = true;
57         store(editor.parserEngine.getRules(), KEY_RULES);
58         store(editor.parserEngine.getActions(), KEY_ACTIONS);
59     }
60
61     public void restore() {
62         if(stored) {
63             stored = false;
64             restore(editor.parserEngine.getRules(), KEY_RULES);
65             restore(editor.parserEngine.getActions(), KEY_ACTIONS);
66         }
67     }
68     
69     public void store(List JavaDoc objects, String JavaDoc key) {
70         Map JavaDoc<Object JavaDoc,EditorPersistentObject> m = persistence.get(key);
71         if(m == null) {
72             m = new HashMap JavaDoc<Object JavaDoc, EditorPersistentObject>();
73             persistence.put(key, m);
74         }
75
76         m.clear();
77         if(objects == null)
78             return;
79
80         for(int index=0; index<objects.size(); index++) {
81             EditorPersistentObject o = (EditorPersistentObject)objects.get(index);
82             m.put(o.getPersistentID(), o);
83         }
84     }
85
86     public void restore(List JavaDoc objects, String JavaDoc key) {
87         Map JavaDoc<Object JavaDoc,EditorPersistentObject> m = persistence.get(key);
88         if(m == null)
89             return;
90
91         if(objects == null)
92             return;
93
94         for(int index=0; index<objects.size(); index++) {
95             EditorPersistentObject o = (EditorPersistentObject) objects.get(index);
96             EditorPersistentObject oldObject = m.get(o.getPersistentID());
97             if(oldObject != null)
98                 o.persistentAssign(oldObject);
99         }
100     }
101 }
102
Popular Tags