PageRenderTime 79ms CodeModel.GetById 70ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/IOProgressMonitor.java

#
Java | 218 lines | 147 code | 26 blank | 45 comment | 16 complexity | 76926815081f3d6ef0699d087d58ac03 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.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import org.gjt.sp.jedit.io.VFSManager;
 31import org.gjt.sp.jedit.*;
 32import 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 17758 2010-05-07 13:20:28Z kpouer $
 40 */
 41public 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	@Override
 75	public void addNotify()
 76	{
 77		VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
 78		super.addNotify();
 79	} //}}}
 80
 81	//{{{ removeNotify() method
 82	@Override
 83	public void removeNotify()
 84	{
 85		VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
 86		super.removeNotify();
 87	} //}}}
 88
 89	//{{{ Private members
 90
 91	//{{{ Instance variables
 92	private final JLabel caption;
 93	private final ThreadProgress[] threads;
 94	private final WorkThreadHandler workThreadHandler;
 95	//}}}
 96
 97	//{{{ updateCaption() method
 98	private void updateCaption()
 99	{
100		String[] args = { String.valueOf(VFSManager.getIOThreadPool()
101			.getRequestCount()) };
102		caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
103	} //}}}
104
105	//}}}
106
107	//{{{ WorkThreadHandler class
108	class WorkThreadHandler implements WorkThreadProgressListener
109	{
110		public void statusUpdate(WorkThreadPool threadPool, final int threadIndex)
111		{
112			ThreadUtilities.runInDispatchThread(new Runnable()
113			{
114				public void run()
115				{
116					updateCaption();
117					threads[threadIndex].update();
118				}
119			});
120		}
121
122		public void progressUpdate(WorkThreadPool threadPool, final int threadIndex)
123		{
124			ThreadUtilities.runInDispatchThread(new Runnable()
125			{
126				public void run()
127				{
128					updateCaption();
129					threads[threadIndex].update();
130				}
131			});
132		}
133	} //}}}
134
135	//{{{ ThreadProgress class
136	class ThreadProgress extends JPanel
137	{
138		//{{{ ThreadProgress constructor
139		ThreadProgress(int index)
140		{
141			super(new BorderLayout(12,12));
142
143			this.index = index;
144
145			Box box = new Box(BoxLayout.Y_AXIS);
146			box.add(Box.createGlue());
147			box.add(progress = new JProgressBar());
148			progress.setStringPainted(true);
149			box.add(Box.createGlue());
150			add(BorderLayout.CENTER,box);
151
152			abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
153			abort.addActionListener(new ActionHandler());
154			add(BorderLayout.EAST,abort);
155
156			update();
157		} //}}}
158
159		//{{{ update() method
160		public void update()
161		{
162			WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
163			if(thread.isRequestRunning())
164			{
165				if (progress.isIndeterminate())
166				{
167					if (thread.getProgressMaximum() != 0)
168						progress.setIndeterminate(false);
169				}
170				else if (thread.getProgressMaximum() == 0)
171					progress.setIndeterminate(true);
172				
173				abort.setEnabled(true);
174				String status = thread.getStatus();
175				if(status == null)
176					status = "";
177				progress.setString(status);
178				progress.setMaximum(thread.getProgressMaximum());
179				//System.err.println("value: " + thread.getProgressValue());
180				progress.setValue(thread.getProgressValue());
181			}
182			else
183			{
184				abort.setEnabled(false);
185				progress.setString(jEdit.getProperty("io-progress-monitor"
186					+ ".idle"));
187				progress.setIndeterminate(false);
188				progress.setValue(0);
189			}
190		} //}}}
191
192		//{{{ Private members
193		private final int index;
194		private final JProgressBar progress;
195		private final JButton abort;
196		//}}}
197
198		//{{{ ActionHandler class
199		class ActionHandler implements ActionListener
200		{
201			public void actionPerformed(ActionEvent evt)
202			{
203				if(evt.getSource() == abort)
204				{
205					int result = GUIUtilities.confirm(
206						IOProgressMonitor.this,"abort",null,
207						JOptionPane.YES_NO_OPTION,
208						JOptionPane.QUESTION_MESSAGE);
209					if(result == JOptionPane.YES_OPTION)
210					{
211						VFSManager.getIOThreadPool().getThread(index)
212							.abortCurrentRequest();
213					}
214				}
215			}
216		} //}}}
217	} //}}}
218}