PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/IOProgressMonitor.java

#
Java | 218 lines | 147 code | 26 blank | 45 comment | 16 complexity | 76926815081f3d6ef0699d087d58ac03 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 17758 2010-05-07 13:20:28Z 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. @Override
  65. public void addNotify()
  66. {
  67. VFSManager.getIOThreadPool().addProgressListener(workThreadHandler);
  68. super.addNotify();
  69. } //}}}
  70. //{{{ removeNotify() method
  71. @Override
  72. public void removeNotify()
  73. {
  74. VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
  75. super.removeNotify();
  76. } //}}}
  77. //{{{ Private members
  78. //{{{ Instance variables
  79. private final JLabel caption;
  80. private final ThreadProgress[] threads;
  81. private final WorkThreadHandler workThreadHandler;
  82. //}}}
  83. //{{{ updateCaption() method
  84. private void updateCaption()
  85. {
  86. String[] args = { String.valueOf(VFSManager.getIOThreadPool()
  87. .getRequestCount()) };
  88. caption.setText(jEdit.getProperty("io-progress-monitor.caption",args));
  89. } //}}}
  90. //}}}
  91. //{{{ WorkThreadHandler class
  92. class WorkThreadHandler implements WorkThreadProgressListener
  93. {
  94. public void statusUpdate(WorkThreadPool threadPool, final int threadIndex)
  95. {
  96. ThreadUtilities.runInDispatchThread(new Runnable()
  97. {
  98. public void run()
  99. {
  100. updateCaption();
  101. threads[threadIndex].update();
  102. }
  103. });
  104. }
  105. public void progressUpdate(WorkThreadPool threadPool, final int threadIndex)
  106. {
  107. ThreadUtilities.runInDispatchThread(new Runnable()
  108. {
  109. public void run()
  110. {
  111. updateCaption();
  112. threads[threadIndex].update();
  113. }
  114. });
  115. }
  116. } //}}}
  117. //{{{ ThreadProgress class
  118. class ThreadProgress extends JPanel
  119. {
  120. //{{{ ThreadProgress constructor
  121. ThreadProgress(int index)
  122. {
  123. super(new BorderLayout(12,12));
  124. this.index = index;
  125. Box box = new Box(BoxLayout.Y_AXIS);
  126. box.add(Box.createGlue());
  127. box.add(progress = new JProgressBar());
  128. progress.setStringPainted(true);
  129. box.add(Box.createGlue());
  130. add(BorderLayout.CENTER,box);
  131. abort = new JButton(jEdit.getProperty("io-progress-monitor.abort"));
  132. abort.addActionListener(new ActionHandler());
  133. add(BorderLayout.EAST,abort);
  134. update();
  135. } //}}}
  136. //{{{ update() method
  137. public void update()
  138. {
  139. WorkThread thread = VFSManager.getIOThreadPool().getThread(index);
  140. if(thread.isRequestRunning())
  141. {
  142. if (progress.isIndeterminate())
  143. {
  144. if (thread.getProgressMaximum() != 0)
  145. progress.setIndeterminate(false);
  146. }
  147. else if (thread.getProgressMaximum() == 0)
  148. progress.setIndeterminate(true);
  149. abort.setEnabled(true);
  150. String status = thread.getStatus();
  151. if(status == null)
  152. status = "";
  153. progress.setString(status);
  154. progress.setMaximum(thread.getProgressMaximum());
  155. //System.err.println("value: " + thread.getProgressValue());
  156. progress.setValue(thread.getProgressValue());
  157. }
  158. else
  159. {
  160. abort.setEnabled(false);
  161. progress.setString(jEdit.getProperty("io-progress-monitor"
  162. + ".idle"));
  163. progress.setIndeterminate(false);
  164. progress.setValue(0);
  165. }
  166. } //}}}
  167. //{{{ Private members
  168. private final int index;
  169. private final JProgressBar progress;
  170. private final JButton abort;
  171. //}}}
  172. //{{{ ActionHandler class
  173. class ActionHandler implements ActionListener
  174. {
  175. public void actionPerformed(ActionEvent evt)
  176. {
  177. if(evt.getSource() == abort)
  178. {
  179. int result = GUIUtilities.confirm(
  180. IOProgressMonitor.this,"abort",null,
  181. JOptionPane.YES_NO_OPTION,
  182. JOptionPane.QUESTION_MESSAGE);
  183. if(result == JOptionPane.YES_OPTION)
  184. {
  185. VFSManager.getIOThreadPool().getThread(index)
  186. .abortCurrentRequest();
  187. }
  188. }
  189. }
  190. } //}}}
  191. } //}}}
  192. }