KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > IOProgressMonitor


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 /**
36  * The IO progressMonitor is the panel that will show JProgressBar for
37  * IO threads.
38  *
39  * @version $Id: IOProgressMonitor.java 5357 2006-03-14 09:45:51Z kpouer $
40  */

41 public class IOProgressMonitor extends JPanel
42 {
43     //{{{ IOProgressMonitor constructor
44
public IOProgressMonitor()
45     {
46         super(new BorderLayout());
47         caption = new JLabel();
48         updateCaption();
49         add(BorderLayout.NORTH,caption);
50
51         threads = new ThreadProgress[VFSManager.getIOThreadPool()
52             .getThreadCount()];
53
54         Box box = new Box(BoxLayout.Y_AXIS);
55         for(int i = 0; i < threads.length; i++)
56         {
57             if(i != 0)
58                 box.add(Box.createVerticalStrut(6));
59
60             threads[i] = new ThreadProgress(i);
61             box.add(threads[i]);
62         }
63
64         JPanel threadPanel = new JPanel(new BorderLayout());
65         threadPanel.setBorder(new EmptyBorder(6,6,6,6));
66         threadPanel.add(BorderLayout.NORTH,box);
67
68         add(BorderLayout.CENTER,new JScrollPane(threadPanel));
69
70         workThreadHandler = new WorkThreadHandler();
71     } //}}}
72

73     //{{{ addNotify() method
74
public void addNotify()
75     {
76         VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
77         super.addNotify();
78     } //}}}
79

80     //{{{ removeNotify() method
81
public void removeNotify()
82     {
83         VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
84         super.removeNotify();
85     } //}}}
86

87     //{{{ Private members
88

89     //{{{ Instance variables
90
private JLabel caption;
91     private ThreadProgress[] threads;
92     private WorkThreadHandler workThreadHandler;
93     //}}}
94

95     //{{{ updateCaption() method
96
private void updateCaption()
97     {
98         String JavaDoc[] args = { String.valueOf(VFSManager.getIOThreadPool()
99             .getRequestCount()) };
100         caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
101     } //}}}
102

103     //}}}
104

105     //{{{ WorkThreadHandler class
106
class WorkThreadHandler implements WorkThreadProgressListener
107     {
108         public void statusUpdate(final WorkThreadPool threadPool, final int threadIndex)
109         {
110             SwingUtilities.invokeLater(new Runnable JavaDoc()
111             {
112                 public void run()
113                 {
114                     updateCaption();
115                     threads[threadIndex].update();
116                 }
117             });
118         }
119
120         public void progressUpdate(final WorkThreadPool threadPool, final int threadIndex)
121         {
122             SwingUtilities.invokeLater(new Runnable JavaDoc()
123             {
124                 public void run()
125                 {
126                     updateCaption();
127                     threads[threadIndex].update();
128                 }
129             });
130         }
131     } //}}}
132

133     //{{{ ThreadProgress class
134
class ThreadProgress extends JPanel
135     {
136         //{{{ ThreadProgress constructor
137
public ThreadProgress(int index)
138         {
139             super(new BorderLayout(12,12));
140
141             this.index = index;
142
143             Box box = new Box(BoxLayout.Y_AXIS);
144             box.add(Box.createGlue());
145             box.add(progress = new JProgressBar());
146             progress.setStringPainted(true);
147             box.add(Box.createGlue());
148             ThreadProgress.this.add(BorderLayout.CENTER,box);
149
150             abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
151             abort.addActionListener(new ActionHandler());
152             ThreadProgress.this.add(BorderLayout.EAST,abort);
153
154             update();
155         } //}}}
156

157         //{{{ update() method
158
public void update()
159         {
160             WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
161             if(thread.isRequestRunning())
162             {
163                 if (progress.isIndeterminate())
164                 {
165                     if (thread.getProgressMaximum() != 0)
166                         progress.setIndeterminate(false);
167                 }
168                 else if (thread.getProgressMaximum() == 0)
169                     progress.setIndeterminate(true);
170                 
171                 abort.setEnabled(true);
172                 String JavaDoc status = thread.getStatus();
173                 if(status == null)
174                     status = "";
175                 progress.setString(status);
176                 progress.setMaximum(thread.getProgressMaximum());
177                 //System.err.println("value: " + thread.getProgressValue());
178
progress.setValue(thread.getProgressValue());
179             }
180             else
181             {
182                 abort.setEnabled(false);
183                 progress.setString(jEdit.getProperty("io-progress-monitor"
184                     + ".idle"));
185                 progress.setIndeterminate(false);
186                 progress.setValue(0);
187             }
188         } //}}}
189

190         //{{{ Private members
191
private int index;
192         private JProgressBar progress;
193         private JButton abort;
194         //}}}
195

196         //{{{ ActionHandler class
197
class ActionHandler implements ActionListener
198         {
199             public void actionPerformed(ActionEvent evt)
200             {
201                 if(evt.getSource() == abort)
202                 {
203                     int result = GUIUtilities.confirm(
204                         IOProgressMonitor.this,"abort",null,
205                         JOptionPane.YES_NO_OPTION,
206                         JOptionPane.QUESTION_MESSAGE);
207                     if(result == JOptionPane.YES_OPTION)
208                     {
209                         VFSManager.getIOThreadPool().getThread(index)
210                             .abortCurrentRequest();
211                     }
212                 }
213             }
214         } //}}}
215
} //}}}
216
}
217
Popular Tags