/*FreeMind - A Program for creating and viewing Mindmaps
 *Copyright (C) 2000-2004  Joerg Mueller, Daniel Polansky, Christian Foltin and others.
 *
 *See COPYING for Details
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either version 2
 *of the License, or (at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program; if not, write to the Free Software
 *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Created on 25.08.2004
 */
/*$Id: MacChanges.java.not_here,v 1.1.4.5 2005/06/22 19:40:13 christianfoltin Exp $*/
package accessories.plugins;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;

import javax.swing.SwingUtilities;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;

import freemind.extensions.HookRegistration;
import freemind.main.FreeMindMain;
import freemind.main.XMLParseException;
import freemind.modes.MindMap;
import freemind.modes.ModeController;

/** This plugin changes some things for mac users.
 * @author foltin
 */
public class MacChanges extends ApplicationAdapter implements HookRegistration {

	private ModeController controller;

	private MindMap mMap;

	private Logger logger;
	
	private static Application fmMacApplication;

	public MacChanges(ModeController controller, MindMap map) {
		this.controller = controller;
		mMap = map;
		FreeMindMain frame = controller.getFrame();
		logger = frame.getLogger(this.getClass().getName());
		if(fmMacApplication==null){
			fmMacApplication = Application.getApplication();
			fmMacApplication.addApplicationListener(this);
			fmMacApplication.addPreferencesMenuItem();
			fmMacApplication.addAboutMenuItem();
			fmMacApplication.setEnabledPreferencesMenu(true);
//			fmMacApplication.removePreferencesMenuItem();
		}
		frame.setProperty("keystroke_add_child", frame.getProperty("keystroke_add_child_mac"));
	}

	public void register() {
	}

	public void deRegister() {
	}

	public void handleQuit(ApplicationEvent event) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				controller.getController().quit.actionPerformed(null);
			}
		});
		event.setHandled(true);
		throw new IllegalStateException("Stop Pending User Confirmation");
	}

	public void handleAbout(ApplicationEvent event) {
		controller.getController().about.actionPerformed(null);
		event.setHandled(true);
	}
	public void handleOpenFile(ApplicationEvent event) {
		try {
			controller.load(new File(event.getFilename()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		event.setHandled(true);
	}
	
	public void handlePreferences(ApplicationEvent event) {
		controller.getController().propertyAction.actionPerformed(null);
		event.setHandled(true);
	}
}