1
36
37
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58
65 public class ProgressBarDemo extends DemoModule {
66
67
70 public static void main(String[] args) {
71 ProgressBarDemo demo = new ProgressBarDemo(null);
72 demo.mainImpl();
73 }
74
75
78 public ProgressBarDemo(SwingSet2 swingset) {
79 super(swingset, "ProgressBarDemo", "toolbar/JProgressBar.gif");
82
83 createProgressPanel();
84 }
85
86 javax.swing.Timer timer;
87 Action loadAction;
88 Action stopAction;
89 JProgressBar progressBar;
90 JTextArea progressTextArea;
91
92 void updateDragEnabled(boolean dragEnabled) {
93 progressTextArea.setDragEnabled(dragEnabled);
94 }
95
96 public void createProgressPanel() {
97 getDemoPanel().setLayout(new BorderLayout());
98
99 JPanel textWrapper = new JPanel(new BorderLayout());
100 textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
101 textWrapper.setAlignmentX(LEFT_ALIGNMENT);
102 progressTextArea = new MyTextArea();
103
104 progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_name"));
105 progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_description"));
106 textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER);
107
108 getDemoPanel().add(textWrapper, BorderLayout.CENTER);
109
110 JPanel progressPanel = new JPanel();
111 getDemoPanel().add(progressPanel, BorderLayout.SOUTH);
112
113 progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) {
114 public Dimension getPreferredSize() {
115 return new Dimension(300, super.getPreferredSize().height);
116 }
117 };
118 progressBar.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_loading_progress"));
119
120 progressPanel.add(progressBar);
121 progressPanel.add(createLoadButton());
122 progressPanel.add(createStopButton());
123 }
124
125 public JButton createLoadButton() {
126 loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) {
127 public void actionPerformed(ActionEvent e) {
128 if(timer == null) {
129 loadAction.setEnabled(false);
130 stopAction.setEnabled(true);
131 if(progressBar.getValue() == progressBar.getMaximum()) {
132 progressBar.setValue(0);
134 progressTextArea.setText("");
135 textLocation = 0;
136 }
137 timer = new javax.swing.Timer(18, createTextLoadAction());
138 timer.start();
139 }
140 }
141 };
142 return createButton(loadAction);
143 }
144
145 public JButton createStopButton() {
146 stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) {
147 public void actionPerformed(ActionEvent e) {
148 if(timer != null) {
149 timer.stop();
150 timer = null;
151 }
152 loadAction.setEnabled(true);
153 stopAction.setEnabled(false);
154 }
155 };
156 return createButton(stopAction);
157 }
158
159 public JButton createButton(Action a) {
160 JButton b = new JButton();
161 b.putClientProperty("displayActionText", Boolean.TRUE);
165 b.setAction(a);
166 return b;
167 }
168
169
170 int textLocation = 0;
171
172 String text = getString("ProgressBarDemo.text");
173
174 public Action createTextLoadAction() {
175 return new AbstractAction("text load action") {
176 public void actionPerformed (ActionEvent e) {
177 if(progressBar.getValue() < progressBar.getMaximum()) {
178 progressBar.setValue(progressBar.getValue() + 1);
179 progressTextArea.append(text.substring(textLocation, textLocation+1));
180 textLocation++;
181 } else {
182 if(timer != null) {
183 timer.stop();
184 timer = null;
185 loadAction.setEnabled(true);
186 stopAction.setEnabled(false);
187 }
188 }
189 }
190 };
191 }
192
193
194 class MyTextArea extends JTextArea {
195 public MyTextArea() {
196 super(null, 0, 0);
197 setEditable(false);
198 setText("");
199 }
200
201 public float getAlignmentX () {
202 return LEFT_ALIGNMENT;
203 }
204
205 public float getAlignmentY () {
206 return TOP_ALIGNMENT;
207 }
208 }
209 }
210
211
212