Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: macos/MacOSHandler.java


1   /* 
2    * :tabSize=8:indentSize=8: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  package macos;
24  
25  //{{{ Imports
26  import com.apple.mrj.*;
27  import java.io.*;
28  import org.gjt.sp.jedit.*;
29  import org.gjt.sp.jedit.gui.*;
30  import org.gjt.sp.jedit.msg.*;
31  import org.gjt.sp.jedit.options.GlobalOptions;
32  import org.gjt.sp.util.Log;
33  //}}}
34  
35  public class MacOSHandler implements MRJQuitHandler, MRJAboutHandler,
36    MRJOpenDocumentHandler, MRJPrefsHandler, Handler
37  {
38    //{{{ Variables
39    private Buffer    lastOpenFile;
40    private ExitThread  et = new ExitThread();
41    
42    private final MRJOSType defaultType = new MRJOSType(jEdit.getProperty("MacOSPlugin.default.type"));
43    private final MRJOSType defaultCreator = new MRJOSType(jEdit.getProperty("MacOSPlugin.default.creator"));
44    //}}}
45    
46    //{{{ Constructor
47    public MacOSHandler()
48    {
49      if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar",
50        jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar"))
51      )
52        System.setProperty("com.apple.macos.useScreenMenuBar","true");
53      else
54        System.setProperty("com.apple.macos.useScreenMenuBar","false");
55      
56      if (jEdit.getBooleanProperty("MacOSPlugin.liveResize",
57        jEdit.getBooleanProperty("MacOSPlugin.default.liveResize"))
58      )
59        System.setProperty("com.apple.mrj.application.live-resize","true");
60      else
61        System.setProperty("com.apple.mrj.application.live-resize","false");
62    } //}}}
63    
64    //{{{ handleQuit() method
65    public void handleQuit()
66    {
67      // Need this to get around the double call bug
68      // in MRJ.
69      if (!et.isAlive())
70        // Spawn a new thread. This is a work around because of a
71        // bug in Mac OS X 10.1's MRJToolkit
72        et.start();
73      else
74        Log.log(Log.DEBUG,this,"ExitThread still alive.");
75      
76      throw new IllegalStateException("Exiting: aborting default exit");
77    } //}}}
78    
79    //{{{ handleAbout() method
80    public void handleAbout()
81    {
82      new AboutDialog(jEdit.getActiveView());
83    } //}}}
84  
85    //{{{ handlePrefs() method
86    public void handlePrefs()
87    {
88      new GlobalOptions(jEdit.getActiveView());
89    } //}}}
90    
91    //{{{ handleOpenFile() method
92    public void handleOpenFile(File file)
93    {
94      View view = jEdit.getActiveView();
95      Buffer buffer;
96      
97      if (view == null && MacOSPlugin.started)
98      {
99        if (jEdit.getBooleanProperty("restore.cli"))
100         view = jEdit.newView(null,jEdit.restoreOpenFiles());
101       else
102         view = jEdit.newView(null,jEdit.newFile(null));
103     }
104     
105     if ((buffer = jEdit.openFile(view,file.getPath())) != null)
106     {
107       lastOpenFile = buffer;
108     }
109     else
110       Log.log(Log.ERROR,this,"Error opening file.");
111   } //}}}
112 
113   //{{{ handleOpenFile() method
114   public void handleOpenFile(ViewUpdate msg)
115   {
116     if(msg.getWhat() == ViewUpdate.CREATED)
117     {
118       if(lastOpenFile != null)
119         msg.getView().setBuffer(lastOpenFile);
120       MacOSPlugin.started = true;
121     }
122   } //}}}
123   
124   //{{{ handleFileCodes() method
125   public void handleFileCodes(BufferUpdate msg)
126   {
127     Buffer buffer = msg.getBuffer();
128     File bufFile = new File(buffer.getPath());
129     
130     // Set type/creator on save
131     if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED)
132     {
133       try
134       {
135         MRJFileUtils.setFileTypeAndCreator( bufFile,
136           (MRJOSType)buffer.getProperty("MacOSPlugin.type"),
137           (MRJOSType)buffer.getProperty("MacOSPlugin.creator") );
138       }
139       catch (Exception e)
140       {
141         Log.log(Log.ERROR,this,"Error setting type/creator for "+bufFile.getPath());
142       }
143     }
144     // Add type/creator to local buffer property list on open
145     else if (msg.getWhat() == BufferUpdate.CREATED )
146     {
147       // This ensures that a type/creator will be assigned if an error occurs
148       buffer.setProperty("MacOSPlugin.type",defaultType);
149       buffer.setProperty("MacOSPlugin.creator",defaultCreator);
150       
151       if (jEdit.getBooleanProperty("MacOSPlugin.preserveCodes",
152         jEdit.getBooleanProperty("MacOSPlugin.default.preserveCodes")))
153       {
154         try
155         {
156           MRJOSType type = MRJFileUtils.getFileType(bufFile);
157           MRJOSType creator = MRJFileUtils.getFileCreator(bufFile);
158           
159           if (!type.equals(new MRJOSType("")))
160             buffer.setProperty("MacOSPlugin.type",type);
161           if (!creator.equals(new MRJOSType("")))
162             buffer.setProperty("MacOSPlugin.creator",creator);
163         }
164         catch (Exception e) {} // This will happen when a new file is created
165       }
166       Log.log(Log.DEBUG,this,"Assigned MRJOSTypes " + buffer.getProperty("MacOSPlugin.type")
167       + "/" + buffer.getProperty("MacOSPlugin.creator") + " to " + bufFile.getPath());
168     }
169   } //}}}
170   
171   //{{{ ExitThread class
172   class ExitThread extends Thread
173   {
174     public void run()
175     {
176       jEdit.exit(jEdit.getActiveView(),true);
177       et = new ExitThread();
178     }
179   } //}}}
180 }