ProgressBarDemo.java  
1   /*
2    * @(#)ProgressBarDemo.java 1.10 04/07/26
3    * 
4    * Copyright (c) 2004 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.10 04/07/26
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.10 07/26/04
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[] 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 timer;
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         if(timer == null) {
129             loadAction.setEnabled(false);
130             stopAction.setEnabled(true);
131                     if(progressBar.getValue() == progressBar.getMaximum()) {
132                         // start again from the beginning
133                         progressBar.setValue(0);
134                         progressTextArea.setText("");
135                         textLocation = 0;
136                     }
137             timer = new javax.swing.Timer(18, createTextLoadAction());
138             timer.start();
139         }
140         }
141     };
142     return createButton(loadAction);
143     }
144 
145     public JButton createStopButton() {
146     stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {
147         public void actionPerformed(ActionEvent e) {
148         if(timer != null) {
149             timer.stop();
150             timer = null;
151         }
152         loadAction.setEnabled(true);
153         stopAction.setEnabled(false);
154         }
155     };
156     return createButton(stopAction);
157     }
158 
159     public JButton createButton(Action a) {
160     JButton b = new JButton();
161     // setting the following client property informs the button to show
162     // the action text as it's name. The default is to not show the
163     // action text.
164     b.putClientProperty("displayActionText", Boolean.TRUE);
165     b.setAction(a);
166     return b;
167     }
168 
169 
170     int textLocation = 0;
171 
172     String text = getString("ProgressBarDemo.text");
173 
174     public Action createTextLoadAction() {
175     return new AbstractAction("text load action") {
176         public void actionPerformed (ActionEvent e) {
177         if(progressBar.getValue() < progressBar.getMaximum()) {
178             progressBar.setValue(progressBar.getValue() + 1);
179             progressTextArea.append(text.substring(textLocation, textLocation+1));
180             textLocation++;
181         } else {
182             if(timer != null) {
183             timer.stop();
184             timer = null;
185             loadAction.setEnabled(true);
186             stopAction.setEnabled(false);
187             }
188         }
189         }
190     };
191     }
192 
193 
194     class MyTextArea extends JTextArea {
195         public MyTextArea() {
196             super(null, 0, 0);
197         setEditable(false);
198         setText("");
199         }
200 
201         public float getAlignmentX () {
202             return LEFT_ALIGNMENT;
203         }
204  
205         public float getAlignmentY () {
206             return TOP_ALIGNMENT;
207         }
208     }
209 }
210 
211 
212