KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > interactive > SwingProgressIndicator


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.interactive;
17
18 import javax.swing.JFrame JavaDoc;
19 import javax.swing.JProgressBar JavaDoc;
20 import javax.swing.SwingUtilities JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.HeadlessException JavaDoc;
23
24
25 /**
26  * TODO: Add documentation
27  *
28  * @author Fyodor Kupolov
29  * @version 1.0
30  */

31 public class SwingProgressIndicator extends ProgressIndicatorBase {
32     private ProgressWindow w;
33
34     public SwingProgressIndicator(String JavaDoc title) {
35         w = new ProgressWindow(title);
36     }
37
38     protected void show(final String JavaDoc label, final double progress) {
39         if (!w.isVisible()) {
40             w.setVisible(true);
41         }
42
43         w.update(label, (int) (progress * 100));
44
45         try {
46             Thread.sleep(400);
47         } catch (InterruptedException JavaDoc e) {
48             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
49
}
50     }
51
52     protected void onComplete(final String JavaDoc label) {
53         super.onComplete(label);
54         w.setVisible(false);
55         w.dispose();
56     }
57
58     private static class ProgressWindow extends JFrame JavaDoc {
59         JProgressBar JavaDoc pb = new JProgressBar JavaDoc(0, 100);
60
61         public ProgressWindow(String JavaDoc title) throws HeadlessException JavaDoc {
62             super(title);
63             pb.setStringPainted(true);
64             pb.setPreferredSize(new Dimension JavaDoc(500, 20));
65             getContentPane().add(pb);
66             pack();
67             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68
69             setLocationRelativeTo(null);
70         }
71
72         public void update(final String JavaDoc message, final int progress) {
73             SwingUtilities.invokeLater(new Runnable JavaDoc() {
74                 public void run() {
75                     pb.setValue(progress);
76                     pb.setString(message);
77                 }
78             });
79         }
80     }
81 }
82
Popular Tags