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

/bundles/plugins-trunk/Sessions/sessions/SessionSwitcher.java

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