From 74244efc944a68643d2b0f7219de713c882cdf81 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Apr 2011 17:41:32 -0700 Subject: [PATCH] Add avatar store field to UI plug-in instance This store loads and saves when the bundle starts and stops. Change-Id: I26cf039cca00f9ff8a93591d4e09c061de38bf5e Signed-off-by: Kevin Sawicki Signed-off-by: Chris Aniszczyk --- .../META-INF/MANIFEST.MF | 2 + .../mylyn/github/ui/internal/GitHubUi.java | 211 ++++++++++++++++-- 2 files changed, 191 insertions(+), 22 deletions(-) diff --git a/org.eclipse.mylyn.github.ui/META-INF/MANIFEST.MF b/org.eclipse.mylyn.github.ui/META-INF/MANIFEST.MF index 700432c2a..5faba4925 100644 --- a/org.eclipse.mylyn.github.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.mylyn.github.ui/META-INF/MANIFEST.MF @@ -2,6 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Eclipse EGit Mylyn GitHub UI Plug-in (Incubation) Bundle-SymbolicName: org.eclipse.mylyn.github.ui;singleton:=true +Bundle-Activator: org.eclipse.mylyn.github.ui.internal.GitHubUi Bundle-Version: 0.1.0.qualifier Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-Vendor: Eclipse EGit @@ -18,3 +19,4 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.5.0", org.eclipse.mylyn.commons.ui;bundle-version="3.4.0", org.eclipse.core.resources;bundle-version="3.5.0" Export-Package: org.eclipse.mylyn.github.ui.internal;x-internal:=true +Bundle-ActivationPolicy: lazy diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubUi.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubUi.java index 36ef13514..09e6c0359 100644 --- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubUi.java +++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubUi.java @@ -12,43 +12,210 @@ *******************************************************************************/ package org.eclipse.mylyn.github.ui.internal; -import org.eclipse.core.runtime.ILog; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * GitHub UI plug-in + */ +public class GitHubUi extends AbstractUIPlugin { -public class GitHubUi { + /** + * BUNDLE_ID + */ public static final String BUNDLE_ID = "org.eclipse.mylyn.github.ui"; - - public static IStatus createStatus(int severity,String message) { - return new Status(severity,BUNDLE_ID,message); + + /** + * STORE_NAME + */ + public static final String STORE_NAME = "avatars.ser"; //$NON-NLS-1$ + + /** + * Create status + * + * @param severity + * @param message + * @return status + */ + public static IStatus createStatus(int severity, String message) { + return new Status(severity, BUNDLE_ID, message); } - - public static IStatus createStatus(int severity,String message,Throwable e) { - return new Status(severity,BUNDLE_ID,message,e); + + /** + * Create status + * + * @param severity + * @param message + * @param e + * @return status + */ + public static IStatus createStatus(int severity, String message, Throwable e) { + return new Status(severity, BUNDLE_ID, message, e); } - + + /** + * Create error status from message + * + * @param message + * @return status + */ public static IStatus createErrorStatus(String message) { return createStatus(IStatus.ERROR, message); } - - public static IStatus createErrorStatus(String message,Throwable t) { - return createStatus(IStatus.ERROR, message,t); + + /** + * Create error status from message and throwable + * + * @param message + * @param t + * @return status + */ + public static IStatus createErrorStatus(String message, Throwable t) { + return createStatus(IStatus.ERROR, message, t); } + /** + * Create error status from throwable + * + * @param e + * @return status + */ public static IStatus createErrorStatus(Throwable e) { - return createStatus(IStatus.ERROR, "Unexpected error: "+e.getMessage(),e); - } - - public static ILog getLog() { - return Platform.getLog(Platform.getBundle(BUNDLE_ID)); + return createStatus(IStatus.ERROR, + "Unexpected error: " + e.getMessage(), e); } - - public static void logError(String message,Throwable t) { - getLog().log(createErrorStatus(message, t)); + + /** + * Log message and throwable as error + * + * @param message + * @param t + */ + public static void logError(String message, Throwable t) { + INSTANCE.getLog().log(createErrorStatus(message, t)); } - + + /** + * Log throwable as error + * + * @param t + */ public static void logError(Throwable t) { - getLog().log(createErrorStatus(t.getMessage(), t)); + INSTANCE.getLog().log(createErrorStatus(t.getMessage(), t)); } + + private static GitHubUi INSTANCE; + + /** + * Get default activator + * + * @return plug-in + */ + public static GitHubUi getDefault() { + return INSTANCE; + } + + private AvatarStore store = null; + + /** + * Create plug-in + */ + public GitHubUi() { + + } + + /** + * Get avatar store + * + * @return avatar store + */ + public AvatarStore getStore() { + return this.store; + } + + /** + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + INSTANCE = this; + loadAvatars(context); + } + + /** + * Load avatars + * + * @param context + */ + protected void loadAvatars(BundleContext context) { + IPath location = Platform.getStateLocation(context.getBundle()); + File file = location.append(STORE_NAME).toFile(); + if (file.exists()) { + ObjectInputStream stream = null; + try { + stream = new ObjectInputStream(new FileInputStream(file)); + this.store = (AvatarStore) stream.readObject(); + } catch (IOException e) { + logError("Error reading avatar store", e); //$NON-NLS-1$ + } catch (ClassNotFoundException cnfe) { + logError("Error reading avatar store", cnfe); //$NON-NLS-1$ + } finally { + if (stream != null) + try { + stream.close(); + } catch (IOException ignore) { + // Ignored + } + } + } + if (this.store == null) + this.store = new AvatarStore(); + } + + /** + * Save avatars + * + * @param context + */ + protected void saveAvatars(BundleContext context) { + // TODO need to avoid Platform. use the context + IPath location = Platform.getStateLocation(context.getBundle()); + File file = location.append(STORE_NAME).toFile(); + + ObjectOutputStream stream = null; + try { + stream = new ObjectOutputStream(new FileOutputStream(file)); + stream.writeObject(this.store); + } catch (IOException e) { + logError("Error writing avatar store", e); //$NON-NLS-1$ + } finally { + if (stream != null) + try { + stream.close(); + } catch (IOException ignore) { + // Ignored + } + } + } + + /** + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + super.stop(context); + INSTANCE = null; + saveAvatars(context); + } + }