KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ProgressBarDemo


1 /*
2  * @(#)ProgressBarDemo.java 1.12 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ProgressBarDemo.java 1.12 05/11/17
39  */

40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58 /**
59  * JProgressBar Demo
60  *
61  * @version 1.12 11/17/05
62  * @author Jeff Dinkins
63  # @author Peter Korn (accessibility support)
64  */

65 public class ProgressBarDemo extends DemoModule {
66
67     /**
68      * main method allows us to run as a standalone demo.
69      */

70     public static void main(String JavaDoc[] args) {
71     ProgressBarDemo demo = new ProgressBarDemo(null);
72     demo.mainImpl();
73     }
74
75     /**
76      * ProgressBarDemo Constructor
77      */

78     public ProgressBarDemo(SwingSet2 swingset) {
79     // Set the title for this demo, and an icon used to represent this
80
// demo inside the SwingSet2 app.
81
super(swingset, "ProgressBarDemo", "toolbar/JProgressBar.gif");
82
83     createProgressPanel();
84     }
85
86     javax.swing.Timer JavaDoc timer = new javax.swing.Timer JavaDoc(18, createTextLoadAction());
87     Action loadAction;
88     Action stopAction;
89     JProgressBar progressBar;
90     JTextArea progressTextArea;
91
92     void updateDragEnabled(boolean dragEnabled) {
93         progressTextArea.setDragEnabled(dragEnabled);
94     }
95     
96     public void createProgressPanel() {
97     getDemoPanel().setLayout(new BorderLayout());
98
99     JPanel textWrapper = new JPanel(new BorderLayout());
100     textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
101     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
102     progressTextArea = new MyTextArea();
103         
104     progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_name"));
105     progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_description"));
106     textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);
107
108     getDemoPanel().add(textWrapper, BorderLayout.CENTER);
109
110     JPanel progressPanel = new JPanel();
111     getDemoPanel().add(progressPanel, BorderLayout.SOUTH);
112
113     progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {
114         public Dimension getPreferredSize() {
115         return new Dimension(300, super.getPreferredSize().height);
116         }
117     };
118     progressBar.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_loading_progress"));
119
120     progressPanel.add(progressBar);
121     progressPanel.add(createLoadButton());
122     progressPanel.add(createStopButton());
123     }
124
125     public JButton createLoadButton() {
126     loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) {
127         public void actionPerformed(ActionEvent e) {
128         loadAction.setEnabled(false);
129         stopAction.setEnabled(true);
130                 if (progressBar.getValue() == progressBar.getMaximum()) {
131                     progressBar.setValue(0);
132                     textLocation = 0;
133                     progressTextArea.setText("");
134                 }
135         timer.start();
136         }
137     };
138     return createButton(loadAction);
139     }
140
141     public JButton createStopButton() {
142     stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {
143         public void actionPerformed(ActionEvent e) {
144         timer.stop();
145         loadAction.setEnabled(true);
146         stopAction.setEnabled(false);
147         }
148     };
149     return createButton(stopAction);
150     }
151
152     public JButton createButton(Action a) {
153     JButton b = new JButton();
154     // setting the following client property informs the button to show
155
// the action text as it's name. The default is to not show the
156
// action text.
157
b.putClientProperty("displayActionText", Boolean.TRUE);
158     b.setAction(a);
159     return b;
160     }
161
162
163     int textLocation = 0;
164
165     String JavaDoc text = getString("ProgressBarDemo.text");
166
167     public Action createTextLoadAction() {
168     return new AbstractAction("text load action") {
169         public void actionPerformed (ActionEvent e) {
170         if(progressBar.getValue() < progressBar.getMaximum()) {
171             progressBar.setValue(progressBar.getValue() + 1);
172             progressTextArea.append(text.substring(textLocation, textLocation+1));
173             textLocation++;
174         } else {
175             timer.stop();
176             loadAction.setEnabled(true);
177             stopAction.setEnabled(false);
178         }
179         }
180     };
181     }
182
183
184     class MyTextArea extends JTextArea {
185         public MyTextArea() {
186             super(null, 0, 0);
187         setEditable(false);
188         setText("");
189         }
190
191         public float getAlignmentX () {
192             return LEFT_ALIGNMENT;
193         }
194  
195         public float getAlignmentY () {
196             return TOP_ALIGNMENT;
197         }
198     }
199 }
200
201
202
Popular Tags