/plugins/Sessions/tags/sessions_1_2_3/sessions/SessionsPlugin.java

# · Java · 307 lines · 203 code · 45 blank · 59 comment · 49 complexity · 5860ce294688be4c5f04cc01a7e5acef MD5 · raw file

  1. /*
  2. * SessionsPlugin.java
  3. * Copyright (c) 2000,2001 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package sessions;
  22. import java.awt.BorderLayout;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import javax.swing.JCheckBox;
  26. import javax.swing.JComponent;
  27. import javax.swing.JOptionPane;
  28. import javax.swing.SwingUtilities;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.jedit.gui.DockableWindowManager;
  31. import org.gjt.sp.jedit.msg.*;
  32. import org.gjt.sp.util.Log;
  33. /**
  34. * The Sessions plugin.
  35. *
  36. * @author Dirk Moebius
  37. */
  38. public class SessionsPlugin extends EBPlugin
  39. {
  40. public void start()
  41. {
  42. boolean restore = jEdit.getBooleanProperty("restore");
  43. boolean restore_cli = jEdit.getBooleanProperty("restore.cli");
  44. boolean autosave = jEdit.getBooleanProperty("sessions.switcher.autoSave", true);
  45. if ((!restore || !restore_cli) && autosave)
  46. {
  47. // Show a warning if either "Restore previously open files on startup"
  48. // or "Restore even if file names were specified on the command line"
  49. // is off and Session Autosave is on:
  50. showInfoMessage("sessions.manager.info.restore_autosave");
  51. }
  52. else if (!restore)
  53. {
  54. // "Restore previously open files on startup" is off.
  55. // The last open session won't be restored.
  56. // Show an information dialog.
  57. showInfoMessage("sessions.manager.info.restore");
  58. }
  59. SessionManager mgr = SessionManager.getInstance();
  60. // Though we don't need to load the current session's files, we
  61. // still need to load the custom properties into memory.
  62. mgr.getCurrentSessionInstance().open(jEdit.getActiveView(), false);
  63. // Put the session name in the jEdit title bar
  64. mgr.setSessionNameInTitleBar();
  65. mgr.refreshTitleBar();
  66. if (jEdit.getBooleanProperty("sessions.switcher.showToolBar", false))
  67. {
  68. // Add SessionSwitcher to existing Views
  69. View[] views = jEdit.getViews();
  70. for (int i = 0; i < views.length; i++)
  71. {
  72. addSessionSwitcher(views[i]);
  73. }
  74. }
  75. }
  76. public void stop()
  77. {
  78. // Remove SessionSwitcher from existing Views
  79. View[] views = jEdit.getViews();
  80. for (int i = 0; i < views.length; i++)
  81. {
  82. removeSessionSwitcher(views[i]);
  83. }
  84. // update the title bar
  85. SessionManager mgr = SessionManager.getInstance();
  86. mgr.restoreTitleBarText();
  87. mgr.refreshTitleBar();
  88. }
  89. public void handleMessage(EBMessage message)
  90. {
  91. if (message instanceof ViewUpdate)
  92. {
  93. ViewUpdate vu = (ViewUpdate) message;
  94. if (vu.getWhat() == ViewUpdate.CREATED)
  95. {
  96. if(jEdit.getBooleanProperty("sessions.switcher.showToolBar", false))
  97. addSessionSwitcher(vu.getView());
  98. }
  99. else if (vu.getWhat() == ViewUpdate.CLOSED)
  100. {
  101. viewSessionSwitchers.remove(vu.getView());
  102. }
  103. }
  104. else if (message instanceof EditorExitRequested)
  105. {
  106. EditorExitRequested eemsg = (EditorExitRequested) message;
  107. handleEditorExit(eemsg.getView());
  108. }
  109. else if (message instanceof PropertiesChanged)
  110. {
  111. handlePropertiesChanged();
  112. }
  113. else if (message instanceof SessionPropertiesShowing)
  114. {
  115. SessionPropertiesShowing smsg = (SessionPropertiesShowing) message;
  116. smsg.addPropertyPane(new DefaultSessionPropertyPane(smsg.getSession()));
  117. }
  118. }
  119. public static boolean isBufferListAvailable(View view)
  120. {
  121. // check if BufferList class is there
  122. Object bufferlist = jEdit.getPlugin("bufferlist.BufferListPlugin");
  123. if(bufferlist == null)
  124. return false;
  125. // check version, 0.8 is required
  126. String version = jEdit.getProperty("plugin.bufferlist.BufferListPlugin.version");
  127. if(version == null || version.length() == 0 || MiscUtilities.compareStrings(version, "0.8", true) < 0)
  128. return false;
  129. // check if docked
  130. if(getBufferList(view) == null)
  131. return false;
  132. return true;
  133. }
  134. private static JComponent getBufferList(View view)
  135. {
  136. DockableWindowManager mgr = view.getDockableWindowManager();
  137. return mgr.getDockable("bufferlist");
  138. }
  139. private void handleEditorExit(View view)
  140. {
  141. // remember the last open session:
  142. SessionManager mgr = SessionManager.getInstance();
  143. mgr.saveCurrentSessionProperty();
  144. // if autosave sessions is on, save current session silently:
  145. if (jEdit.getBooleanProperty("sessions.switcher.autoSave", true))
  146. {
  147. Log.log(Log.DEBUG, this, "autosaving current session...");
  148. mgr.saveCurrentSession(view, true);
  149. }
  150. }
  151. private void handlePropertiesChanged()
  152. {
  153. boolean show = jEdit.getBooleanProperty("sessions.switcher.showToolBar", false);
  154. View view = jEdit.getFirstView();
  155. while (view != null)
  156. {
  157. if (show)
  158. addSessionSwitcher(view);
  159. else
  160. removeSessionSwitcher(view);
  161. view = view.getNext();
  162. }
  163. SessionManager mgr = SessionManager.getInstance();
  164. if (jEdit.getBooleanProperty("sessions.switcher.showSessionNameInTitleBar", true) )
  165. {
  166. mgr.setSessionNameInTitleBar();
  167. }
  168. else
  169. {
  170. mgr.restoreTitleBarText();
  171. }
  172. mgr.refreshTitleBar();
  173. }
  174. private synchronized void addSessionSwitcher(final View view)
  175. {
  176. // remove old
  177. removeSessionSwitcher(view);
  178. // create new
  179. final SessionSwitcher switcher = new SessionSwitcher(view, true);
  180. viewSessionSwitchers.put(view, switcher);
  181. if(jEdit.getBooleanProperty("sessions.switcher.showJEditToolBar", false))
  182. {
  183. if(view.getToolBar() != null)
  184. {
  185. // Add to jEdit's default toolbar:
  186. // We need to add it later. Cannot add it right now, because if the View
  187. // receives the PropertiesChanged message, it removes and recreates
  188. // the toolbar. If the View receives the message after _we_ received it,
  189. // then our switcher would be gone.
  190. SwingUtilities.invokeLater(new Runnable()
  191. {
  192. public void run()
  193. {
  194. view.getToolBar().add(switcher);
  195. view.getRootPane().revalidate();
  196. }
  197. });
  198. }
  199. }
  200. else if(jEdit.getBooleanProperty("sessions.switcher.showInsideBufferList", false))
  201. {
  202. final JComponent bufferlist = getBufferList(view);
  203. if(bufferlist != null)
  204. {
  205. // Add to BufferList:
  206. // Again, we need to do this later, because the BufferList component
  207. // might not yet be there, especially when jEdit is starting or the
  208. // view is being created.
  209. SwingUtilities.invokeLater(new Runnable()
  210. {
  211. public void run()
  212. {
  213. bufferlist.add(BorderLayout.NORTH, switcher);
  214. bufferlist.revalidate();
  215. }
  216. });
  217. }
  218. }
  219. else
  220. {
  221. // Add as a _new_ toolbar to the View, below the main toolbar:
  222. view.addToolBar(switcher);
  223. }
  224. }
  225. private synchronized void removeSessionSwitcher(View view)
  226. {
  227. SessionSwitcher switcher = (SessionSwitcher) viewSessionSwitchers.get(view);
  228. if (switcher != null)
  229. {
  230. // Try to remove toolbar
  231. // (this does nothing if there is no switcher)
  232. view.removeToolBar(switcher);
  233. viewSessionSwitchers.remove(view);
  234. // Try to remove from jEdit's default toolbar
  235. // (this does nothing if there is no switcher)
  236. if(view.getToolBar() != null)
  237. {
  238. view.getToolBar().remove(switcher);
  239. view.getRootPane().revalidate();
  240. }
  241. // Try to remove from BufferList
  242. JComponent bufferlist = getBufferList(view);
  243. if(bufferlist != null)
  244. bufferlist.remove(switcher);
  245. }
  246. }
  247. private void showInfoMessage(String key)
  248. {
  249. if (!jEdit.getBooleanProperty(key + ".notAgain"))
  250. {
  251. String title = jEdit.getProperty(key + ".title");
  252. String msg = jEdit.getProperty(key + ".message");
  253. String msg2 = jEdit.getProperty("sessions.manager.info.dontShowAgain");
  254. JCheckBox notAgain = new JCheckBox(msg2, false);
  255. GUIUtilities.hideSplashScreen();
  256. JOptionPane.showMessageDialog(null, new Object[] { msg, notAgain },
  257. title, JOptionPane.INFORMATION_MESSAGE);
  258. jEdit.setBooleanProperty(key + ".notAgain", notAgain.isSelected());
  259. }
  260. }
  261. private Hashtable viewSessionSwitchers = new Hashtable();
  262. }