001: /*
002: * IOProgressMonitor.java - I/O progress monitor
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2002 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.*;
028: import java.awt.event.*;
029: import java.awt.*;
030: import org.gjt.sp.jedit.io.VFSManager;
031: import org.gjt.sp.jedit.*;
032: import org.gjt.sp.util.*;
033:
034: //}}}
035:
036: /**
037: * The IO progressMonitor is the panel that will show JProgressBar for
038: * IO threads.
039: *
040: * @version $Id: IOProgressMonitor.java 5357 2006-03-14 09:45:51Z kpouer $
041: */
042: public class IOProgressMonitor extends JPanel {
043: //{{{ IOProgressMonitor constructor
044: public IOProgressMonitor() {
045: super (new BorderLayout());
046: caption = new JLabel();
047: updateCaption();
048: add(BorderLayout.NORTH, caption);
049:
050: threads = new ThreadProgress[VFSManager.getIOThreadPool()
051: .getThreadCount()];
052:
053: Box box = new Box(BoxLayout.Y_AXIS);
054: for (int i = 0; i < threads.length; i++) {
055: if (i != 0)
056: box.add(Box.createVerticalStrut(6));
057:
058: threads[i] = new ThreadProgress(i);
059: box.add(threads[i]);
060: }
061:
062: JPanel threadPanel = new JPanel(new BorderLayout());
063: threadPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
064: threadPanel.add(BorderLayout.NORTH, box);
065:
066: add(BorderLayout.CENTER, new JScrollPane(threadPanel));
067:
068: workThreadHandler = new WorkThreadHandler();
069: } //}}}
070:
071: //{{{ addNotify() method
072: public void addNotify() {
073: VFSManager.getIOThreadPool().addProgressListener(
074: workThreadHandler);
075: super .addNotify();
076: } //}}}
077:
078: //{{{ removeNotify() method
079: public void removeNotify() {
080: VFSManager.getIOThreadPool().removeProgressListener(
081: workThreadHandler);
082: super .removeNotify();
083: } //}}}
084:
085: //{{{ Private members
086:
087: //{{{ Instance variables
088: private JLabel caption;
089: private ThreadProgress[] threads;
090: private WorkThreadHandler workThreadHandler;
091:
092: //}}}
093:
094: //{{{ updateCaption() method
095: private void updateCaption() {
096: String[] args = { String.valueOf(VFSManager.getIOThreadPool()
097: .getRequestCount()) };
098: caption.setText(jEdit.getProperty(
099: "io-progress-monitor.caption", args));
100: } //}}}
101:
102: //}}}
103:
104: //{{{ WorkThreadHandler class
105: class WorkThreadHandler implements WorkThreadProgressListener {
106: public void statusUpdate(final WorkThreadPool threadPool,
107: final int threadIndex) {
108: SwingUtilities.invokeLater(new Runnable() {
109: public void run() {
110: updateCaption();
111: threads[threadIndex].update();
112: }
113: });
114: }
115:
116: public void progressUpdate(final WorkThreadPool threadPool,
117: final int threadIndex) {
118: SwingUtilities.invokeLater(new Runnable() {
119: public void run() {
120: updateCaption();
121: threads[threadIndex].update();
122: }
123: });
124: }
125: } //}}}
126:
127: //{{{ ThreadProgress class
128: class ThreadProgress extends JPanel {
129: //{{{ ThreadProgress constructor
130: public ThreadProgress(int index) {
131: super (new BorderLayout(12, 12));
132:
133: this .index = index;
134:
135: Box box = new Box(BoxLayout.Y_AXIS);
136: box.add(Box.createGlue());
137: box.add(progress = new JProgressBar());
138: progress.setStringPainted(true);
139: box.add(Box.createGlue());
140: ThreadProgress.this .add(BorderLayout.CENTER, box);
141:
142: abort = new JButton(jEdit
143: .getProperty("io-progress-monitor.abort"));
144: abort.addActionListener(new ActionHandler());
145: ThreadProgress.this .add(BorderLayout.EAST, abort);
146:
147: update();
148: } //}}}
149:
150: //{{{ update() method
151: public void update() {
152: WorkThread thread = VFSManager.getIOThreadPool().getThread(
153: index);
154: if (thread.isRequestRunning()) {
155: if (progress.isIndeterminate()) {
156: if (thread.getProgressMaximum() != 0)
157: progress.setIndeterminate(false);
158: } else if (thread.getProgressMaximum() == 0)
159: progress.setIndeterminate(true);
160:
161: abort.setEnabled(true);
162: String status = thread.getStatus();
163: if (status == null)
164: status = "";
165: progress.setString(status);
166: progress.setMaximum(thread.getProgressMaximum());
167: //System.err.println("value: " + thread.getProgressValue());
168: progress.setValue(thread.getProgressValue());
169: } else {
170: abort.setEnabled(false);
171: progress.setString(jEdit
172: .getProperty("io-progress-monitor" + ".idle"));
173: progress.setIndeterminate(false);
174: progress.setValue(0);
175: }
176: } //}}}
177:
178: //{{{ Private members
179: private int index;
180: private JProgressBar progress;
181: private JButton abort;
182:
183: //}}}
184:
185: //{{{ ActionHandler class
186: class ActionHandler implements ActionListener {
187: public void actionPerformed(ActionEvent evt) {
188: if (evt.getSource() == abort) {
189: int result = GUIUtilities.confirm(
190: IOProgressMonitor.this , "abort", null,
191: JOptionPane.YES_NO_OPTION,
192: JOptionPane.QUESTION_MESSAGE);
193: if (result == JOptionPane.YES_OPTION) {
194: VFSManager.getIOThreadPool().getThread(index)
195: .abortCurrentRequest();
196: }
197: }
198: }
199: } //}}}
200: } //}}}
201: }
|