1 /*
   2  *
   3  * Copyright (c) 2007, 2018 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      */
  61     public static void main(String[] args) {
  62         ProgressBarDemo demo = new ProgressBarDemo(null);
  63         demo.mainImpl();
  64     }
  65 
  66     /**
  67      * ProgressBarDemo Constructor
  68      */
  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 
  83     void updateDragEnabled(boolean dragEnabled) {
  84         progressTextArea.setDragEnabled(dragEnabled);
  85     }
  86 
  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         progressBar.setFocusable(true);
 111 
 112         progressPanel.add(progressBar);
 113         progressPanel.add(createLoadButton());
 114         progressPanel.add(createStopButton());
 115     }
 116 
 117     public JButton createLoadButton() {
 118         loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) {
 119             public void actionPerformed(ActionEvent e) {
 120                 loadAction.setEnabled(false);
 121                 stopAction.setEnabled(true);
 122                 if (progressBar.getValue() == progressBar.getMaximum()) {
 123                     progressBar.setValue(0);
 124                     textLocation = 0;
 125                     progressTextArea.setText("");
 126                 }
 127                 timer.start();
 128             }
 129         };
 130         return createButton(loadAction);
 131     }
 132 
 133     public JButton createStopButton() {
 134         stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {
 135             public void actionPerformed(ActionEvent e) {
 136                 timer.stop();
 137                 loadAction.setEnabled(true);
 138                 stopAction.setEnabled(false);
 139             }
 140         };
 141         return createButton(stopAction);
 142     }
 143 
 144     public JButton createButton(Action a) {
 145         JButton b = new JButton();
 146         // setting the following client property informs the button to show
 147         // the action text as it's name. The default is to not show the
 148         // action text.
 149         b.putClientProperty("displayActionText", Boolean.TRUE);
 150         b.setAction(a);
 151         return b;
 152     }
 153 
 154 
 155     int textLocation = 0;
 156 
 157     String text = getString("ProgressBarDemo.text");
 158 
 159     public Action createTextLoadAction() {
 160         return new AbstractAction("text load action") {
 161             public void actionPerformed (ActionEvent e) {
 162                 if(progressBar.getValue() < progressBar.getMaximum()) {
 163                     progressBar.setValue(progressBar.getValue() + 1);
 164                     progressTextArea.append(text.substring(textLocation, textLocation+1));
 165                     textLocation++;
 166                 } else {
 167                         timer.stop();
 168                         loadAction.setEnabled(true);
 169                         stopAction.setEnabled(false);
 170                 }
 171             }
 172         };
 173     }
 174 
 175 
 176     class MyTextArea extends JTextArea {
 177         public MyTextArea() {
 178             super(null, 0, 0);
 179             setEditable(false);
 180             setText("");
 181         }
 182 
 183         public float getAlignmentX () {
 184             return LEFT_ALIGNMENT;
 185         }
 186 
 187         public float getAlignmentY () {
 188             return TOP_ALIGNMENT;
 189         }
 190     }
 191 }