/plugins/Sessions/tags/sessions_1_4_0/sessions/SessionSwitcher.java

# · Java · 261 lines · 164 code · 57 blank · 40 comment · 30 complexity · 1c543abbb40ffc14ec8f7485fe0f57d5 MD5 · raw file

  1. /*
  2. * SessionSwitcher.java - toolbar for switching between jEdit sessions
  3. * Copyright (c) 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.awt.Dimension;
  24. import java.awt.Insets;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ItemEvent;
  28. import java.awt.event.ItemListener;
  29. import java.util.Vector;
  30. import javax.swing.*;
  31. import org.gjt.sp.jedit.*;
  32. import org.gjt.sp.jedit.io.VFSManager;
  33. import org.gjt.sp.jedit.msg.PropertiesChanged;
  34. import org.gjt.sp.util.Log;
  35. /**
  36. * A control panel for switching between jEdit sessions.
  37. *
  38. * @author Dirk Moebius
  39. */
  40. public class SessionSwitcher
  41. extends JToolBar
  42. implements ActionListener, ItemListener, EBComponent
  43. {
  44. public SessionSwitcher(View view, boolean isInDefaultToolBar)
  45. {
  46. super();
  47. this.view = view;
  48. Insets nullInsets = new Insets(0,0,0,0);
  49. combo = new JComboBox(SessionManager.getInstance().getSessionNames());
  50. combo.setSelectedItem(SessionManager.getInstance().getCurrentSession());
  51. combo.setEditable(false);
  52. combo.addItemListener(this);
  53. save = new JButton(new ImageIcon(getClass().getResource("Save24.gif")));
  54. save.setMargin(nullInsets);
  55. save.setToolTipText(jEdit.getProperty("sessions.switcher.save.tooltip"));
  56. save.setFocusPainted(false);
  57. save.addActionListener(this);
  58. saveAs = new JButton(new ImageIcon(getClass().getResource("SaveAs24.gif")));
  59. saveAs.setMargin(nullInsets);
  60. saveAs.setToolTipText(jEdit.getProperty("sessions.switcher.saveAs.tooltip"));
  61. saveAs.setFocusPainted(false);
  62. saveAs.addActionListener(this);
  63. reload = new JButton(new ImageIcon(getClass().getResource("Redo24.gif")));
  64. reload.setMargin(nullInsets);
  65. reload.setToolTipText(jEdit.getProperty("sessions.switcher.reload.tooltip"));
  66. reload.setFocusPainted(false);
  67. reload.addActionListener(this);
  68. props = new JButton(new ImageIcon(getClass().getResource("History24.gif")));
  69. props.setMargin(nullInsets);
  70. props.setToolTipText(jEdit.getProperty("sessions.switcher.props.tooltip"));
  71. props.setFocusPainted(false);
  72. props.addActionListener(this);
  73. prefs = new JButton(new ImageIcon(getClass().getResource("Preferences24.gif")));
  74. prefs.setMargin(nullInsets);
  75. prefs.setToolTipText(jEdit.getProperty("sessions.switcher.prefs.tooltip"));
  76. prefs.setFocusPainted(false);
  77. prefs.addActionListener(this);
  78. setFloatable(false);
  79. putClientProperty("JToolBar.isRollover", Boolean.TRUE);
  80. addSeparator(new Dimension(5,5)); // just add a little space at begin, looks better
  81. add(combo);
  82. addSeparator();
  83. add(save);
  84. add(saveAs);
  85. add(reload);
  86. //add(props); // FIXME: not yet - EXPERIMENTAL
  87. add(prefs);
  88. // if we're not added to jEdit's default toolbar, then add some glue at
  89. // the end of the toolbar, so that we're left aligned and the combo box
  90. // doesn't get too long:
  91. if(!isInDefaultToolBar)
  92. add(Box.createGlue());
  93. updateTitle();
  94. }
  95. /** adds itself to EditBus on display */
  96. public void addNotify()
  97. {
  98. super.addNotify();
  99. EditBus.addToBus(this);
  100. }
  101. /** removes itself from EditBus on undisplay */
  102. public void removeNotify()
  103. {
  104. super.removeNotify();
  105. EditBus.removeFromBus(this);
  106. }
  107. // BEGIN EBComponent implementation
  108. public void handleMessage(EBMessage message)
  109. {
  110. if (message instanceof PropertiesChanged)
  111. updateTitle();
  112. else if (message instanceof SessionChanged)
  113. handleSessionChanged((SessionChanged)message);
  114. else if (message instanceof SessionListChanged)
  115. handleSessionListChanged((SessionListChanged)message);
  116. }
  117. // END EBComponent implementation
  118. // BEGIN ActionListener implementation
  119. public void actionPerformed(ActionEvent evt)
  120. {
  121. if (evt.getSource() == save)
  122. SessionManager.getInstance().saveCurrentSession(view);
  123. else if (evt.getSource() == saveAs)
  124. SessionManager.getInstance().saveCurrentSessionAs(view);
  125. else if (evt.getSource() == reload)
  126. SessionManager.getInstance().reloadCurrentSession(view);
  127. else if (evt.getSource() == props)
  128. SessionManager.getInstance().showSessionPropertiesDialog(view);
  129. else if (evt.getSource() == prefs)
  130. SessionManager.getInstance().showSessionManagerDialog(view);
  131. }
  132. // END ActionListener implementation
  133. // BEGIN ItemListener implementation
  134. public void itemStateChanged(ItemEvent e)
  135. {
  136. if(e.getStateChange() != ItemEvent.SELECTED || e.getItem() == null)
  137. return;
  138. String currentSession = SessionManager.getInstance().getCurrentSession();
  139. final String selectedSession = e.getItem().toString();
  140. if (selectedSession.equals(currentSession)) return;
  141. SessionManager.getInstance().setCurrentSession(view, selectedSession);
  142. // The session may not have been changed (eg. if the session change
  143. // was cancelled by the user while closing all open buffers).
  144. // Make sure the combo box is correct by calling ...
  145. updateSessionComboBox();
  146. }
  147. // END ItemListener implementation
  148. private void handleSessionChanged(SessionChanged msg) {
  149. updateSessionComboBox();
  150. }
  151. private void updateSessionComboBox()
  152. {
  153. final String newSession = SessionManager.getInstance().getCurrentSession();
  154. SwingUtilities.invokeLater(new Runnable()
  155. {
  156. public void run()
  157. {
  158. combo.setSelectedItem(newSession);
  159. }
  160. });
  161. }
  162. private void handleSessionListChanged(SessionListChanged msg)
  163. {
  164. final String[] sessions = msg.getSessionManager().getSessionNames();
  165. SwingUtilities.invokeLater(new Runnable()
  166. {
  167. public void run()
  168. {
  169. combo.setModel(new DefaultComboBoxModel(sessions));
  170. }
  171. });
  172. }
  173. private void updateTitle()
  174. {
  175. if (jEdit.getBooleanProperty("sessions.switcher.showTitle", true))
  176. addTitle();
  177. else
  178. removeTitle();
  179. }
  180. private void addTitle()
  181. {
  182. if (title != null)
  183. return; // already added
  184. title = new JLabel(jEdit.getProperty("sessions.switcher.title"));
  185. add(title, 0);
  186. revalidate();
  187. }
  188. private void removeTitle()
  189. {
  190. if(title == null)
  191. return; // already removed
  192. remove(title);
  193. revalidate();
  194. title = null;
  195. }
  196. private View view;
  197. private JComboBox combo;
  198. private JButton save;
  199. private JButton saveAs;
  200. private JButton reload;
  201. private JButton props;
  202. private JButton prefs;
  203. private JLabel title;
  204. }