ProgressBarDemo.java revision 13978:1993af50385d
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos.progressbar;
24
25import java.awt.BorderLayout;
26import java.awt.Dimension;
27import java.awt.event.ActionEvent;
28import javax.swing.*;
29import javax.swing.border.BevelBorder;
30import javax.swing.border.SoftBevelBorder;
31
32import com.sun.swingset3.DemoProperties;
33import com.sun.swingset3.demos.ResourceManager;
34
35/**
36 * JProgressBar Demo
37 *
38 * @version 1.12 11/17/05
39 * @author Jeff Dinkins # @author Peter Korn (accessibility support)
40 */
41@DemoProperties(
42        value = "ProgressBar Demo",
43        category = "Controls",
44        description = "Demonstrates the JProgressBar, a control which displays progress to the user",
45        sourceFiles = {
46            "com/sun/swingset3/demos/progressbar/ProgressBarDemo.java",
47            "com/sun/swingset3/demos/ResourceManager.java",
48            "com/sun/swingset3/demos/progressbar/resources/ProgressBarDemo.properties",
49            "com/sun/swingset3/demos/progressbar/resources/images/ProgressBarDemo.gif"
50        }
51)
52public class ProgressBarDemo extends JPanel {
53
54    private static final ResourceManager resourceManager = new ResourceManager(ProgressBarDemo.class);
55    public static final String STOP_BUTTON = resourceManager.getString("ProgressBarDemo.stop_button");
56    public static final String START_BUTTON = resourceManager.getString("ProgressBarDemo.start_button");
57    public static final String DEMO_TITLE = ProgressBarDemo.class.getAnnotation(DemoProperties.class).value();
58
59    /**
60     * main method allows us to run as a standalone demo.
61     *
62     * @param args
63     */
64    public static void main(String[] args) {
65        JFrame frame = new JFrame(DEMO_TITLE);
66
67        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68        frame.getContentPane().add(new ProgressBarDemo());
69        frame.setPreferredSize(new Dimension(800, 600));
70        frame.pack();
71        frame.setLocationRelativeTo(null);
72        frame.setVisible(true);
73    }
74
75    /**
76     * ProgressBarDemo Constructor
77     */
78    public ProgressBarDemo() {
79        createProgressPanel();
80    }
81
82    private final javax.swing.Timer timer = new javax.swing.Timer(18, createTextLoadAction());
83    private Action loadAction;
84    private Action stopAction;
85    private JProgressBar progressBar;
86    private JTextArea progressTextArea;
87
88    private void createProgressPanel() {
89        setLayout(new BorderLayout());
90
91        JPanel textWrapper = new JPanel(new BorderLayout());
92        textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
93        textWrapper.setAlignmentX(LEFT_ALIGNMENT);
94        progressTextArea = new MyTextArea();
95
96        progressTextArea.getAccessibleContext().setAccessibleName(
97                resourceManager.getString("ProgressBarDemo.accessible_text_area_name"));
98        progressTextArea.getAccessibleContext().setAccessibleDescription(
99                resourceManager.getString("ProgressBarDemo.accessible_text_area_description"));
100        textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);
101
102        add(textWrapper, BorderLayout.CENTER);
103
104        JPanel progressPanel = new JPanel();
105        add(progressPanel, BorderLayout.SOUTH);
106
107        progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {
108            @Override
109            public Dimension getPreferredSize() {
110                return new Dimension(300, super.getPreferredSize().height);
111            }
112        };
113        progressBar.getAccessibleContext().setAccessibleName(
114                resourceManager.getString("ProgressBarDemo.accessible_text_loading_progress"));
115
116        progressPanel.add(progressBar);
117        progressPanel.add(createLoadButton());
118        progressPanel.add(createStopButton());
119    }
120
121    private JButton createLoadButton() {
122        loadAction = new AbstractAction(START_BUTTON) {
123            @Override
124            public void actionPerformed(ActionEvent e) {
125                loadAction.setEnabled(false);
126                stopAction.setEnabled(true);
127                if (progressBar.getValue() == progressBar.getMaximum()) {
128                    progressBar.setValue(0);
129                    textLocation = 0;
130                    progressTextArea.setText("");
131                }
132                timer.start();
133            }
134        };
135        return createButton(loadAction);
136    }
137
138    private JButton createStopButton() {
139        stopAction = new AbstractAction(STOP_BUTTON) {
140            @Override
141            public void actionPerformed(ActionEvent e) {
142                timer.stop();
143                loadAction.setEnabled(true);
144                stopAction.setEnabled(false);
145            }
146        };
147        return createButton(stopAction);
148    }
149
150    private static JButton createButton(Action a) {
151        JButton b = new JButton();
152        // setting the following client property informs the button to show
153        // the action text as it's name. The default is to not show the
154        // action text.
155        b.putClientProperty("displayActionText", Boolean.TRUE);
156        b.setAction(a);
157        return b;
158    }
159
160    private int textLocation = 0;
161
162    private final String text = resourceManager.getString("ProgressBarDemo.text");
163
164    private Action createTextLoadAction() {
165        return new AbstractAction("text load action") {
166            @Override
167            public void actionPerformed(ActionEvent e) {
168                if (progressBar.getValue() < progressBar.getMaximum()) {
169                    progressBar.setValue(progressBar.getValue() + 1);
170                    progressTextArea.append(text.substring(textLocation, textLocation + 1));
171                    textLocation++;
172                } else {
173                    timer.stop();
174                    loadAction.setEnabled(true);
175                    stopAction.setEnabled(false);
176                }
177            }
178        };
179    }
180
181    private static class MyTextArea extends JTextArea {
182
183        private MyTextArea() {
184            super(null, 0, 0);
185            setEditable(false);
186            setText("");
187        }
188
189        @Override
190        public float getAlignmentX() {
191            return LEFT_ALIGNMENT;
192        }
193
194        @Override
195        public float getAlignmentY() {
196            return TOP_ALIGNMENT;
197        }
198    }
199}
200