/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/pluginmgr/PluginManagerProgress.java

# · Java · 188 lines · 127 code · 27 blank · 34 comment · 6 complexity · 3db70d7933aa3db56250b070b8aa97a7 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. package org.gjt.sp.jedit.pluginmgr;
  23. //{{{ Imports
  24. import javax.swing.border.*;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import org.gjt.sp.jedit.*;
  29. //}}}
  30. class PluginManagerProgress extends JDialog
  31. {
  32. //{{{ PluginManagerProgress constructor
  33. public PluginManagerProgress(PluginManager dialog, Roster roster)
  34. {
  35. super(dialog,jEdit.getProperty("plugin-manager.progress"),true);
  36. this.roster = roster;
  37. JPanel content = new JPanel(new BorderLayout(12,12));
  38. content.setBorder(new EmptyBorder(12,12,12,12));
  39. setContentPane(content);
  40. progress = new JProgressBar();
  41. progress.setStringPainted(true);
  42. progress.setString(jEdit.getProperty("plugin-manager.progress"));
  43. int maximum = 0;
  44. count = roster.getOperationCount();
  45. for(int i = 0; i < count; i++)
  46. {
  47. maximum += roster.getOperation(i).getMaximum();
  48. }
  49. progress.setMaximum(maximum);
  50. content.add(BorderLayout.NORTH,progress);
  51. stop = new JButton(jEdit.getProperty("plugin-manager.progress.stop"));
  52. stop.addActionListener(new ActionHandler());
  53. JPanel panel = new JPanel(new FlowLayout(
  54. FlowLayout.CENTER,0,0));
  55. panel.add(stop);
  56. content.add(BorderLayout.CENTER,panel);
  57. addWindowListener(new WindowHandler());
  58. pack();
  59. setLocationRelativeTo(dialog);
  60. show();
  61. } //}}}
  62. //{{{ setValue() method
  63. public void setValue(final int value)
  64. {
  65. SwingUtilities.invokeLater(new Runnable()
  66. {
  67. public void run()
  68. {
  69. progress.setValue(valueSoFar + value);
  70. }
  71. });
  72. } //}}}
  73. //{{{ done() method
  74. public void done()
  75. {
  76. try
  77. {
  78. if(done == count)
  79. {
  80. SwingUtilities.invokeAndWait(new Runnable()
  81. {
  82. public void run()
  83. {
  84. dispose();
  85. }
  86. });
  87. }
  88. else
  89. {
  90. SwingUtilities.invokeAndWait(new Runnable()
  91. {
  92. public void run()
  93. {
  94. valueSoFar += roster.getOperation(done - 1)
  95. .getMaximum();
  96. progress.setValue(valueSoFar);
  97. done++;
  98. }
  99. });
  100. }
  101. }
  102. catch(Exception e)
  103. {
  104. }
  105. } //}}}
  106. //{{{ Private members
  107. //{{{ Instance variables
  108. private Thread thread;
  109. private String type;
  110. private JProgressBar progress;
  111. private JButton stop;
  112. private int count;
  113. private int done = 1;
  114. // progress value as of start of current task
  115. private int valueSoFar;
  116. private Roster roster;
  117. //}}}
  118. //{{{ ActionHandler class
  119. class ActionHandler implements ActionListener
  120. {
  121. public void actionPerformed(ActionEvent evt)
  122. {
  123. if(evt.getSource() == stop)
  124. {
  125. thread.stop();
  126. dispose();
  127. }
  128. }
  129. } //}}}
  130. //{{{ WindowHandler class
  131. class WindowHandler extends WindowAdapter
  132. {
  133. boolean done;
  134. public void windowOpened(WindowEvent evt)
  135. {
  136. if(done)
  137. return;
  138. done = true;
  139. thread = new RosterThread();
  140. thread.start();
  141. }
  142. public void windowClosing(WindowEvent evt)
  143. {
  144. thread.stop();
  145. dispose();
  146. }
  147. } //}}}
  148. //{{{ RosterThread class
  149. class RosterThread extends Thread
  150. {
  151. RosterThread()
  152. {
  153. super("Plugin manager thread");
  154. }
  155. public void run()
  156. {
  157. roster.performOperationsInWorkThread(PluginManagerProgress.this);
  158. }
  159. } //}}}
  160. //}}}
  161. }