001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.awt.Component;
021: import java.awt.IllegalComponentStateException;
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.WindowAdapter;
026: import java.awt.event.WindowEvent;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.util.Locale;
030: import javax.accessibility.Accessible;
031: import javax.accessibility.AccessibleComponent;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleRole;
034: import javax.accessibility.AccessibleStateSet;
035: import javax.accessibility.AccessibleText;
036: import javax.accessibility.AccessibleValue;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039: import javax.swing.text.AttributeSet;
040: import org.apache.harmony.luni.util.NotImplementedException;
041:
042: /**
043: * <p>
044: * <i>ProgressMonitor</i>
045: * </p>
046: * <h3>Implementation Notes:</h3>
047: * <ul>
048: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
049: * optimization, not as a guarantee of serialization compatibility.</li>
050: * </ul>
051: */
052: public class ProgressMonitor implements Accessible {
053: protected class AccessibleProgressMonitor extends AccessibleContext
054: implements AccessibleText, ChangeListener,
055: PropertyChangeListener {
056: protected AccessibleProgressMonitor() {
057: }
058:
059: public void stateChanged(ChangeEvent e)
060: throws NotImplementedException {
061: throw new NotImplementedException();
062: }
063:
064: public void propertyChange(PropertyChangeEvent e)
065: throws NotImplementedException {
066: throw new NotImplementedException();
067: }
068:
069: @Override
070: public String getAccessibleName()
071: throws NotImplementedException {
072: throw new NotImplementedException();
073: }
074:
075: @Override
076: public String getAccessibleDescription()
077: throws NotImplementedException {
078: throw new NotImplementedException();
079: }
080:
081: @Override
082: public AccessibleRole getAccessibleRole()
083: throws NotImplementedException {
084: throw new NotImplementedException();
085: }
086:
087: @Override
088: public AccessibleStateSet getAccessibleStateSet()
089: throws NotImplementedException {
090: throw new NotImplementedException();
091: }
092:
093: @Override
094: public Accessible getAccessibleParent()
095: throws NotImplementedException {
096: throw new NotImplementedException();
097: }
098:
099: @Override
100: public int getAccessibleIndexInParent()
101: throws NotImplementedException {
102: throw new NotImplementedException();
103: }
104:
105: @Override
106: public int getAccessibleChildrenCount()
107: throws NotImplementedException {
108: throw new NotImplementedException();
109: }
110:
111: @Override
112: public Accessible getAccessibleChild(int i)
113: throws NotImplementedException {
114: throw new NotImplementedException();
115: }
116:
117: @Override
118: public Locale getLocale()
119: throws IllegalComponentStateException,
120: NotImplementedException {
121: throw new NotImplementedException();
122: }
123:
124: @Override
125: public AccessibleComponent getAccessibleComponent()
126: throws NotImplementedException {
127: throw new NotImplementedException();
128: }
129:
130: @Override
131: public AccessibleValue getAccessibleValue()
132: throws NotImplementedException {
133: throw new NotImplementedException();
134: }
135:
136: @Override
137: public AccessibleText getAccessibleText()
138: throws NotImplementedException {
139: throw new NotImplementedException();
140: }
141:
142: public int getIndexAtPoint(Point p)
143: throws NotImplementedException {
144: throw new NotImplementedException();
145: }
146:
147: public Rectangle getCharacterBounds(int i)
148: throws NotImplementedException {
149: throw new NotImplementedException();
150: }
151:
152: public int getCharCount() throws NotImplementedException {
153: throw new NotImplementedException();
154: }
155:
156: public int getCaretPosition() throws NotImplementedException {
157: throw new NotImplementedException();
158: }
159:
160: public String getAtIndex(int part, int index)
161: throws NotImplementedException {
162: throw new NotImplementedException();
163: }
164:
165: public String getAfterIndex(int part, int index)
166: throws NotImplementedException {
167: throw new NotImplementedException();
168: }
169:
170: public String getBeforeIndex(int part, int index)
171: throws NotImplementedException {
172: throw new NotImplementedException();
173: }
174:
175: public AttributeSet getCharacterAttribute(int i)
176: throws NotImplementedException {
177: throw new NotImplementedException();
178: }
179:
180: public int getSelectionStart() throws NotImplementedException {
181: throw new NotImplementedException();
182: }
183:
184: public int getSelectionEnd() throws NotImplementedException {
185: throw new NotImplementedException();
186: }
187:
188: public String getSelectedText() throws NotImplementedException {
189: throw new NotImplementedException();
190: }
191: }
192:
193: private Action cancelAction = new AbstractAction() {
194: private static final long serialVersionUID = 1L;
195:
196: public void actionPerformed(ActionEvent e) {
197: close();
198: isCancelled = true;
199: }
200: };
201:
202: protected AccessibleContext accessibleContext;
203:
204: private static final int DEFAULT_MILLIS_TO_DECIDE = 500;
205:
206: private static final int DEFAULT_MILLIS_TO_POPUP = 2000;
207:
208: private int millisToDecideToPopup = DEFAULT_MILLIS_TO_DECIDE;
209:
210: private int millisToPopup = DEFAULT_MILLIS_TO_POPUP;
211:
212: private Timer shouldShowTimer;
213:
214: private int max;
215:
216: private int min;
217:
218: private int progress;
219:
220: private JProgressBar progressBar;
221:
222: private JDialog progressDialog;
223:
224: private Component parentComponent;
225:
226: private JLabel noteLabel;
227:
228: private Object message;
229:
230: private boolean shouldShow;
231:
232: private boolean isCancelled;
233:
234: public ProgressMonitor(Component parentComponent, Object message,
235: String note, int min, int max) {
236: this .parentComponent = parentComponent;
237: this .message = message;
238: noteLabel = new JLabel(note);
239: this .min = min;
240: progress = min;
241: this .max = max;
242: startShouldShowTimer();
243: }
244:
245: public void setProgress(int progress) {
246: int oldProgress = this .progress;
247: if (progress >= max) {
248: close();
249: } else if (progress > this .progress) {
250: this .progress = progress;
251: if (shouldShow && !isCancelled
252: && progressVisible(oldProgress, progress)) {
253: if (progressDialog == null) {
254: showDialog();
255: }
256: progressBar.setValue(progress);
257: }
258: }
259: }
260:
261: public void close() {
262: if (progressDialog != null) {
263: progressDialog.dispose();
264: progressDialog = null;
265: }
266: }
267:
268: public int getMinimum() {
269: return min;
270: }
271:
272: public void setMinimum(int min) {
273: this .min = min;
274: if (progressBar != null) {
275: progressBar.setMinimum(min);
276: }
277: }
278:
279: public int getMaximum() {
280: return max;
281: }
282:
283: public void setMaximum(int max) {
284: this .max = max;
285: if (progressBar != null) {
286: progressBar.setMaximum(max);
287: }
288: }
289:
290: public boolean isCanceled() {
291: return isCancelled;
292: }
293:
294: public void setMillisToDecideToPopup(int millisToDecideToPopup) {
295: this .millisToDecideToPopup = millisToDecideToPopup;
296: if (shouldShowTimer != null) {
297: shouldShowTimer.setDelay(millisToDecideToPopup);
298: }
299: }
300:
301: public int getMillisToDecideToPopup() {
302: return millisToDecideToPopup;
303: }
304:
305: public void setMillisToPopup(int millisToPopup) {
306: this .millisToPopup = millisToPopup;
307: }
308:
309: public int getMillisToPopup() {
310: return millisToPopup;
311: }
312:
313: public void setNote(String note) {
314: noteLabel.setText(note);
315: }
316:
317: public String getNote() {
318: return noteLabel.getText();
319: }
320:
321: public AccessibleContext getAccessibleContext() {
322: if (accessibleContext == null) {
323: accessibleContext = new AccessibleProgressMonitor();
324: }
325: return accessibleContext;
326: }
327:
328: private void startShouldShowTimer() {
329: shouldShow = false;
330: if (shouldShowTimer == null) {
331: shouldShowTimer = new Timer(millisToDecideToPopup,
332: new AbstractAction() {
333: private static final long serialVersionUID = 1L;
334:
335: public void actionPerformed(ActionEvent e) {
336: shouldShow = (max - min)
337: * millisToDecideToPopup > millisToPopup
338: * (progress - min);
339: }
340: });
341: }
342: shouldShowTimer.setRepeats(false);
343: shouldShowTimer.restart();
344: }
345:
346: private void showDialog() {
347: progressBar = new JProgressBar(min, max);
348: JButton cancelButton = new JButton();
349: cancelButton.setAction(cancelAction);
350: cancelButton.setText(UIManager
351: .getString("OptionPane.cancelButtonText"));
352: cancelButton.setMnemonic(UIManager
353: .getInt("OptionPane.cancelButtonMnemonic"));
354: Object[] topPanel = { message, noteLabel, progressBar };
355: Object[] bottomPanel = { cancelButton };
356: JOptionPane dialogCreator = new JOptionPane(topPanel,
357: JOptionPane.INFORMATION_MESSAGE,
358: JOptionPane.DEFAULT_OPTION, null, bottomPanel);
359: progressDialog = dialogCreator.createDialog(parentComponent,
360: UIManager.getString("ProgressMonitor.progressText"));
361: progressDialog.setModal(false);
362: progressDialog.addWindowListener(new WindowAdapter() {
363: @Override
364: public void windowClosing(WindowEvent e) {
365: cancelAction.actionPerformed(null);
366: }
367: });
368: progressDialog.setVisible(true);
369: }
370:
371: private boolean progressVisible(int oldProgress, int newProgress) {
372: return (newProgress - oldProgress) * 100 >= (max - min);
373: }
374: }
|