KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > CheatSheetPlugin


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;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.io.OutputStreamWriter JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.SafeRunner;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.eclipse.jface.resource.ImageRegistry;
33 import org.eclipse.jface.util.SafeRunnable;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.ui.IMemento;
36 import org.eclipse.ui.XMLMemento;
37 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
38 import org.eclipse.ui.plugin.AbstractUIPlugin;
39 import org.osgi.framework.Bundle;
40 import org.osgi.framework.BundleContext;
41
42 /**
43  * The main plugin class for cheat sheets.
44  */

45
46 public class CheatSheetPlugin extends AbstractUIPlugin {
47
48     public final static String JavaDoc PLUGIN_ID = "org.eclipse.help.base"; //$NON-NLS-1$
49

50     //The shared instance of this plugin.
51
static CheatSheetPlugin plugin;
52
53     //Resource bundle.
54
//private boolean resourceBundleInitialized = false;
55
//private ResourceBundle resourceBundle;
56
private CheatSheetHistory history = null;
57     private DocumentBuilder JavaDoc documentBuilder = null;
58     
59     private static final String JavaDoc HISTORY_FILENAME = "history.xml"; //$NON-NLS-1$
60
private static final String JavaDoc MEMENTO_TAG_CHEATSHEET = "cheatsheet"; //$NON-NLS-1$
61
private static final String JavaDoc MEMENTO_TAG_VERSION = "version"; //$NON-NLS-1$
62
private static final String JavaDoc VERSION_STRING[] = { "0.0", "3.0.0" }; //$NON-NLS-1$ //$NON-NLS-2$
63
private static final String JavaDoc MEMENTO_TAG_CHEATSHEET_HISTORY = "cheatsheetHistory"; //$NON-NLS-1$
64

65     public static final IPath ICONS_PATH = new Path("$nl$/icons/"); //$NON-NLS-1$
66
public static final String JavaDoc T_OBJ = "obj16/"; //$NON-NLS-1$
67
public static final String JavaDoc T_ELCL = "elcl16/"; //$NON-NLS-1$
68
public static final String JavaDoc T_DLCL = "dlcl16/"; //$NON-NLS-1$
69
public static final String JavaDoc T_VIEW = "view16/"; //$NON-NLS-1$
70

71     /**
72      * The constructor.
73      */

74     public CheatSheetPlugin() {
75         super();
76     }
77
78     /**
79      * Returns the shared instance.
80      */

81     public static CheatSheetPlugin getPlugin() {
82         return plugin;
83     }
84
85     /**
86      * Returns the image in Cheat Sheet's image registry with the given key,
87      * or <code>null</code> if none.
88      * Convenience method equivalent to
89      * <pre>
90      * CheatSheetPlugin.getImageRegistry().get(key)
91      * </pre>
92      *
93      * @param key the key
94      * @return the image, or <code>null</code> if none
95      */

96     public Image getImage(String JavaDoc key) {
97         Image image = getImageRegistry().get(key);
98         return image;
99     }
100
101     /**
102      * Returns the CheatSheetHistory
103      */

104     public CheatSheetHistory getCheatSheetHistory() {
105         if (history == null) {
106             history = new CheatSheetHistory(CheatSheetRegistryReader.getInstance());
107             restoreCheatSheetHistory();
108         }
109         return history;
110     }
111
112     /**
113      * Get a file from the state folder.
114      */

115     private File JavaDoc getCheatSheetStateFile(String JavaDoc filename) {
116         IPath path = CheatSheetPlugin.getPlugin().getStateLocation();
117         path = path.append(filename);
118         return path.toFile();
119     }
120
121     /**
122      * Returns the DocumentBuilder to be used by the cheat sheets.
123      */

124     public DocumentBuilder JavaDoc getDocumentBuilder() {
125         if(documentBuilder == null) {
126             try {
127                 documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
128             } catch (Exception JavaDoc e) {
129                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.ERROR_CREATING_DOCUMENT_BUILDER, e);
130                 CheatSheetPlugin.getPlugin().getLog().log(status);
131             }
132         }
133         return documentBuilder;
134     }
135
136     /**
137      * Logs an Error message with an exception.
138      */

139     public static synchronized void logError(String JavaDoc message, Throwable JavaDoc ex) {
140         if (message == null)
141             message = ""; //$NON-NLS-1$
142
Status errorStatus = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK,
143                 message, ex);
144         CheatSheetPlugin.getPlugin().getLog().log(errorStatus);
145     }
146     
147     protected void initializeImageRegistry(ImageRegistry reg) {
148         IPath path = ICONS_PATH.append(T_OBJ).append("cheatsheet_obj.gif");//$NON-NLS-1$
149
ImageDescriptor imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
150         reg.put(ICheatSheetResource.CHEATSHEET_OBJ, imageDescriptor);
151         
152         path = ICONS_PATH.append(T_OBJ).append("skip_status.gif");//$NON-NLS-1$
153
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
154         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_SKIP, imageDescriptor);
155
156         path = ICONS_PATH.append(T_OBJ).append("complete_status.gif");//$NON-NLS-1$
157
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
158         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_COMPLETE, imageDescriptor);
159
160         path = ICONS_PATH.append(T_ELCL).append("linkto_help.gif");//$NON-NLS-1$
161
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
162         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_HELP, imageDescriptor);
163
164         path = ICONS_PATH.append(T_ELCL).append("start_cheatsheet.gif");//$NON-NLS-1$
165
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
166         reg.put(ICheatSheetResource.CHEATSHEET_START, imageDescriptor);
167
168         path = ICONS_PATH.append(T_ELCL).append("restart_cheatsheet.gif");//$NON-NLS-1$
169
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
170         reg.put(ICheatSheetResource.CHEATSHEET_RESTART, imageDescriptor);
171
172         path = ICONS_PATH.append(T_ELCL).append("start_task.gif");//$NON-NLS-1$
173
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
174         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_START, imageDescriptor);
175
176         path = ICONS_PATH.append(T_ELCL).append("skip_task.gif");//$NON-NLS-1$
177
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
178         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_SKIP, imageDescriptor);
179
180         path = ICONS_PATH.append(T_ELCL).append("complete_task.gif");//$NON-NLS-1$
181
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
182         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_COMPLETE, imageDescriptor);
183
184         path = ICONS_PATH.append(T_ELCL).append("restart_task.gif");//$NON-NLS-1$
185
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
186         reg.put(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_RESTART, imageDescriptor);
187             
188         path = ICONS_PATH.append(T_ELCL).append("return_to_start.gif");//$NON-NLS-1$
189
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
190         reg.put(ICheatSheetResource.CHEATSHEET_RETURN, imageDescriptor);
191     
192         path = ICONS_PATH.append(T_OBJ).append("error.gif");//$NON-NLS-1$
193
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
194         reg.put(ICheatSheetResource.ERROR, imageDescriptor);
195         
196         // Images used by composites
197

198         path = ICONS_PATH.append(T_OBJ).append("composite_obj.gif");//$NON-NLS-1$
199
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
200         reg.put(ICheatSheetResource.COMPOSITE_OBJ, imageDescriptor);
201         
202         path = ICONS_PATH.append(T_OBJ).append("information.gif");//$NON-NLS-1$
203
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
204         reg.put(ICheatSheetResource.INFORMATION, imageDescriptor);
205         
206         path = ICONS_PATH.append(T_OBJ).append("warning.gif");//$NON-NLS-1$
207
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
208         reg.put(ICheatSheetResource.WARNING, imageDescriptor);
209
210         path = ICONS_PATH.append(T_ELCL).append("start_ccs_task.gif");//$NON-NLS-1$
211
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
212         reg.put(ICheatSheetResource.COMPOSITE_TASK_START, imageDescriptor);
213
214         path = ICONS_PATH.append(T_ELCL).append("skip_ccs_task.gif");//$NON-NLS-1$
215
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
216         reg.put(ICheatSheetResource.COMPOSITE_TASK_SKIP, imageDescriptor);
217
218         path = ICONS_PATH.append(T_ELCL).append("review_ccs_task.gif");//$NON-NLS-1$
219
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
220         reg.put(ICheatSheetResource.COMPOSITE_TASK_REVIEW, imageDescriptor);
221
222         path = ICONS_PATH.append(T_ELCL).append("goto_ccs_task.gif");//$NON-NLS-1$
223
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
224         reg.put(ICheatSheetResource.COMPOSITE_GOTO_TASK, imageDescriptor);
225
226         path = ICONS_PATH.append(T_ELCL).append("restart_all.gif");//$NON-NLS-1$
227
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
228         reg.put(ICheatSheetResource.COMPOSITE_RESTART_ALL, imageDescriptor);
229
230         path = ICONS_PATH.append(T_VIEW).append("cheatsheet_view.gif");//$NON-NLS-1$
231
imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path);
232         reg.put(ICheatSheetResource.CHEATSHEET_VIEW, imageDescriptor);
233     }
234
235     /**
236      * Restores the state of the previously saved cheatsheet history
237      */

238     private void restoreCheatSheetHistory() {
239         SafeRunner.run(new SafeRunnable() {
240             public void run() {
241                 IMemento memento;
242                 memento = readMemento(HISTORY_FILENAME);
243                 if (memento != null) {
244                     IMemento childMem = memento.getChild(MEMENTO_TAG_CHEATSHEET_HISTORY);
245                     if (childMem != null) {
246                         history.restoreState(childMem);
247                     }
248                 }
249             }
250             public void handleException(Throwable JavaDoc e) {
251                 String JavaDoc message = Messages.ERROR_READING_STATE_FILE;
252                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
253                 CheatSheetPlugin.getPlugin().getLog().log(status);
254             }
255         });
256     }
257     
258     /**
259      * Read a memento from the state directory for the cheatsheets plugin
260      * @param filename A simple filename
261      * @return A memento read from the state directory or null if the memento could not be read
262      */

263     public XMLMemento readMemento(String JavaDoc filename) {
264         XMLMemento memento;
265         InputStreamReader JavaDoc reader = null;
266
267         try {
268             // Read the cheatsheet state file.
269
final File JavaDoc stateFile = getCheatSheetStateFile(filename);
270
271             FileInputStream JavaDoc input = new FileInputStream JavaDoc(stateFile);
272             reader = new InputStreamReader JavaDoc(input, "utf-8"); //$NON-NLS-1$
273
memento = XMLMemento.createReadRoot(reader);
274
275             
276         } catch (FileNotFoundException JavaDoc e) {
277             memento = null;
278             // Do nothing, the file will not exist the first time the workbench in used.
279
} catch (Exception JavaDoc e) {
280             String JavaDoc message = Messages.ERROR_READING_STATE_FILE;
281             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
282             CheatSheetPlugin.getPlugin().getLog().log(status);
283             memento = null;
284         } finally {
285             try {
286                 if (reader != null)
287                     reader.close();
288             } catch (IOException JavaDoc e) {
289                 // Not much to do, just catch the exception and keep going.
290
String JavaDoc message = Messages.ERROR_READING_STATE_FILE;
291                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
292                 CheatSheetPlugin.getPlugin().getLog().log(status);
293             }
294         }
295         return memento;
296     }
297
298     /**
299      * Saves the current cheatsheet history so it can be restored later on
300      */

301     private void saveCheatSheetHistory() {
302         SafeRunner.run(new SafeRunnable() {
303             public void run() {
304                 XMLMemento memento = XMLMemento.createWriteRoot(MEMENTO_TAG_CHEATSHEET);
305
306                 // Save the version number.
307
memento.putString(MEMENTO_TAG_VERSION, VERSION_STRING[1]);
308
309                 // Save perspective history.
310
getCheatSheetHistory().saveState(memento.createChild(MEMENTO_TAG_CHEATSHEET_HISTORY));
311
312                 IStatus status = saveMemento(memento, HISTORY_FILENAME);
313                 if (!status.isOK()) {
314                     CheatSheetPlugin.getPlugin().getLog().log(status);
315                 }
316             }
317             public void handleException(Throwable JavaDoc e) {
318                 String JavaDoc message = Messages.ERROR_WRITING_STATE_FILE;
319                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
320                 CheatSheetPlugin.getPlugin().getLog().log(status);
321             }
322         });
323     }
324     
325     /**
326      * Save the memento to a file in this plugins state area
327      * @param memento The memento to save
328      * @param filename A simple filename
329      * @return OK_Status if the memento was saved without error, otherwise an error
330      * status
331      */

332     public IStatus saveMemento(XMLMemento memento, String JavaDoc filename) {
333         // Save the IMemento to a file.
334
File JavaDoc stateFile = getCheatSheetStateFile(filename);
335         OutputStreamWriter JavaDoc writer = null;
336         try {
337             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(stateFile);
338             writer = new OutputStreamWriter JavaDoc(stream, "utf-8"); //$NON-NLS-1$
339
memento.save(writer);
340             return Status.OK_STATUS;
341         } catch (IOException JavaDoc e) {
342             stateFile.delete();
343             String JavaDoc message = Messages.ERROR_WRITING_STATE_FILE;
344             IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
345             return status;
346         } finally {
347             try {
348                 if (writer != null)
349                     writer.close();
350             } catch (IOException JavaDoc e) {
351                 String JavaDoc message = Messages.ERROR_WRITING_STATE_FILE;
352                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
353                 CheatSheetPlugin.getPlugin().getLog().log(status);
354             }
355         }
356     }
357
358     /* (non-Javadoc)
359      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
360      */

361     public void start(BundleContext context) throws Exception JavaDoc {
362         super.start(context);
363
364         plugin = this;
365         
366         // allow the MRU history to be lazily initialized by getCheatSheetHistory
367
}
368
369     /* (non-Javadoc)
370      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
371      */

372     public void stop(BundleContext context) throws Exception JavaDoc {
373         super.stop(context);
374         
375         // save the MRU history if necessary
376
// if we never restored history, let existing memento stand
377
if (history != null) {
378             saveCheatSheetHistory();
379         }
380         
381         CheatSheetRegistryReader.getInstance().stop();
382     }
383
384     /*
385      * Since 3.1.1. Load from icon paths with $NL$
386      */

387     public static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path) {
388         URL JavaDoc url= FileLocator.find(bundle, path, null);
389         if (url != null) {
390             return ImageDescriptor.createFromURL(url);
391         }
392         return null;
393     }
394     
395 }
396
Popular Tags