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