1 11 package org.eclipse.ui.internal.cheatsheets; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStreamReader ; 19 import java.io.OutputStreamWriter ; 20 import java.net.URL ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 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 45 46 public class CheatSheetPlugin extends AbstractUIPlugin { 47 48 public final static String PLUGIN_ID = "org.eclipse.help.base"; 50 static CheatSheetPlugin plugin; 52 53 private CheatSheetHistory history = null; 57 private DocumentBuilder documentBuilder = null; 58 59 private static final String HISTORY_FILENAME = "history.xml"; private static final String MEMENTO_TAG_CHEATSHEET = "cheatsheet"; private static final String MEMENTO_TAG_VERSION = "version"; private static final String VERSION_STRING[] = { "0.0", "3.0.0" }; private static final String MEMENTO_TAG_CHEATSHEET_HISTORY = "cheatsheetHistory"; 65 public static final IPath ICONS_PATH = new Path("$nl$/icons/"); public static final String T_OBJ = "obj16/"; public static final String T_ELCL = "elcl16/"; public static final String T_DLCL = "dlcl16/"; public static final String T_VIEW = "view16/"; 71 74 public CheatSheetPlugin() { 75 super(); 76 } 77 78 81 public static CheatSheetPlugin getPlugin() { 82 return plugin; 83 } 84 85 96 public Image getImage(String key) { 97 Image image = getImageRegistry().get(key); 98 return image; 99 } 100 101 104 public CheatSheetHistory getCheatSheetHistory() { 105 if (history == null) { 106 history = new CheatSheetHistory(CheatSheetRegistryReader.getInstance()); 107 restoreCheatSheetHistory(); 108 } 109 return history; 110 } 111 112 115 private File getCheatSheetStateFile(String filename) { 116 IPath path = CheatSheetPlugin.getPlugin().getStateLocation(); 117 path = path.append(filename); 118 return path.toFile(); 119 } 120 121 124 public DocumentBuilder getDocumentBuilder() { 125 if(documentBuilder == null) { 126 try { 127 documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 128 } catch (Exception 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 139 public static synchronized void logError(String message, Throwable ex) { 140 if (message == null) 141 message = ""; 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"); 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"); 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"); 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"); 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"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 166 reg.put(ICheatSheetResource.CHEATSHEET_START, imageDescriptor); 167 168 path = ICONS_PATH.append(T_ELCL).append("restart_cheatsheet.gif"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 170 reg.put(ICheatSheetResource.CHEATSHEET_RESTART, imageDescriptor); 171 172 path = ICONS_PATH.append(T_ELCL).append("start_task.gif"); 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"); 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"); 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"); 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"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 190 reg.put(ICheatSheetResource.CHEATSHEET_RETURN, imageDescriptor); 191 192 path = ICONS_PATH.append(T_OBJ).append("error.gif"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 194 reg.put(ICheatSheetResource.ERROR, imageDescriptor); 195 196 198 path = ICONS_PATH.append(T_OBJ).append("composite_obj.gif"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 200 reg.put(ICheatSheetResource.COMPOSITE_OBJ, imageDescriptor); 201 202 path = ICONS_PATH.append(T_OBJ).append("information.gif"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 204 reg.put(ICheatSheetResource.INFORMATION, imageDescriptor); 205 206 path = ICONS_PATH.append(T_OBJ).append("warning.gif"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 208 reg.put(ICheatSheetResource.WARNING, imageDescriptor); 209 210 path = ICONS_PATH.append(T_ELCL).append("start_ccs_task.gif"); 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"); 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"); 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"); 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"); 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"); imageDescriptor = createImageDescriptor(getPlugin().getBundle(), path); 232 reg.put(ICheatSheetResource.CHEATSHEET_VIEW, imageDescriptor); 233 } 234 235 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 e) { 251 String 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 263 public XMLMemento readMemento(String filename) { 264 XMLMemento memento; 265 InputStreamReader reader = null; 266 267 try { 268 final File stateFile = getCheatSheetStateFile(filename); 270 271 FileInputStream input = new FileInputStream (stateFile); 272 reader = new InputStreamReader (input, "utf-8"); memento = XMLMemento.createReadRoot(reader); 274 275 276 } catch (FileNotFoundException e) { 277 memento = null; 278 } catch (Exception e) { 280 String 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 e) { 289 String 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 301 private void saveCheatSheetHistory() { 302 SafeRunner.run(new SafeRunnable() { 303 public void run() { 304 XMLMemento memento = XMLMemento.createWriteRoot(MEMENTO_TAG_CHEATSHEET); 305 306 memento.putString(MEMENTO_TAG_VERSION, VERSION_STRING[1]); 308 309 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 e) { 318 String 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 332 public IStatus saveMemento(XMLMemento memento, String filename) { 333 File stateFile = getCheatSheetStateFile(filename); 335 OutputStreamWriter writer = null; 336 try { 337 FileOutputStream stream = new FileOutputStream (stateFile); 338 writer = new OutputStreamWriter (stream, "utf-8"); memento.save(writer); 340 return Status.OK_STATUS; 341 } catch (IOException e) { 342 stateFile.delete(); 343 String 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 e) { 351 String 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 361 public void start(BundleContext context) throws Exception { 362 super.start(context); 363 364 plugin = this; 365 366 } 368 369 372 public void stop(BundleContext context) throws Exception { 373 super.stop(context); 374 375 if (history != null) { 378 saveCheatSheetHistory(); 379 } 380 381 CheatSheetRegistryReader.getInstance().stop(); 382 } 383 384 387 public static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path) { 388 URL 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 |