diff --git a/libgrowl/src/java/info/growl/GrowlNative.java b/libgrowl/src/java/info/growl/GrowlNative.java index 65a18e84..482f15d2 100644 --- a/libgrowl/src/java/info/growl/GrowlNative.java +++ b/libgrowl/src/java/info/growl/GrowlNative.java @@ -37,175 +37,144 @@ import javax.imageio.ImageIO; /** * Growl notification implementation. This uses JNI to send messages to Growl. - * + * * @author Michael Stringer * @version 0.1 */ class GrowlNative implements Growl { - private String appName; - private List notifications; - private List callbackListeners; + private final String appName; + private final List notifications; + private final List callbackListeners; private byte[] imageData; private native void sendNotification(String appName, String name, - String title, String message, String callbackContext, byte[] icon); + String title, String message, String callbackContext, byte[] icon); private native void registerApp(String appName, byte[] image, - List notifications); + List notifications); /** * Creates a new GrowlNative instance for the specified * application name. - * - * @param appName - * The name of the application sending notifications. + * + * @param appName The name of the application sending notifications. */ GrowlNative(String appName) { - notifications = new ArrayList(); - callbackListeners = new ArrayList(); - this.appName = appName; + notifications = new ArrayList(); + callbackListeners = new ArrayList(); + this.appName = appName; } + @SuppressWarnings({"UnusedDeclaration"}) void fireCallbacks(String callbackContext) { - for (GrowlCallbackListener listener : callbackListeners) { - listener.notificationWasClicked(callbackContext); - } + for (GrowlCallbackListener listener : callbackListeners) { + listener.notificationWasClicked(callbackContext); + } } /** * {@inheritDoc} */ public void register() throws GrowlException { - registerApp(appName, imageData, notifications); + registerApp(appName, imageData, notifications); } /** * {@inheritDoc} */ public void addNotification(String name, boolean enabledByDefault) { - notifications.add(new NotificationType(name, enabledByDefault)); + notifications.add(new NotificationType(name, enabledByDefault)); } /** * {@inheritDoc} */ public void addCallbackListener(GrowlCallbackListener listener) { - callbackListeners.add(listener); + callbackListeners.add(listener); } /** * {@inheritDoc} */ public void setIcon(RenderedImage icon) throws GrowlException { - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(icon, "png", baos); + imageData = convertImage(icon); + } - 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(); } /** * {@inheritDoc} */ public void sendNotification(String name, String title, String body) - throws GrowlException { - if (!notifications.contains(new NotificationType(name, false))) { - System.out.println("contains: " + notifications); - throw new GrowlException("Unregistered notification name [" + name - + "]"); - } - sendNotification(appName, name, title, body, null, null); + throws GrowlException { + sendNotification(name, title, body, null, null); } /** * {@inheritDoc} */ - public void sendNotification(String name, String title, String body, - RenderedImage icon) throws GrowlException { - 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); - } + public void sendNotification(String name, String title, String body, RenderedImage icon) throws GrowlException { + sendNotification(name, title, body, null, icon); } /** * {@inheritDoc} */ - public void sendNotification(String name, String title, String body, - String callbackContext) throws GrowlException { - 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); + public void sendNotification(String name, String title, String body, String callbackContext) throws GrowlException { + sendNotification(name, title, body, callbackContext, null); } /** * {@inheritDoc} */ - public void sendNotification(String name, String title, String body, - String callbackContext, RenderedImage icon) throws GrowlException { - if (!notifications.contains(new NotificationType(name, false))) { - System.out.println("contains: " + notifications); - throw new GrowlException("Unregistered notification name [" + name - + "]"); - } + public void sendNotification(String name, String title, String body, String callbackContext, RenderedImage icon) + throws GrowlException { + if (!notifications.contains(new NotificationType(name, false))) { + 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, callbackContext, image); - } catch (IOException ioe) { - throw new GrowlException("Failed to convert Image", ioe); - } + sendNotification(appName, name, title, body, callbackContext, convertImage(icon)); } private class NotificationType { - private String name; - private boolean enabledByDefault; + private final String name; + private final boolean enabledByDefault; - public NotificationType(String name, boolean enabledByDefault) { - this.name = name; - this.enabledByDefault = enabledByDefault; - } + private NotificationType(String name, boolean enabledByDefault) { + this.name = name; + this.enabledByDefault = enabledByDefault; + } - public String getName() { - return name; - } + @SuppressWarnings({"UnusedDeclaration"}) + public String getName() { + return name; + } - public boolean isEnabledByDefault() { - return enabledByDefault; - } + @SuppressWarnings({"UnusedDeclaration"}) + public boolean isEnabledByDefault() { + return enabledByDefault; + } - public boolean equals(Object other) { - if (!(other instanceof NotificationType)) { - return false; - } + @Override + public boolean equals(Object other) { + return (other instanceof NotificationType) && + name.equals(((NotificationType) other).name); + } - NotificationType otherType = (NotificationType) other; - - return name.equals(otherType.name); - } + @Override + public int hashCode() { + return name.hashCode(); + } } } diff --git a/libgrowl/src/java/info/growl/GrowlUtils.java b/libgrowl/src/java/info/growl/GrowlUtils.java index 916d5daa..1958de0b 100644 --- a/libgrowl/src/java/info/growl/GrowlUtils.java +++ b/libgrowl/src/java/info/growl/GrowlUtils.java @@ -32,65 +32,65 @@ import java.util.Map; /** * Utility class for sending notifications using Growl. - * + * * @author Michael Stringer * @version 0.1 */ public final class GrowlUtils { private static final boolean GROWL_LOADED; - private static Map instances = new HashMap(); + private static final Map instances = new HashMap(); static { - boolean loaded = false; - try { - System.loadLibrary("growl"); - loaded = true; - } catch (UnsatisfiedLinkError ule) { - System.out.println("Failed to load Growl library"); - } + boolean loaded = false; + try { + System.loadLibrary("growl"); + loaded = true; + } catch (UnsatisfiedLinkError ule) { + // ignore: growl not available + } - GROWL_LOADED = loaded; + GROWL_LOADED = loaded; } /** * Utility method - should not be instantiated. */ private GrowlUtils() { - } /** * Gets a Growl instance to use for the specified application * name. Multiple calls to this method will return the same instance. - * - * @param appName - * The name of the application. + * + * @param appName The name of the application. * @return The Growl instance to use. */ public static Growl getGrowlInstance(String appName) { - Growl instance = instances.get(appName); + synchronized (instances) { + Growl instance = instances.get(appName); - if (instance == null) { - if (GROWL_LOADED) { - instance = new GrowlNative(appName); - } else { - instance = new DummyGrowl(); - } + if (instance == null) { + if (GROWL_LOADED) { + instance = new GrowlNative(appName); + } else { + instance = new DummyGrowl(); + } - instances.put(appName, instance); - } + instances.put(appName, instance); + } - return instance; + return instance; + } } /** * Gets whether messages can be sent to Growl. If this returns * false then {@link #getGrowlInstance(String)} will return a * dummy object. - * + * * @return true if messages can be sent to Growl. */ public static boolean isGrowlLoaded() { - return GROWL_LOADED; + return GROWL_LOADED; } } diff --git a/libgrowl/src/java/info/growl/test/TestGrowl.java b/libgrowl/src/java/info/growl/test/TestGrowl.java index e570279a..c6af0040 100644 --- a/libgrowl/src/java/info/growl/test/TestGrowl.java +++ b/libgrowl/src/java/info/growl/test/TestGrowl.java @@ -47,7 +47,7 @@ import javax.swing.SwingUtilities; /** * Simple test class for Growl. - * + * * @author Michael Stringer * @version 0.1 */ @@ -55,132 +55,138 @@ public class TestGrowl extends JFrame { private static final String APP_NAME = "Test Java App"; private static final String NOTIF_3_CALLBACK = "Notif3"; + /** + * Test Growl. + */ public TestGrowl() { - super("Growl for Java"); - setSize(320, 290); + super("Growl for Java"); + setSize(320, 290); - buildComponents(); - setVisible(true); - setDefaultCloseOperation(EXIT_ON_CLOSE); + buildComponents(); + setVisible(true); + setDefaultCloseOperation(EXIT_ON_CLOSE); } private void buildComponents() { - getContentPane().setLayout(new GridLayout(6, 1)); + getContentPane().setLayout(new GridLayout(6, 1)); - Action action = new AbstractAction("Register") { - public void actionPerformed(ActionEvent ae) { - try { - Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); + Action action = new AbstractAction("Register") { + public void actionPerformed(ActionEvent ae) { + try { + Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); - growl.addNotification("Test Notification 1", true); - growl.addNotification("Test Notification 2", false); - growl.addNotification("Test Notification 3", true); + growl.addNotification("Test Notification 1", true); + growl.addNotification("Test Notification 2", false); + growl.addNotification("Test Notification 3", true); - GrowlCallbackListener listener = new GrowlCallbackListener() { - public void notificationWasClicked( - final String clickContext) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - if (NOTIF_3_CALLBACK.equals(clickContext)) { - JOptionPane - .showMessageDialog( - TestGrowl.this, - "User clicked on 'Test Notification 3'"); - } - } - }); - } - }; + GrowlCallbackListener listener = new GrowlCallbackListener() { + public void notificationWasClicked( + final String clickContext) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + if (NOTIF_3_CALLBACK.equals(clickContext)) { + JOptionPane + .showMessageDialog( + TestGrowl.this, + "User clicked on 'Test Notification 3'"); + } + } + }); + } + }; - growl.addCallbackListener(listener); - growl.register(); - } catch (GrowlException ge) { - ge.printStackTrace(); - } - } - }; + growl.addCallbackListener(listener); + growl.register(); + } catch (GrowlException ge) { + ge.printStackTrace(); + } + } + }; - getContentPane().add(new JButton(action)); + getContentPane().add(new JButton(action)); - action = new AbstractAction("Send 'Test Notification 1'") { - public void actionPerformed(ActionEvent ae) { - try { - Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); + action = new AbstractAction("Send 'Test Notification 1'") { + public void actionPerformed(ActionEvent ae) { + try { + Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); - growl.sendNotification("Test Notification 1", - "Test Notif 1", "This is a test"); - } catch (GrowlException ge) { - ge.printStackTrace(); - } - } - }; - getContentPane().add(new JButton(action)); + growl.sendNotification("Test Notification 1", + "Test Notif 1", "This is a test"); + } catch (GrowlException ge) { + ge.printStackTrace(); + } + } + }; + getContentPane().add(new JButton(action)); - action = new AbstractAction("Send 'Test Notification 2'") { - public void actionPerformed(ActionEvent ae) { - try { - Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); - BufferedImage image = ImageIO.read(TestGrowl.class - .getResource("/images/duke.gif")); - growl.sendNotification("Test Notification 2", - "Test Notif 2", "This is another test", image); - } catch (GrowlException ge) { - ge.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }; - getContentPane().add(new JButton(action)); + action = new AbstractAction("Send 'Test Notification 2'") { + public void actionPerformed(ActionEvent ae) { + try { + Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); + BufferedImage image = ImageIO.read(TestGrowl.class + .getResource("/images/duke.gif")); + growl.sendNotification("Test Notification 2", + "Test Notif 2", "This is another test", image); + } catch (GrowlException ge) { + ge.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + getContentPane().add(new JButton(action)); - action = new AbstractAction("Test Callback 'Notification 3'") { - public void actionPerformed(ActionEvent ae) { - try { - Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); + action = new AbstractAction("Test Callback 'Notification 3'") { + public void actionPerformed(ActionEvent ae) { + try { + Growl growl = GrowlUtils.getGrowlInstance(APP_NAME); - growl.sendNotification("Test Notification 3", - "Callback Test", "Click me - I dares you!", NOTIF_3_CALLBACK); - } catch (GrowlException ge) { - ge.printStackTrace(); - } - } - }; - getContentPane().add(new JButton(action)); + growl.sendNotification("Test Notification 3", + "Callback Test", "Click me - I dares you!", NOTIF_3_CALLBACK); + } catch (GrowlException ge) { + ge.printStackTrace(); + } + } + }; + getContentPane().add(new JButton(action)); - action = new AbstractAction("Reg & Test App 2") { - public void actionPerformed(ActionEvent ae) { - try { - Growl growl = GrowlUtils.getGrowlInstance("Other App"); + action = new AbstractAction("Reg & Test App 2") { + public void actionPerformed(ActionEvent ae) { + try { + Growl growl = GrowlUtils.getGrowlInstance("Other App"); - growl.addNotification("A Notification", true); + growl.addNotification("A Notification", true); - BufferedImage image = ImageIO.read(TestGrowl.class - .getResource("/images/duke.gif")); - growl.setIcon(image); + BufferedImage image = ImageIO.read(TestGrowl.class + .getResource("/images/duke.gif")); + growl.setIcon(image); - growl.register(); - growl.sendNotification("A Notification", "Testin", - "Blah de blah blah"); - } catch (GrowlException ge) { - ge.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }; - getContentPane().add(new JButton(action)); + growl.register(); + growl.sendNotification("A Notification", "Testin", + "Blah de blah blah"); + } catch (GrowlException ge) { + ge.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + getContentPane().add(new JButton(action)); - action = new AbstractAction("Exit") { - public void actionPerformed(ActionEvent ae) { - System.exit(0); - } - }; - getContentPane().add(new JButton(action)); + action = new AbstractAction("Exit") { + public void actionPerformed(ActionEvent ae) { + System.exit(0); + } + }; + getContentPane().add(new JButton(action)); } - public static final void main(String[] args) { - new TestGrowl(); + /** + * Test growl. + * @param args args + */ + public static void main(String[] args) { + //noinspection ResultOfObjectAllocationIgnored + new TestGrowl(); } }