PageRenderTime 84ms CodeModel.GetById 30ms app.highlight 19ms RepoModel.GetById 31ms app.codeStats 0ms

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

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