View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2005 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import java.awt.Point;
22  import java.io.File;
23  
24  import javax.swing.JDialog;
25  import javax.swing.JFrame;
26  import javax.swing.JLabel;
27  import javax.swing.JPanel;
28  import javax.swing.JProgressBar;
29  import javax.swing.SpringLayout;
30  
31  public class ProgressDialog extends JDialog {
32  
33      protected int maxTimeInSeconds;
34      protected JProgressBar progressBar;
35      protected Thread task;
36      protected boolean keepGoing = true;
37      protected final int MULTIPLIER = 10;
38      protected TestCaseDocsExecutor tcde;
39  
40      public ProgressDialog(JFrame rootFrame, int maxTimeInSeconds, TestCasePane testCasePane, File script){
41          super(rootFrame);
42          this.maxTimeInSeconds = maxTimeInSeconds;
43          Point rootLocation = rootFrame.getLocation();
44          int height = rootLocation.y + rootFrame.getHeight() / 2;
45          int width = rootLocation.x + rootFrame.getWidth() / 2;
46          setUndecorated(true);
47  
48          tcde = new TestCaseDocsExecutor(script,testCasePane);
49          tcde.setContextClassLoader(Utils.createClassLoader());
50          init();
51          setLocation(width, height);
52          tcde.setProgressDialog(this);
53          beginWaitingOnTask();
54          setModal(true);
55          setVisible(true);
56      }
57  
58      private void init(){
59          JPanel progressPanel = new JPanel(new SpringLayout());
60          progressBar = new JProgressBar(0, maxTimeInSeconds * MULTIPLIER);
61          JLabel generating = new JLabel("Generating Documentation");
62          progressPanel.add(generating);
63  
64          progressPanel.add(progressBar);
65          SpringUtilities.makeCompactGrid(progressPanel,
66                                          2, 1, //rows, cols  
67                                          6, 6, //initX, initY
68                                          6, 6); //xPad, yPad
69          getContentPane().add(progressPanel);
70          pack();
71      }
72  
73      private void beginWaitingOnTask(){
74          task = new Thread(){
75              public void run() {
76                  int interval = 100;
77                  for (int i = 0; keepGoing && i <= (maxTimeInSeconds * MULTIPLIER); i++) {
78                      try {
79                          sleep(interval);
80                          progressBar.setValue(i);
81                      } catch (InterruptedException e) {
82                          keepGoing = false;
83                          break;
84                      }
85                  }
86              }
87          };
88          tcde.start();
89          task.start();
90      }
91  
92      public void taskCompleted(){
93          keepGoing = false;
94          progressBar.setValue(maxTimeInSeconds);
95          dispose();
96      }
97  }