KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > model > CompositeCheatSheetSaveHelper


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

11 package org.eclipse.ui.internal.cheatsheets.composite.model;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
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 /**
33  * Class to save and restore composite cheatsheet state using a memento
34  * There is a child memento for each task which contains keys for the
35  * state complete. There is also a grandchild memento for
36  * each task that has been started.
37  */

38
39 public class CompositeCheatSheetSaveHelper {
40     private static final String JavaDoc DOT_XML = ".xml"; //$NON-NLS-1$
41
private Map JavaDoc taskMementoMap;
42     private ICheatSheetStateManager stateManager;
43
44     /**
45      * Constructor
46      */

47     public CompositeCheatSheetSaveHelper(ICheatSheetStateManager stateManager) {
48         super();
49         this.stateManager = stateManager;
50     }
51
52     public IStatus loadCompositeState(CompositeCheatSheetModel model, Map JavaDoc 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 JavaDoc createTaskMap(XMLMemento readMemento) {
67         Map JavaDoc map = new HashMap JavaDoc();
68         IMemento[] tasks = readMemento.getChildren(ICompositeCheatsheetTags.TASK);
69         for (int i = 0; i < tasks.length; i++) {
70             String JavaDoc 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 JavaDoc taskMap, AbstractTask task) {
79         ICompositeCheatSheetTask[] children = task.getSubtasks();
80         IMemento memento = (IMemento)taskMap.get(task.getId());
81         if (memento != null) {
82             String JavaDoc 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 JavaDoc key = childMemento.getString(ICompositeCheatsheetTags.KEY);
103             String JavaDoc value = childMemento.getString(ICompositeCheatsheetTags.VALUE);
104             manager.setData(key, value);
105         }
106     }
107     
108     private void loadLayoutData(XMLMemento readMemento, Map JavaDoc 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 JavaDoc key = childMemento.getString(ICompositeCheatsheetTags.KEY);
116             String JavaDoc value = childMemento.getString(ICompositeCheatsheetTags.VALUE);
117             layoutData.put(key, value);
118         }
119     }
120
121     /**
122      * Save the state of a composite cheat sheet model
123      * @param model
124      * @param selectedTask
125      * @param layoutData Will contain pairs of name/value Strings used to save and restore layout
126      * @return
127      */

128     public IStatus saveCompositeState(CompositeCheatSheetModel model, Map JavaDoc 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 JavaDoc data = ((CheatSheetManager)manager).getData();
146         saveMap(writeMemento, data, ICompositeCheatsheetTags.CHEAT_SHEET_MANAGER);
147     }
148
149     private void saveMap(XMLMemento writeMemento, Map JavaDoc data, String JavaDoc tag) {
150         for (Iterator JavaDoc iter = data.keySet().iterator(); iter.hasNext();) {
151             String JavaDoc key = (String JavaDoc)iter.next();
152             String JavaDoc value = (String JavaDoc) 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 this is an editable task that has been started, completed or skipped save the editor state
165
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                 // The editor has not been started so save its previously loaded state
172
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 JavaDoc 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 JavaDoc id) {
208         if (taskMementoMap != null) {
209             taskMementoMap.remove(id);
210         }
211     }
212
213 }
214
Popular Tags