1 /*
2  *
3  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *   - Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *
12  *   - Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *   - Neither the name of Oracle nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 import javax.swing.*;
35 import javax.swing.event.*;
36 import javax.swing.text.*;
37 import javax.swing.border.*;
38 import javax.swing.colorchooser.*;
39 import javax.swing.filechooser.*;
40 import javax.accessibility.*;
41 
42 import java.awt.*;
43 import java.awt.event.*;
44 import java.beans.*;
45 import java.util.*;
46 import java.io.*;
47 import java.applet.*;
48 import java.net.*;
49 
50 /**
51  * JProgressBar Demo
52  *
53  * @author Jeff Dinkins
54  # @author Peter Korn (accessibility support)
55  */
56 public class ProgressBarDemo extends DemoModule {
57 
58     /**
59      * main method allows us to run as a standalone demo.
60      */
main(String[] args)61     public static void main(String[] args) {
62         ProgressBarDemo demo = new ProgressBarDemo(null);
63         demo.mainImpl();
64     }
65 
66     /**
67      * ProgressBarDemo Constructor
68      */
ProgressBarDemo(SwingSet2 swingset)69     public ProgressBarDemo(SwingSet2 swingset) {
70         // Set the title for this demo, and an icon used to represent this
71         // demo inside the SwingSet2 app.
72         super(swingset, "ProgressBarDemo", "toolbar/JProgressBar.gif");
73 
74         createProgressPanel();
75     }
76 
77     javax.swing.Timer timer = new javax.swing.Timer(18, createTextLoadAction());
78     Action loadAction;
79     Action stopAction;
80     JProgressBar progressBar;
81     JTextArea progressTextArea;
82 
updateDragEnabled(boolean dragEnabled)83     void updateDragEnabled(boolean dragEnabled) {
84         progressTextArea.setDragEnabled(dragEnabled);
85     }
86 
createProgressPanel()87     public void createProgressPanel() {
88         getDemoPanel().setLayout(new BorderLayout());
89 
90         JPanel textWrapper = new JPanel(new BorderLayout());
91         textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
92         textWrapper.setAlignmentX(LEFT_ALIGNMENT);
93         progressTextArea = new MyTextArea();
94 
95         progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_name"));
96         progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_description"));
97         textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);
98 
99         getDemoPanel().add(textWrapper, BorderLayout.CENTER);
100 
101         JPanel progressPanel = new JPanel();
102         getDemoPanel().add(progressPanel, BorderLayout.SOUTH);
103 
104         progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {
105             public Dimension getPreferredSize() {
106                 return new Dimension(300, super.getPreferredSize().height);
107             }
108         };
109         progressBar.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_loading_progress"));
110 
111         progressPanel.add(progressBar);
112         progressPanel.add(createLoadButton());
113         progressPanel.add(createStopButton());
114     }
115 
createLoadButton()116     public JButton createLoadButton() {
117         loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) {
118             public void actionPerformed(ActionEvent e) {
119                 loadAction.setEnabled(false);
120                 stopAction.setEnabled(true);
121                 if (progressBar.getValue() == progressBar.getMaximum()) {
122                     progressBar.setValue(0);
123                     textLocation = 0;
124                     progressTextArea.setText("");
125                 }
126                 timer.start();
127             }
128         };
129         return createButton(loadAction);
130     }
131 
createStopButton()132     public JButton createStopButton() {
133         stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {
134             public void actionPerformed(ActionEvent e) {
135                 timer.stop();
136                 loadAction.setEnabled(true);
137                 stopAction.setEnabled(false);
138             }
139         };
140         return createButton(stopAction);
141     }
142 
createButton(Action a)143     public JButton createButton(Action a) {
144         JButton b = new JButton();
145         // setting the following client property informs the button to show
146         // the action text as it's name. The default is to not show the
147         // action text.
148         b.putClientProperty("displayActionText", Boolean.TRUE);
149         b.setAction(a);
150         return b;
151     }
152 
153 
154     int textLocation = 0;
155 
156     String text = getString("ProgressBarDemo.text");
157 
createTextLoadAction()158     public Action createTextLoadAction() {
159         return new AbstractAction("text load action") {
160             public void actionPerformed (ActionEvent e) {
161                 if(progressBar.getValue() < progressBar.getMaximum()) {
162                     progressBar.setValue(progressBar.getValue() + 1);
163                     progressTextArea.append(text.substring(textLocation, textLocation+1));
164                     textLocation++;
165                 } else {
166                         timer.stop();
167                         loadAction.setEnabled(true);
168                         stopAction.setEnabled(false);
169                 }
170             }
171         };
172     }
173 
174 
175     class MyTextArea extends JTextArea {
176         public MyTextArea() {
177             super(null, 0, 0);
178             setEditable(false);
179             setText("");
180         }
181 
182         public float getAlignmentX () {
183             return LEFT_ALIGNMENT;
184         }
185 
186         public float getAlignmentY () {
187             return TOP_ALIGNMENT;
188         }
189     }
190 }
191