001: /*
002: * CloseDialog.java - Close all buffers dialog
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2000 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.border.*;
027: import javax.swing.event.*;
028: import javax.swing.*;
029: import java.awt.event.*;
030: import java.awt.*;
031: import org.gjt.sp.jedit.bufferio.BufferIORequest;
032: import org.gjt.sp.jedit.io.*;
033: import org.gjt.sp.jedit.*;
034:
035: //}}}
036:
037: public class CloseDialog extends EnhancedDialog {
038: //{{{ CloseDialog constructor
039: public CloseDialog(View view) {
040: super (view, jEdit.getProperty("close.title"), true);
041:
042: this .view = view;
043:
044: JPanel content = new JPanel(new BorderLayout(12, 12));
045: content.setBorder(new EmptyBorder(12, 12, 12, 12));
046: setContentPane(content);
047:
048: Box iconBox = new Box(BoxLayout.Y_AXIS);
049: iconBox.add(new JLabel(UIManager
050: .getIcon("OptionPane.warningIcon")));
051: iconBox.add(Box.createGlue());
052: content.add(BorderLayout.WEST, iconBox);
053:
054: JPanel centerPanel = new JPanel(new BorderLayout());
055:
056: JLabel label = new JLabel(jEdit.getProperty("close.caption"));
057: label.setBorder(new EmptyBorder(0, 0, 6, 0));
058: centerPanel.add(BorderLayout.NORTH, label);
059:
060: bufferList = new JList(bufferModel = new DefaultListModel());
061: bufferList.setVisibleRowCount(10);
062: bufferList.addListSelectionListener(new ListHandler());
063:
064: Buffer[] buffers = jEdit.getBuffers();
065: for (int i = 0; i < buffers.length; i++) {
066: Buffer buffer = buffers[i];
067: if (buffer.isDirty()) {
068: bufferModel.addElement(buffer.getPath());
069: }
070: }
071:
072: centerPanel.add(BorderLayout.CENTER,
073: new JScrollPane(bufferList));
074:
075: content.add(BorderLayout.CENTER, centerPanel);
076:
077: ActionHandler actionListener = new ActionHandler();
078:
079: Box buttons = new Box(BoxLayout.X_AXIS);
080: buttons.add(Box.createGlue());
081: buttons.add(selectAll = new JButton(jEdit
082: .getProperty("close.selectAll")));
083: selectAll.setMnemonic(jEdit.getProperty(
084: "close.selectAll.mnemonic").charAt(0));
085: selectAll.addActionListener(actionListener);
086: buttons.add(Box.createHorizontalStrut(6));
087: buttons
088: .add(save = new JButton(jEdit.getProperty("close.save")));
089: save.setMnemonic(jEdit.getProperty("close.save.mnemonic")
090: .charAt(0));
091: save.addActionListener(actionListener);
092: buttons.add(Box.createHorizontalStrut(6));
093: buttons.add(discard = new JButton(jEdit
094: .getProperty("close.discard")));
095: discard.setMnemonic(jEdit.getProperty("close.discard.mnemonic")
096: .charAt(0));
097: discard.addActionListener(actionListener);
098: buttons.add(Box.createHorizontalStrut(6));
099: buttons.add(cancel = new JButton(jEdit
100: .getProperty("common.cancel")));
101: cancel.addActionListener(actionListener);
102: buttons.add(Box.createGlue());
103:
104: bufferList.setSelectedIndex(0);
105:
106: content.add(BorderLayout.SOUTH, buttons);
107:
108: GUIUtilities.requestFocus(this , bufferList);
109:
110: pack();
111: setLocationRelativeTo(view);
112: setVisible(true);
113: } //}}}
114:
115: //{{{ isOK() method
116: public boolean isOK() {
117: return ok;
118: } //}}}
119:
120: //{{{ ok() method
121: public void ok() {
122: // do nothing
123: } //}}}
124:
125: //{{{ cancel() method
126: public void cancel() {
127: dispose();
128: } //}}}
129:
130: //{{{ Private members
131: private View view;
132: private JList bufferList;
133: private DefaultListModel bufferModel;
134: private JButton selectAll;
135: private JButton save;
136: private JButton discard;
137: private JButton cancel;
138:
139: private boolean ok; // only set if all buffers saved/closed
140:
141: boolean selectAllFlag;
142:
143: private void updateButtons() {
144: int index = bufferList.getSelectedIndex();
145: save.getModel().setEnabled(index != -1);
146: discard.getModel().setEnabled(index != -1);
147: } //}}}
148:
149: //{{{ ActionHandler class
150: class ActionHandler implements ActionListener {
151: public void actionPerformed(ActionEvent evt) {
152: Object source = evt.getSource();
153: if (source == selectAll) {
154: // I'm too tired to think of a better way
155: // to handle this right now.
156: try {
157: selectAllFlag = true;
158:
159: bufferList.setSelectionInterval(0, bufferModel
160: .getSize() - 1);
161: } finally {
162: selectAllFlag = false;
163: }
164: bufferList.requestFocus();
165: } else if (source == save) {
166: Object[] paths = bufferList.getSelectedValues();
167:
168: for (int i = 0; i < paths.length; i++) {
169: String path = (String) paths[i];
170: Buffer buffer = jEdit.getBuffer(path);
171: if (!buffer.save(view, null, true, true))
172: return;
173: VFSManager.waitForRequests();
174: if (buffer
175: .getBooleanProperty(BufferIORequest.ERROR_OCCURRED))
176: return;
177: jEdit._closeBuffer(view, buffer);
178: bufferModel.removeElement(path);
179: }
180:
181: if (bufferModel.getSize() == 0) {
182: ok = true;
183: dispose();
184: } else {
185: bufferList.setSelectedIndex(0);
186: bufferList.requestFocus();
187: }
188: } else if (source == discard) {
189: Object[] paths = bufferList.getSelectedValues();
190:
191: for (int i = 0; i < paths.length; i++) {
192: String path = (String) paths[i];
193: Buffer buffer = jEdit.getBuffer(path);
194: jEdit._closeBuffer(view, buffer);
195: bufferModel.removeElement(path);
196: }
197:
198: if (bufferModel.getSize() == 0) {
199: ok = true;
200: dispose();
201: } else {
202: bufferList.setSelectedIndex(0);
203: bufferList.requestFocus();
204: }
205: } else if (source == cancel)
206: cancel();
207: }
208: } //}}}
209:
210: //{{{ ListHandler class
211: class ListHandler implements ListSelectionListener {
212: public void valueChanged(ListSelectionEvent evt) {
213: if (selectAllFlag)
214: return;
215:
216: int index = bufferList.getSelectedIndex();
217: if (index != -1)
218: view.goToBuffer(jEdit.getBuffer((String) bufferModel
219: .getElementAt(index)));
220:
221: updateButtons();
222: }
223: } //}}}
224: }
|