growl.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 466 行 · 第 1/2 页

JAVA
466
字号
/** * Growl.java *  * Version: * $Id:$ * * Revisions: * $Log:$ * */package com.growl;import com.apple.cocoa.foundation.NSDistributedNotificationCenter;import com.apple.cocoa.foundation.NSArray;import com.apple.cocoa.foundation.NSDictionary;import com.apple.cocoa.foundation.NSMutableDictionary;import com.apple.cocoa.foundation.NSData;import com.apple.cocoa.application.NSImage;/** * A class that encapsulates the "work" of talking to growl * * @author Karl Adam */public class Growl {	// defines	/** The name of the growl registration notification for DNC. */	public static final String GROWL_APP_REGISTRATION = "GrowlApplicationRegistrationNotification";	//  Ticket Defines	/** Ticket key for the application name. */	public static final String GROWL_APP_NAME = "ApplicationName";	/** Ticket key for the application icon. */	public static final String GROWL_APP_ICON = "ApplicationIcon";	/** Ticket key for the default notifactions. */	public static final String GROWL_NOTIFICATIONS_DEFAULT = "DefaultNotifications";	/** Ticket key for all notifactions. */	public static final String GROWL_NOTIFICATIONS_ALL = "AllNotifications";	//  Notification Defines	/** The name of the growl notification for DNC. */	public static final String GROWL_NOTIFICATION = "GrowlNotification";	/** Notification key for the name. */	public static final String GROWL_NOTIFICATION_NAME = "NotificationName";	/** Notification key for the title. */	public static final String GROWL_NOTIFICATION_TITLE = "NotificationTitle";	/** Notification key for the description. */	public static final String GROWL_NOTIFICATION_DESCRIPTION = "NotificationDescription";	/** Notification key for the icon. */	public static final String GROWL_NOTIFICATION_ICON = "NotificationIcon";	/** Notification key for the application icon. */	public static final String GROWL_NOTIFICATION_APP_ICON = "NotificationAppIcon";	/** Notification key for the sticky flag. */	public static final String GROWL_NOTIFICATION_STICKY = "NotificationSticky";	// Actual instance data	private boolean      registered;    // We should only register once	private String       appName;       // "Application" Name	private NSData       appImageData;  // "application" Icon	private NSDictionary regDict;       // Registration Dictionary	private NSArray      allNotes;      // All notifications	private NSArray      defNotes;      // Default Notifications	private NSDistributedNotificationCenter theCenter;	//************  Constructors **************//	/**	 * Convenience method to contruct a growl instance, defers to Growl(String 	 * inAppName, NSData inImageData, NSArray inAllNotes, NSArray inDefNotes, 	 * boolean registerNow) with empty arrays for your notifications.	 *	 *	 * @param inAppName - The Name of your "application"	 * @param inImage - The NSImage Icon for your Application	 *	 */	public Growl(String inAppName, NSImage inImage) {			this(inAppName,			  inImage.TIFFRepresentation(),			  new NSArray(),			  new NSArray(),			  false);	}	/**	 * Convenience method to contruct a growl instance, defers to Growl(String 	 * inAppName, NSData inImageData, NSArray inAllNotes, NSArray inDefNotes, 	 * boolean registerNow) with empty arrays for your notifications.	 *	 * @param inAppName - The Name of your "Application"	 * @param inImageData - The NSData for your NSImage	 */	public Growl(String inAppName, NSData inImageData) {		this(inAppName,			 inImageData,			 new NSArray(),			 new NSArray(),			 false);	}	/**	 * Convenience method to contruct a growl instance, defers to Growl(String 	 * inAppName, NSData inImageData, NSArray inAllNotes, NSArray inDefNotes, 	 * boolean registerNow) with empty arrays for your notifications.	 * 	 * @param inAppName - The Name of your "Application"	 * @param inImagePath - The path to your icon	 *	 */	public Growl(String inAppName, String inImagePath) {			this(inAppName, 			 new NSImage(inImagePath, false).TIFFRepresentation(), 			 new NSArray(), 			 new NSArray(), 			 false);	}	/**	 * Convenience method to contruct a growl instance, defers to Growl(String 	 * inAppName, NSData inImageData, NSArray inAllNotes, NSArray inDefNotes, 	 * boolean registerNow) with the arrays passed here and empty Data for the icon.	 *	 * @param inAppName - The Name of your "Application"	 * @param inAllNotes - A String Array with the name of all your notifications	 * @param inDefNotes - A String Array with the na,es of the Notifications on 	 *                     by default	 */	public Growl(String inAppName, String [] inAllNotes, String [] inDefNotes) {		this(inAppName, 			 new NSData(), 			 new NSArray(inAllNotes), 			 new NSArray(inDefNotes), 			 false);	}	/**	 * Convenience method to contruct a growl instance, defers to Growl(String 	 * inAppName, NSData inImageData, NSArray inAllNotes, NSArray inDefNotes, 	 * boolean registerNow) with empty arrays for your notifications.	 *	 * @param inAppName - The Name of your "Application"	 * @param inImageData - The Data of your "Application"'s icon	 * @param inAllNotes - The NSArray of Strings of all your Notifications	 * @param inDefNotes - The NSArray of Strings of your default Notifications	 * @param registerNow - Since we have all the necessary info we can go ahead 	 *                      and register	 */	public Growl(String inAppName, NSData inImageData, NSArray inAllNotes, 	   NSArray inDefNotes, boolean registerNow) {		appName = inAppName;		appImageData = inImageData;		allNotes = inAllNotes;		defNotes = inDefNotes;		theCenter  = (NSDistributedNotificationCenter)NSDistributedNotificationCenter.defaultCenter();		if (registerNow) {			register();		}	}	//************  Commonly Used Methods **************//	/**	 * Register all our notifications with Growl, this should only be called	 * once.	 * @return <code>true</code>.	 */	public boolean register() {		if (!registered) {			// Construct our dictionary			// Make the arrays of objects then keys			Object [] objects = { appName, allNotes, defNotes, appImageData };			String [] keys = { GROWL_APP_NAME, 					GROWL_NOTIFICATIONS_ALL, 					GROWL_NOTIFICATIONS_DEFAULT,					GROWL_APP_ICON};			// Make the Dictionary			regDict = new NSDictionary(objects, keys);			theCenter.postNotification(GROWL_APP_REGISTRATION,	// notificationName									   (String)null,			// anObject									   regDict,					// userInfoDictionary									   true);					// deliverImmediately		}		return true;	}	/**	 * The fun part is actually sending those notifications we worked so hard for	 * so here we let growl know about things we think the user would like, and growl	 * decides if that is the case.	 *	 * @param inNotificationName - The name of one of the notifications we told growl	 *                             about.	 * @param inIconData - The NSData for the icon for this notification, can be null	 * @param inTitle - The Title of our Notification as Growl will show it	 * @param inDescription - The Description of our Notification as Growl will 	 *                        display it	 * @param inExtraInfo - Growl is flexible and allows Display Plugins to do as they 	 *                      please with thier own special keys and values, you may use 	 *                      them here. These may be ignored by either the user's 	 *                      preferences or the current Display Plugin. This can be null	 * @param inSticky - Whether the Growl notification should be sticky	 *	 * @throws Exception When a notification is not known	 */	public void notifyGrowlOf(String inNotificationName, NSData inIconData, 			       String inTitle, String inDescription, 			       NSDictionary inExtraInfo, boolean inSticky) throws Exception {		NSMutableDictionary noteDict = new NSMutableDictionary();		if (!allNotes.containsObject(inNotificationName)) {			throw new Exception("Undefined Notification attempted");		}		noteDict.setObjectForKey(inNotificationName, GROWL_NOTIFICATION_NAME);		noteDict.setObjectForKey(inTitle, GROWL_NOTIFICATION_TITLE);		noteDict.setObjectForKey(inDescription, GROWL_NOTIFICATION_DESCRIPTION);		noteDict.setObjectForKey(appName, GROWL_APP_NAME);		if (inIconData != null) {			noteDict.setObjectForKey(inIconData, GROWL_NOTIFICATION_ICON);		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?