Skip to content

Commit

Permalink
Add avatar store field to UI plug-in instance
Browse files Browse the repository at this point in the history
This store loads and saves when the bundle starts
and stops.

Change-Id: I26cf039cca00f9ff8a93591d4e09c061de38bf5e
Signed-off-by: Kevin Sawicki <[email protected]>
Signed-off-by: Chris Aniszczyk <[email protected]>
  • Loading branch information
kevinsawicki authored and caniszczyk committed Apr 12, 2011
1 parent d62e2e4 commit 74244ef
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 22 deletions.
2 changes: 2 additions & 0 deletions org.eclipse.mylyn.github.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 74244ef

Please sign in to comment.