001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.model;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.Status;
018: import org.eclipse.ui.IMemento;
019: import org.eclipse.ui.XMLMemento;
020: import org.eclipse.ui.cheatsheets.ICheatSheetManager;
021: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
022: import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
023: import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
024: import org.eclipse.ui.internal.cheatsheets.state.ICheatSheetStateManager;
025: import org.eclipse.ui.internal.cheatsheets.state.NoSaveStateManager;
026: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
027: import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
028: import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask;
029: import org.eclipse.ui.internal.provisional.cheatsheets.TaskEditor;
030:
031: /**
032: * Class to save and restore composite cheatsheet state using a memento
033: * There is a child memento for each task which contains keys for the
034: * state complete. There is also a grandchild memento for
035: * each task that has been started.
036: */
037:
038: public class CompositeCheatSheetSaveHelper {
039: private static final String DOT_XML = ".xml"; //$NON-NLS-1$
040: private Map taskMementoMap;
041: private ICheatSheetStateManager stateManager;
042:
043: /**
044: * Constructor
045: */
046: public CompositeCheatSheetSaveHelper(
047: ICheatSheetStateManager stateManager) {
048: super ();
049: this .stateManager = stateManager;
050: }
051:
052: public IStatus loadCompositeState(CompositeCheatSheetModel model,
053: Map layoutData) {
054: if (stateManager instanceof NoSaveStateManager)
055: return Status.OK_STATUS;
056: XMLMemento readMemento = CheatSheetPlugin.getPlugin()
057: .readMemento(model.getId() + DOT_XML);
058: if (readMemento == null) {
059: return Status.OK_STATUS;
060: }
061: taskMementoMap = createTaskMap(readMemento);
062: loadTaskState(taskMementoMap, (AbstractTask) model
063: .getRootTask());
064: loadCheatsheetManagerData(readMemento, model
065: .getCheatSheetManager());
066: loadLayoutData(readMemento, layoutData);
067: model.sendTaskChangeEvents();
068: return Status.OK_STATUS;
069: }
070:
071: private Map createTaskMap(XMLMemento readMemento) {
072: Map map = new HashMap();
073: IMemento[] tasks = readMemento
074: .getChildren(ICompositeCheatsheetTags.TASK);
075: for (int i = 0; i < tasks.length; i++) {
076: String taskId = tasks[i]
077: .getString(ICompositeCheatsheetTags.TASK_ID);
078: if (taskId != null) {
079: map.put(taskId, tasks[i]);
080: }
081: }
082: return map;
083: }
084:
085: private void loadTaskState(Map taskMap, AbstractTask task) {
086: ICompositeCheatSheetTask[] children = task.getSubtasks();
087: IMemento memento = (IMemento) taskMap.get(task.getId());
088: if (memento != null) {
089: String state = memento
090: .getString(ICompositeCheatsheetTags.STATE);
091: if (state != null) {
092: task.setStateNoNotify(Integer.parseInt(state));
093: }
094: }
095: if (task instanceof TaskGroup) {
096: for (int i = 0; i < children.length; i++) {
097: loadTaskState(taskMap, (AbstractTask) children[i]);
098: }
099: ((TaskGroup) task).checkState();
100: }
101: }
102:
103: private void loadCheatsheetManagerData(XMLMemento readMemento,
104: ICheatSheetManager manager) {
105: if (manager == null) {
106: return;
107: }
108: IMemento[] children = readMemento
109: .getChildren(ICompositeCheatsheetTags.CHEAT_SHEET_MANAGER);
110: for (int i = 0; i < children.length; i++) {
111: IMemento childMemento = children[i];
112: String key = childMemento
113: .getString(ICompositeCheatsheetTags.KEY);
114: String value = childMemento
115: .getString(ICompositeCheatsheetTags.VALUE);
116: manager.setData(key, value);
117: }
118: }
119:
120: private void loadLayoutData(XMLMemento readMemento, Map layoutData) {
121: if (layoutData == null) {
122: return;
123: }
124: IMemento[] children = readMemento
125: .getChildren(ICompositeCheatsheetTags.LAYOUT_DATA);
126: for (int i = 0; i < children.length; i++) {
127: IMemento childMemento = children[i];
128: String key = childMemento
129: .getString(ICompositeCheatsheetTags.KEY);
130: String value = childMemento
131: .getString(ICompositeCheatsheetTags.VALUE);
132: layoutData.put(key, value);
133: }
134: }
135:
136: /**
137: * Save the state of a composite cheat sheet model
138: * @param model
139: * @param selectedTask
140: * @param layoutData Will contain pairs of name/value Strings used to save and restore layout
141: * @return
142: */
143: public IStatus saveCompositeState(CompositeCheatSheetModel model,
144: Map layoutData) {
145: if (stateManager instanceof NoSaveStateManager)
146: return Status.OK_STATUS;
147: XMLMemento writeMemento = XMLMemento
148: .createWriteRoot(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET_STATE);
149: writeMemento.putString(IParserTags.ID, model.getId());
150: saveTaskState(writeMemento, (AbstractTask) model.getRootTask());
151: saveCheatSheetManagerData(writeMemento, model
152: .getCheatSheetManager());
153: taskMementoMap = createTaskMap(writeMemento);
154: if (layoutData != null) {
155: saveMap(writeMemento, layoutData,
156: ICompositeCheatsheetTags.LAYOUT_DATA);
157: }
158: return CheatSheetPlugin.getPlugin().saveMemento(writeMemento,
159: model.getId() + DOT_XML);
160: }
161:
162: private void saveCheatSheetManagerData(XMLMemento writeMemento,
163: ICheatSheetManager manager) {
164: if (!(manager instanceof CheatSheetManager)) {
165: return;
166: }
167: Map data = ((CheatSheetManager) manager).getData();
168: saveMap(writeMemento, data,
169: ICompositeCheatsheetTags.CHEAT_SHEET_MANAGER);
170: }
171:
172: private void saveMap(XMLMemento writeMemento, Map data, String tag) {
173: for (Iterator iter = data.keySet().iterator(); iter.hasNext();) {
174: String key = (String) iter.next();
175: String value = (String) data.get(key);
176: IMemento childMemento = writeMemento.createChild(tag);
177: childMemento.putString(ICompositeCheatsheetTags.KEY, key);
178: childMemento.putString(ICompositeCheatsheetTags.VALUE,
179: value);
180: }
181: }
182:
183: private void saveTaskState(IMemento writeMemento, AbstractTask task) {
184: IMemento childMemento = writeMemento
185: .createChild(ICompositeCheatsheetTags.TASK);
186: childMemento.putString(ICompositeCheatsheetTags.TASK_ID, task
187: .getId());
188: childMemento.putString(ICompositeCheatsheetTags.STATE, Integer
189: .toString(task.getState()));
190:
191: // If this is an editable task that has been started, completed or skipped save the editor state
192: if (task instanceof IEditableTask
193: && task.getState() != ICompositeCheatSheetTask.NOT_STARTED) {
194: TaskEditor editor = getEditor(task);
195: if (editor != null) {
196: IMemento taskDataMemento = childMemento
197: .createChild(ICompositeCheatsheetTags.TASK_DATA);
198: editor.saveState(taskDataMemento);
199: } else {
200: // The editor has not been started so save its previously loaded state
201: IMemento taskData = getTaskMemento(task.getId());
202: if (taskData != null) {
203: IMemento previousDataMemento = childMemento
204: .createChild(ICompositeCheatsheetTags.TASK_DATA);
205: previousDataMemento.putMemento(taskData);
206: }
207: }
208: }
209: ICompositeCheatSheetTask[] subtasks = task.getSubtasks();
210: for (int i = 0; i < subtasks.length; i++) {
211: saveTaskState(writeMemento, (AbstractTask) subtasks[i]);
212: }
213: }
214:
215: private TaskEditor getEditor(AbstractTask task) {
216: if (task instanceof EditableTask) {
217: return ((EditableTask) task).getEditor();
218: }
219: return null;
220: }
221:
222: public IMemento getTaskMemento(String id) {
223: if (taskMementoMap == null) {
224: return null;
225: }
226: IMemento childMemento = (IMemento) taskMementoMap.get(id);
227: if (childMemento == null) {
228: return null;
229: }
230: return childMemento
231: .getChild(ICompositeCheatsheetTags.TASK_DATA);
232: }
233:
234: public void clearTaskMementos() {
235: taskMementoMap = null;
236: }
237:
238: public void clearTaskMemento(String id) {
239: if (taskMementoMap != null) {
240: taskMementoMap.remove(id);
241: }
242: }
243:
244: }
|