/plugins/BufferList/tags/bufferlist_0_5_1/SessionManagerDialog.java

# · Java · 243 lines · 161 code · 38 blank · 44 comment · 30 complexity · 28753c6236dd67f682f9afa722a8f980 MD5 · raw file

  1. /*
  2. * SessionManagerDialog.java - a dialog for managing sessions
  3. * Copyright (c) 2001 Dirk Moebius
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. import java.awt.Component;
  20. import java.awt.Dimension;
  21. import java.awt.GridBagLayout;
  22. import java.awt.GridBagConstraints;
  23. import java.awt.Insets;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import java.io.File;
  29. import java.io.FilenameFilter;
  30. import java.util.Vector;
  31. import javax.swing.*;
  32. import javax.swing.event.ListSelectionEvent;
  33. import javax.swing.event.ListSelectionListener;
  34. import org.gjt.sp.jedit.jEdit;
  35. import org.gjt.sp.jedit.GUIUtilities;
  36. import org.gjt.sp.jedit.View;
  37. import org.gjt.sp.jedit.gui.EnhancedDialog;
  38. import org.gjt.sp.util.Log;
  39. /**
  40. * A modal dialog for managing jEdit sessions and session files.
  41. *
  42. * @author Dirk Moebius
  43. */
  44. public class SessionManagerDialog extends EnhancedDialog implements ActionListener, ListSelectionListener {
  45. public SessionManagerDialog(View view, String currentSession) {
  46. super(view, jEdit.getProperty("bufferlist.manager.title"), true);
  47. this.currentSession = currentSession;
  48. lSessions = new JList(SessionManager.getSessionNames());
  49. lSessions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  50. lSessions.addListSelectionListener(this);
  51. lSessions.addMouseListener(new MouseHandler()); // for double-clicks
  52. JScrollPane scrSessions = new JScrollPane(lSessions);
  53. scrSessions.setPreferredSize(new Dimension(200, 100));
  54. bRename = new JButton(jEdit.getProperty("bufferlist.manager.rename"));
  55. bRename.addActionListener(this);
  56. bDelete = new JButton(jEdit.getProperty("bufferlist.manager.delete"));
  57. bDelete.addActionListener(this);
  58. bChangeTo = new JButton(jEdit.getProperty("bufferlist.manager.changeTo"));
  59. bChangeTo.setDefaultCapable(true);
  60. bChangeTo.addActionListener(this);
  61. bClose = new JButton(jEdit.getProperty("bufferlist.manager.close"));
  62. bClose.addActionListener(this);
  63. Insets inset10 = new Insets(10, 10, 10, 10);
  64. Insets insetButton = new Insets(10, 0, 0, 10);
  65. Insets insetLast = new Insets(10, 0, 10, 10);
  66. getContentPane().setLayout(new GridBagLayout());
  67. addComponent(scrSessions, 1, 3, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, inset10);
  68. addComponent(bRename, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  69. addComponent(bDelete, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  70. addComponent(bChangeTo, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  71. addComponent(new JSeparator(), GridBagConstraints.REMAINDER, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  72. addComponent(new JPanel(), 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  73. addComponent(bClose, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetLast);
  74. getRootPane().setDefaultButton(bChangeTo);
  75. lSessions.setSelectedValue(currentSession, true);
  76. pack();
  77. setLocationRelativeTo(view);
  78. setVisible(true);
  79. }
  80. /**
  81. * Invoked when ENTER or the Ok button is pressed.
  82. * Switches the session and closes the dialog. Don't show it again afterwards.
  83. */
  84. public void ok() {
  85. if (lSessions.getSelectedValue() != null)
  86. selectedSession = lSessions.getSelectedValue().toString();
  87. setVisible(false);
  88. dispose();
  89. }
  90. /**
  91. * Invoked when ESC or the Cancel button is pressed.
  92. * Closes the dialog. Don't show it again afterwards.
  93. */
  94. public void cancel() {
  95. setVisible(false);
  96. dispose();
  97. }
  98. public String getSelectedSession() {
  99. return selectedSession;
  100. }
  101. /**
  102. * Invoked when any of the buttons is pressed.
  103. */
  104. public void actionPerformed(ActionEvent evt) {
  105. if (evt.getSource() == bClose) {
  106. cancel();
  107. }
  108. else if (evt.getSource() == bChangeTo) {
  109. ok();
  110. }
  111. else if (evt.getSource() == bRename) {
  112. String oldName = lSessions.getSelectedValue().toString();
  113. String newName = SessionManager.inputSessionName(this, oldName);
  114. if (newName != null) {
  115. File oldFile = new File(SessionManager.createSessionFileName(oldName));
  116. File newFile = new File(SessionManager.createSessionFileName(newName));
  117. boolean ok = oldFile.renameTo(newFile);
  118. setNewListModel();
  119. if (ok) {
  120. lSessions.setSelectedValue(newName, true);
  121. if (oldName.equals(currentSession))
  122. currentSession = newName;
  123. } else {
  124. GUIUtilities.error(this, "bufferlist.manager.error.rename", new Object[] { oldFile, newFile });
  125. }
  126. }
  127. }
  128. else if (evt.getSource() == bDelete) {
  129. String name = lSessions.getSelectedValue().toString();
  130. File file = new File(SessionManager.createSessionFileName(name));
  131. boolean ok = file.delete();
  132. setNewListModel();
  133. if (ok) {
  134. lSessions.setSelectedValue("default", true);
  135. if (name.equals(currentSession))
  136. currentSession = null;
  137. } else {
  138. GUIUtilities.error(this, "bufferlist.manager.error.delete", new Object[] { file });
  139. }
  140. }
  141. }
  142. /**
  143. * Invoked when the selected item in the lSessions list changes.
  144. */
  145. public void valueChanged(ListSelectionEvent evt) {
  146. Object[] values = lSessions.getSelectedValues();
  147. if (values == null || values.length == 0) {
  148. // no selection
  149. bRename.setEnabled(false);
  150. bDelete.setEnabled(false);
  151. bChangeTo.setEnabled(false);
  152. } else {
  153. boolean isCurrentSession = values[0].toString().equals(currentSession);
  154. boolean isDefaultSession = values[0].toString().equalsIgnoreCase("default");
  155. bChangeTo.setEnabled(!isCurrentSession);
  156. bRename.setEnabled(!isDefaultSession);
  157. bDelete.setEnabled(!isDefaultSession);
  158. }
  159. }
  160. private void setNewListModel() {
  161. final Vector listData = SessionManager.getSessionNames();
  162. lSessions.setModel(new AbstractListModel() {
  163. public int getSize() { return listData.size(); }
  164. public Object getElementAt(int i) { return listData.elementAt(i); }
  165. });
  166. }
  167. /**
  168. * Convenience method for adding components to the GridBagLayout of this dialog.
  169. */
  170. private void addComponent(Component comp,
  171. int gridwidth, int gridheight,
  172. double weightx, double weighty,
  173. int anchor, int fill,
  174. Insets insets)
  175. {
  176. GridBagConstraints constraints = new GridBagConstraints();
  177. constraints.gridwidth = gridwidth;
  178. constraints.gridheight = gridheight;
  179. constraints.weightx = weightx;
  180. constraints.weighty = weighty;
  181. constraints.anchor = anchor;
  182. constraints.fill = fill;
  183. constraints.insets = insets;
  184. GridBagLayout gridBag = (GridBagLayout) getContentPane().getLayout();
  185. gridBag.setConstraints(comp, constraints);
  186. getContentPane().add(comp);
  187. }
  188. /**
  189. * A <tt>MouseListener</tt> for handling double-clicks on the sessions list.
  190. */
  191. private class MouseHandler extends MouseAdapter {
  192. public void mousePressed(MouseEvent evt) {
  193. if (evt.getSource() == lSessions && evt.getClickCount() == 2) {
  194. ok();
  195. }
  196. }
  197. }
  198. JList lSessions; // cannot be private due to compiler bug
  199. private JButton bRename;
  200. private JButton bDelete;
  201. private JButton bChangeTo;
  202. private JButton bClose;
  203. private String selectedSession;
  204. private String currentSession;
  205. }