PageRenderTime 87ms CodeModel.GetById 73ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/pluginmgr/PluginManagerProgress.java

#
Java | 234 lines | 145 code | 30 blank | 59 comment | 6 complexity | da4761b03c66fa45426b758b50438bf9 MD5 | raw file
  1/*
  2 * PluginManagerProgress.java - Plugin download progress meter
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2001 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.pluginmgr;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.util.ProgressObserver;
 32//}}}
 33
 34class PluginManagerProgress extends JDialog implements ProgressObserver
 35{
 36	//{{{ PluginManagerProgress constructor
 37	public PluginManagerProgress(PluginManager dialog, Roster roster)
 38	{
 39		super(dialog,jEdit.getProperty("plugin-manager.progress"),true);
 40
 41		this.roster = roster;
 42
 43		JPanel content = new JPanel(new BorderLayout(12,12));
 44		content.setBorder(new EmptyBorder(12,12,12,12));
 45		setContentPane(content);
 46
 47		progress = new JProgressBar();
 48		progress.setStringPainted(true);
 49		progress.setString(jEdit.getProperty("plugin-manager.progress"));
 50
 51		int maximum = 0;
 52		count = roster.getOperationCount();
 53		for(int i = 0; i < count; i++)
 54		{
 55			maximum += roster.getOperation(i).getMaximum();
 56		}
 57
 58		progress.setMaximum(maximum);
 59		content.add(BorderLayout.NORTH,progress);
 60
 61		stop = new JButton(jEdit.getProperty("plugin-manager.progress.stop"));
 62		stop.addActionListener(new ActionHandler());
 63		JPanel panel = new JPanel(new FlowLayout(
 64			FlowLayout.CENTER,0,0));
 65		panel.add(stop);
 66		content.add(BorderLayout.CENTER,panel);
 67
 68		addWindowListener(new WindowHandler());
 69
 70		pack();
 71		setLocationRelativeTo(dialog);
 72		setVisible(true);
 73	} //}}}
 74
 75	//{{{ setValue() method
 76
 77	/**
 78	 * @param value the new value
 79	 * @deprecated Use {@link #setValue(long)}
 80	 */
 81	public void setValue(final int value)
 82	{
 83		SwingUtilities.invokeLater(new Runnable()
 84		{
 85			public void run()
 86			{
 87				progress.setValue(valueSoFar + value);
 88			}
 89		});
 90	} //}}}
 91
 92	//{{{ setValue() method
 93	/**
 94	 * Update the progress value.
 95	 *
 96	 * @param value the new value
 97	 * @since jEdit 4.3pre3
 98	 */
 99	public void setValue(final long value)
100	{
101		SwingUtilities.invokeLater(new Runnable()
102			{
103				public void run()
104				{
105					progress.setValue(valueSoFar + (int) value);
106				}
107			});
108	} //}}}
109
110	//{{{ setMaximum() method
111	/**
112	 * This method is unused with the plugin manager.
113	 *
114	 * @param value the new max value (it will be ignored)
115	 * @since jEdit 4.3pre3
116	 */
117	public void setMaximum(long value) 
118	{
119	} //}}}
120
121	//{{{ setStatus() method
122	/**
123	 * This method is unused with the plugin manager.
124	 *
125	 * @param status the new status (it will be ignored)
126	 * @since jEdit 4.3pre3
127	 */
128	 public void setStatus(String status) 
129	 {
130		 setTitle(status);
131		 progress.setString(status);
132	} //}}}
133
134	//{{{ done() method
135	public void done()
136	{
137		try
138		{
139			if(done == count)
140			{
141				SwingUtilities.invokeAndWait(new Runnable()
142				{
143					public void run()
144					{
145						dispose();
146					}
147				});
148			}
149			else
150			{
151				SwingUtilities.invokeAndWait(new Runnable()
152				{
153					public void run()
154					{
155						valueSoFar += roster.getOperation(done - 1)
156							.getMaximum();
157						progress.setValue(valueSoFar);
158						done++;
159					}
160				});
161			}
162		}
163		catch(Exception e)
164		{
165		}
166	} //}}}
167
168	//{{{ Private members
169
170	//{{{ Instance variables
171	private Thread thread;
172
173	private JProgressBar progress;
174	private JButton stop;
175	private int count;
176	private int done = 1;
177
178	// progress value as of start of current task
179	private int valueSoFar;
180
181	private Roster roster;
182	//}}}
183
184	//{{{ ActionHandler class
185	class ActionHandler implements ActionListener
186	{
187		public void actionPerformed(ActionEvent evt)
188		{
189			if(evt.getSource() == stop)
190			{
191				thread.stop();
192				dispose();
193			}
194		}
195	} //}}}
196
197	//{{{ WindowHandler class
198	class WindowHandler extends WindowAdapter
199	{
200		boolean done;
201
202		public void windowOpened(WindowEvent evt)
203		{
204			if(done)
205				return;
206
207			done = true;
208			thread = new RosterThread();
209			thread.start();
210		}
211
212		public void windowClosing(WindowEvent evt)
213		{
214			thread.stop();
215			dispose();
216		}
217	} //}}}
218
219	//{{{ RosterThread class
220	class RosterThread extends Thread
221	{
222		RosterThread()
223		{
224			super("Plugin manager thread");
225		}
226
227		public void run()
228		{
229			roster.performOperationsInWorkThread(PluginManagerProgress.this);
230		}
231	} //}}}
232
233	//}}}
234}