Improve Growl exception handling, remove System.out and a few fixes from audit

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1048 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-05-13 12:48:10 +00:00
parent 437837e21c
commit a7c218857b
3 changed files with 204 additions and 229 deletions

View File

@ -42,9 +42,9 @@ import javax.imageio.ImageIO;
* @version 0.1 * @version 0.1
*/ */
class GrowlNative implements Growl { class GrowlNative implements Growl {
private String appName; private final String appName;
private List<NotificationType> notifications; private final List<NotificationType> notifications;
private List<GrowlCallbackListener> callbackListeners; private final List<GrowlCallbackListener> callbackListeners;
private byte[] imageData; private byte[] imageData;
private native void sendNotification(String appName, String name, private native void sendNotification(String appName, String name,
@ -57,8 +57,7 @@ class GrowlNative implements Growl {
* Creates a new <code>GrowlNative</code> instance for the specified * Creates a new <code>GrowlNative</code> instance for the specified
* application name. * application name.
* *
* @param appName * @param appName The name of the application sending notifications.
* The name of the application sending notifications.
*/ */
GrowlNative(String appName) { GrowlNative(String appName) {
notifications = new ArrayList<NotificationType>(); notifications = new ArrayList<NotificationType>();
@ -66,6 +65,7 @@ class GrowlNative implements Growl {
this.appName = appName; this.appName = appName;
} }
@SuppressWarnings({"UnusedDeclaration"})
void fireCallbacks(String callbackContext) { void fireCallbacks(String callbackContext) {
for (GrowlCallbackListener listener : callbackListeners) { for (GrowlCallbackListener listener : callbackListeners) {
listener.notificationWasClicked(callbackContext); listener.notificationWasClicked(callbackContext);
@ -97,15 +97,20 @@ class GrowlNative implements Growl {
* {@inheritDoc} * {@inheritDoc}
*/ */
public void setIcon(RenderedImage icon) throws GrowlException { public void setIcon(RenderedImage icon) throws GrowlException {
try { imageData = convertImage(icon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(icon, "png", baos);
imageData = baos.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
protected byte[] convertImage(RenderedImage icon) throws GrowlException {
if (icon == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(icon, "png", baos);
} catch (IOException ioe) {
throw new GrowlException("Failed to convert Image", ioe);
}
return baos.toByteArray();
} }
/** /**
@ -113,99 +118,63 @@ class GrowlNative implements Growl {
*/ */
public void sendNotification(String name, String title, String body) public void sendNotification(String name, String title, String body)
throws GrowlException { throws GrowlException {
if (!notifications.contains(new NotificationType(name, false))) { sendNotification(name, title, body, null, null);
System.out.println("contains: " + notifications);
throw new GrowlException("Unregistered notification name [" + name
+ "]");
}
sendNotification(appName, name, title, body, null, null);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void sendNotification(String name, String title, String body, public void sendNotification(String name, String title, String body, RenderedImage icon) throws GrowlException {
RenderedImage icon) throws GrowlException { sendNotification(name, title, body, null, icon);
if (!notifications.contains(new NotificationType(name, false))) {
System.out.println("contains: " + notifications);
throw new GrowlException("Unregistered notification name [" + name
+ "]");
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(icon, "png", baos);
byte[] image = baos.toByteArray();
sendNotification(appName, name, title, body, null, image);
} catch (IOException ioe) {
throw new GrowlException("Failed to convert Image", ioe);
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void sendNotification(String name, String title, String body, public void sendNotification(String name, String title, String body, String callbackContext) throws GrowlException {
String callbackContext) throws GrowlException { sendNotification(name, title, body, callbackContext, null);
if (!notifications.contains(new NotificationType(name, false))) {
System.out.println("contains: " + notifications);
throw new GrowlException("Unregistered notification name [" + name
+ "]");
}
sendNotification(appName, name, title, body, callbackContext, null);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void sendNotification(String name, String title, String body, public void sendNotification(String name, String title, String body, String callbackContext, RenderedImage icon)
String callbackContext, RenderedImage icon) throws GrowlException { throws GrowlException {
if (!notifications.contains(new NotificationType(name, false))) { if (!notifications.contains(new NotificationType(name, false))) {
System.out.println("contains: " + notifications); throw new GrowlException("Unregistered notification name [" + name + ']');
throw new GrowlException("Unregistered notification name [" + name
+ "]");
} }
try { sendNotification(appName, name, title, body, callbackContext, convertImage(icon));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(icon, "png", baos);
byte[] image = baos.toByteArray();
sendNotification(appName, name, title, body, callbackContext, image);
} catch (IOException ioe) {
throw new GrowlException("Failed to convert Image", ioe);
}
} }
private class NotificationType { private class NotificationType {
private String name; private final String name;
private boolean enabledByDefault; private final boolean enabledByDefault;
public NotificationType(String name, boolean enabledByDefault) { private NotificationType(String name, boolean enabledByDefault) {
this.name = name; this.name = name;
this.enabledByDefault = enabledByDefault; this.enabledByDefault = enabledByDefault;
} }
@SuppressWarnings({"UnusedDeclaration"})
public String getName() { public String getName() {
return name; return name;
} }
@SuppressWarnings({"UnusedDeclaration"})
public boolean isEnabledByDefault() { public boolean isEnabledByDefault() {
return enabledByDefault; return enabledByDefault;
} }
@Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof NotificationType)) { return (other instanceof NotificationType) &&
return false; name.equals(((NotificationType) other).name);
} }
NotificationType otherType = (NotificationType) other; @Override
public int hashCode() {
return name.equals(otherType.name); return name.hashCode();
} }
} }
} }

View File

@ -38,7 +38,7 @@ import java.util.Map;
*/ */
public final class GrowlUtils { public final class GrowlUtils {
private static final boolean GROWL_LOADED; private static final boolean GROWL_LOADED;
private static Map<String, Growl> instances = new HashMap<String, Growl>(); private static final Map<String, Growl> instances = new HashMap<String, Growl>();
static { static {
boolean loaded = false; boolean loaded = false;
@ -46,7 +46,7 @@ public final class GrowlUtils {
System.loadLibrary("growl"); System.loadLibrary("growl");
loaded = true; loaded = true;
} catch (UnsatisfiedLinkError ule) { } catch (UnsatisfiedLinkError ule) {
System.out.println("Failed to load Growl library"); // ignore: growl not available
} }
GROWL_LOADED = loaded; GROWL_LOADED = loaded;
@ -56,18 +56,17 @@ public final class GrowlUtils {
* Utility method - should not be instantiated. * Utility method - should not be instantiated.
*/ */
private GrowlUtils() { private GrowlUtils() {
} }
/** /**
* Gets a <code>Growl</code> instance to use for the specified application * Gets a <code>Growl</code> instance to use for the specified application
* name. Multiple calls to this method will return the same instance. * name. Multiple calls to this method will return the same instance.
* *
* @param appName * @param appName The name of the application.
* The name of the application.
* @return The <code>Growl</code> instance to use. * @return The <code>Growl</code> instance to use.
*/ */
public static Growl getGrowlInstance(String appName) { public static Growl getGrowlInstance(String appName) {
synchronized (instances) {
Growl instance = instances.get(appName); Growl instance = instances.get(appName);
if (instance == null) { if (instance == null) {
@ -82,6 +81,7 @@ public final class GrowlUtils {
return instance; return instance;
} }
}
/** /**
* Gets whether messages can be sent to Growl. If this returns * Gets whether messages can be sent to Growl. If this returns

View File

@ -55,6 +55,9 @@ public class TestGrowl extends JFrame {
private static final String APP_NAME = "Test Java App"; private static final String APP_NAME = "Test Java App";
private static final String NOTIF_3_CALLBACK = "Notif3"; private static final String NOTIF_3_CALLBACK = "Notif3";
/**
* Test Growl.
*/
public TestGrowl() { public TestGrowl() {
super("Growl for Java"); super("Growl for Java");
setSize(320, 290); setSize(320, 290);
@ -127,7 +130,6 @@ public class TestGrowl extends JFrame {
} catch (GrowlException ge) { } catch (GrowlException ge) {
ge.printStackTrace(); ge.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -165,7 +167,6 @@ public class TestGrowl extends JFrame {
} catch (GrowlException ge) { } catch (GrowlException ge) {
ge.printStackTrace(); ge.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -180,7 +181,12 @@ public class TestGrowl extends JFrame {
getContentPane().add(new JButton(action)); getContentPane().add(new JButton(action));
} }
public static final void main(String[] args) { /**
* Test growl.
* @param args args
*/
public static void main(String[] args) {
//noinspection ResultOfObjectAllocationIgnored
new TestGrowl(); new TestGrowl();
} }
} }