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