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

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/CloseDialog.java

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