001: /*
002: * PluginManagerProgress.java - Plugin download progress meter
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2001 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.pluginmgr;
024:
025: //{{{ Imports
026: import javax.swing.border.*;
027: import javax.swing.*;
028: import java.awt.event.*;
029: import java.awt.*;
030: import org.gjt.sp.jedit.*;
031: import org.gjt.sp.util.ProgressObserver;
032:
033: //}}}
034:
035: class PluginManagerProgress extends JDialog implements ProgressObserver {
036: //{{{ PluginManagerProgress constructor
037: public PluginManagerProgress(PluginManager dialog, Roster roster) {
038: super (dialog, jEdit.getProperty("plugin-manager.progress"),
039: true);
040:
041: this .roster = roster;
042:
043: JPanel content = new JPanel(new BorderLayout(12, 12));
044: content.setBorder(new EmptyBorder(12, 12, 12, 12));
045: setContentPane(content);
046:
047: progress = new JProgressBar();
048: progress.setStringPainted(true);
049: progress
050: .setString(jEdit.getProperty("plugin-manager.progress"));
051:
052: int maximum = 0;
053: count = roster.getOperationCount();
054: for (int i = 0; i < count; i++) {
055: maximum += roster.getOperation(i).getMaximum();
056: }
057:
058: progress.setMaximum(maximum);
059: content.add(BorderLayout.NORTH, progress);
060:
061: stop = new JButton(jEdit
062: .getProperty("plugin-manager.progress.stop"));
063: stop.addActionListener(new ActionHandler());
064: JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0,
065: 0));
066: panel.add(stop);
067: content.add(BorderLayout.CENTER, panel);
068:
069: addWindowListener(new WindowHandler());
070:
071: pack();
072: setLocationRelativeTo(dialog);
073: setVisible(true);
074: } //}}}
075:
076: //{{{ setValue() method
077:
078: /**
079: * @param value the new value
080: * @deprecated Use {@link #setValue(long)}
081: */
082: public void setValue(final int value) {
083: SwingUtilities.invokeLater(new Runnable() {
084: public void run() {
085: progress.setValue(valueSoFar + value);
086: }
087: });
088: } //}}}
089:
090: //{{{ setValue() method
091: /**
092: * Update the progress value.
093: *
094: * @param value the new value
095: * @since jEdit 4.3pre3
096: */
097: public void setValue(final long value) {
098: SwingUtilities.invokeLater(new Runnable() {
099: public void run() {
100: progress.setValue(valueSoFar + (int) value);
101: }
102: });
103: } //}}}
104:
105: //{{{ setMaximum() method
106: /**
107: * This method is unused with the plugin manager.
108: *
109: * @param value the new max value (it will be ignored)
110: * @since jEdit 4.3pre3
111: */
112: public void setMaximum(long value) {
113: } //}}}
114:
115: //{{{ setStatus() method
116: /**
117: * This method is unused with the plugin manager.
118: *
119: * @param status the new status (it will be ignored)
120: * @since jEdit 4.3pre3
121: */
122: public void setStatus(String status) {
123: setTitle(status);
124: progress.setString(status);
125: } //}}}
126:
127: //{{{ done() method
128: public void done() {
129: try {
130: if (done == count) {
131: SwingUtilities.invokeAndWait(new Runnable() {
132: public void run() {
133: dispose();
134: }
135: });
136: } else {
137: SwingUtilities.invokeAndWait(new Runnable() {
138: public void run() {
139: valueSoFar += roster.getOperation(done - 1)
140: .getMaximum();
141: progress.setValue(valueSoFar);
142: done++;
143: }
144: });
145: }
146: } catch (Exception e) {
147: }
148: } //}}}
149:
150: //{{{ Private members
151:
152: //{{{ Instance variables
153: private Thread thread;
154:
155: private JProgressBar progress;
156: private JButton stop;
157: private int count;
158: private int done = 1;
159:
160: // progress value as of start of current task
161: private int valueSoFar;
162:
163: private Roster roster;
164:
165: //}}}
166:
167: //{{{ ActionHandler class
168: class ActionHandler implements ActionListener {
169: public void actionPerformed(ActionEvent evt) {
170: if (evt.getSource() == stop) {
171: thread.stop();
172: dispose();
173: }
174: }
175: } //}}}
176:
177: //{{{ WindowHandler class
178: class WindowHandler extends WindowAdapter {
179: boolean done;
180:
181: public void windowOpened(WindowEvent evt) {
182: if (done)
183: return;
184:
185: done = true;
186: thread = new RosterThread();
187: thread.start();
188: }
189:
190: public void windowClosing(WindowEvent evt) {
191: thread.stop();
192: dispose();
193: }
194: } //}}}
195:
196: //{{{ RosterThread class
197: class RosterThread extends Thread {
198: RosterThread() {
199: super ("Plugin manager thread");
200: }
201:
202: public void run() {
203: roster
204: .performOperationsInWorkThread(PluginManagerProgress.this );
205: }
206: } //}}}
207:
208: //}}}
209: }
|