PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 320 lines | 210 code | 50 blank | 60 comment | 28 complexity | 7366addde7a50102bcb5909a1107883c 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. * 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. // try to show as many sessions as possible (maximum 25)
  68. int numSessions = lSessions.getModel().getSize();
  69. if (numSessions > 25)
  70. numSessions = 25;
  71. lSessions.setVisibleRowCount(numSessions);
  72. JScrollPane scrSessions = new JScrollPane(lSessions);
  73. bRename = new JButton(jEdit.getProperty("sessions.manager.rename"));
  74. bRename.addActionListener(this);
  75. bDelete = new JButton(jEdit.getProperty("sessions.manager.delete"));
  76. bDelete.addActionListener(this);
  77. bChangeTo = new JButton(jEdit.getProperty("sessions.manager.changeTo"));
  78. bChangeTo.setDefaultCapable(true);
  79. bChangeTo.addActionListener(this);
  80. bClose = new JButton(jEdit.getProperty("sessions.manager.close"));
  81. bClose.addActionListener(this);
  82. Insets inset10 = new Insets(10, 10, 10, 10);
  83. Insets insetButton = new Insets(10, 0, 0, 10);
  84. Insets insetLast = new Insets(10, 0, 10, 10);
  85. getContentPane().setLayout(new GridBagLayout());
  86. addComponent(scrSessions, 1, 3, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, inset10);
  87. addComponent(bRename, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  88. addComponent(bDelete, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  89. addComponent(bChangeTo, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
  90. addComponent(new JSeparator(), GridBagConstraints.REMAINDER, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  91. addComponent(new JPanel(), 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
  92. addComponent(bClose, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetLast);
  93. getRootPane().setDefaultButton(bChangeTo);
  94. pack();
  95. setLocationRelativeTo(view);
  96. setVisible(true);
  97. }
  98. /**
  99. * Invoked if ENTER or the Ok button are pressed.
  100. * Switches the session and closes the dialog.
  101. * You shouldn't show it again afterwards, because it is disposed.
  102. */
  103. public void ok()
  104. {
  105. if (lSessions.getSelectedValue() != null)
  106. selectedSession = lSessions.getSelectedValue().toString();
  107. setVisible(false);
  108. dispose();
  109. }
  110. /**
  111. * Invoked if ESC or the Cancel button are pressed.
  112. * Closes the dialog.
  113. * You shouldn't show it again afterwards, because it is disposed.
  114. */
  115. public void cancel()
  116. {
  117. setVisible(false);
  118. dispose();
  119. }
  120. /**
  121. * Return the selected session, or null if the dialog has been cancelled.
  122. */
  123. public String getSelectedSession()
  124. {
  125. return selectedSession;
  126. }
  127. /**
  128. * Return true if the list of sessions has been modified.
  129. * The list has been modified if at least one session has been renamed
  130. * or deleted.
  131. */
  132. public boolean isListModified()
  133. {
  134. return listModified;
  135. }
  136. /**
  137. * Invoked if one of the buttons is pressed.
  138. */
  139. public void actionPerformed(ActionEvent evt)
  140. {
  141. if (evt.getSource() == bClose)
  142. cancel();
  143. else if (evt.getSource() == bChangeTo)
  144. ok();
  145. else if (evt.getSource() == bRename)
  146. rename();
  147. else if (evt.getSource() == bDelete)
  148. delete();
  149. }
  150. /**
  151. * Invoked when the selected item in the sessions list changes.
  152. */
  153. public void valueChanged(ListSelectionEvent evt)
  154. {
  155. Object[] values = lSessions.getSelectedValues();
  156. if (values == null || values.length == 0)
  157. {
  158. // no selection
  159. bRename.setEnabled(false);
  160. bDelete.setEnabled(false);
  161. bChangeTo.setEnabled(false);
  162. }
  163. else
  164. {
  165. boolean isCurrentSession = values[0].toString().equals(currentSession);
  166. boolean isDefaultSession = values[0].toString().equalsIgnoreCase("default");
  167. bChangeTo.setEnabled(!isCurrentSession);
  168. bRename.setEnabled(!isDefaultSession);
  169. bDelete.setEnabled(!isDefaultSession);
  170. }
  171. }
  172. private void rename()
  173. {
  174. // TODO: Move this functionality to SessionManager
  175. String oldName = lSessions.getSelectedValue().toString();
  176. String newName = SessionManager.inputSessionName(this, oldName);
  177. if (newName == null)
  178. return;
  179. // Load the session to be re-named
  180. Session renameSession;
  181. if (oldName.equals(currentSession))
  182. {
  183. renameSession = SessionManager.getInstance().getCurrentSessionInstance();
  184. } else {
  185. renameSession = new Session(oldName);
  186. try {
  187. renameSession.loadXML();
  188. } catch (Exception e) {
  189. GUIUtilities.error(this, "sessions.manager.error.rename", new Object[] { oldName, newName });
  190. return;
  191. }
  192. }
  193. if (renameSession.rename(newName)) // rename succeeded
  194. {
  195. setNewListModel();
  196. lSessions.setSelectedValue(newName, true);
  197. if (oldName.equals(currentSession))
  198. currentSession = newName;
  199. }
  200. else // rename failed
  201. GUIUtilities.error(this, "sessions.manager.error.rename", new Object[] { oldName, newName });
  202. }
  203. private void delete()
  204. {
  205. // TODO: Move this functionality to SessionManager
  206. String name = lSessions.getSelectedValue().toString();
  207. File file = new File(SessionManager.createSessionFileName(name));
  208. if (file.delete())
  209. {
  210. if (name.equals(currentSession))
  211. currentSession = null; // mark the current session as deleted
  212. setNewListModel();
  213. lSessions.setSelectedValue("default", true);
  214. }
  215. else
  216. GUIUtilities.error(this, "sessions.manager.error.delete", new Object[] { file });
  217. }
  218. private void setNewListModel()
  219. {
  220. listModified = true;
  221. final String[] listData = SessionManager.getInstance().getSessionNames();
  222. lSessions.setModel(
  223. new AbstractListModel()
  224. {
  225. public int getSize() { return listData.length; }
  226. public Object getElementAt(int i) { return listData[i]; }
  227. }
  228. );
  229. }
  230. /**
  231. * Convenience method for adding components to the GridBagLayout of this dialog.
  232. */
  233. private void addComponent(Component comp,
  234. int gridwidth, int gridheight,
  235. double weightx, double weighty,
  236. int anchor, int fill,
  237. Insets insets)
  238. {
  239. GridBagConstraints constraints = new GridBagConstraints();
  240. constraints.gridwidth = gridwidth;
  241. constraints.gridheight = gridheight;
  242. constraints.weightx = weightx;
  243. constraints.weighty = weighty;
  244. constraints.anchor = anchor;
  245. constraints.fill = fill;
  246. constraints.insets = insets;
  247. GridBagLayout gridBag = (GridBagLayout) getContentPane().getLayout();
  248. gridBag.setConstraints(comp, constraints);
  249. getContentPane().add(comp);
  250. }
  251. /**
  252. * A <tt>MouseListener</tt> for handling double-clicks on the sessions list.
  253. */
  254. private class MouseHandler extends MouseAdapter
  255. {
  256. public void mousePressed(MouseEvent evt)
  257. {
  258. if (evt.getClickCount() == 2)
  259. ok();
  260. }
  261. }
  262. private JList lSessions;
  263. private JButton bRename;
  264. private JButton bDelete;
  265. private JButton bChangeTo;
  266. private JButton bClose;
  267. private String selectedSession;
  268. private String currentSession;
  269. private boolean listModified;
  270. }