All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.roydesign.mac.MRJ4EventProxy Maven / Gradle / Ivy

/*******************************************************************************

	File:		MRJ4EventProxy.java
	Author:		Steve Roy 
				
	Part of MRJ Adapter, a unified API for easy integration of Mac OS specific
	functionality within your cross-platform Java application.
	
	This library is open source and can be modified and/or distributed under
	the terms of the Artistic License.
	
	
	Change History:
	03/31/03	Created this file - Steve
	08/27/03	Added handling of the new Reopen Application event - Steve
	09/11/03	Added the setHandled(true) calls in the event handlers - Steve
	09/29/03	Removed the setHandled(true) call in Handler.handleQuit()
				because it was causing the VM to call System.exit(), changed
				constructor not to enable the Preferences item - Steve

*******************************************************************************/

package net.roydesign.mac;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import net.roydesign.event.ApplicationEvent;

import java.io.File;

/**
 * Implementation of an application event proxy which supports Mac OS
 * virtual machines that use com.apple.eawt as their integration mechanism.
 * This corresponds to MRJ 4 and above which implements Java 1.4.1 and above.
 * @see MRJEventProxy
 * 
 * @version MRJ Adapter 1.2
 */
final class MRJ4EventProxy extends MRJEventProxy
{
	/**
	 * The single instance of MRJ4EventProxy.
	 */
	private static MRJ4EventProxy instance;
	
	/**
	 * The {@code com.apple.eawt.Application} that we get the
	 * native MRJ events from.
	 */
	private final Application application;
	
	/**
	 * Get the single instance of this class.
	 * @return the single instance of {@code MRJ4EventProxy}
	 */
	public static MRJ4EventProxy getInstance()
	{
		if (instance == null)
			instance = new MRJ4EventProxy();
		return instance;
	}
	
	/**
	 * Construct an MRJ 4 event proxy.
	 */
	private MRJ4EventProxy()
	{
		application = new Application();
		application.addApplicationListener(new Handler());
	}
	
	/**
	 * Get whether the Preferences menu item is enabled or not. This menu
	 * item is automatically provided by the OS on Mac OS X. On classic
	 * Mac OS, this method always returns false.
	 * @return whether the Preferences menu item is enabled
	 */
	@Override
	public boolean isPreferencesEnabled()
	{
		return application.getEnabledPreferencesMenu();
	}
	
	/**
	 * Set whether the Preferences menu item is enabled or not. This menu
	 * item is automatically provided by the OS on Mac OS X. On classic
	 * Mac OS, this method does nothing.
	 * @param enabled whether the menu item is enabled
	 */
	@Override
	public void setPreferencesEnabled(boolean enabled)
	{
		application.setEnabledPreferencesMenu(enabled);
	}
	
	/**
	 * This class implements the listener that handles native events
	 * which it then relays to MRJ Adapter using our unified interface.
	 */
	private class Handler extends ApplicationAdapter
	{
		@Override
		public void handleAbout(com.apple.eawt.ApplicationEvent e)
		{
			fireMenuEvent(ApplicationEvent.ABOUT);
			e.setHandled(true);
		}
		
		@Override
		public void handlePreferences(com.apple.eawt.ApplicationEvent e)
		{
			fireMenuEvent(ApplicationEvent.PREFERENCES);
			e.setHandled(true);
		}
		
		@Override
		public void handleOpenApplication(com.apple.eawt.ApplicationEvent e)
		{
			fireApplicationEvent(ApplicationEvent.OPEN_APPLICATION);
			e.setHandled(true);
		}
		
		@Override
		public void handleReOpenApplication(com.apple.eawt.ApplicationEvent e)
		{
			fireApplicationEvent(ApplicationEvent.REOPEN_APPLICATION);
			e.setHandled(true);
		}
		
		@Override
		public void handleQuit(com.apple.eawt.ApplicationEvent e)
		{
			fireApplicationEvent(ApplicationEvent.QUIT_APPLICATION);
		}
		
		@Override
		public void handleOpenFile(com.apple.eawt.ApplicationEvent e)
		{
			fireDocumentEvent(ApplicationEvent.OPEN_DOCUMENT, new File(e.getFilename()));
			e.setHandled(true);
		}
		
		@Override
		public void handlePrintFile(com.apple.eawt.ApplicationEvent e)
		{
			fireDocumentEvent(ApplicationEvent.PRINT_DOCUMENT, new File(e.getFilename()));
			e.setHandled(true);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy