home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / ProgressPanel.java < prev    next >
Text File  |  1998-02-26  |  6KB  |  185 lines

  1. /*
  2.  * @(#)ProgressPanel.java    1.5 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.text.*;
  23. import com.sun.java.accessibility.*;
  24.  
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import java.util.*;
  28.  
  29.  
  30. /**
  31.  * Demo the Progress Bar
  32.  *
  33.  * @version 1.5 02/02/98
  34.  * @author Jeff Dinkins
  35.  # @author Peter Korn (accessibility support)
  36.  */
  37. public class ProgressPanel extends JPanel 
  38. {
  39.     SwingSet swing;
  40.     JProgressBar progressBar;
  41.     JTextArea progressTextArea;
  42.     JButton loadButton;
  43.     JButton stopButton;
  44.     LoadThread loadThread;
  45.     boolean shouldStop;
  46.     Object  lock = new Object();
  47.  
  48.     public ProgressPanel(SwingSet swing) {
  49.     this.swing = swing;
  50.  
  51.     setLayout(new BorderLayout());
  52.  
  53.     JPanel textWrapper = new JPanel(new BorderLayout());
  54.     textWrapper.setBorder(swing.loweredBorder);
  55.     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
  56.     progressTextArea = new MyTextArea();
  57.     progressTextArea.getAccessibleContext().setAccessibleName("Text progressively being loaded in");
  58.     progressTextArea.getAccessibleContext().setAccessibleDescription("This JTextArea is being filled with text from a buffer progressively a character at a time while the progress bar a the bottom of the window shows the loading progress");
  59.     textWrapper.add(progressTextArea, BorderLayout.CENTER);
  60.  
  61.     add(textWrapper, BorderLayout.CENTER);
  62.  
  63.     JPanel progressPanel = new JPanel();
  64.     add(progressPanel, BorderLayout.SOUTH);
  65.  
  66.     progressBar = new JProgressBar() {
  67.         public Dimension getPreferredSize() {
  68.         return new Dimension(300, super.getPreferredSize().height);
  69.         }
  70.     };
  71.     progressBar.getAccessibleContext().setAccessibleName("Text loading progress");
  72.     progressPanel.add(progressBar);
  73.  
  74.     loadButton = new JButton("Start Loading Text");
  75.     loadButton.addActionListener(new ActionListener() {
  76.         public void actionPerformed(ActionEvent e) {
  77.         startLoading((JButton)e.getSource());
  78.         }
  79.     });
  80.     progressPanel.add(loadButton);
  81.  
  82.         stopButton = new JButton("Stop Loading Text");
  83.         stopButton.addActionListener(new ActionListener() {
  84.             public void actionPerformed(ActionEvent e) {
  85.                 stopLoading((JButton)e.getSource());
  86.             }
  87.         });
  88.         stopButton.setEnabled(false);
  89.     progressPanel.add(stopButton);
  90.     }
  91.  
  92.     public Insets getInsets() {
  93.     return new Insets(10,10,10,10);
  94.     }
  95.  
  96.     public void startLoading(JButton b) {
  97.         if(loadThread == null) {
  98.             Rectangle r;
  99.             loadThread = new LoadThread();
  100.             loadButton.setEnabled(false);
  101.             stopButton.setEnabled(true);
  102.             r = loadButton.getBounds();
  103.             r.x = 0;
  104.             r.y = 0;
  105.             loadButton.paintImmediately(r);
  106.             r = stopButton.getBounds();
  107.             r.x = 0;
  108.             r.y = 0;
  109.             stopButton.paintImmediately(r);
  110.             stopButton.requestFocus();
  111.             shouldStop = false;
  112.             loadThread.start();
  113.         }
  114.     }
  115.  
  116.     public void stopLoading(JButton b) {
  117.         synchronized(lock) {
  118.             shouldStop = true;
  119.             lock.notify();
  120.         }
  121.     }
  122.  
  123.     class LoadThread extends Thread {
  124.     int textLocation;
  125.     String text =
  126.       "      The saying goes: if an infinite number of monkeys\n" +
  127.          "   typed on an infinite number of typewriters, eventually\n" +
  128.          "   all the great works of mankind would emerge. \n\n " +
  129.       "      Now, with today's high speed computers, we can\n " +
  130.          "   finally test the theory...\n\n" +
  131.       "      Lzskd jfy 92y;ho4 th;qlh sd 6yty;q2 hnlj 8sdf. Djfy\n " +
  132.          "   92y;ho4, th;qxhz d7yty; Q0hnlj 23&^ (# ljask djf y92y;h\n " +
  133.          "   fy92y; Sd6y ty;q2h nl jk la gfa harvin garvil lasdfsd\n " +
  134.          "   a83sl la8z 2 be or not to be... that is the question. Hath\n" +
  135.      "   forth not to want a banana, or to be a banana. Banana, I \n" +
  136.      "   knew him banana. Banana banana banana.\n\n" +
  137.          "          Well... it seemed like a good idea...\n\n\n\n\n" +
  138.       "             Hi Montana!";
  139.     LoadThread() {
  140.         super();
  141.     }
  142.  
  143.     public void run () {
  144.         textLocation = 0;
  145.         progressTextArea.setText("");
  146.         progressBar.setValue(0);
  147.         progressBar.setMinimum(0);
  148.         progressBar.setMaximum(text.length());
  149.         while(progressBar.getValue() < progressBar.getMaximum() && !shouldStop) {
  150.         progressBar.setValue(progressBar.getValue() + 1);
  151.         progressTextArea.append(text.substring(textLocation, textLocation+1));
  152.         textLocation++;
  153.                 synchronized(lock) {
  154.                     if(shouldStop)
  155.                         break;
  156.                     try {
  157.                         lock.wait(20);
  158.                     } catch (java.lang.InterruptedException e) { }
  159.                 }
  160.         }
  161.             loadButton.setEnabled(true);
  162.             loadButton.repaint();
  163.             stopButton.setEnabled(false);
  164.             stopButton.repaint();
  165.             loadButton.requestFocus();
  166.             loadThread = null;
  167.     }
  168.     }
  169.  
  170.     class MyTextArea extends JTextArea {
  171.         public MyTextArea() {
  172.             super(null, 0, 0);
  173.         setEditable(false);
  174.         }
  175.  
  176.         public float getAlignmentX () {
  177.             return LEFT_ALIGNMENT;
  178.         }
  179.  
  180.         public float getAlignmentY () {
  181.             return TOP_ALIGNMENT;
  182.         }
  183.     }
  184. }
  185.