PageRenderTime 130ms CodeModel.GetById 122ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/pluginmgr/PluginManagerProgress.java

#
Java | 220 lines | 138 code | 28 blank | 54 comment | 6 complexity | abe73ed90881a783678ceffa85aafd48 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	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		count = roster.getOperationCount();
 52		int maximum = 0;
 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	 * Update the progress value.
 78	 *
 79	 * @param value the new value
 80	 * @since jEdit 4.3pre3
 81	 */
 82	public void setValue(final long value)
 83	{
 84		SwingUtilities.invokeLater(new Runnable()
 85			{
 86				public void run()
 87				{
 88					progress.setValue(valueSoFar + (int) value);
 89				}
 90			});
 91	} //}}}
 92
 93	//{{{ setMaximum() method
 94	/**
 95	 * This method is unused with the plugin manager.
 96	 *
 97	 * @param value the new max value (it will be ignored)
 98	 * @since jEdit 4.3pre3
 99	 */
100	public void setMaximum(long value) 
101	{
102	} //}}}
103
104	//{{{ setStatus() method
105	/**
106	 * This method is unused with the plugin manager.
107	 *
108	 * @param status the new status (it will be ignored)
109	 * @since jEdit 4.3pre3
110	 */
111	 public void setStatus(String status) 
112	 {
113		 setTitle(status);
114		 progress.setString(status);
115	} //}}}
116
117	//{{{ done() method
118	public void done()
119	{
120		try
121		{
122			if(done == count)
123			{
124				SwingUtilities.invokeAndWait(new Runnable()
125				{
126					public void run()
127					{
128						dispose();
129					}
130				});
131			}
132			else
133			{
134				SwingUtilities.invokeAndWait(new Runnable()
135				{
136					public void run()
137					{
138						valueSoFar += roster.getOperation(done - 1)
139							.getMaximum();
140						progress.setValue(valueSoFar);
141						done++;
142					}
143				});
144			}
145		}
146		catch(Exception e)
147		{
148		}
149	} //}}}
150
151	//{{{ Private members
152
153	//{{{ Instance variables
154	private Thread thread;
155
156	private final JProgressBar progress;
157	private final JButton stop;
158	private final int count;
159	private int done = 1;
160
161	// progress value as of start of current task
162	private int valueSoFar;
163
164	private final Roster roster;
165	//}}}
166
167	//{{{ ActionHandler class
168	class ActionHandler implements ActionListener
169	{
170		public void actionPerformed(ActionEvent evt)
171		{
172			if(evt.getSource() == stop)
173			{
174				thread.stop();
175				dispose();
176			}
177		}
178	} //}}}
179
180	//{{{ WindowHandler class
181	class WindowHandler extends WindowAdapter
182	{
183		boolean done;
184
185		@Override
186		public void windowOpened(WindowEvent evt)
187		{
188			if(done)
189				return;
190
191			done = true;
192			thread = new RosterThread();
193			thread.start();
194		}
195
196		@Override
197		public void windowClosing(WindowEvent evt)
198		{
199			thread.stop();
200			dispose();
201		}
202	} //}}}
203
204	//{{{ RosterThread class
205	class RosterThread extends Thread
206	{
207		RosterThread()
208		{
209			super("Plugin manager thread");
210		}
211
212		@Override
213		public void run()
214		{
215			roster.performOperationsInWorkThread(PluginManagerProgress.this);
216		}
217	} //}}}
218
219	//}}}
220}