Source code: org/gjt/sp/jedit/gui/IOProgressMonitor.java
1 /*
2 * IOProgressMonitor.java - I/O progress monitor
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 2000, 2002 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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26 import javax.swing.border.*;
27 import javax.swing.*;
28 import java.awt.event.*;
29 import java.awt.*;
30 import org.gjt.sp.jedit.io.VFSManager;
31 import org.gjt.sp.jedit.*;
32 import org.gjt.sp.util.*;
33 //}}}
34
35 public class IOProgressMonitor extends JPanel
36 {
37 //{{{ IOProgressMonitor constructor
38 public IOProgressMonitor()
39 {
40 super(new BorderLayout());
41 caption = new JLabel();
42 updateCaption();
43 add(BorderLayout.NORTH,caption);
44
45 threads = new ThreadProgress[VFSManager.getIOThreadPool()
46 .getThreadCount()];
47
48 Box box = new Box(BoxLayout.Y_AXIS);
49 for(int i = 0; i < threads.length; i++)
50 {
51 if(i != 0)
52 box.add(Box.createVerticalStrut(6));
53
54 threads[i] = new ThreadProgress(i);
55 box.add(threads[i]);
56 }
57
58 JPanel threadPanel = new JPanel(new BorderLayout());
59 threadPanel.setBorder(new EmptyBorder(6,6,6,6));
60 threadPanel.add(BorderLayout.NORTH,box);
61
62 add(BorderLayout.CENTER,new JScrollPane(threadPanel));
63
64 workThreadHandler = new WorkThreadHandler();
65 } //}}}
66
67 //{{{ addNotify() method
68 public void addNotify()
69 {
70 VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
71 super.addNotify();
72 } //}}}
73
74 //{{{ removeNotify() method
75 public void removeNotify()
76 {
77 VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
78 super.removeNotify();
79 } //}}}
80
81 //{{{ Private members
82
83 //{{{ Instance variables
84 private JLabel caption;
85 private ThreadProgress[] threads;
86 private WorkThreadHandler workThreadHandler;
87 //}}}
88
89 //{{{ updateCaption() method
90 private void updateCaption()
91 {
92 String[] args = { String.valueOf(VFSManager.getIOThreadPool()
93 .getRequestCount()) };
94 caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
95 } //}}}
96
97 //}}}
98
99 //{{{ WorkThreadHandler class
100 class WorkThreadHandler implements WorkThreadProgressListener
101 {
102 public void statusUpdate(final WorkThreadPool pool, final int index)
103 {
104 SwingUtilities.invokeLater(new Runnable()
105 {
106 public void run()
107 {
108 updateCaption();
109 threads[index].update();
110 }
111 });
112 }
113
114 public void progressUpdate(final WorkThreadPool pool, final int index)
115 {
116 SwingUtilities.invokeLater(new Runnable()
117 {
118 public void run()
119 {
120 updateCaption();
121 threads[index].update();
122 }
123 });
124 }
125 } //}}}
126
127 //{{{ ThreadProgress class
128 class ThreadProgress extends JPanel
129 {
130 //{{{ ThreadProgress constructor
131 public ThreadProgress(int index)
132 {
133 super(new BorderLayout(12,12));
134
135 this.index = index;
136
137 Box box = new Box(BoxLayout.Y_AXIS);
138 box.add(Box.createGlue());
139 box.add(progress = new JProgressBar());
140 progress.setStringPainted(true);
141 box.add(Box.createGlue());
142 ThreadProgress.this.add(BorderLayout.CENTER,box);
143
144 abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
145 abort.addActionListener(new ActionHandler());
146 ThreadProgress.this.add(BorderLayout.EAST,abort);
147
148 update();
149 } //}}}
150
151 //{{{ update() method
152 public void update()
153 {
154 WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
155 if(thread.isRequestRunning())
156 {
157 abort.setEnabled(true);
158 String status = thread.getStatus();
159 if(status == null)
160 status = "";
161 progress.setString(status);
162 progress.setMaximum(thread.getProgressMaximum());
163 //System.err.println("value: " + thread.getProgressValue());
164 progress.setValue(thread.getProgressValue());
165 }
166 else
167 {
168 abort.setEnabled(false);
169 progress.setString(jEdit.getProperty("io-progress-monitor"
170 + ".idle"));
171 progress.setValue(0);
172 }
173 } //}}}
174
175 //{{{ Private members
176 private int index;
177 private JProgressBar progress;
178 private JButton abort;
179 //}}}
180
181 //{{{ ActionHandler class
182 class ActionHandler implements ActionListener
183 {
184 public void actionPerformed(ActionEvent evt)
185 {
186 if(evt.getSource() == abort)
187 {
188 int result = GUIUtilities.confirm(
189 IOProgressMonitor.this,"abort",null,
190 JOptionPane.YES_NO_OPTION,
191 JOptionPane.QUESTION_MESSAGE);
192 if(result == JOptionPane.YES_OPTION)
193 {
194 VFSManager.getIOThreadPool().getThread(index)
195 .abortCurrentRequest();
196 }
197 }
198 }
199 } //}}}
200 } //}}}
201 }