1 7 8 9 10 package javax.swing; 11 12 13 14 import java.io.*; 15 import java.awt.BorderLayout ; 16 import java.awt.Frame ; 17 import java.awt.Dialog ; 18 import java.awt.Window ; 19 import java.awt.Component ; 20 import java.awt.Container ; 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.awt.event.WindowListener ; 24 import java.awt.event.WindowAdapter ; 25 import java.awt.event.WindowEvent ; 26 27 import java.awt.IllegalComponentStateException ; 28 import java.awt.Point ; 29 import java.awt.Rectangle ; 30 import java.text.*; 31 import java.util.Locale ; 32 import javax.accessibility.*; 33 import javax.swing.event.*; 34 import javax.swing.text.*; 35 36 37 64 public class ProgressMonitor extends Object implements Accessible 65 { 66 private ProgressMonitor root; 67 private JDialog dialog; 68 private JOptionPane pane; 69 private JProgressBar myBar; 70 private JLabel noteLabel; 71 private Component parentComponent; 72 private String note; 73 private Object [] cancelOption = null; 74 private Object 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 108 public ProgressMonitor(Component parentComponent, 109 Object message, 110 String note, 111 int min, 112 int max) { 113 this(parentComponent, message, note, min, max, null); 114 } 115 116 117 private ProgressMonitor(Component parentComponent, 118 Object message, 119 String note, 120 int min, 121 int max, 122 ProgressMonitor group) { 123 this.min = min; 124 this.max = max; 125 this.parentComponent = parentComponent; 126 127 cancelOption = new Object [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 147 { 148 ProgressOptionPane(Object 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 public JDialog createDialog(Component parentComponent, String title) { 168 final JDialog dialog; 169 170 Window window = JOptionPane.getWindowForComponent(parentComponent); 171 if (window instanceof Frame ) { 172 dialog = new JDialog ((Frame )window, title, false); 173 } else { 174 dialog = new JDialog ((Dialog )window, title, false); 175 } 176 if (window instanceof SwingUtilities.SharedOwnerFrame ) { 177 WindowListener ownerShutdownListener = 178 (WindowListener )SwingUtilities.getSharedOwnerFrameShutdownListener(); 179 dialog.addWindowListener(ownerShutdownListener); 180 } 181 Container contentPane = dialog.getContentPane(); 182 183 contentPane.setLayout(new BorderLayout ()); 184 contentPane.add(this, BorderLayout.CENTER); 185 dialog.pack(); 186 dialog.setLocationRelativeTo(parentComponent); 187 dialog.addWindowListener(new WindowAdapter () { 188 boolean gotFocus = false; 189 190 public void windowClosing(WindowEvent we) { 191 setValue(cancelOption[0]); 192 } 193 194 public void windowActivated(WindowEvent we) { 195 if (!gotFocus) { 197 selectInitialValue(); 198 gotFocus = true; 199 } 200 } 201 }); 202 203 addPropertyChangeListener(new PropertyChangeListener () { 204 public void propertyChange(PropertyChangeEvent 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 222 228 public AccessibleContext getAccessibleContext() { 229 return ProgressMonitor.this.getAccessibleContext(); 230 } 231 232 235 private AccessibleContext getAccessibleJOptionPane() { 236 return super.getAccessibleContext(); 237 } 238 } 239 240 241 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 (); 276 myBar.setMinimum(min); 277 myBar.setMaximum(max); 278 myBar.setValue(nv); 279 if (note != null) noteLabel = new JLabel (note); 280 pane = new ProgressOptionPane(new Object [] {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 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 316 public int getMinimum() { 317 return min; 318 } 319 320 321 327 public void setMinimum(int m) { 328 min = m; 329 } 330 331 332 338 public int getMaximum() { 339 return max; 340 } 341 342 343 349 public void setMaximum(int m) { 350 max = m; 351 } 352 353 354 357 public boolean isCanceled() { 358 if (pane == null) return false; 359 Object v = pane.getValue(); 360 return ((v != null) && 361 (cancelOption.length == 1) && 362 (v.equals(cancelOption[0]))); 363 } 364 365 366 374 public void setMillisToDecideToPopup(int millisToDecideToPopup) { 375 this.millisToDecideToPopup = millisToDecideToPopup; 376 } 377 378 379 385 public int getMillisToDecideToPopup() { 386 return millisToDecideToPopup; 387 } 388 389 390 398 public void setMillisToPopup(int millisToPopup) { 399 this.millisToPopup = millisToPopup; 400 } 401 402 403 408 public int getMillisToPopup() { 409 return millisToPopup; 410 } 411 412 413 421 public void setNote(String note) { 422 this.note = note; 423 if (noteLabel != null) { 424 noteLabel.setText(note); 425 } 426 } 427 428 429 436 public String getNote() { 437 return note; 438 } 439 440 444 448 protected AccessibleContext accessibleContext = null; 449 450 private AccessibleContext accessibleJOptionPane = null; 451 452 460 public AccessibleContext getAccessibleContext() { 461 if (accessibleContext == null) { 462 accessibleContext = new AccessibleProgressMonitor(); 463 } 464 if (pane != null && accessibleJOptionPane == null) { 465 if (accessibleContext instanceof AccessibleProgressMonitor) { 472 ((AccessibleProgressMonitor)accessibleContext).optionPaneCreated(); 473 } 474 } 475 return accessibleContext; 476 } 477 478 483 protected class AccessibleProgressMonitor extends AccessibleContext 484 implements AccessibleText, ChangeListener , PropertyChangeListener { 485 486 511 512 private Object oldModelValue; 513 514 517 protected AccessibleProgressMonitor() { 518 } 519 520 532 private void optionPaneCreated() { 533 accessibleJOptionPane = 534 ((ProgressOptionPane)pane).getAccessibleJOptionPane(); 535 536 if (myBar != null) { 538 myBar.addChangeListener(this); 539 } 540 541 if (noteLabel != null) { 543 noteLabel.addPropertyChangeListener(this); 544 } 545 } 546 547 553 public void stateChanged(ChangeEvent e) { 554 if (e == null) { 555 return; 556 } 557 if (myBar != null) { 558 Object newModelValue = myBar.getValue(); 560 firePropertyChange(ACCESSIBLE_VALUE_PROPERTY, 561 oldModelValue, 562 newModelValue); 563 oldModelValue = newModelValue; 564 } 565 } 566 567 574 public void propertyChange(PropertyChangeEvent e) { 575 if (e.getSource() == noteLabel && e.getPropertyName() == "text") { 576 firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, 0); 578 } 579 } 580 581 582 583 598 public String getAccessibleName() { 599 if (accessibleName != null) { return accessibleName; 601 } else if (accessibleJOptionPane != null) { 602 return accessibleJOptionPane.getAccessibleName(); 604 } 605 return null; 606 } 607 608 620 public String getAccessibleDescription() { 621 if (accessibleDescription != null) { return accessibleDescription; 623 } else if (accessibleJOptionPane != null) { 624 return accessibleJOptionPane.getAccessibleDescription(); 626 } 627 return null; 628 } 629 630 648 public AccessibleRole getAccessibleRole() { 649 return AccessibleRole.PROGRESS_MONITOR; 650 } 651 652 664 public AccessibleStateSet getAccessibleStateSet() { 665 if (accessibleJOptionPane != null) { 666 return accessibleJOptionPane.getAccessibleStateSet(); 668 } 669 return null; 670 } 671 672 678 public Accessible getAccessibleParent() { 679 if (dialog != null) { 680 return (Accessible)dialog; 681 } 682 return null; 683 } 684 685 688 private AccessibleContext getParentAccessibleContext() { 689 if (dialog != null) { 690 return dialog.getAccessibleContext(); 691 } 692 return null; 693 } 694 695 705 public int getAccessibleIndexInParent() { 706 if (accessibleJOptionPane != null) { 707 return accessibleJOptionPane.getAccessibleIndexInParent(); 709 } 710 return -1; 711 } 712 713 718 public int getAccessibleChildrenCount() { 719 AccessibleContext ac = getPanelAccessibleContext(); 722 if (ac != null) { 723 return ac.getAccessibleChildrenCount(); 724 } 725 return 0; 726 } 727 728 738 public Accessible getAccessibleChild(int i) { 739 AccessibleContext ac = getPanelAccessibleContext(); 742 if (ac != null) { 743 return ac.getAccessibleChild(i); 744 } 745 return null; 746 } 747 748 752 private AccessibleContext getPanelAccessibleContext() { 753 if (myBar != null) { 754 Component c = myBar.getParent(); 755 if (c instanceof Accessible) { 756 return ((Accessible)c).getAccessibleContext(); 757 } 758 } 759 return null; 760 } 761 762 774 public Locale getLocale() throws IllegalComponentStateException { 775 if (accessibleJOptionPane != null) { 776 return accessibleJOptionPane.getLocale(); 778 } 779 return null; 780 } 781 782 783 784 791 public AccessibleComponent getAccessibleComponent() { 792 if (accessibleJOptionPane != null) { 793 return accessibleJOptionPane.getAccessibleComponent(); 795 } 796 return null; 797 } 798 799 806 public AccessibleValue getAccessibleValue() { 807 if (myBar != null) { 808 return myBar.getAccessibleContext().getAccessibleValue(); 810 } 811 return null; 812 } 813 814 821 public AccessibleText getAccessibleText() { 822 if (getNoteLabelAccessibleText() != null) { 823 return this; 824 } 825 return null; 826 } 827 828 831 private AccessibleText getNoteLabelAccessibleText() { 832 if (noteLabel != null) { 833 return noteLabel.getAccessibleContext().getAccessibleText(); 836 } 837 return null; 838 } 839 840 841 842 851 public int getIndexAtPoint(Point p) { 852 AccessibleText at = getNoteLabelAccessibleText(); 853 if (at != null && sameWindowAncestor(pane, noteLabel)) { 854 Point 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 875 public Rectangle getCharacterBounds(int i) { 876 AccessibleText at = getNoteLabelAccessibleText(); 877 if (at != null && sameWindowAncestor(pane, noteLabel)) { 878 Rectangle 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 893 private boolean sameWindowAncestor(Component src, Component dest) { 894 if (src == null || dest == null) { 895 return false; 896 } 897 return SwingUtilities.getWindowAncestor(src) == 898 SwingUtilities.getWindowAncestor(dest); 899 } 900 901 906 public int getCharCount() { 907 AccessibleText at = getNoteLabelAccessibleText(); 908 if (at != null) { return at.getCharCount(); 910 } 911 return -1; 912 } 913 914 921 public int getCaretPosition() { 922 AccessibleText at = getNoteLabelAccessibleText(); 923 if (at != null) { return at.getCaretPosition(); 925 } 926 return -1; 927 } 928 929 936 public String getAtIndex(int part, int index) { 937 AccessibleText at = getNoteLabelAccessibleText(); 938 if (at != null) { return at.getAtIndex(part, index); 940 } 941 return null; 942 } 943 944 951 public String getAfterIndex(int part, int index) { 952 AccessibleText at = getNoteLabelAccessibleText(); 953 if (at != null) { return at.getAfterIndex(part, index); 955 } 956 return null; 957 } 958 959 966 public String getBeforeIndex(int part, int index) { 967 AccessibleText at = getNoteLabelAccessibleText(); 968 if (at != null) { return at.getBeforeIndex(part, index); 970 } 971 return null; 972 } 973 974 980 public AttributeSet getCharacterAttribute(int i) { 981 AccessibleText at = getNoteLabelAccessibleText(); 982 if (at != null) { return at.getCharacterAttribute(i); 984 } 985 return null; 986 } 987 988 995 public int getSelectionStart() { 996 AccessibleText at = getNoteLabelAccessibleText(); 997 if (at != null) { return at.getSelectionStart(); 999 } 1000 return -1; 1001 } 1002 1003 1010 public int getSelectionEnd() { 1011 AccessibleText at = getNoteLabelAccessibleText(); 1012 if (at != null) { return at.getSelectionEnd(); 1014 } 1015 return -1; 1016 } 1017 1018 1023 public String getSelectedText() { 1024 AccessibleText at = getNoteLabelAccessibleText(); 1025 if (at != null) { return at.getSelectedText(); 1027 } 1028 return null; 1029 } 1030 1031 } 1032 1034} 1035 | Popular Tags |