1 11 package org.eclipse.ui.internal.cheatsheets.composite.model; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.ui.IMemento; 20 import org.eclipse.ui.XMLMemento; 21 import org.eclipse.ui.cheatsheets.ICheatSheetManager; 22 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin; 23 import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags; 24 import org.eclipse.ui.internal.cheatsheets.data.IParserTags; 25 import org.eclipse.ui.internal.cheatsheets.state.ICheatSheetStateManager; 26 import org.eclipse.ui.internal.cheatsheets.state.NoSaveStateManager; 27 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager; 28 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask; 29 import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask; 30 import org.eclipse.ui.internal.provisional.cheatsheets.TaskEditor; 31 32 38 39 public class CompositeCheatSheetSaveHelper { 40 private static final String DOT_XML = ".xml"; private Map taskMementoMap; 42 private ICheatSheetStateManager stateManager; 43 44 47 public CompositeCheatSheetSaveHelper(ICheatSheetStateManager stateManager) { 48 super(); 49 this.stateManager = stateManager; 50 } 51 52 public IStatus loadCompositeState(CompositeCheatSheetModel model, Map layoutData) { 53 if (stateManager instanceof NoSaveStateManager) return Status.OK_STATUS; 54 XMLMemento readMemento = CheatSheetPlugin.getPlugin().readMemento(model.getId() + DOT_XML); 55 if (readMemento == null) { 56 return Status.OK_STATUS; 57 } 58 taskMementoMap = createTaskMap(readMemento); 59 loadTaskState(taskMementoMap, (AbstractTask)model.getRootTask()); 60 loadCheatsheetManagerData(readMemento, model.getCheatSheetManager()); 61 loadLayoutData(readMemento, layoutData); 62 model.sendTaskChangeEvents(); 63 return Status.OK_STATUS; 64 } 65 66 private Map createTaskMap(XMLMemento readMemento) { 67 Map map = new HashMap (); 68 IMemento[] tasks = readMemento.getChildren(ICompositeCheatsheetTags.TASK); 69 for (int i = 0; i < tasks.length; i++) { 70 String taskId = tasks[i].getString(ICompositeCheatsheetTags.TASK_ID); 71 if (taskId != null) { 72 map.put(taskId, tasks[i]); 73 } 74 } 75 return map; 76 } 77 78 private void loadTaskState(Map taskMap, AbstractTask task) { 79 ICompositeCheatSheetTask[] children = task.getSubtasks(); 80 IMemento memento = (IMemento)taskMap.get(task.getId()); 81 if (memento != null) { 82 String state = memento.getString(ICompositeCheatsheetTags.STATE); 83 if (state != null) { 84 task.setStateNoNotify(Integer.parseInt(state)); 85 } 86 } 87 if (task instanceof TaskGroup) { 88 for (int i = 0; i < children.length; i++) { 89 loadTaskState(taskMap, (AbstractTask) children[i]); 90 } 91 ((TaskGroup)task).checkState(); 92 } 93 } 94 95 private void loadCheatsheetManagerData(XMLMemento readMemento, ICheatSheetManager manager) { 96 if (manager == null) { 97 return; 98 } 99 IMemento[] children = readMemento.getChildren(ICompositeCheatsheetTags.CHEAT_SHEET_MANAGER); 100 for (int i = 0; i < children.length; i++) { 101 IMemento childMemento = children[i]; 102 String key = childMemento.getString(ICompositeCheatsheetTags.KEY); 103 String value = childMemento.getString(ICompositeCheatsheetTags.VALUE); 104 manager.setData(key, value); 105 } 106 } 107 108 private void loadLayoutData(XMLMemento readMemento, Map layoutData) { 109 if (layoutData == null) { 110 return; 111 } 112 IMemento[] children = readMemento.getChildren(ICompositeCheatsheetTags.LAYOUT_DATA); 113 for (int i = 0; i < children.length; i++) { 114 IMemento childMemento = children[i]; 115 String key = childMemento.getString(ICompositeCheatsheetTags.KEY); 116 String value = childMemento.getString(ICompositeCheatsheetTags.VALUE); 117 layoutData.put(key, value); 118 } 119 } 120 121 128 public IStatus saveCompositeState(CompositeCheatSheetModel model, Map layoutData) { 129 if (stateManager instanceof NoSaveStateManager) return Status.OK_STATUS; 130 XMLMemento writeMemento = XMLMemento.createWriteRoot(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET_STATE); 131 writeMemento.putString(IParserTags.ID, model.getId()); 132 saveTaskState(writeMemento, (AbstractTask)model.getRootTask()); 133 saveCheatSheetManagerData(writeMemento, model.getCheatSheetManager()); 134 taskMementoMap = createTaskMap(writeMemento); 135 if (layoutData != null) { 136 saveMap(writeMemento, layoutData, ICompositeCheatsheetTags.LAYOUT_DATA); 137 } 138 return CheatSheetPlugin.getPlugin().saveMemento(writeMemento, model.getId() + DOT_XML); 139 } 140 141 private void saveCheatSheetManagerData(XMLMemento writeMemento, ICheatSheetManager manager) { 142 if (!(manager instanceof CheatSheetManager)) { 143 return; 144 } 145 Map data = ((CheatSheetManager)manager).getData(); 146 saveMap(writeMemento, data, ICompositeCheatsheetTags.CHEAT_SHEET_MANAGER); 147 } 148 149 private void saveMap(XMLMemento writeMemento, Map data, String tag) { 150 for (Iterator iter = data.keySet().iterator(); iter.hasNext();) { 151 String key = (String )iter.next(); 152 String value = (String ) data.get(key); 153 IMemento childMemento = writeMemento.createChild(tag); 154 childMemento.putString(ICompositeCheatsheetTags.KEY, key); 155 childMemento.putString(ICompositeCheatsheetTags.VALUE, value); 156 } 157 } 158 159 private void saveTaskState(IMemento writeMemento, AbstractTask task) { 160 IMemento childMemento = writeMemento.createChild(ICompositeCheatsheetTags.TASK); 161 childMemento.putString(ICompositeCheatsheetTags.TASK_ID, task.getId()); 162 childMemento.putString(ICompositeCheatsheetTags.STATE, Integer.toString(task.getState())); 163 164 if (task instanceof IEditableTask && task.getState() != ICompositeCheatSheetTask.NOT_STARTED) { 166 TaskEditor editor = getEditor(task); 167 if (editor != null) { 168 IMemento taskDataMemento = childMemento.createChild(ICompositeCheatsheetTags.TASK_DATA); 169 editor.saveState(taskDataMemento); 170 } else { 171 IMemento taskData = getTaskMemento(task.getId()); 173 if (taskData != null) { 174 IMemento previousDataMemento = childMemento.createChild(ICompositeCheatsheetTags.TASK_DATA); 175 previousDataMemento.putMemento(taskData); 176 } 177 } 178 } 179 ICompositeCheatSheetTask[] subtasks = task.getSubtasks(); 180 for (int i = 0; i < subtasks.length; i++) { 181 saveTaskState(writeMemento, (AbstractTask)subtasks[i]); 182 } 183 } 184 185 private TaskEditor getEditor(AbstractTask task) { 186 if (task instanceof EditableTask) { 187 return ((EditableTask)task).getEditor(); 188 } 189 return null; 190 } 191 192 public IMemento getTaskMemento(String id) { 193 if (taskMementoMap == null) { 194 return null; 195 } 196 IMemento childMemento = (IMemento)taskMementoMap.get(id); 197 if (childMemento == null) { 198 return null; 199 } 200 return childMemento.getChild(ICompositeCheatsheetTags.TASK_DATA); 201 } 202 203 public void clearTaskMementos() { 204 taskMementoMap = null; 205 } 206 207 public void clearTaskMemento(String id) { 208 if (taskMementoMap != null) { 209 taskMementoMap.remove(id); 210 } 211 } 212 213 } 214 | Popular Tags |