PageRenderTime 145ms CodeModel.GetById 90ms app.highlight 14ms RepoModel.GetById 37ms app.codeStats 0ms

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

#
Java | 181 lines | 137 code | 24 blank | 20 comment | 6 complexity | c45a0a6fc6c39b5dc4484e05b4fb179f MD5 | raw file
  1/*
  2 * IOProgressMonitor.java - I/O progress monitor
  3 * Copyright (C) 2000 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.border.*;
 23import javax.swing.event.*;
 24import javax.swing.*;
 25import java.awt.event.*;
 26import java.awt.*;
 27import org.gjt.sp.jedit.io.VFSManager;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.util.*;
 30
 31public class IOProgressMonitor extends JFrame
 32{
 33	public IOProgressMonitor()
 34	{
 35		super(jEdit.getProperty("io-progress-monitor.title"));
 36
 37		JPanel content = new JPanel(new BorderLayout());
 38		content.setBorder(new EmptyBorder(12,12,12,12));
 39		setContentPane(content);
 40
 41		caption = new JLabel();
 42		updateCaption();
 43		content.add(BorderLayout.NORTH,caption);
 44
 45		Box threadBox = new Box(BoxLayout.Y_AXIS);
 46		threads = new ThreadProgress[VFSManager.getIOThreadPool()
 47			.getThreadCount()];
 48		for(int i = 0; i < threads.length; i++)
 49		{
 50			threadBox.add(Box.createVerticalStrut(6));
 51
 52			threads[i] = new ThreadProgress(i);
 53			threadBox.add(threads[i]);
 54		}
 55
 56		content.add(BorderLayout.CENTER,threadBox);
 57
 58		workThreadHandler = new WorkThreadHandler();
 59		VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
 60
 61		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 62
 63		pack();
 64		GUIUtilities.loadGeometry(this,"io-progress-monitor");
 65		show();
 66	}
 67
 68	public void dispose()
 69	{
 70		GUIUtilities.saveGeometry(this,"io-progress-monitor");
 71		VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
 72		super.dispose();
 73	}
 74
 75	// private members
 76	private JLabel caption;
 77	private ThreadProgress[] threads;
 78	private WorkThreadHandler workThreadHandler;
 79
 80	private void updateCaption()
 81	{
 82		String[] args = { String.valueOf(VFSManager.getIOThreadPool()
 83			.getRequestCount()) };
 84		caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
 85	}
 86
 87	class WorkThreadHandler implements WorkThreadProgressListener
 88	{
 89		public void statusUpdate(final WorkThreadPool pool, final int index)
 90		{
 91			SwingUtilities.invokeLater(new Runnable()
 92			{
 93				public void run()
 94				{
 95					updateCaption();
 96					threads[index].update();
 97				}
 98			});
 99		}
100
101		public void progressUpdate(final WorkThreadPool pool, final int index)
102		{
103			SwingUtilities.invokeLater(new Runnable()
104			{
105				public void run()
106				{
107					updateCaption();
108					threads[index].update();
109				}
110			});
111		}
112	}
113
114	class ThreadProgress extends JPanel
115	{
116		public ThreadProgress(int index)
117		{
118			super(new BorderLayout());
119
120			this.index = index;
121
122			JPanel box = new JPanel();
123			box.setBorder(new EmptyBorder(0,0,0,12));
124			box.setLayout(new BoxLayout(box,BoxLayout.Y_AXIS));
125			box.add(Box.createGlue());
126			box.add(progress = new JProgressBar());
127			progress.setStringPainted(true);
128			box.add(Box.createGlue());
129			ThreadProgress.this.add(BorderLayout.CENTER,box);
130
131			abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
132			abort.addActionListener(new ActionHandler());
133			ThreadProgress.this.add(BorderLayout.EAST,abort);
134
135			update();
136		}
137
138		public void update()
139		{
140			WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
141			if(thread.isRequestRunning())
142			{
143				abort.setEnabled(true);
144				progress.setString(thread.getStatus());
145				progress.setMaximum(thread.getProgressMaximum());
146				progress.setValue(thread.getProgressValue());
147			}
148			else
149			{
150				abort.setEnabled(false);
151				progress.setString(jEdit.getProperty("io-progress-monitor"
152					+ ".idle"));
153				progress.setValue(0);
154			}
155		}
156
157		// private members
158		private int index;
159		private JProgressBar progress;
160		private JButton abort;
161
162		class ActionHandler implements ActionListener
163		{
164			public void actionPerformed(ActionEvent evt)
165			{
166				if(evt.getSource() == abort)
167				{
168					int result = GUIUtilities.confirm(
169						IOProgressMonitor.this,"abort",null,
170						JOptionPane.YES_NO_OPTION,
171						JOptionPane.QUESTION_MESSAGE);
172					if(result == JOptionPane.YES_OPTION)
173					{
174						VFSManager.getIOThreadPool().getThread(index)
175							.abortCurrentRequest();
176					}
177				}
178			}
179		}
180	}
181}