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

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

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