PageRenderTime 73ms CodeModel.GetById 64ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/jars/MacOS/MacOSPlugin.java

#
Java | 159 lines | 105 code | 11 blank | 43 comment | 23 complexity | 733db0a8b9a28fd531383a5f01742595 MD5 | raw file
  1/* 
  2 * :tabSize=4:indentSize=4:noTabs=false:
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * MacOSPlugin.java - Main class Mac OS Plugin
  6 * Copyright (C) 2001, 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 java.lang.*;
 27import javax.swing.*;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.jedit.gui.*;
 30import org.gjt.sp.jedit.msg.*;
 31import org.gjt.sp.util.Log;
 32//}}}
 33
 34public class MacOSPlugin extends EBPlugin implements MRJQuitHandler
 35, MRJAboutHandler, MRJOpenDocumentHandler
 36{
 37    //{{{ Instance variables
 38	private boolean onStartup = true;
 39    private String lastFilePath;
 40	private ExitThread exitThread = new ExitThread();
 41	
 42	private final MRJOSType defaultType = new MRJOSType(jEdit.getProperty("MacOS.default.type"));
 43	private final MRJOSType defaultCreator = new MRJOSType(jEdit.getProperty("MacOS.default.creator"));
 44	//}}}
 45    
 46	//{{{ Methods
 47	
 48	//{{{ start() method
 49	public void start()
 50    {		
 51		if(System.getProperty("os.name").indexOf("Mac OS") != -1)
 52        {
 53			// Register handlers
 54			MRJApplicationUtils.registerQuitHandler(this);
 55    		MRJApplicationUtils.registerAboutHandler(this);
 56			MRJApplicationUtils.registerOpenDocumentHandler(this);
 57    	} else {
 58            Log.log(Log.ERROR,this,"This plugin requires Mac OS.");
 59        }
 60	}//}}}
 61	
 62	//{{{ handleQuit() method
 63	public void handleQuit()
 64    {
 65		// Need this to get around the double call bug
 66		// in MRJ.
 67		if (!exitThread.isAlive())
 68			// Spawn a new thread. This is a work around because of a
 69			// bug in Mac OS X 10.1's MRJToolkit
 70			exitThread.start();
 71		else
 72			Log.log(Log.DEBUG,this,"exitThread still alive.");
 73	}//}}}
 74	
 75	//{{{ handleAbout() method
 76	public void handleAbout()
 77    {
 78		new AboutDialog(jEdit.getLastView());
 79	}//}}}
 80
 81	//{{{ handleOpenFile() method
 82	public void handleOpenFile(File file)
 83    {
 84		if (jEdit.openFile(jEdit.getLastView(),file.getPath()) != null)
 85        {
 86            lastFilePath = file.getPath();
 87        } else {
 88            Log.log(Log.ERROR,this,"Error opening file.");
 89        }
 90	}//}}}
 91
 92	//{{{ handleMessage() method
 93    public void handleMessage(EBMessage message)
 94    {
 95        // This is necessary to have a file opened from the Finder
 96    	// before jEdit is running set as the currently active
 97    	// buffer.
 98        if ((message instanceof ViewUpdate) && onStartup)
 99        {
100            if( ((ViewUpdate)message).getWhat() == ViewUpdate.CREATED)
101            {
102                if(lastFilePath != null)
103                {
104                	jEdit.getLastView().setBuffer(jEdit.getBuffer(lastFilePath));
105                }
106                onStartup = false;
107            }
108        }
109		// Set type/creator codes for files
110        else if (message instanceof BufferUpdate)
111        {
112            Buffer buffer = ((BufferUpdate)message).getBuffer();
113			if ( ((BufferUpdate)message).getWhat() == BufferUpdate.DIRTY_CHANGED && !buffer.isDirty() )
114            {
115				try
116                {
117					MRJFileUtils.setFileTypeAndCreator( buffer.getFile(),
118					(MRJOSType)(buffer.getProperty("MacOS.type")),
119					(MRJOSType)(buffer.getProperty("MacOS.creator")) );
120				}
121            	catch (Exception e)
122            	{
123            		Log.log(Log.ERROR,this,"Error setting type/creator: file missing");
124            	}
125            }
126			// Add type/creator to local buffer property list, if they exist.
127			else if ( ((BufferUpdate)message).getWhat() == BufferUpdate.CREATED )
128			{
129				buffer.setProperty("MacOS.type",defaultType);
130				buffer.setProperty("MacOS.creator",defaultCreator);
131				try
132				{
133					MRJOSType	type	= MRJFileUtils.getFileType(buffer.getFile());
134					MRJOSType	creator	= MRJFileUtils.getFileCreator(buffer.getFile());
135					if (!type.equals(new MRJOSType("")) && !creator.equals(new MRJOSType("")))
136					{
137						buffer.setProperty("MacOS.type",type);
138						buffer.setProperty("MacOS.creator",creator);
139					}
140				}
141				catch (Exception e) {}
142				Log.log(Log.DEBUG,this,"Assigned MRJOSTypes " + buffer.getProperty("MacOS.type")
143				+ "/" + buffer.getProperty("MacOS.creator") + " to " + buffer.getName());
144			}
145        }
146    }//}}}
147	
148	//}}}
149	
150	//{{{ ExitThread class
151	class ExitThread extends Thread
152	{
153		public void run()
154		{
155			jEdit.exit(jEdit.getLastView(),false);
156			exitThread = new ExitThread();
157		}
158	}//}}}
159}