ProgressBarDemo.java revision 14246:0be735572230
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.getContentPane().add(new ProgressBarDemo());
68        frame.setPreferredSize(new Dimension(800, 600));
69        frame.pack();
70        frame.setLocationRelativeTo(null);
71        frame.setVisible(true);
72    }
73
74    /**
75     * ProgressBarDemo Constructor
76     */
77    public ProgressBarDemo() {
78        createProgressPanel();
79    }
80
81    private final javax.swing.Timer timer = new javax.swing.Timer(18, createTextLoadAction());
82    private Action loadAction;
83    private Action stopAction;
84    private JProgressBar progressBar;
85    private JTextArea progressTextArea;
86
87    private void createProgressPanel() {
88        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(
96                resourceManager.getString("ProgressBarDemo.accessible_text_area_name"));
97        progressTextArea.getAccessibleContext().setAccessibleDescription(
98                resourceManager.getString("ProgressBarDemo.accessible_text_area_description"));
99        textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);
100
101        add(textWrapper, BorderLayout.CENTER);
102
103        JPanel progressPanel = new JPanel();
104        add(progressPanel, BorderLayout.SOUTH);
105
106        progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {
107            @Override
108            public Dimension getPreferredSize() {
109                return new Dimension(300, super.getPreferredSize().height);
110            }
111        };
112        progressBar.getAccessibleContext().setAccessibleName(
113                resourceManager.getString("ProgressBarDemo.accessible_text_loading_progress"));
114
115        progressPanel.add(progressBar);
116        progressPanel.add(createLoadButton());
117        progressPanel.add(createStopButton());
118    }
119
120    private JButton createLoadButton() {
121        loadAction = new AbstractAction(START_BUTTON) {
122            @Override
123            public void actionPerformed(ActionEvent e) {
124                loadAction.setEnabled(false);
125                stopAction.setEnabled(true);
126                if (progressBar.getValue() == progressBar.getMaximum()) {
127                    progressBar.setValue(0);
128                    textLocation = 0;
129                    progressTextArea.setText("");
130                }
131                timer.start();
132            }
133        };
134        return createButton(loadAction);
135    }
136
137    private JButton createStopButton() {
138        stopAction = new AbstractAction(STOP_BUTTON) {
139            @Override
140            public void actionPerformed(ActionEvent e) {
141                timer.stop();
142                loadAction.setEnabled(true);
143                stopAction.setEnabled(false);
144            }
145        };
146        return createButton(stopAction);
147    }
148
149    private static JButton createButton(Action a) {
150        JButton b = new JButton();
151        // setting the following client property informs the button to show
152        // the action text as it's name. The default is to not show the
153        // action text.
154        b.putClientProperty("displayActionText", Boolean.TRUE);
155        b.setAction(a);
156        return b;
157    }
158
159    private int textLocation = 0;
160
161    private final String text = resourceManager.getString("ProgressBarDemo.text");
162
163    private Action createTextLoadAction() {
164        return new AbstractAction("text load action") {
165            @Override
166            public void actionPerformed(ActionEvent e) {
167                if (progressBar.getValue() < progressBar.getMaximum()) {
168                    progressBar.setValue(progressBar.getValue() + 1);
169                    progressTextArea.append(text.substring(textLocation, textLocation + 1));
170                    textLocation++;
171                } else {
172                    timer.stop();
173                    loadAction.setEnabled(true);
174                    stopAction.setEnabled(false);
175                }
176            }
177        };
178    }
179
180    private static class MyTextArea extends JTextArea {
181
182        private MyTextArea() {
183            super(null, 0, 0);
184            setEditable(false);
185            setText("");
186        }
187
188        @Override
189        public float getAlignmentX() {
190            return LEFT_ALIGNMENT;
191        }
192
193        @Override
194        public float getAlignmentY() {
195            return TOP_ALIGNMENT;
196        }
197    }
198}
199