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

# · Java · 305 lines · 201 code · 44 blank · 60 comment · 49 complexity · 097ed17479fb9e311ce039f8f692b878 MD5 · raw file

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