PageRenderTime 60ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 271 lines | 192 code | 38 blank | 41 comment | 7 complexity | 246f2c2633988ab1c21f694880ad384c 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.*;
 31//}}}
 32
 33public class PluginManagerProgress extends JDialog
 34{
 35	//{{{ PluginManagerProgress constructor
 36	public PluginManagerProgress(JDialog dialog, String type, Roster roster)
 37	{
 38		super(JOptionPane.getFrameForComponent(dialog),
 39			jEdit.getProperty("plugin-manager.progress."
 40			+ type + "-task"),true);
 41
 42		this.roster = roster;
 43		this.type = type;
 44
 45		JPanel content = new JPanel(new BorderLayout(12,12));
 46		content.setBorder(new EmptyBorder(12,12,12,12));
 47		setContentPane(content);
 48
 49		globalProgress = new JProgressBar();
 50		globalProgress.setStringPainted(true);
 51		globalProgress.setString(jEdit.getProperty("plugin-manager.progress."
 52			+ type + "-task"));
 53
 54		count = roster.getOperationCount();
 55
 56		globalProgress.setMaximum(count);
 57		content.add(BorderLayout.NORTH,globalProgress);
 58
 59		localProgress = new JProgressBar();
 60		localProgress.setStringPainted(true);
 61		content.add(BorderLayout.CENTER,localProgress);
 62
 63		stop = new JButton(jEdit.getProperty("plugin-manager.progress.stop"));
 64		stop.addActionListener(new ActionHandler());
 65		JPanel panel = new JPanel();
 66		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 67		panel.add(Box.createGlue());
 68		panel.add(stop);
 69		panel.add(Box.createGlue());
 70		content.add(BorderLayout.SOUTH,panel);
 71
 72		addWindowListener(new WindowHandler());
 73
 74		pack();
 75
 76		Dimension screen = getToolkit().getScreenSize();
 77		Dimension size = getSize();
 78		size.width = Math.max(size.width,500);
 79		setSize(size);
 80		setLocationRelativeTo(dialog);
 81
 82		show();
 83	} //}}}
 84
 85	//{{{ removing() method
 86	public void removing(String plugin)
 87	{
 88		String[] args = { plugin };
 89		showMessage(jEdit.getProperty("plugin-manager.progress.removing",args));
 90		stop.setEnabled(true);
 91	} //}}}
 92
 93	//{{{ downloading() method
 94	public void downloading(String plugin)
 95	{
 96		String[] args = { plugin };
 97		showMessage(jEdit.getProperty("plugin-manager.progress.downloading",args));
 98		stop.setEnabled(true);
 99	} //}}}
100
101	//{{{ installing() method
102	public void installing(String plugin)
103	{
104		String[] args = { plugin };
105		showMessage(jEdit.getProperty("plugin-manager.progress.installing",args));
106		stop.setEnabled(false);
107	} //}}}
108
109	//{{{ setMaximum() method
110	public void setMaximum(final int total)
111	{
112		SwingUtilities.invokeLater(new Runnable()
113		{
114			public void run()
115			{
116				localProgress.setMaximum(total);
117			}
118		});
119	} //}}}
120
121	//{{{ setValue() method
122	public void setValue(final int value)
123	{
124		SwingUtilities.invokeLater(new Runnable()
125		{
126			public void run()
127			{
128				localProgress.setValue(value);
129			}
130		});
131	} //}}}
132
133	//{{{ done() method
134	public void done(final boolean ok)
135	{
136		this.ok |= ok;
137
138		try
139		{
140			if(!ok || done == count)
141			{
142				SwingUtilities.invokeAndWait(new Runnable()
143				{
144					public void run()
145					{
146						dispose();
147						if(ok)
148						{
149							GUIUtilities.message(PluginManagerProgress.this,
150								"plugin-manager." + type
151								+ "-done",null);
152						}
153						else
154						{
155							// user will see an error in any case
156
157							//GUIUtilities.message(PluginManagerProgress.this,
158							//	"plugin-manager.failed",null);
159						}
160					}
161				});
162			}
163			else
164			{
165				SwingUtilities.invokeAndWait(new Runnable()
166				{
167					public void run()
168					{
169						globalProgress.setValue(done++);
170						localProgress.setValue(0);
171					}
172				});
173			}
174		}
175		catch(Exception e)
176		{
177		}
178	} //}}}
179
180	//{{{ isOK() method
181	public boolean isOK()
182	{
183		return ok;
184	} //}}}
185
186	//{{{ Private members
187
188	//{{{ Instance variables
189	private Thread thread;
190
191	private String type;
192
193	private JProgressBar globalProgress, localProgress;
194	private JButton stop;
195	private int count;
196	private int done = 1;
197
198	private boolean ok;
199
200	private Roster roster;
201	//}}}
202
203	//{{{ showMessage() method
204	private void showMessage(final String msg)
205	{
206		try
207		{
208			SwingUtilities.invokeAndWait(new Runnable()
209			{
210				public void run()
211				{
212					localProgress.setString(msg);
213				}
214			});
215		}
216		catch(Exception e)
217		{
218		}
219
220		Thread.yield();
221	} //}}}
222
223	//{{{ ActionHandler class
224	class ActionHandler implements ActionListener
225	{
226		public void actionPerformed(ActionEvent evt)
227		{
228			if(evt.getSource() == stop)
229			{
230				thread.stop();
231				dispose();
232			}
233		}
234	} //}}}
235
236	//{{{ WindowHandler class
237	class WindowHandler extends WindowAdapter
238	{
239		boolean done;
240
241		public void windowOpened(WindowEvent evt)
242		{
243			if(done)
244				return;
245
246			done = true;
247			thread = new RosterThread();
248			thread.start();
249		}
250
251		public void windowClosing(WindowEvent evt)
252		{
253			thread.stop();
254			dispose();
255		}
256	} //}}}
257
258	//{{{ RosterThread class
259	class RosterThread extends Thread
260	{
261		RosterThread()
262		{
263			super("Plugin manager thread");
264		}
265
266		public void run()
267		{
268			roster.performOperations(PluginManagerProgress.this);
269		}
270	} //}}}
271}