PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/CloseDialog.java

#
Java | 261 lines | 193 code | 31 blank | 37 comment | 28 complexity | 8ce16f0b524fea6c780220a9aa74d9a7 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. * CloseDialog.java - Close all buffers dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import java.util.Collection;
  25. import java.util.Arrays;
  26. import javax.swing.border.*;
  27. import javax.swing.event.*;
  28. import javax.swing.*;
  29. import java.awt.event.*;
  30. import java.awt.*;
  31. import org.gjt.sp.jedit.bufferio.BufferIORequest;
  32. import org.gjt.sp.jedit.io.*;
  33. import org.gjt.sp.jedit.*;
  34. import org.gjt.sp.util.Log;
  35. //}}}
  36. /**
  37. * @author Slava Pestov
  38. */
  39. public class CloseDialog extends EnhancedDialog
  40. {
  41. //{{{ CloseDialog constructor
  42. public CloseDialog(View view)
  43. {
  44. this(view, Arrays.asList(jEdit.getBuffers()));
  45. }
  46. public CloseDialog(View view, Collection<Buffer> buffers)
  47. {
  48. super(view,jEdit.getProperty("close.title"),true);
  49. this.view = view;
  50. JPanel content = new JPanel(new BorderLayout(12,12));
  51. content.setBorder(new EmptyBorder(12,12,12,12));
  52. setContentPane(content);
  53. Box iconBox = new Box(BoxLayout.Y_AXIS);
  54. iconBox.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
  55. iconBox.add(Box.createGlue());
  56. content.add(BorderLayout.WEST,iconBox);
  57. JPanel centerPanel = new JPanel(new BorderLayout());
  58. JLabel label = new JLabel(jEdit.getProperty("close.caption"));
  59. label.setBorder(new EmptyBorder(0,0,6,0));
  60. centerPanel.add(BorderLayout.NORTH,label);
  61. bufferList = new JList(bufferModel = new DefaultListModel());
  62. bufferList.setVisibleRowCount(10);
  63. bufferList.addListSelectionListener(new ListHandler());
  64. for(Buffer buffer: buffers)
  65. {
  66. if(buffer.isDirty())
  67. bufferModel.addElement(buffer.getPath());
  68. }
  69. centerPanel.add(BorderLayout.CENTER,new JScrollPane(bufferList));
  70. content.add(BorderLayout.CENTER,centerPanel);
  71. ActionHandler actionListener = new ActionHandler();
  72. Box buttons = new Box(BoxLayout.X_AXIS);
  73. buttons.add(Box.createGlue());
  74. buttons.add(selectAll = new JButton(jEdit.getProperty("close.selectAll")));
  75. selectAll.setMnemonic(jEdit.getProperty("close.selectAll.mnemonic").charAt(0));
  76. selectAll.addActionListener(actionListener);
  77. buttons.add(Box.createHorizontalStrut(6));
  78. buttons.add(save = new JButton(jEdit.getProperty("close.save")));
  79. save.setMnemonic(jEdit.getProperty("close.save.mnemonic").charAt(0));
  80. save.addActionListener(actionListener);
  81. buttons.add(Box.createHorizontalStrut(6));
  82. buttons.add(discard = new JButton(jEdit.getProperty("close.discard")));
  83. discard.setMnemonic(jEdit.getProperty("close.discard.mnemonic").charAt(0));
  84. discard.addActionListener(actionListener);
  85. buttons.add(Box.createHorizontalStrut(6));
  86. buttons.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  87. cancel.addActionListener(actionListener);
  88. buttons.add(Box.createGlue());
  89. bufferList.setSelectedIndex(0);
  90. content.add(BorderLayout.SOUTH,buttons);
  91. content.getRootPane().setDefaultButton(cancel);
  92. GUIUtilities.requestFocus(this,bufferList);
  93. pack();
  94. setLocationRelativeTo(view);
  95. setVisible(true);
  96. } //}}}
  97. //{{{ isOK() method
  98. public boolean isOK()
  99. {
  100. return ok;
  101. } //}}}
  102. //{{{ ok() method
  103. @Override
  104. public void ok()
  105. {
  106. // do nothing
  107. } //}}}
  108. //{{{ cancel() method
  109. @Override
  110. public void cancel()
  111. {
  112. dispose();
  113. } //}}}
  114. //{{{ Private members
  115. private final View view;
  116. private final JList bufferList;
  117. private final DefaultListModel bufferModel;
  118. private final JButton selectAll;
  119. private final JButton save;
  120. private final JButton discard;
  121. private final JButton cancel;
  122. private boolean ok; // only set if all buffers saved/closed
  123. boolean selectAllFlag;
  124. private void updateButtons()
  125. {
  126. int index = bufferList.getSelectedIndex();
  127. save.getModel().setEnabled(index != -1);
  128. discard.getModel().setEnabled(index != -1);
  129. } //}}}
  130. //{{{ ActionHandler class
  131. private class ActionHandler implements ActionListener
  132. {
  133. public void actionPerformed(ActionEvent evt)
  134. {
  135. Object source = evt.getSource();
  136. if(source == selectAll)
  137. {
  138. // I'm too tired to think of a better way
  139. // to handle this right now.
  140. try
  141. {
  142. selectAllFlag = true;
  143. bufferList.setSelectionInterval(0,
  144. bufferModel.getSize() - 1);
  145. }
  146. finally
  147. {
  148. selectAllFlag = false;
  149. }
  150. bufferList.requestFocus();
  151. }
  152. else if(source == save)
  153. {
  154. Object[] paths = bufferList.getSelectedValues();
  155. for(int i = 0; i < paths.length; i++)
  156. {
  157. String path = (String)paths[i];
  158. Buffer buffer = jEdit.getBuffer(path);
  159. if(!buffer.save(view,null,true,true))
  160. return;
  161. VFSManager.waitForRequests();
  162. if(buffer.getBooleanProperty(BufferIORequest
  163. .ERROR_OCCURRED))
  164. return;
  165. jEdit._closeBuffer(view,buffer);
  166. bufferModel.removeElement(path);
  167. }
  168. if(bufferModel.getSize() == 0)
  169. {
  170. ok = true;
  171. dispose();
  172. }
  173. else
  174. {
  175. bufferList.setSelectedIndex(0);
  176. bufferList.requestFocus();
  177. }
  178. }
  179. else if(source == discard)
  180. {
  181. Object[] paths = bufferList.getSelectedValues();
  182. for(int i = 0; i < paths.length; i++)
  183. {
  184. String path = (String)paths[i];
  185. Buffer buffer = jEdit.getBuffer(path);
  186. jEdit._closeBuffer(view,buffer);
  187. bufferModel.removeElement(path);
  188. }
  189. if(bufferModel.getSize() == 0)
  190. {
  191. ok = true;
  192. dispose();
  193. }
  194. else
  195. {
  196. bufferList.setSelectedIndex(0);
  197. bufferList.requestFocus();
  198. }
  199. }
  200. else if(source == cancel)
  201. cancel();
  202. }
  203. } //}}}
  204. //{{{ ListHandler class
  205. private class ListHandler implements ListSelectionListener
  206. {
  207. public void valueChanged(ListSelectionEvent evt)
  208. {
  209. if(selectAllFlag)
  210. return;
  211. int index = bufferList.getSelectedIndex();
  212. if(index != -1)
  213. {
  214. String path = (String) bufferModel.getElementAt(index);
  215. Buffer buffer = jEdit.getBuffer(path);
  216. if (buffer == null)
  217. {
  218. // it seems this buffer was already closed
  219. Log.log(Log.DEBUG, this, "Buffer " + path + " is already closed");
  220. bufferModel.removeElementAt(index);
  221. }
  222. else
  223. {
  224. view.showBuffer(buffer);
  225. }
  226. }
  227. updateButtons();
  228. }
  229. } //}}}
  230. }