KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > data > CheatSheetSaveHelper


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.data;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.ui.IMemento;
31 import org.eclipse.ui.XMLMemento;
32 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
33 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
34 import org.eclipse.ui.internal.cheatsheets.Messages;
35 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
36 import org.eclipse.ui.internal.cheatsheets.views.CoreItem;
37 import org.eclipse.ui.internal.cheatsheets.views.SubItemCompositeHolder;
38 import org.eclipse.ui.internal.cheatsheets.views.ViewItem;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.NamedNodeMap JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 public class CheatSheetSaveHelper {
44
45     // Get the path to the cheatsheet folder in the .metadata folder of
46
// workspace.
47
protected IPath savePath;
48
49     private static final String JavaDoc DOT_XML = ".xml"; //$NON-NLS-1$
50

51     /**
52      * Constructor for CheatSheetSaveHelper.
53      */

54     public CheatSheetSaveHelper() {
55         super();
56         savePath = CheatSheetPlugin.getPlugin().getStateLocation();
57     }
58
59
60     /**
61      * Create the properties used to save the state of a cheatsheet
62      * @param currentItemNum the current item
63      * @param items a list of the items in this cheatsheet
64      * @param buttonIsDown
65      * @param expandRestoreStates
66      * @param csID the cheatsheet id
67      * @param contentPath will be null if the cheatsheet was launched using information from
68      * the registry, otherwise it is the url of the cheatsheet content file.
69      */

70     public Properties JavaDoc createProperties(int currentItemNum, ArrayList JavaDoc items,
71             boolean buttonIsDown, ArrayList JavaDoc expandRestoreStates, String JavaDoc csID, String JavaDoc contentPath) {
72         Properties JavaDoc props = new Properties JavaDoc();
73         Hashtable JavaDoc subcompletedTable = new Hashtable JavaDoc(10);
74         Hashtable JavaDoc subskippedTable = new Hashtable JavaDoc(10);
75
76         int buttonState = 0;
77         if (buttonIsDown)
78             buttonState = 1;
79
80         props.put(IParserTags.ID, csID);
81         props.put(IParserTags.CURRENT, Integer.toString(currentItemNum));
82         if (contentPath != null) {
83             props.put(IParserTags.CONTENT_URL, contentPath);
84         }
85         ArrayList JavaDoc completedList = new ArrayList JavaDoc();
86         ArrayList JavaDoc expandedList = new ArrayList JavaDoc();
87
88         if (expandRestoreStates == null)
89             expandRestoreStates = new ArrayList JavaDoc();
90
91         // Assemble lists of expanded items and completed items.
92
for (int i = 0; i < items.size(); i++) {
93             ViewItem item = (ViewItem) items.get(i);
94             if (item.isCompleted()) {
95                 completedList.add(Integer.toString(i));
96             }
97             if (item.isExpanded()) {
98                 expandedList.add(Integer.toString(i));
99             }
100
101             if (item instanceof CoreItem) {
102                 CoreItem withsubs = (CoreItem) item;
103                 ArrayList JavaDoc compList = withsubs
104                         .getListOfSubItemCompositeHolders();
105                 if (compList != null) {
106                     StringBuffer JavaDoc skippedsubItems = new StringBuffer JavaDoc();
107                     StringBuffer JavaDoc completedsubItems = new StringBuffer JavaDoc();
108                     for (int j = 0; j < compList.size(); j++) {
109                         SubItemCompositeHolder sch = (SubItemCompositeHolder) compList
110                                 .get(j);
111                         if (sch.isCompleted())
112                             completedsubItems.append(Integer.toString(j) + ","); //$NON-NLS-1$
113
if (sch.isSkipped())
114                             skippedsubItems.append(Integer.toString(j) + ","); //$NON-NLS-1$
115
}
116                     if (completedsubItems.toString().length() > 0) {
117                         String JavaDoc csi = completedsubItems.toString();
118                         if (csi.endsWith(",")) //$NON-NLS-1$
119
csi = csi.substring(0, csi.length() - 1);
120                         subcompletedTable.put(Integer.toString(i), csi);
121
122                     }
123                     if (skippedsubItems.toString().length() > 0) {
124                         String JavaDoc csi = skippedsubItems.toString();
125                         if (csi.endsWith(",")) //$NON-NLS-1$
126
csi = csi.substring(0, csi.length() - 1);
127                         subskippedTable.put(Integer.toString(i), csi);
128                     }
129                 }
130             }
131         }
132
133         // put expanded item list, completed list, button state
134
props.put(IParserTags.COMPLETED, completedList);
135         props.put(IParserTags.EXPANDED, expandedList);
136         props.put(IParserTags.EXPANDRESTORE, expandRestoreStates);
137         props.put(IParserTags.BUTTON, Integer.toString(buttonState));
138         if (subcompletedTable != null)
139             props.put(IParserTags.SUBITEMCOMPLETED, subcompletedTable);
140         if (subskippedTable != null)
141             props.put(IParserTags.SUBITEMSKIPPED, subskippedTable);
142
143         return props;
144     }
145
146     /**
147      * Method parses attribute from named node map. Returns value as string.
148      */

149     protected String JavaDoc getAttributeWithName(NamedNodeMap JavaDoc map, String JavaDoc name) {
150         try {
151             return map.getNamedItem(name).getNodeValue();
152         } catch (Exception JavaDoc e) {
153             return null;
154         }
155     }
156
157     public Path getStateFile(String JavaDoc csID) {
158         return getStateFile(csID, savePath);
159     }
160     
161     protected Path getStateFile(String JavaDoc csID, IPath rootPath) {
162         return new Path(rootPath.append(csID + ".xml").toOSString()); //$NON-NLS-1$
163
}
164
165     // Attempts to read an xml file from the provided url. Returns a Dom
166
// Document object if parses ok,
167
// returns null if the parse or read fails.
168
protected Document JavaDoc readXMLFile(URL JavaDoc url) {
169         InputStream JavaDoc is = null;
170         InputSource JavaDoc source = null;
171
172         try {
173             is = url.openStream();
174             if (is != null) {
175                 source = new InputSource JavaDoc(is);
176             }
177         } catch (Exception JavaDoc e) {
178             return null;
179         }
180
181         if (source == null)
182             return null;
183
184         try {
185             DocumentBuilder JavaDoc documentBuilder = CheatSheetPlugin.getPlugin()
186                     .getDocumentBuilder();
187             return documentBuilder.parse(source);
188         } catch (Exception JavaDoc e) {
189         } finally {
190             try {
191                 if (is != null)
192                     is.close();
193             } catch (IOException JavaDoc ioe) {
194             }
195         }
196
197         return null;
198     }
199     
200     /**
201      * @param saveProperties
202      * @param contentPath
203      * @param csm
204      */

205     public IStatus saveState(Properties JavaDoc properties, CheatSheetManager csm) {
206         String JavaDoc csID = (String JavaDoc) properties.get(IParserTags.ID);
207         XMLMemento writeMemento = XMLMemento.createWriteRoot(IParserTags.CHEATSHEET_STATE);
208         IStatus status = saveToMemento(properties, csm, writeMemento);
209         if (!status.isOK()) {
210             return status;
211         }
212         return CheatSheetPlugin.getPlugin().saveMemento(writeMemento, csID + DOT_XML);
213     }
214     
215     public IStatus saveToMemento(Properties JavaDoc properties, CheatSheetManager csm, IMemento writeMemento) {
216         
217         String JavaDoc csID = (String JavaDoc) properties.get(IParserTags.ID);
218         try {
219             writeMemento.putString(IParserTags.BUTTONSTATE, (String JavaDoc) properties
220                     .get(IParserTags.BUTTON));
221             writeMemento.putString(IParserTags.ITEM, (String JavaDoc) properties.get(IParserTags.CURRENT));
222             writeMemento.putString(IParserTags.ID, (String JavaDoc)properties.get(IParserTags.ID));
223             String JavaDoc contentPath = (String JavaDoc)properties.get(IParserTags.CONTENT_URL);
224             if (contentPath != null) {
225                 writeMemento.putString(IParserTags.CONTENT_URL, contentPath);
226             }
227
228             addListOfStringsToMemento(writeMemento, properties, IParserTags.COMPLETED);
229             addListOfStringsToMemento(writeMemento, properties, IParserTags.EXPANDED);
230             addListOfStringsToMemento(writeMemento, properties, IParserTags.EXPANDRESTORE);
231
232             addMapToMemento(writeMemento, csm.getData(), IParserTags.MANAGERDATA);
233             addMapToMemento(writeMemento, (Map JavaDoc)properties.get(IParserTags.SUBITEMCOMPLETED), IParserTags.SUBITEMCOMPLETED);
234             addMapToMemento(writeMemento, (Map JavaDoc)properties.get(IParserTags.SUBITEMSKIPPED), IParserTags.SUBITEMSKIPPED);
235
236         } catch (Exception JavaDoc e) {
237             String JavaDoc message = NLS.bind(Messages.ERROR_SAVING_STATEFILE_URL,
238                     (new Object JavaDoc[] { csID }));
239             IStatus status = new Status(IStatus.ERROR,
240                     ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK,
241                     message, e);
242             return status;
243             //CheatSheetPlugin.getPlugin().getLog().log(status);
244
}
245         return Status.OK_STATUS;
246     }
247
248     /**
249      * @param csID The id of this cheatsheet
250      * @return The state of this cheatsheet or null
251      */

252     public Properties JavaDoc loadState(String JavaDoc csID) {
253         XMLMemento readMemento = CheatSheetPlugin.getPlugin().readMemento(csID + DOT_XML);
254         if (readMemento == null) {
255             return null;
256         }
257         return loadFromMemento(readMemento);
258     }
259     
260     public Properties JavaDoc loadFromMemento(IMemento memento) {
261         Properties JavaDoc properties = new Properties JavaDoc();
262         properties.put(IParserTags.BUTTON, memento.getString(IParserTags.BUTTONSTATE));
263         properties.put(IParserTags.CURRENT, memento.getString(IParserTags.ITEM));
264         properties.put(IParserTags.ID, memento.getString(IParserTags.ID));
265         String JavaDoc contentURL = memento.getString(IParserTags.CONTENT_URL);
266         if (contentURL != null) {
267             properties.put(IParserTags.CONTENT_URL, contentURL);
268         }
269
270         getListOfStringsFromMemento(memento, properties, IParserTags.COMPLETED);
271         getListOfStringsFromMemento(memento, properties, IParserTags.EXPANDED);
272         getListOfStringsFromMemento(memento, properties, IParserTags.EXPANDRESTORE);
273
274         getMapFromMemento(memento, properties, IParserTags.SUBITEMCOMPLETED);
275         getMapFromMemento(memento, properties, IParserTags.SUBITEMSKIPPED);
276         getMapFromMemento(memento, properties, IParserTags.MANAGERDATA);
277         return properties;
278     }
279
280     private void addListOfStringsToMemento(IMemento memento, Properties JavaDoc properties, String JavaDoc key) {
281         List JavaDoc list = (List JavaDoc)properties.get(key);
282         if (list == null) {
283             return;
284         }
285         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
286             IMemento childMemento = memento.createChild(key);
287             childMemento.putString(IParserTags.ITEM,(String JavaDoc)iter.next());
288         }
289     }
290     
291
292     private void addMapToMemento(IMemento memento, Map JavaDoc map, String JavaDoc mapName) {
293         if (map == null) {
294             return;
295         }
296         for (Iterator JavaDoc iter = map.keySet().iterator(); iter.hasNext();) {
297             IMemento childMemento = memento.createChild(mapName);
298             String JavaDoc itemKey = (String JavaDoc)iter.next();
299             childMemento.putString(IParserTags.MANAGERDATAKEY,(itemKey));
300             childMemento.putString(IParserTags.MANAGERDATAVALUE,(String JavaDoc)map.get(itemKey));
301         }
302     }
303     
304     
305     private void getMapFromMemento(IMemento memento, Properties JavaDoc properties, String JavaDoc mapName) {
306         IMemento[] children = memento.getChildren(mapName);
307         Map JavaDoc map = new Hashtable JavaDoc();
308         for (int i = 0; i < children.length; i++) {
309             map.put(children[i].getString(IParserTags.MANAGERDATAKEY),
310                     children[i].getString(IParserTags.MANAGERDATAVALUE));
311         }
312         properties.put(mapName, map);
313     }
314     
315     private void getListOfStringsFromMemento(IMemento memento, Properties JavaDoc properties, String JavaDoc key) {
316         IMemento[] children = memento.getChildren(key);
317         List JavaDoc list = new ArrayList JavaDoc();
318         for (int i = 0; i < children.length; i++) {
319             list.add(children[i].getString(IParserTags.ITEM));
320         }
321         properties.put(key, list);
322     }
323
324 }
325
Popular Tags