/plugins/Sessions/branches/branch_sessions_1_0/sessions/SessionManagerDialog.java

# · Java · 300 lines · 196 code · 48 blank · 56 comment · 25 complexity · 0b4fb4906f435811963b83fee8915637 MD5 · raw file

  1. /*
  2. * SessionManagerDialog.java - a dialog for managing 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.Component;
  23. import java.awt.Dimension;
  24. import java.awt.GridBagLayout;
  25. import java.awt.GridBagConstraints;
  26. import java.awt.Insets;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.event.MouseAdapter;
  30. import java.awt.event.MouseEvent;
  31. import java.io.File;
  32. import java.io.FilenameFilter;
  33. import java.util.Vector;
  34. import javax.swing.*;
  35. import javax.swing.event.ListSelectionEvent;
  36. import javax.swing.event.ListSelectionListener;
  37. import org.gjt.sp.jedit.jEdit;
  38. import org.gjt.sp.jedit.EditBus;
  39. import org.gjt.sp.jedit.GUIUtilities;
  40. import org.gjt.sp.jedit.View;
  41. import org.gjt.sp.jedit.gui.EnhancedDialog;
  42. import org.gjt.sp.util.Log;
  43. /**
  44. * A modal dialog for managing jEdit sessions and session files.
  45. *
  46. * @author Dirk Moebius
  47. */
  48. class SessionManagerDialog
  49. extends EnhancedDialog
  50. implements ActionListener, ListSelectionListener
  51. {
  52. public SessionManagerDialog(View view, final String currentSession)
  53. {
  54. super(view, jEdit.getProperty("sessions.manager.title"), true);
  55. this.currentSession = currentSession;
  56. lSessions = new JList(SessionManager.getInstance().getSessionNames());
  57. lSessions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  58. lSessions.addListSelectionListener(this);
  59. lSessions.addMouseListener(new MouseHandler()); // for double-clicks
  60. SwingUtilities.invokeLater(new Runnable()
  61. {
  62. public void run()
  63. {
  64. lSessions.setSelectedValue(currentSession, true);
  65. }
  66. });
  67. JScrollPane scrSessions = new JScrollPane(lSessions);
  68. scrSessions.setPreferredSize(new Dimension(200, 100));
  69. bRename = new JButton(jEdit.getProperty("sessions.manager.rename"));
  70. bRename.addActionListener(this);
  71. bDelete = new JButton(jEdit.getProperty("sessions.manager.delete"));
  72. bDelete.addActionListener(this);
  73. bChangeTo = new JButton(jEdit.getProperty("sessions.manager.changeTo"));
  74. bChangeTo.setDefaultCapable(true);
  75. bChangeTo.addActionListener(this);
  76. bClose = new JButton(jEdit.getProperty("sessions.manager.close"));
  77. bClose.addActionListener(this);
  78. Insets inset10 = new Insets(10, 10, 10, 10);
  79. Insets insetButton = new Insets(10, 0, 0, 10);
  80. Insets insetLast = new Insets(10, 0, 10, 10);
  81. getContentPane().setLayout(new GridBagLayout());
  82. addComponent(scrSessions, 1, 3, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, inset10);
  83. addComponent(bRename, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  84. addComponent(bDelete, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  85. addComponent(bChangeTo, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  86. addComponent(new JSeparator(), GridBagConstraints.REMAINDER, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  87. addComponent(new JPanel(), 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  88. addComponent(bClose, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetLast);
  89. getRootPane().setDefaultButton(bChangeTo);
  90. pack();
  91. setLocationRelativeTo(view);
  92. setVisible(true);
  93. }
  94. /**
  95. * Invoked if ENTER or the Ok button are pressed.
  96. * Switches the session and closes the dialog.
  97. * You shouldn't show it again afterwards, because it is disposed.
  98. */
  99. public void ok()
  100. {
  101. if (lSessions.getSelectedValue() != null)
  102. selectedSession = lSessions.getSelectedValue().toString();
  103. setVisible(false);
  104. dispose();
  105. }
  106. /**
  107. * Invoked if ESC or the Cancel button are pressed.
  108. * Closes the dialog.
  109. * You shouldn't show it again afterwards, because it is disposed.
  110. */
  111. public void cancel()
  112. {
  113. setVisible(false);
  114. dispose();
  115. }
  116. /**
  117. * Return the selected session, or null if the dialog has been cancelled.
  118. */
  119. public String getSelectedSession()
  120. {
  121. return selectedSession;
  122. }
  123. /**
  124. * Return true if the list of sessions has been modified.
  125. * The list has been modified if at least one session has been renamed
  126. * or deleted.
  127. */
  128. public boolean isListModified()
  129. {
  130. return listModified;
  131. }
  132. /**
  133. * Invoked if one of the buttons is pressed.
  134. */
  135. public void actionPerformed(ActionEvent evt)
  136. {
  137. if (evt.getSource() == bClose)
  138. cancel();
  139. else if (evt.getSource() == bChangeTo)
  140. ok();
  141. else if (evt.getSource() == bRename)
  142. rename();
  143. else if (evt.getSource() == bDelete)
  144. delete();
  145. }
  146. /**
  147. * Invoked when the selected item in the sessions list changes.
  148. */
  149. public void valueChanged(ListSelectionEvent evt)
  150. {
  151. Object[] values = lSessions.getSelectedValues();
  152. if (values == null || values.length == 0)
  153. {
  154. // no selection
  155. bRename.setEnabled(false);
  156. bDelete.setEnabled(false);
  157. bChangeTo.setEnabled(false);
  158. }
  159. else
  160. {
  161. boolean isCurrentSession = values[0].toString().equals(currentSession);
  162. boolean isDefaultSession = values[0].toString().equalsIgnoreCase("default");
  163. bChangeTo.setEnabled(!isCurrentSession);
  164. bRename.setEnabled(!isDefaultSession);
  165. bDelete.setEnabled(!isDefaultSession);
  166. }
  167. }
  168. private void rename()
  169. {
  170. String oldName = lSessions.getSelectedValue().toString();
  171. String newName = SessionManager.inputSessionName(this, oldName);
  172. if (newName == null)
  173. return;
  174. File oldFile = new File(SessionManager.createSessionFileName(oldName));
  175. File newFile = new File(SessionManager.createSessionFileName(newName));
  176. if (oldFile.renameTo(newFile))
  177. {
  178. setNewListModel();
  179. lSessions.setSelectedValue(newName, true);
  180. if (oldName.equals(currentSession))
  181. currentSession = newName;
  182. }
  183. else
  184. GUIUtilities.error(this, "sessions.manager.error.rename", new Object[] { oldFile, newFile });
  185. }
  186. private void delete()
  187. {
  188. String name = lSessions.getSelectedValue().toString();
  189. File file = new File(SessionManager.createSessionFileName(name));
  190. if (file.delete())
  191. {
  192. if (name.equals(currentSession))
  193. currentSession = null; // mark the current session as deleted
  194. setNewListModel();
  195. lSessions.setSelectedValue("default", true);
  196. }
  197. else
  198. GUIUtilities.error(this, "sessions.manager.error.delete", new Object[] { file });
  199. }
  200. private void setNewListModel()
  201. {
  202. listModified = true;
  203. final String[] listData = SessionManager.getInstance().getSessionNames();
  204. lSessions.setModel(
  205. new AbstractListModel()
  206. {
  207. public int getSize() { return listData.length; }
  208. public Object getElementAt(int i) { return listData[i]; }
  209. }
  210. );
  211. }
  212. /**
  213. * Convenience method for adding components to the GridBagLayout of this dialog.
  214. */
  215. private void addComponent(Component comp,
  216. int gridwidth, int gridheight,
  217. double weightx, double weighty,
  218. int anchor, int fill,
  219. Insets insets)
  220. {
  221. GridBagConstraints constraints = new GridBagConstraints();
  222. constraints.gridwidth = gridwidth;
  223. constraints.gridheight = gridheight;
  224. constraints.weightx = weightx;
  225. constraints.weighty = weighty;
  226. constraints.anchor = anchor;
  227. constraints.fill = fill;
  228. constraints.insets = insets;
  229. GridBagLayout gridBag = (GridBagLayout) getContentPane().getLayout();
  230. gridBag.setConstraints(comp, constraints);
  231. getContentPane().add(comp);
  232. }
  233. /**
  234. * A <tt>MouseListener</tt> for handling double-clicks on the sessions list.
  235. */
  236. private class MouseHandler extends MouseAdapter
  237. {
  238. public void mousePressed(MouseEvent evt)
  239. {
  240. if (evt.getClickCount() == 2)
  241. ok();
  242. }
  243. }
  244. private JList lSessions;
  245. private JButton bRename;
  246. private JButton bDelete;
  247. private JButton bChangeTo;
  248. private JButton bClose;
  249. private String selectedSession;
  250. private String currentSession;
  251. private boolean listModified;
  252. }