PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/IOProgressMonitor.java

#
Java | 216 lines | 145 code | 26 blank | 45 comment | 16 complexity | a62b7ee6e1c0f00ecddd8de4deac1d1d MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * IOProgressMonitor.java - I/O progress monitor
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2002 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.gui;
  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.io.VFSManager;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.util.*;
  31. //}}}
  32. /**
  33. * The IO progressMonitor is the panel that will show JProgressBar for
  34. * IO threads.
  35. *
  36. * @version $Id: IOProgressMonitor.java 5357 2006-03-14 09:45:51Z kpouer $
  37. */
  38. public class IOProgressMonitor extends JPanel
  39. {
  40. //{{{ IOProgressMonitor constructor
  41. public IOProgressMonitor()
  42. {
  43. super(new BorderLayout());
  44. caption = new JLabel();
  45. updateCaption();
  46. add(BorderLayout.NORTH,caption);
  47. threads = new ThreadProgress[VFSManager.getIOThreadPool()
  48. .getThreadCount()];
  49. Box box = new Box(BoxLayout.Y_AXIS);
  50. for(int i = 0; i < threads.length; i++)
  51. {
  52. if(i != 0)
  53. box.add(Box.createVerticalStrut(6));
  54. threads[i] = new ThreadProgress(i);
  55. box.add(threads[i]);
  56. }
  57. JPanel threadPanel = new JPanel(new BorderLayout());
  58. threadPanel.setBorder(new EmptyBorder(6,6,6,6));
  59. threadPanel.add(BorderLayout.NORTH,box);
  60. add(BorderLayout.CENTER,new JScrollPane(threadPanel));
  61. workThreadHandler = new WorkThreadHandler();
  62. } //}}}
  63. //{{{ addNotify() method
  64. public void addNotify()
  65. {
  66. VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
  67. super.addNotify();
  68. } //}}}
  69. //{{{ removeNotify() method
  70. public void removeNotify()
  71. {
  72. VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
  73. super.removeNotify();
  74. } //}}}
  75. //{{{ Private members
  76. //{{{ Instance variables
  77. private JLabel caption;
  78. private ThreadProgress[] threads;
  79. private WorkThreadHandler workThreadHandler;
  80. //}}}
  81. //{{{ updateCaption() method
  82. private void updateCaption()
  83. {
  84. String[] args = { String.valueOf(VFSManager.getIOThreadPool()
  85. .getRequestCount()) };
  86. caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
  87. } //}}}
  88. //}}}
  89. //{{{ WorkThreadHandler class
  90. class WorkThreadHandler implements WorkThreadProgressListener
  91. {
  92. public void statusUpdate(final WorkThreadPool threadPool, final int threadIndex)
  93. {
  94. SwingUtilities.invokeLater(new Runnable()
  95. {
  96. public void run()
  97. {
  98. updateCaption();
  99. threads[threadIndex].update();
  100. }
  101. });
  102. }
  103. public void progressUpdate(final WorkThreadPool threadPool, final int threadIndex)
  104. {
  105. SwingUtilities.invokeLater(new Runnable()
  106. {
  107. public void run()
  108. {
  109. updateCaption();
  110. threads[threadIndex].update();
  111. }
  112. });
  113. }
  114. } //}}}
  115. //{{{ ThreadProgress class
  116. class ThreadProgress extends JPanel
  117. {
  118. //{{{ ThreadProgress constructor
  119. public ThreadProgress(int index)
  120. {
  121. super(new BorderLayout(12,12));
  122. this.index = index;
  123. Box box = new Box(BoxLayout.Y_AXIS);
  124. box.add(Box.createGlue());
  125. box.add(progress = new JProgressBar());
  126. progress.setStringPainted(true);
  127. box.add(Box.createGlue());
  128. ThreadProgress.this.add(BorderLayout.CENTER,box);
  129. abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
  130. abort.addActionListener(new ActionHandler());
  131. ThreadProgress.this.add(BorderLayout.EAST,abort);
  132. update();
  133. } //}}}
  134. //{{{ update() method
  135. public void update()
  136. {
  137. WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
  138. if(thread.isRequestRunning())
  139. {
  140. if (progress.isIndeterminate())
  141. {
  142. if (thread.getProgressMaximum() != 0)
  143. progress.setIndeterminate(false);
  144. }
  145. else if (thread.getProgressMaximum() == 0)
  146. progress.setIndeterminate(true);
  147. abort.setEnabled(true);
  148. String status = thread.getStatus();
  149. if(status == null)
  150. status = "";
  151. progress.setString(status);
  152. progress.setMaximum(thread.getProgressMaximum());
  153. //System.err.println("value: " + thread.getProgressValue());
  154. progress.setValue(thread.getProgressValue());
  155. }
  156. else
  157. {
  158. abort.setEnabled(false);
  159. progress.setString(jEdit.getProperty("io-progress-monitor"
  160. + ".idle"));
  161. progress.setIndeterminate(false);
  162. progress.setValue(0);
  163. }
  164. } //}}}
  165. //{{{ Private members
  166. private int index;
  167. private JProgressBar progress;
  168. private JButton abort;
  169. //}}}
  170. //{{{ ActionHandler class
  171. class ActionHandler implements ActionListener
  172. {
  173. public void actionPerformed(ActionEvent evt)
  174. {
  175. if(evt.getSource() == abort)
  176. {
  177. int result = GUIUtilities.confirm(
  178. IOProgressMonitor.this,"abort",null,
  179. JOptionPane.YES_NO_OPTION,
  180. JOptionPane.QUESTION_MESSAGE);
  181. if(result == JOptionPane.YES_OPTION)
  182. {
  183. VFSManager.getIOThreadPool().getThread(index)
  184. .abortCurrentRequest();
  185. }
  186. }
  187. }
  188. } //}}}
  189. } //}}}
  190. }