Previous Book Contents Book Index Next

Inside Macintosh: Programming With MRJToolkit /
Chapter 1 - Using MRJToolkit


Responding to Simple System Events

On the Mac OS platform, applications can respond to simple events passed to it by the Finder or other applications. For example, if the user drops a file onto an application's icon, the Finder launches the application (if not already open) and passes it an "open file" event instructing it to open the file if possible.

Note
Events on the Mac OS platform are handled through Apple Events, which are described in detail in Inside Macintosh: Interapplication Communication. No knowlege of Apple Events is needed when using MRJToolkit.
MRJToolkit allows your Java application to respond to the following system events:

MRJToolkit also allows your application to respond to an "About box selected" event when the About item in the Apple menu is selected. An About box is a window that gives information about the application, such as the version number, serial number, and credits (members of the development team and so on).

To make your application aware of the event, you must implement an interface that handles the particular event and then register the handler method with MRJToolkit. For example, to create a quit handler, you must implement the interface MRJQuitHandler. This interface has the form shown in Listing 1-5.

Listing 1-5 The MRJQuitHandler interface

package.com.apple.mrj

public interface MRJQuitHandler {
   public void handleQuit();
}
To use this interface, the class that implements it must contain a method named handleQuit that defines the actions to take when the application receives a quit request. In addition, the application must also register the method name with MRJToolkit by calling the appropriate registration method in the class com.apple.mrj.MRJApplicationUtils. For example, to register the quit event handler, you must call the MRJRegisterQuitHandler method. Listing 1-6 shows an example of implementing a quit handler.

Listing 1-6 Implementing the quit event handler

import com.apple.mrj.*;
import java.io.*;

...

class QuitTest implements MRJQuitHandler {
   
   public QuitTest() {
      System.out.println("Select Quit from the Apple menu or shutdown
               to test event handler.");

      MRJApplicationUtils.registerQuitHandler(this);
   }
      
   public void handleQuit() {
      System.exit(0); /* Quit the MRJ runtime */
      }
}
Since the call to registerQuitHandler occurs within the class that contains handleQuit, it can use the this variable to reference the handler.

Table 1-1 shows the available handler interfaces, their corresponding registration methods, and the conditions under which an event is sent. All the registration methods are members of the class com.apple.mrj.MRJApplicationUtils.
Table 1-1 Event handling methods
InterfaceHandler NameRegistration MethodWhen Activated
MRJOpenDocumentHandlerhandleOpenFileregisterOpenDocumentHandlerWhen the Finder requests that a file be opened (for example, when the user drags an appropriate file onto the application icon).
MRJQuitHandlerhandleQuitregisterQuitHandlerWhen the application receives a request to quit (for example, before shutting down, or if the user selected the default Quit Item in the Apple Menu).
MRJPrintDocumentHandlerhandlePrintFileregisterPrintDocumentHandlerWhen the Finder requests that the application print (for example, if the user selects a file in the Finder and chooses the Print menu item).
MRJAboutHandlerhandleAboutregisterAboutHandlerWhen the user selects the About item in the application's Apple Menu.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
12 NOV 1997