PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 17ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/IOProgressMonitor.java

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