PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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