Author Topic: [Java+OSX] Growl notifications  (Read 4368 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java+OSX] Growl notifications
« on: November 25, 2007, 04:26:28 pm »
When you download Growl, it comes with a Java class that allows you to interact with Growl. If you develop in Eclipse, or you do not have access to an OSX machine at all, you'll have a lot of trouble getting this to work properly.

I modified the class to use reflection to discover the com.apple.cocoa dependencies, and fail gracefully if they do not exist. If you use the original version of this class, you must be aware, in your application code, of several of the NS* classes in the cocoa package in order to initialize the Growl object, which makes it very difficult to fail gracefully when, for example, you are not using OSX, and these com.apple.cocoa.NS* classes simply do not exist. This modified version keeps that responsibility localized.

http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/bot/gui/notifications/Growl.java

In my bot, I try to initialize the SystemTray object introduced in Java 6. If it doesn't exist, or the system doesn't support it, it'll try to fall back on Growl.
Code: [Select]
try {
if(!SystemTray.isSupported())
throw new NoClassDefFoundError();
} catch(NoClassDefFoundError e) {
Out.error(GuiEventHandler.class, "System tray is not supported; trying growl");

try {
growl = new Growl("BNU-Bot", "Contents/Resources/Icon.icns");
} catch(Exception ex) {
Out.exception(ex);
Out.error(GuiEventHandler.class, "Growl is not supported either");
GlobalSettings.enableTrayIcon = false;
}
return;
}

Then you can send generic messages that will automatically be sent to either the tray icon or to Growl with this method:
Code: [Select]
private void notifySystemTray(String gt, String headline, String text) {
// Require that enableTrayPopups is set
if(!GlobalSettings.enableTrayPopups)
return;

// If popups are not always enabled, require that the window is defocused
if(!GlobalSettings.enableTrayPopupsAlways)
if(GuiDesktop.getInstance().isFocused())
return;

TrayIcon tray = GuiDesktop.getTray();
if(tray != null)
tray.displayMessage(headline, text, TrayIcon.MessageType.INFO);

Growl growl = GuiDesktop.getGrowl();
if(growl != null)
try {
growl.notifyGrowlOf(gt, headline, text);
} catch (Exception e) {
Out.exception(e);
}
}

Here's an example:
Code: [Select]
public void channelJoin(BNetUser user) {
userList.showUser(user);
if(GlobalSettings.displayJoinParts)
mainTextArea.channelInfo(user + " has joined the channel" + user.getStatString().toString() + ".");
notifySystemTray(
Growl.CHANNEL_USER_JOIN,
user.toString(),
"joined the channel" + user.getStatString().toString());
channelTextPane.setText(channel + " (" + userList.count() + ")");
}

public void channelLeave(BNetUser user) {
userList.removeUser(user);
if(GlobalSettings.displayJoinParts)
mainTextArea.channelInfo(user + " has left the channel.");
notifySystemTray(
Growl.CHANNEL_USER_PART,
user.toString(),
"left the channel");
channelTextPane.setText(channel + " (" + userList.count() + ")");
}

This is what it looks like:

« Last Edit: November 25, 2007, 04:31:21 pm by Camel »

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!