Path: blob/master/src/demo/share/jfc/SwingSet2/ProgressBarDemo.java
41152 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* JProgressBar Demo51*52* @author Jeff Dinkins53# @author Peter Korn (accessibility support)54*/55public class ProgressBarDemo extends DemoModule {5657/**58* main method allows us to run as a standalone demo.59*/60public static void main(String[] args) {61ProgressBarDemo demo = new ProgressBarDemo(null);62demo.mainImpl();63}6465/**66* ProgressBarDemo Constructor67*/68public ProgressBarDemo(SwingSet2 swingset) {69// Set the title for this demo, and an icon used to represent this70// demo inside the SwingSet2 app.71super(swingset, "ProgressBarDemo", "toolbar/JProgressBar.gif");7273createProgressPanel();74}7576javax.swing.Timer timer = new javax.swing.Timer(18, createTextLoadAction());77Action loadAction;78Action stopAction;79JProgressBar progressBar;80JTextArea progressTextArea;8182void updateDragEnabled(boolean dragEnabled) {83progressTextArea.setDragEnabled(dragEnabled);84}8586public void createProgressPanel() {87getDemoPanel().setLayout(new BorderLayout());8889JPanel textWrapper = new JPanel(new BorderLayout());90textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));91textWrapper.setAlignmentX(LEFT_ALIGNMENT);92progressTextArea = new MyTextArea();9394progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_name"));95progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_description"));96textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);9798getDemoPanel().add(textWrapper, BorderLayout.CENTER);99100JPanel progressPanel = new JPanel();101getDemoPanel().add(progressPanel, BorderLayout.SOUTH);102103progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {104public Dimension getPreferredSize() {105return new Dimension(300, super.getPreferredSize().height);106}107};108progressBar.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_loading_progress"));109110progressPanel.add(progressBar);111progressPanel.add(createLoadButton());112progressPanel.add(createStopButton());113}114115public JButton createLoadButton() {116loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) {117public void actionPerformed(ActionEvent e) {118loadAction.setEnabled(false);119stopAction.setEnabled(true);120if (progressBar.getValue() == progressBar.getMaximum()) {121progressBar.setValue(0);122textLocation = 0;123progressTextArea.setText("");124}125timer.start();126}127};128return createButton(loadAction);129}130131public JButton createStopButton() {132stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {133public void actionPerformed(ActionEvent e) {134timer.stop();135loadAction.setEnabled(true);136stopAction.setEnabled(false);137}138};139return createButton(stopAction);140}141142public JButton createButton(Action a) {143JButton b = new JButton();144// setting the following client property informs the button to show145// the action text as it's name. The default is to not show the146// action text.147b.putClientProperty("displayActionText", Boolean.TRUE);148b.setAction(a);149return b;150}151152153int textLocation = 0;154155String text = getString("ProgressBarDemo.text");156157public Action createTextLoadAction() {158return new AbstractAction("text load action") {159public void actionPerformed (ActionEvent e) {160if(progressBar.getValue() < progressBar.getMaximum()) {161progressBar.setValue(progressBar.getValue() + 1);162progressTextArea.append(text.substring(textLocation, textLocation+1));163textLocation++;164} else {165timer.stop();166loadAction.setEnabled(true);167stopAction.setEnabled(false);168}169}170};171}172173174class MyTextArea extends JTextArea {175public MyTextArea() {176super(null, 0, 0);177setEditable(false);178setText("");179}180181public float getAlignmentX () {182return LEFT_ALIGNMENT;183}184185public float getAlignmentY () {186return TOP_ALIGNMENT;187}188}189}190191192