1 package org.antlr.works.editor; 2 3 import org.antlr.works.components.grammar.CEditorGrammar; 4 5 import java.util.HashMap ; 6 import java.util.List ; 7 import java.util.Map ; 8 38 39 public class EditorPersistence { 40 41 private static final String KEY_RULES = "rules"; 42 private static final String KEY_ACTIONS = "actions"; 43 44 private Map <String ,Map <Object ,EditorPersistentObject>> persistence = new HashMap <String , Map <Object ,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 objects, String key) { 70 Map <Object ,EditorPersistentObject> m = persistence.get(key); 71 if(m == null) { 72 m = new HashMap <Object , 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 objects, String key) { 87 Map <Object ,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 |