KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ProgressMonitor


1 /*
2  * @(#)ProgressMonitor.java 1.30 04/04/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9
10 package javax.swing;
11
12
13
14 import java.io.*;
15 import java.awt.BorderLayout JavaDoc;
16 import java.awt.Frame JavaDoc;
17 import java.awt.Dialog JavaDoc;
18 import java.awt.Window JavaDoc;
19 import java.awt.Component JavaDoc;
20 import java.awt.Container JavaDoc;
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.awt.event.WindowListener JavaDoc;
24 import java.awt.event.WindowAdapter JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26
27 import java.awt.IllegalComponentStateException JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.text.*;
31 import java.util.Locale JavaDoc;
32 import javax.accessibility.*;
33 import javax.swing.event.*;
34 import javax.swing.text.*;
35
36
37 /** A class to monitor the progress of some operation. If it looks
38  * like the operation will take a while, a progress dialog will be popped up.
39  * When the ProgressMonitor is created it is given a numeric range and a
40  * descriptive string. As the operation progresses, call the setProgress method
41  * to indicate how far along the [min,max] range the operation is.
42  * Initially, there is no ProgressDialog. After the first millisToDecideToPopup
43  * milliseconds (default 500) the progress monitor will predict how long
44  * the operation will take. If it is longer than millisToPopup (default 2000,
45  * 2 seconds) a ProgressDialog will be popped up.
46  * <p>
47  * From time to time, when the Dialog box is visible, the progress bar will
48  * be updated when setProgress is called. setProgress won't always update
49  * the progress bar, it will only be done if the amount of progress is
50  * visibly significant.
51  *
52  * <p>
53  *
54  * For further documentation and examples see
55  * <a
56  href="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html">How to Monitor Progress</a>,
57  * a section in <em>The Java Tutorial.</em>
58  *
59  * @see ProgressMonitorInputStream
60  * @author James Gosling
61  * @author Lynn Monsanto (accessibility)
62  * @version 1.30 04/15/04
63  */

64 public class ProgressMonitor extends Object JavaDoc implements Accessible
65 {
66     private ProgressMonitor JavaDoc root;
67     private JDialog JavaDoc dialog;
68     private JOptionPane JavaDoc pane;
69     private JProgressBar JavaDoc myBar;
70     private JLabel JavaDoc noteLabel;
71     private Component JavaDoc parentComponent;
72     private String JavaDoc note;
73     private Object JavaDoc[] cancelOption = null;
74     private Object JavaDoc message;
75     private long T0;
76     private int millisToDecideToPopup = 500;
77     private int millisToPopup = 2000;
78     private int min;
79     private int max;
80     private int v;
81     private int lastDisp;
82     private int reportDelta;
83
84
85     /**
86      * Constructs a graphic object that shows progress, typically by filling
87      * in a rectangular bar as the process nears completion.
88      *
89      * @param parentComponent the parent component for the dialog box
90      * @param message a descriptive message that will be shown
91      * to the user to indicate what operation is being monitored.
92      * This does not change as the operation progresses.
93      * See the message parameters to methods in
94      * {@link JOptionPane#message}
95      * for the range of values.
96      * @param note a short note describing the state of the
97      * operation. As the operation progresses, you can call
98      * setNote to change the note displayed. This is used,
99      * for example, in operations that iterate through a
100      * list of files to show the name of the file being processes.
101      * If note is initially null, there will be no note line
102      * in the dialog box and setNote will be ineffective
103      * @param min the lower bound of the range
104      * @param max the upper bound of the range
105      * @see JDialog
106      * @see JOptionPane
107      */

108     public ProgressMonitor(Component JavaDoc parentComponent,
109                            Object JavaDoc message,
110                            String JavaDoc note,
111                            int min,
112                            int max) {
113         this(parentComponent, message, note, min, max, null);
114     }
115
116
117     private ProgressMonitor(Component JavaDoc parentComponent,
118                             Object JavaDoc message,
119                             String JavaDoc note,
120                             int min,
121                             int max,
122                             ProgressMonitor JavaDoc group) {
123         this.min = min;
124         this.max = max;
125         this.parentComponent = parentComponent;
126
127         cancelOption = new Object JavaDoc[1];
128         cancelOption[0] = UIManager.getString("OptionPane.cancelButtonText");
129
130         reportDelta = (max - min) / 100;
131         if (reportDelta < 1) reportDelta = 1;
132         v = min;
133         this.message = message;
134         this.note = note;
135         if (group != null) {
136             root = (group.root != null) ? group.root : group;
137             T0 = root.T0;
138             dialog = root.dialog;
139         }
140         else {
141             T0 = System.currentTimeMillis();
142         }
143     }
144
145
146     private class ProgressOptionPane extends JOptionPane JavaDoc
147     {
148         ProgressOptionPane(Object JavaDoc messageList) {
149             super(messageList,
150                   JOptionPane.INFORMATION_MESSAGE,
151                   JOptionPane.DEFAULT_OPTION,
152                   null,
153                   ProgressMonitor.this.cancelOption,
154                   null);
155         }
156
157
158         public int getMaxCharactersPerLineCount() {
159             return 60;
160         }
161
162
163         // Equivalent to JOptionPane.createDialog,
164
// but create a modeless dialog.
165
// This is necessary because the Solaris implementation doesn't
166
// support Dialog.setModal yet.
167
public JDialog JavaDoc createDialog(Component JavaDoc parentComponent, String JavaDoc title) {
168             final JDialog JavaDoc dialog;
169         
170         Window JavaDoc window = JOptionPane.getWindowForComponent(parentComponent);
171         if (window instanceof Frame JavaDoc) {
172         dialog = new JDialog JavaDoc((Frame JavaDoc)window, title, false);
173         } else {
174         dialog = new JDialog JavaDoc((Dialog JavaDoc)window, title, false);
175         }
176         if (window instanceof SwingUtilities.SharedOwnerFrame JavaDoc) {
177         WindowListener JavaDoc ownerShutdownListener =
178             (WindowListener JavaDoc)SwingUtilities.getSharedOwnerFrameShutdownListener();
179         dialog.addWindowListener(ownerShutdownListener);
180         }
181             Container JavaDoc contentPane = dialog.getContentPane();
182
183             contentPane.setLayout(new BorderLayout JavaDoc());
184             contentPane.add(this, BorderLayout.CENTER);
185             dialog.pack();
186             dialog.setLocationRelativeTo(parentComponent);
187             dialog.addWindowListener(new WindowAdapter JavaDoc() {
188                 boolean gotFocus = false;
189
190                 public void windowClosing(WindowEvent JavaDoc we) {
191                     setValue(cancelOption[0]);
192                 }
193
194                 public void windowActivated(WindowEvent JavaDoc we) {
195                     // Once window gets focus, set initial focus
196
if (!gotFocus) {
197                         selectInitialValue();
198                         gotFocus = true;
199                     }
200                 }
201             });
202
203             addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
204                 public void propertyChange(PropertyChangeEvent JavaDoc event) {
205                     if(dialog.isVisible() &&
206                        event.getSource() == ProgressOptionPane.this &&
207                        (event.getPropertyName().equals(VALUE_PROPERTY) ||
208                         event.getPropertyName().equals(INPUT_VALUE_PROPERTY))){
209                         dialog.setVisible(false);
210                         dialog.dispose();
211                     }
212                 }
213             });
214
215             return dialog;
216         }
217         
218     /////////////////
219
// Accessibility support for ProgressOptionPane
220
////////////////
221

222     /**
223      * Gets the AccessibleContext for the ProgressOptionPane
224      *
225      * @return the AccessibleContext for the ProgressOptionPane
226      * @since 1.5
227      */

228     public AccessibleContext getAccessibleContext() {
229         return ProgressMonitor.this.getAccessibleContext();
230     }
231
232     /*
233      * Returns the AccessibleJOptionPane
234      */

235     private AccessibleContext getAccessibleJOptionPane() {
236         return super.getAccessibleContext();
237     }
238     }
239
240
241     /**
242      * Indicate the progress of the operation being monitored.
243      * If the specified value is >= the maximum, the progress
244      * monitor is closed.
245      * @param nv an int specifying the current value, between the
246      * maximum and minimum specified for this component
247      * @see #setMinimum
248      * @see #setMaximum
249      * @see #close
250      */

251     public void setProgress(int nv) {
252         v = nv;
253         if (nv >= max) {
254             close();
255         }
256         else if (nv >= lastDisp + reportDelta) {
257             lastDisp = nv;
258             if (myBar != null) {
259                 myBar.setValue(nv);
260             }
261             else {
262                 long T = System.currentTimeMillis();
263                 long dT = (int)(T-T0);
264                 if (dT >= millisToDecideToPopup) {
265                     int predictedCompletionTime;
266                     if (nv > min) {
267                         predictedCompletionTime = (int)((long)dT *
268                                                         (max - min) /
269                                                         (nv - min));
270                     }
271                     else {
272                         predictedCompletionTime = millisToPopup;
273                     }
274                     if (predictedCompletionTime >= millisToPopup) {
275                         myBar = new JProgressBar JavaDoc();
276                         myBar.setMinimum(min);
277                         myBar.setMaximum(max);
278                         myBar.setValue(nv);
279                         if (note != null) noteLabel = new JLabel JavaDoc(note);
280                         pane = new ProgressOptionPane(new Object JavaDoc[] {message,
281                                                                     noteLabel,
282                                                                     myBar});
283                         dialog = pane.createDialog(parentComponent,
284                             UIManager.getString(
285                                 "ProgressMonitor.progressText"));
286                         dialog.show();
287                     }
288                 }
289             }
290         }
291     }
292
293
294     /**
295      * Indicate that the operation is complete. This happens automatically
296      * when the value set by setProgress is >= max, but it may be called
297      * earlier if the operation ends early.
298      */

299     public void close() {
300         if (dialog != null) {
301             dialog.setVisible(false);
302             dialog.dispose();
303             dialog = null;
304             pane = null;
305             myBar = null;
306         }
307     }
308
309
310     /**
311      * Returns the minimum value -- the lower end of the progress value.
312      *
313      * @return an int representing the minimum value
314      * @see #setMinimum
315      */

316     public int getMinimum() {
317         return min;
318     }
319
320
321     /**
322      * Specifies the minimum value.
323      *
324      * @param m an int specifying the minimum value
325      * @see #getMinimum
326      */

327     public void setMinimum(int m) {
328         min = m;
329     }
330
331
332     /**
333      * Returns the maximum value -- the higher end of the progress value.
334      *
335      * @return an int representing the maximum value
336      * @see #setMaximum
337      */

338     public int getMaximum() {
339         return max;
340     }
341
342
343     /**
344      * Specifies the maximum value.
345      *
346      * @param m an int specifying the maximum value
347      * @see #getMaximum
348      */

349     public void setMaximum(int m) {
350         max = m;
351     }
352
353
354     /**
355      * Returns true if the user hits the Cancel button in the progress dialog.
356      */

357     public boolean isCanceled() {
358         if (pane == null) return false;
359         Object JavaDoc v = pane.getValue();
360         return ((v != null) &&
361                 (cancelOption.length == 1) &&
362                 (v.equals(cancelOption[0])));
363     }
364
365
366     /**
367      * Specifies the amount of time to wait before deciding whether or
368      * not to popup a progress monitor.
369      *
370      * @param millisToDecideToPopup an int specifying the time to wait,
371      * in milliseconds
372      * @see #getMillisToDecideToPopup
373      */

374     public void setMillisToDecideToPopup(int millisToDecideToPopup) {
375         this.millisToDecideToPopup = millisToDecideToPopup;
376     }
377
378
379     /**
380      * Returns the amount of time this object waits before deciding whether
381      * or not to popup a progress monitor.
382      *
383      * @see #setMillisToDecideToPopup
384      */

385     public int getMillisToDecideToPopup() {
386         return millisToDecideToPopup;
387     }
388
389
390     /**
391      * Specifies the amount of time it will take for the popup to appear.
392      * (If the predicted time remaining is less than this time, the popup
393      * won't be displayed.)
394      *
395      * @param millisToPopup an int specifying the time in milliseconds
396      * @see #getMillisToPopup
397      */

398     public void setMillisToPopup(int millisToPopup) {
399         this.millisToPopup = millisToPopup;
400     }
401
402
403     /**
404      * Returns the amount of time it will take for the popup to appear.
405      *
406      * @see #setMillisToPopup
407      */

408     public int getMillisToPopup() {
409         return millisToPopup;
410     }
411
412
413     /**
414      * Specifies the additional note that is displayed along with the
415      * progress message. Used, for example, to show which file the
416      * is currently being copied during a multiple-file copy.
417      *
418      * @param note a String specifying the note to display
419      * @see #getNote
420      */

421     public void setNote(String JavaDoc note) {
422         this.note = note;
423         if (noteLabel != null) {
424             noteLabel.setText(note);
425         }
426     }
427
428
429     /**
430      * Specifies the additional note that is displayed along with the
431      * progress message.
432      *
433      * @return a String specifying the note to display
434      * @see #setNote
435      */

436     public String JavaDoc getNote() {
437         return note;
438     }
439
440     /////////////////
441
// Accessibility support
442
////////////////
443

444     /*
445      * The <code>AccessibleContext</code> for the <code>ProgressMonitor</code>
446      * @since 1.5
447      */

448     protected AccessibleContext accessibleContext = null;
449
450     private AccessibleContext accessibleJOptionPane = null;
451
452     /**
453      * Gets the <code>AccessibleContext</code> for the
454      * <code>ProgressMonitor</code>
455      *
456      * @return the <code>AccessibleContext</code> for the
457      * <code>ProgressMonitor</code>
458      * @since 1.5
459      */

460     public AccessibleContext getAccessibleContext() {
461     if (accessibleContext == null) {
462         accessibleContext = new AccessibleProgressMonitor();
463     }
464     if (pane != null && accessibleJOptionPane == null) {
465         // Notify the AccessibleProgressMonitor that the
466
// ProgressOptionPane was created. It is necessary
467
// to poll for ProgressOptionPane creation because
468
// the ProgressMonitor does not have a Component
469
// to add a listener to until the ProgressOptionPane
470
// is created.
471
if (accessibleContext instanceof AccessibleProgressMonitor) {
472         ((AccessibleProgressMonitor)accessibleContext).optionPaneCreated();
473         }
474     }
475     return accessibleContext;
476     }
477     
478     /**
479      * <code>AccessibleProgressMonitor</code> implements accessibility
480      * support for the <code>ProgressMonitor</code> class.
481      * @since 1.5
482      */

483     protected class AccessibleProgressMonitor extends AccessibleContext
484         implements AccessibleText, ChangeListener JavaDoc, PropertyChangeListener JavaDoc {
485
486     /*
487      * The accessibility hierarchy for ProgressMonitor is a flattened
488      * version of the ProgressOptionPane component hierarchy.
489      *
490      * The ProgressOptionPane component hierarchy is:
491      * JDialog
492      * ProgressOptionPane
493      * JPanel
494      * JPanel
495      * JLabel
496      * JLabel
497      * JProgressBar
498      *
499      * The AccessibleProgessMonitor accessibility hierarchy is:
500      * AccessibleJDialog
501      * AccessibleProgressMonitor
502      * AccessibleJLabel
503      * AccessibleJLabel
504      * AccessibleJProgressBar
505      *
506      * The abstraction presented to assitive technologies by
507      * the AccessibleProgressMonitor is that a dialog contains a
508      * progress monitor with three children: a message, a note
509      * label and a progress bar.
510      */

511
512     private Object JavaDoc oldModelValue;
513
514     /**
515      * AccessibleProgressMonitor constructor
516      */

517     protected AccessibleProgressMonitor() {
518     }
519
520     /*
521      * Initializes the AccessibleContext now that the ProgressOptionPane
522      * has been created. Because the ProgressMonitor is not a Component
523      * implementing the Accessible interface, an AccessibleContext
524      * must be synthesized from the ProgressOptionPane and its children.
525      *
526      * For other AWT and Swing classes, the inner class that implements
527      * accessibility for the class extends the inner class that implements
528      * implements accessibility for the super class. AccessibleProgressMonitor
529      * cannot extend AccessibleJOptionPane and must therefore delegate calls
530      * to the AccessibleJOptionPane.
531      */

532     private void optionPaneCreated() {
533         accessibleJOptionPane =
534         ((ProgressOptionPane)pane).getAccessibleJOptionPane();
535
536         // add a listener for progress bar ChangeEvents
537
if (myBar != null) {
538         myBar.addChangeListener(this);
539         }
540
541         // add a listener for note label PropertyChangeEvents
542
if (noteLabel != null) {
543         noteLabel.addPropertyChangeListener(this);
544         }
545     }
546
547         /**
548          * Invoked when the target of the listener has changed its state.
549          *
550          * @param e a <code>ChangeEvent</code> object. Must not be null.
551      * @throws NullPointerException if the parameter is null.
552      */

553     public void stateChanged(ChangeEvent JavaDoc e) {
554             if (e == null) {
555                 return;
556             }
557         if (myBar != null) {
558         // the progress bar value changed
559
Object JavaDoc newModelValue = myBar.getValue();
560         firePropertyChange(ACCESSIBLE_VALUE_PROPERTY,
561                    oldModelValue,
562                    newModelValue);
563         oldModelValue = newModelValue;
564         }
565     }
566
567     /**
568      * This method gets called when a bound property is changed.
569      *
570      * @param e A <code>PropertyChangeEvent</code> object describing
571      * the event source and the property that has changed. Must not be null.
572      * @throws NullPointerException if the parameter is null.
573      */

574     public void propertyChange(PropertyChangeEvent JavaDoc e) {
575         if (e.getSource() == noteLabel && e.getPropertyName() == "text") {
576         // the note label text changed
577
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, 0);
578         }
579     }
580
581     /* ===== Begin AccessileContext ===== */
582
583     /**
584      * Gets the accessibleName property of this object. The accessibleName
585      * property of an object is a localized String that designates the purpose
586      * of the object. For example, the accessibleName property of a label
587      * or button might be the text of the label or button itself. In the
588      * case of an object that doesn't display its name, the accessibleName
589      * should still be set. For example, in the case of a text field used
590      * to enter the name of a city, the accessibleName for the en_US locale
591      * could be 'city.'
592      *
593      * @return the localized name of the object; null if this
594      * object does not have a name
595      *
596      * @see #setAccessibleName
597      */

598     public String JavaDoc getAccessibleName() {
599         if (accessibleName != null) { // defined in AccessibleContext
600
return accessibleName;
601         } else if (accessibleJOptionPane != null) {
602         // delegate to the AccessibleJOptionPane
603
return accessibleJOptionPane.getAccessibleName();
604         }
605         return null;
606     }
607     
608     /**
609      * Gets the accessibleDescription property of this object. The
610      * accessibleDescription property of this object is a short localized
611      * phrase describing the purpose of the object. For example, in the
612      * case of a 'Cancel' button, the accessibleDescription could be
613      * 'Ignore changes and close dialog box.'
614      *
615      * @return the localized description of the object; null if
616      * this object does not have a description
617      *
618      * @see #setAccessibleDescription
619      */

620     public String JavaDoc getAccessibleDescription() {
621         if (accessibleDescription != null) { // defined in AccessibleContext
622
return accessibleDescription;
623         } else if (accessibleJOptionPane != null) {
624         // delegate to the AccessibleJOptionPane
625
return accessibleJOptionPane.getAccessibleDescription();
626         }
627         return null;
628     }
629
630     /**
631      * Gets the role of this object. The role of the object is the generic
632      * purpose or use of the class of this object. For example, the role
633      * of a push button is AccessibleRole.PUSH_BUTTON. The roles in
634      * AccessibleRole are provided so component developers can pick from
635      * a set of predefined roles. This enables assistive technologies to
636      * provide a consistent interface to various tweaked subclasses of
637      * components (e.g., use AccessibleRole.PUSH_BUTTON for all components
638      * that act like a push button) as well as distinguish between sublasses
639      * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes
640      * and AccessibleRole.RADIO_BUTTON for radio buttons).
641      * <p>Note that the AccessibleRole class is also extensible, so
642      * custom component developers can define their own AccessibleRole's
643      * if the set of predefined roles is inadequate.
644      *
645      * @return an instance of AccessibleRole describing the role of the object
646      * @see AccessibleRole
647      */

648     public AccessibleRole getAccessibleRole() {
649         return AccessibleRole.PROGRESS_MONITOR;
650     }
651     
652     /**
653      * Gets the state set of this object. The AccessibleStateSet of an object
654      * is composed of a set of unique AccessibleStates. A change in the
655      * AccessibleStateSet of an object will cause a PropertyChangeEvent to
656      * be fired for the ACCESSIBLE_STATE_PROPERTY property.
657      *
658      * @return an instance of AccessibleStateSet containing the
659      * current state set of the object
660      * @see AccessibleStateSet
661      * @see AccessibleState
662      * @see #addPropertyChangeListener
663      */

664     public AccessibleStateSet getAccessibleStateSet() {
665         if (accessibleJOptionPane != null) {
666         // delegate to the AccessibleJOptionPane
667
return accessibleJOptionPane.getAccessibleStateSet();
668         }
669         return null;
670     }
671
672     /**
673      * Gets the Accessible parent of this object.
674      *
675      * @return the Accessible parent of this object; null if this
676      * object does not have an Accessible parent
677      */

678     public Accessible getAccessibleParent() {
679         if (dialog != null) {
680         return (Accessible)dialog;
681         }
682         return null;
683     }
684
685     /*
686      * Returns the parent AccessibleContext
687      */

688     private AccessibleContext getParentAccessibleContext() {
689         if (dialog != null) {
690         return dialog.getAccessibleContext();
691         }
692         return null;
693     }
694
695     /**
696      * Gets the 0-based index of this object in its accessible parent.
697      *
698      * @return the 0-based index of this object in its parent; -1 if this
699      * object does not have an accessible parent.
700      *
701      * @see #getAccessibleParent
702      * @see #getAccessibleChildrenCount
703      * @see #getAccessibleChild
704      */

705     public int getAccessibleIndexInParent() {
706         if (accessibleJOptionPane != null) {
707         // delegate to the AccessibleJOptionPane
708
return accessibleJOptionPane.getAccessibleIndexInParent();
709         }
710         return -1;
711     }
712
713     /**
714      * Returns the number of accessible children of the object.
715      *
716      * @return the number of accessible children of the object.
717      */

718     public int getAccessibleChildrenCount() {
719         // return the number of children in the JPanel containing
720
// the message, note label and progress bar
721
AccessibleContext ac = getPanelAccessibleContext();
722         if (ac != null) {
723         return ac.getAccessibleChildrenCount();
724         }
725         return 0;
726     }
727
728     /**
729      * Returns the specified Accessible child of the object. The Accessible
730      * children of an Accessible object are zero-based, so the first child
731      * of an Accessible child is at index 0, the second child is at index 1,
732      * and so on.
733      *
734      * @param i zero-based index of child
735      * @return the Accessible child of the object
736      * @see #getAccessibleChildrenCount
737      */

738     public Accessible getAccessibleChild(int i) {
739         // return a child in the JPanel containing the message, note label
740
// and progress bar
741
AccessibleContext ac = getPanelAccessibleContext();
742         if (ac != null) {
743         return ac.getAccessibleChild(i);
744         }
745         return null;
746     }
747     
748     /*
749      * Returns the AccessibleContext for the JPanel containing the
750      * message, note label and progress bar
751      */

752     private AccessibleContext getPanelAccessibleContext() {
753         if (myBar != null) {
754         Component JavaDoc c = myBar.getParent();
755         if (c instanceof Accessible) {
756             return ((Accessible)c).getAccessibleContext();
757         }
758         }
759         return null;
760     }
761
762     /**
763      * Gets the locale of the component. If the component does not have a
764      * locale, then the locale of its parent is returned.
765      *
766      * @return this component's locale. If this component does not have
767      * a locale, the locale of its parent is returned.
768      *
769      * @exception IllegalComponentStateException
770      * If the Component does not have its own locale and has not yet been
771      * added to a containment hierarchy such that the locale can be
772      * determined from the containing parent.
773      */

774     public Locale JavaDoc getLocale() throws IllegalComponentStateException JavaDoc {
775         if (accessibleJOptionPane != null) {
776         // delegate to the AccessibleJOptionPane
777
return accessibleJOptionPane.getLocale();
778         }
779         return null;
780     }
781
782     /* ===== end AccessibleContext ===== */
783
784     /**
785      * Gets the AccessibleComponent associated with this object that has a
786      * graphical representation.
787      *
788      * @return AccessibleComponent if supported by object; else return null
789      * @see AccessibleComponent
790      */

791     public AccessibleComponent getAccessibleComponent() {
792         if (accessibleJOptionPane != null) {
793         // delegate to the AccessibleJOptionPane
794
return accessibleJOptionPane.getAccessibleComponent();
795         }
796         return null;
797     }
798
799     /**
800      * Gets the AccessibleValue associated with this object that supports a
801      * Numerical value.
802      *
803      * @return AccessibleValue if supported by object; else return null
804      * @see AccessibleValue
805      */

806         public AccessibleValue getAccessibleValue() {
807         if (myBar != null) {
808         // delegate to the AccessibleJProgressBar
809
return myBar.getAccessibleContext().getAccessibleValue();
810         }
811         return null;
812         }
813
814     /**
815      * Gets the AccessibleText associated with this object presenting
816      * text on the display.
817      *
818      * @return AccessibleText if supported by object; else return null
819      * @see AccessibleText
820      */

821     public AccessibleText getAccessibleText() {
822         if (getNoteLabelAccessibleText() != null) {
823         return this;
824         }
825         return null;
826     }
827
828     /*
829      * Returns the note label AccessibleText
830      */

831     private AccessibleText getNoteLabelAccessibleText() {
832         if (noteLabel != null) {
833         // AccessibleJLabel implements AccessibleText if the
834
// JLabel contains HTML text
835
return noteLabel.getAccessibleContext().getAccessibleText();
836         }
837         return null;
838     }
839
840     /* ===== Begin AccessibleText impl ===== */
841
842     /**
843      * Given a point in local coordinates, return the zero-based index
844      * of the character under that Point. If the point is invalid,
845      * this method returns -1.
846      *
847      * @param p the Point in local coordinates
848      * @return the zero-based index of the character under Point p; if
849      * Point is invalid return -1.
850      */

851     public int getIndexAtPoint(Point JavaDoc p) {
852         AccessibleText at = getNoteLabelAccessibleText();
853         if (at != null && sameWindowAncestor(pane, noteLabel)) {
854         // convert point from the option pane bounds
855
// to the note label bounds.
856
Point JavaDoc noteLabelPoint = SwingUtilities.convertPoint(pane,
857                                    p,
858                                    noteLabel);
859         if (noteLabelPoint != null) {
860             return at.getIndexAtPoint(noteLabelPoint);
861         }
862         }
863         return -1;
864     }
865
866     /**
867      * Determines the bounding box of the character at the given
868      * index into the string. The bounds are returned in local
869      * coordinates. If the index is invalid an empty rectangle is returned.
870      *
871      * @param i the index into the String
872      * @return the screen coordinates of the character's bounding box,
873      * if index is invalid return an empty rectangle.
874      */

875     public Rectangle JavaDoc getCharacterBounds(int i) {
876         AccessibleText at = getNoteLabelAccessibleText();
877         if (at != null && sameWindowAncestor(pane, noteLabel)) {
878         // return rectangle in the option pane bounds
879
Rectangle JavaDoc noteLabelRect = at.getCharacterBounds(i);
880         if (noteLabelRect != null) {
881             return SwingUtilities.convertRectangle(noteLabel,
882                                noteLabelRect,
883                                pane);
884         }
885         }
886         return null;
887     }
888
889     /*
890      * Returns whether source and destination components have the
891      * same window ancestor
892      */

893     private boolean sameWindowAncestor(Component JavaDoc src, Component JavaDoc dest) {
894         if (src == null || dest == null) {
895         return false;
896         }
897         return SwingUtilities.getWindowAncestor(src) ==
898         SwingUtilities.getWindowAncestor(dest);
899     }
900
901     /**
902      * Returns the number of characters (valid indicies)
903      *
904      * @return the number of characters
905      */

906     public int getCharCount() {
907         AccessibleText at = getNoteLabelAccessibleText();
908         if (at != null) { // JLabel contains HTML text
909
return at.getCharCount();
910         }
911         return -1;
912     }
913
914     /**
915      * Returns the zero-based offset of the caret.
916      *
917      * Note: That to the right of the caret will have the same index
918      * value as the offset (the caret is between two characters).
919      * @return the zero-based offset of the caret.
920      */

921     public int getCaretPosition() {
922         AccessibleText at = getNoteLabelAccessibleText();
923         if (at != null) { // JLabel contains HTML text
924
return at.getCaretPosition();
925         }
926         return -1;
927     }
928
929     /**
930      * Returns the String at a given index.
931      *
932      * @param part the CHARACTER, WORD, or SENTENCE to retrieve
933      * @param index an index within the text
934      * @return the letter, word, or sentence
935      */

936     public String JavaDoc getAtIndex(int part, int index) {
937         AccessibleText at = getNoteLabelAccessibleText();
938         if (at != null) { // JLabel contains HTML text
939
return at.getAtIndex(part, index);
940         }
941         return null;
942     }
943
944     /**
945      * Returns the String after a given index.
946      *
947      * @param part the CHARACTER, WORD, or SENTENCE to retrieve
948      * @param index an index within the text
949      * @return the letter, word, or sentence
950      */

951     public String JavaDoc getAfterIndex(int part, int index) {
952         AccessibleText at = getNoteLabelAccessibleText();
953         if (at != null) { // JLabel contains HTML text
954
return at.getAfterIndex(part, index);
955         }
956         return null;
957     }
958
959     /**
960      * Returns the String before a given index.
961      *
962      * @param part the CHARACTER, WORD, or SENTENCE to retrieve
963      * @param index an index within the text
964      * @return the letter, word, or sentence
965      */

966     public String JavaDoc getBeforeIndex(int part, int index) {
967         AccessibleText at = getNoteLabelAccessibleText();
968         if (at != null) { // JLabel contains HTML text
969
return at.getBeforeIndex(part, index);
970         }
971         return null;
972     }
973
974     /**
975      * Returns the AttributeSet for a given character at a given index
976      *
977      * @param i the zero-based index into the text
978      * @return the AttributeSet of the character
979      */

980     public AttributeSet getCharacterAttribute(int i) {
981         AccessibleText at = getNoteLabelAccessibleText();
982         if (at != null) { // JLabel contains HTML text
983
return at.getCharacterAttribute(i);
984         }
985         return null;
986     }
987
988     /**
989      * Returns the start offset within the selected text.
990      * If there is no selection, but there is
991      * a caret, the start and end offsets will be the same.
992      *
993      * @return the index into the text of the start of the selection
994      */

995     public int getSelectionStart() {
996         AccessibleText at = getNoteLabelAccessibleText();
997         if (at != null) { // JLabel contains HTML text
998
return at.getSelectionStart();
999         }
1000        return -1;
1001    }
1002
1003    /**
1004     * Returns the end offset within the selected text.
1005     * If there is no selection, but there is
1006     * a caret, the start and end offsets will be the same.
1007     *
1008     * @return the index into teh text of the end of the selection
1009     */

1010    public int getSelectionEnd() {
1011        AccessibleText at = getNoteLabelAccessibleText();
1012        if (at != null) { // JLabel contains HTML text
1013
return at.getSelectionEnd();
1014        }
1015        return -1;
1016    }
1017
1018    /**
1019     * Returns the portion of the text that is selected.
1020     *
1021     * @return the String portion of the text that is selected
1022     */

1023    public String JavaDoc getSelectedText() {
1024        AccessibleText at = getNoteLabelAccessibleText();
1025        if (at != null) { // JLabel contains HTML text
1026
return at.getSelectedText();
1027        }
1028        return null;
1029    }
1030    /* ===== End AccessibleText impl ===== */
1031    }
1032    // inner class AccessibleProgressMonitor
1033

1034}
1035
Popular Tags