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

# · Java · 242 lines · 175 code · 34 blank · 33 comment · 26 complexity · 1edfb6e7640c1859906c21eb8790244b MD5 · raw file

  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 javax.swing.border.*;
  25. import javax.swing.event.*;
  26. import javax.swing.*;
  27. import java.awt.event.*;
  28. import java.awt.*;
  29. import org.gjt.sp.jedit.io.*;
  30. import org.gjt.sp.jedit.*;
  31. //}}}
  32. public class CloseDialog extends EnhancedDialog
  33. {
  34. //{{{ CloseDialog constructor
  35. public CloseDialog(View view)
  36. {
  37. super(view,jEdit.getProperty("close.title"),true);
  38. this.view = view;
  39. JPanel content = new JPanel(new BorderLayout(12,12));
  40. content.setBorder(new EmptyBorder(12,12,12,12));
  41. setContentPane(content);
  42. Box iconBox = new Box(BoxLayout.Y_AXIS);
  43. iconBox.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
  44. iconBox.add(Box.createGlue());
  45. content.add(BorderLayout.WEST,iconBox);
  46. JPanel centerPanel = new JPanel(new BorderLayout());
  47. JLabel label = new JLabel(jEdit.getProperty("close.caption"));
  48. label.setBorder(new EmptyBorder(0,0,6,0));
  49. centerPanel.add(BorderLayout.NORTH,label);
  50. bufferList = new JList(bufferModel = new DefaultListModel());
  51. bufferList.setVisibleRowCount(10);
  52. bufferList.addListSelectionListener(new ListHandler());
  53. Buffer[] buffers = jEdit.getBuffers();
  54. for(int i = 0; i < buffers.length; i++)
  55. {
  56. Buffer buffer = buffers[i];
  57. if(buffer.isDirty())
  58. {
  59. bufferModel.addElement(buffer.getPath());
  60. }
  61. }
  62. centerPanel.add(BorderLayout.CENTER,new JScrollPane(bufferList));
  63. content.add(BorderLayout.CENTER,centerPanel);
  64. ActionHandler actionListener = new ActionHandler();
  65. Box buttons = new Box(BoxLayout.X_AXIS);
  66. buttons.add(Box.createGlue());
  67. buttons.add(selectAll = new JButton(jEdit.getProperty("close.selectAll")));
  68. selectAll.setMnemonic(jEdit.getProperty("close.selectAll.mnemonic").charAt(0));
  69. selectAll.addActionListener(actionListener);
  70. buttons.add(Box.createHorizontalStrut(6));
  71. buttons.add(save = new JButton(jEdit.getProperty("close.save")));
  72. save.setMnemonic(jEdit.getProperty("close.save.mnemonic").charAt(0));
  73. save.addActionListener(actionListener);
  74. buttons.add(Box.createHorizontalStrut(6));
  75. buttons.add(discard = new JButton(jEdit.getProperty("close.discard")));
  76. discard.setMnemonic(jEdit.getProperty("close.discard.mnemonic").charAt(0));
  77. discard.addActionListener(actionListener);
  78. buttons.add(Box.createHorizontalStrut(6));
  79. buttons.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  80. cancel.addActionListener(actionListener);
  81. buttons.add(Box.createGlue());
  82. bufferList.setSelectedIndex(0);
  83. content.add(BorderLayout.SOUTH,buttons);
  84. GUIUtilities.requestFocus(this,bufferList);
  85. pack();
  86. setLocationRelativeTo(view);
  87. show();
  88. } //}}}
  89. //{{{ isOK() method
  90. public boolean isOK()
  91. {
  92. return ok;
  93. } //}}}
  94. //{{{ ok() method
  95. public void ok()
  96. {
  97. // do nothing
  98. } //}}}
  99. //{{{ cancel() method
  100. public void cancel()
  101. {
  102. dispose();
  103. } //}}}
  104. //{{{ Private members
  105. private View view;
  106. private JList bufferList;
  107. private DefaultListModel bufferModel;
  108. private JButton selectAll;
  109. private JButton save;
  110. private JButton discard;
  111. private JButton cancel;
  112. private boolean ok; // only set if all buffers saved/closed
  113. boolean selectAllFlag;
  114. private void updateButtons()
  115. {
  116. int index = bufferList.getSelectedIndex();
  117. save.getModel().setEnabled(index != -1);
  118. discard.getModel().setEnabled(index != -1);
  119. } //}}}
  120. //{{{ ActionHandler class
  121. class ActionHandler implements ActionListener
  122. {
  123. public void actionPerformed(ActionEvent evt)
  124. {
  125. Object source = evt.getSource();
  126. if(source == selectAll)
  127. {
  128. // I'm too tired to think of a better way
  129. // to handle this right now.
  130. try
  131. {
  132. selectAllFlag = true;
  133. bufferList.setSelectionInterval(0,
  134. bufferModel.getSize() - 1);
  135. }
  136. finally
  137. {
  138. selectAllFlag = false;
  139. }
  140. bufferList.requestFocus();
  141. }
  142. else if(source == save)
  143. {
  144. Object[] paths = bufferList.getSelectedValues();
  145. for(int i = 0; i < paths.length; i++)
  146. {
  147. String path = (String)paths[i];
  148. Buffer buffer = jEdit.getBuffer(path);
  149. if(!buffer.save(view,null,true))
  150. return;
  151. VFSManager.waitForRequests();
  152. if(buffer.getBooleanProperty(BufferIORequest
  153. .ERROR_OCCURRED))
  154. return;
  155. jEdit._closeBuffer(view,buffer);
  156. bufferModel.removeElement(path);
  157. }
  158. if(bufferModel.getSize() == 0)
  159. {
  160. ok = true;
  161. dispose();
  162. }
  163. else
  164. {
  165. bufferList.setSelectedIndex(0);
  166. bufferList.requestFocus();
  167. }
  168. }
  169. else if(source == discard)
  170. {
  171. Object[] paths = bufferList.getSelectedValues();
  172. for(int i = 0; i < paths.length; i++)
  173. {
  174. String path = (String)paths[i];
  175. Buffer buffer = jEdit.getBuffer(path);
  176. jEdit._closeBuffer(view,buffer);
  177. bufferModel.removeElement(path);
  178. }
  179. if(bufferModel.getSize() == 0)
  180. {
  181. ok = true;
  182. dispose();
  183. }
  184. else
  185. {
  186. bufferList.setSelectedIndex(0);
  187. bufferList.requestFocus();
  188. }
  189. }
  190. else if(source == cancel)
  191. cancel();
  192. }
  193. } //}}}
  194. //{{{ ListHandler class
  195. class ListHandler implements ListSelectionListener
  196. {
  197. public void valueChanged(ListSelectionEvent evt)
  198. {
  199. if(selectAllFlag)
  200. return;
  201. int index = bufferList.getSelectedIndex();
  202. if(index != -1)
  203. view.setBuffer(jEdit.getBuffer((String)
  204. bufferModel.getElementAt(index)));
  205. updateButtons();
  206. }
  207. } //}}}
  208. }