PageRenderTime 33ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/jars/MacOS/MacOSHandler.java

#
Java | 172 lines | 113 code | 18 blank | 41 comment | 19 complexity | 61b86bc156aa957cf025ecbabb12e26e MD5 | raw file
  1/* 
  2 * :tabSize=4:indentSize=4:noTabs=false:
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * MacOSHandler.java - Various handlers for Mac OS Plugin
  6 * Copyright (C) 2002 Kris Kopicki
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22 
 23//{{{ Imports
 24import com.apple.mrj.*;
 25import java.io.*;
 26import org.gjt.sp.jedit.*;
 27import org.gjt.sp.jedit.gui.*;
 28import org.gjt.sp.jedit.msg.*;
 29import org.gjt.sp.jedit.options.GlobalOptions;
 30import org.gjt.sp.util.Log;
 31//}}}
 32
 33public class MacOSHandler implements MRJQuitHandler, MRJAboutHandler,
 34	MRJOpenDocumentHandler, MRJPrefsHandler, Handler
 35{
 36	
 37//{{{ Variables
 38    private String		lastOpenFile;
 39	private ExitThread	et = new ExitThread();
 40	
 41	private final MRJOSType defaultType = new MRJOSType(jEdit.getProperty("MacOSPlugin.default.type"));
 42	private final MRJOSType defaultCreator = new MRJOSType(jEdit.getProperty("MacOSPlugin.default.creator"));
 43//}}}
 44	
 45	//{{{ Constructor
 46	public MacOSHandler()
 47	{
 48		if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar",
 49			jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar"))
 50		)
 51			System.setProperty("com.apple.macos.useScreenMenuBar","true");
 52		else
 53			System.setProperty("com.apple.macos.useScreenMenuBar","false");
 54		
 55		if (jEdit.getBooleanProperty("MacOSPlugin.liveResize",
 56			jEdit.getBooleanProperty("MacOSPlugin.default.liveResize"))
 57		)
 58			System.setProperty("com.apple.mrj.application.live-resize","true");
 59		else
 60			System.setProperty("com.apple.mrj.application.live-resize","false");
 61	}//}}}
 62	
 63	//{{{ handleQuit() method
 64	public void handleQuit()
 65    {
 66		// Need this to get around the double call bug
 67		// in MRJ.
 68		if (!et.isAlive())
 69			// Spawn a new thread. This is a work around because of a
 70			// bug in Mac OS X 10.1's MRJToolkit
 71			et.start();
 72		else
 73			Log.log(Log.DEBUG,this,"ExitThread still alive.");
 74		
 75		throw new IllegalStateException("Exiting: aborting default exit");
 76	}//}}}
 77	
 78	//{{{ handleAbout() method
 79	public void handleAbout()
 80    {
 81		new AboutDialog(jEdit.getLastView());
 82	}//}}}
 83
 84	//{{{ handlePrefs() method
 85	public void handlePrefs()
 86	{
 87		new GlobalOptions(jEdit.getLastView());
 88	}//}}}
 89	
 90	//{{{ handleOpenFile() method
 91	public void handleOpenFile(File file)
 92    {
 93		if (jEdit.openFile(jEdit.getLastView(),file.getPath()) != null)
 94        {
 95            lastOpenFile = file.getPath();
 96        } else {
 97            Log.log(Log.ERROR,this,"Error opening file.");
 98        }
 99	}//}}}
100
101	//{{{ handleOpenFile() method
102	public void handleOpenFile(ViewUpdate msg)
103    {
104		if(msg.getWhat() == ViewUpdate.CREATED)
105		{
106			if(lastOpenFile != null)
107			{
108				jEdit.getLastView().setBuffer(jEdit.getBuffer(lastOpenFile));
109			}
110			((MacOSPlugin)jEdit.getPlugin("MacOSPlugin")).started(true);
111		}
112	}//}}}
113	
114	//{{{ handleFileCodes() method
115	public void handleFileCodes(BufferUpdate msg)
116	{
117		Buffer buffer = msg.getBuffer();
118		File bufFile = new File(buffer.getPath());
119		
120		// Set type/creator on save
121		if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
122		{
123			try
124			{
125				MRJFileUtils.setFileTypeAndCreator( bufFile,
126					(MRJOSType)buffer.getProperty("MacOSPlugin.type"),
127					(MRJOSType)buffer.getProperty("MacOSPlugin.creator") );
128			}
129			catch (Exception e)
130			{
131				Log.log(Log.ERROR,this,"Error setting type/creator for "+bufFile.getPath());
132			}
133		}
134		// Add type/creator to local buffer property list on open
135		else if (msg.getWhat() == BufferUpdate.CREATED )
136		{
137			// This ensures that a type/creator will be assigned if an error occurs
138			buffer.setProperty("MacOSPlugin.type",defaultType);
139			buffer.setProperty("MacOSPlugin.creator",defaultCreator);
140			
141			if (jEdit.getBooleanProperty("MacOSPlugin.preserveCodes",
142				jEdit.getBooleanProperty("MacOSPlugin.default.preserveCodes")))
143			{
144				try
145				{
146					MRJOSType	type	= MRJFileUtils.getFileType(bufFile);
147					MRJOSType	creator	= MRJFileUtils.getFileCreator(bufFile);
148					
149					if (!type.equals(new MRJOSType("")))
150						buffer.setProperty("MacOSPlugin.type",type);
151					if (!creator.equals(new MRJOSType("")))
152						buffer.setProperty("MacOSPlugin.creator",creator);
153				}
154				catch (Exception e) {} // This will happen when a new file is created
155			}
156			Log.log(Log.DEBUG,this,"Assigned MRJOSTypes " + buffer.getProperty("MacOSPlugin.type")
157			+ "/" + buffer.getProperty("MacOSPlugin.creator") + " to " + bufFile.getPath());
158		}
159	}//}}}
160	
161//{{{ ExitThread class
162	class ExitThread extends Thread
163	{
164		public void run()
165		{
166			jEdit.exit(jEdit.getLastView(),false);
167			et = new ExitThread();
168		}
169	}
170//}}}
171
172}