1 7 package javax.swing; 8 9 10 import java.util.Hashtable ; 11 import java.util.Dictionary ; 12 import java.util.Enumeration ; 13 import java.util.Locale ; 14 import java.util.Vector ; 15 import java.util.EventListener ; 16 import java.util.Set ; 17 import java.util.TreeSet ; 18 import java.util.Map ; 19 import java.util.HashMap ; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.awt.image.VolatileImage ; 24 import java.awt.Graphics2D ; 25 import java.awt.peer.LightweightPeer; 26 import java.awt.dnd.DropTarget ; 27 import java.awt.font.FontRenderContext ; 28 import java.beans.*; 29 30 import java.applet.Applet ; 31 32 import java.io.Serializable ; 33 import java.io.ObjectOutputStream ; 34 import java.io.ObjectInputStream ; 35 import java.io.IOException ; 36 import java.io.ObjectInputValidation ; 37 import java.io.InvalidObjectException ; 38 39 import java.lang.reflect.Method ; 40 41 import javax.swing.border.*; 42 import javax.swing.event.*; 43 import javax.swing.plaf.*; 44 import javax.accessibility.*; 45 46 import com.sun.java.swing.SwingUtilities2; 47 import sun.font.FontDesignMetrics; 48 import sun.swing.AccessibleMethod; 49 50 160 public abstract class JComponent extends Container implements Serializable 161 { 162 166 private static final String uiClassID = "ComponentUI"; 167 168 171 private static final StringBuffer ANCESTOR_NOTIFIER_KEY = new StringBuffer ( 172 "AncestorNotifier"); 173 174 177 private static final StringBuffer TRANSFER_HANDLER_KEY = new StringBuffer ( 178 "TransferHandler"); 179 180 183 private static final StringBuffer INPUT_VERIFIER_KEY = new StringBuffer ( 184 "InputVerifier"); 185 186 189 private static final Hashtable readObjectCallbacks = new Hashtable (1); 190 191 195 private static Set managingFocusForwardTraversalKeys; 196 197 201 private static Set managingFocusBackwardTraversalKeys; 202 203 208 private static boolean suppressDropSupport; 209 210 214 private static boolean checkedSuppressDropSupport; 215 216 private static final int NOT_OBSCURED = 0; 218 private static final int PARTIALLY_OBSCURED = 1; 219 private static final int COMPLETELY_OBSCURED = 2; 220 221 224 static boolean DEBUG_GRAPHICS_LOADED; 225 226 229 private static final Map <Font,FontMetrics> aaFontMap; 230 231 234 private boolean isAlignmentXSet; 235 private float alignmentX; 236 private boolean isAlignmentYSet; 237 private float alignmentY; 238 239 242 243 244 protected transient ComponentUI ui; 245 246 protected EventListenerList listenerList = new EventListenerList(); 247 248 private transient ArrayTable clientProperties; 249 private VetoableChangeSupport vetoableChangeSupport; 250 253 private boolean autoscrolls; 254 private Border border; 255 private int flags; 256 257 258 private InputVerifier inputVerifier = null; 259 260 private boolean verifyInputWhenFocusTarget = true; 261 262 269 transient Component paintingChild; 270 271 276 public static final int WHEN_FOCUSED = 0; 277 278 284 public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1; 285 286 292 public static final int WHEN_IN_FOCUSED_WINDOW = 2; 293 294 297 public static final int UNDEFINED_CONDITION = -1; 298 299 302 private static final String KEYBOARD_BINDINGS_KEY = "_KeyboardBindings"; 303 304 309 private static final String WHEN_IN_FOCUSED_WINDOW_BINDINGS = "_WhenInFocusedWindow"; 310 311 315 public static final String TOOL_TIP_TEXT_KEY = "ToolTipText"; 316 317 private static final String NEXT_FOCUS = "nextFocus"; 318 319 323 private JPopupMenu popupMenu; 324 325 326 private static final int IS_DOUBLE_BUFFERED = 0; 327 private static final int ANCESTOR_USING_BUFFER = 1; 328 private static final int IS_PAINTING_TILE = 2; 329 private static final int IS_OPAQUE = 3; 330 private static final int KEY_EVENTS_ENABLED = 4; 331 private static final int FOCUS_INPUTMAP_CREATED = 5; 332 private static final int ANCESTOR_INPUTMAP_CREATED = 6; 333 private static final int WIF_INPUTMAP_CREATED = 7; 334 private static final int ACTIONMAP_CREATED = 8; 335 private static final int CREATED_DOUBLE_BUFFER = 9; 336 private static final int IS_PRINTING = 11; 338 private static final int IS_PRINTING_ALL = 12; 339 private static final int IS_REPAINTING = 13; 340 341 private static final int WRITE_OBJ_COUNTER_FIRST = 14; 342 private static final int RESERVED_1 = 15; 343 private static final int RESERVED_2 = 16; 344 private static final int RESERVED_3 = 17; 345 private static final int RESERVED_4 = 18; 346 private static final int RESERVED_5 = 19; 347 private static final int RESERVED_6 = 20; 348 private static final int WRITE_OBJ_COUNTER_LAST = 21; 349 350 private static final int REQUEST_FOCUS_DISABLED = 22; 351 private static final int INHERITS_POPUP_MENU = 23; 352 private static final int OPAQUE_SET = 24; 353 private static final int AUTOSCROLLS_SET = 25; 354 private static final int FOCUS_TRAVERSAL_KEYS_FORWARD_SET = 26; 355 private static final int FOCUS_TRAVERSAL_KEYS_BACKWARD_SET = 27; 356 357 360 private static boolean inInputVerifier; 361 362 365 private static java.util.List tempRectangles = new java.util.ArrayList (11); 366 367 368 private InputMap focusInputMap; 369 370 private InputMap ancestorInputMap; 371 372 private ComponentInputMap windowInputMap; 373 374 375 private ActionMap actionMap; 376 377 378 private static final String defaultLocale = "JComponent.defaultLocale"; 379 380 384 private boolean aaText; 385 386 static { 387 aaFontMap = new HashMap <Font,FontMetrics>(); 388 } 389 390 394 static Set <KeyStroke > getManagingFocusForwardTraversalKeys() { 395 if (managingFocusForwardTraversalKeys == null) { 396 managingFocusForwardTraversalKeys = new TreeSet (); 397 managingFocusForwardTraversalKeys.add( 398 KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK)); 399 } 400 return managingFocusForwardTraversalKeys; 401 } 402 403 407 static Set <KeyStroke > getManagingFocusBackwardTraversalKeys() { 408 if (managingFocusBackwardTraversalKeys == null) { 409 managingFocusBackwardTraversalKeys = new TreeSet (); 410 managingFocusBackwardTraversalKeys.add( 411 KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 412 InputEvent.SHIFT_MASK | 413 InputEvent.CTRL_MASK)); 414 } 415 return managingFocusBackwardTraversalKeys; 416 } 417 418 419 423 private static boolean getSuppressDropTarget() { 424 if (!checkedSuppressDropSupport) { 425 Boolean b = (Boolean )java.security.AccessController.doPrivileged( 426 new java.security.PrivilegedAction () { 427 public Object run() { 428 String value = System.getProperty( 429 "suppressSwingDropSupport"); 430 431 if (value != null) { 432 return Boolean.valueOf(value); 433 } 434 return Boolean.FALSE; 435 } 436 } 437 ); 438 suppressDropSupport = b.booleanValue(); 439 checkedSuppressDropSupport = true; 440 } 441 return suppressDropSupport; 442 } 443 444 private static Rectangle fetchRectangle() { 445 synchronized(tempRectangles) { 446 Rectangle rect; 447 int size = tempRectangles.size(); 448 if (size > 0) { 449 rect = (Rectangle)tempRectangles.remove(size - 1); 450 } 451 else { 452 rect = new Rectangle(0, 0, 0, 0); 453 } 454 return rect; 455 } 456 } 457 458 private static void recycleRectangle(Rectangle rect) { 459 synchronized(tempRectangles) { 460 tempRectangles.add(rect); 461 } 462 } 463 464 482 public void setInheritsPopupMenu(boolean value) { 483 setFlag(INHERITS_POPUP_MENU, value); 484 } 485 486 492 public boolean getInheritsPopupMenu() { 493 return getFlag(INHERITS_POPUP_MENU); 494 } 495 496 520 public void setComponentPopupMenu(JPopupMenu popup) { 521 if(popup != null && isLightweight()) { 522 enableEvents(AWTEvent.MOUSE_EVENT_MASK); 523 } 524 this.popupMenu = popup; 525 } 526 527 539 public JPopupMenu getComponentPopupMenu() { 540 541 if(!getInheritsPopupMenu()) { 542 return popupMenu; 543 } 544 545 if(popupMenu == null) { 546 Container parent = getParent(); 548 while (parent != null) { 549 if(parent instanceof JComponent ) { 550 return ((JComponent )parent).getComponentPopupMenu(); 551 } 552 if(parent instanceof Window || 553 parent instanceof Applet ) { 554 break; 556 } 557 parent = parent.getParent(); 558 } 559 return null; 560 } 561 562 return popupMenu; 563 } 564 565 575 public JComponent() { 576 super(); 577 enableEvents(AWTEvent.KEY_EVENT_MASK); 581 if (isManagingFocus()) { 582 LookAndFeel.installProperty(this, 583 "focusTraversalKeysForward", 584 getManagingFocusForwardTraversalKeys()); 585 LookAndFeel.installProperty(this, 586 "focusTraversalKeysBackward", 587 getManagingFocusBackwardTraversalKeys()); 588 } 589 590 super.setLocale( JComponent.getDefaultLocale() ); 591 } 592 593 594 608 public void updateUI() {} 609 610 611 639 protected void setUI(ComponentUI newUI) { 640 645 if (ui != null) { 646 ui.uninstallUI(this); 647 } 648 aaText = false; 650 ComponentUI oldUI = ui; 651 ui = newUI; 652 if (ui != null) { 653 ui.installUI(this); 654 } 655 656 firePropertyChange("UI", oldUI, newUI); 657 revalidate(); 658 repaint(); 659 } 660 661 662 679 public String getUIClassID() { 680 return uiClassID; 681 } 682 683 684 694 protected Graphics getComponentGraphics(Graphics g) { 695 Graphics componentGraphics = g; 696 if (ui != null && DEBUG_GRAPHICS_LOADED) { 697 if ((DebugGraphics.debugComponentCount() != 0) && 698 (shouldDebugGraphics() != 0) && 699 !(g instanceof DebugGraphics )) { 700 componentGraphics = new DebugGraphics (g,this); 701 } 702 } 703 componentGraphics.setColor(getForeground()); 704 componentGraphics.setFont(getFont()); 705 706 return componentGraphics; 707 } 708 709 710 739 protected void paintComponent(Graphics g) { 740 if (ui != null) { 741 Graphics scratchGraphics = (g == null) ? null : g.create(); 742 try { 743 ui.update(scratchGraphics, this); 744 } 745 finally { 746 scratchGraphics.dispose(); 747 } 748 } 749 } 750 751 762 protected void paintChildren(Graphics g) { 763 boolean isJComponent; 764 Graphics sg = null; 765 766 try { 767 synchronized(getTreeLock()) { 768 int i = getComponentCount() - 1; 769 if (i < 0) { 770 return; 771 } 772 sg = (g == null) ? null : g.create(); 773 if (paintingChild != null && 776 (paintingChild instanceof JComponent ) && 777 ((JComponent )paintingChild).isOpaque()) { 778 for (; i >= 0; i--) { 779 if (getComponent(i) == paintingChild){ 780 break; 781 } 782 } 783 } 784 Rectangle tmpRect = fetchRectangle(); 785 boolean checkSiblings = (!isOptimizedDrawingEnabled() && 786 checkIfChildObscuredBySibling()); 787 Rectangle clipBounds = null; 788 if (checkSiblings) { 789 clipBounds = sg.getClipBounds(); 790 if (clipBounds == null) { 791 clipBounds = new Rectangle(0, 0, getWidth(), 792 getHeight()); 793 } 794 } 795 boolean printing = getFlag(IS_PRINTING); 796 for (; i >= 0 ; i--) { 797 Component comp = getComponent(i); 798 if (comp != null && 799 (printing || isLightweightComponent(comp)) && 800 (comp.isVisible() == true)) { 801 Rectangle cr; 802 isJComponent = (comp instanceof JComponent ); 803 804 cr = comp.getBounds(tmpRect); 805 806 boolean hitClip = g.hitClip(cr.x, cr.y, cr.width, cr.height); 807 808 if (hitClip) { 809 if (checkSiblings && i > 0) { 810 int x = cr.x; 811 int y = cr.y; 812 int width = cr.width; 813 int height = cr.height; 814 SwingUtilities.computeIntersection 815 (clipBounds.x, clipBounds.y, 816 clipBounds.width, clipBounds.height, cr); 817 818 if(getObscuredState(i, cr.x, cr.y, cr.width, 819 cr.height) == COMPLETELY_OBSCURED) { 820 continue; 821 } 822 cr.x = x; 823 cr.y = y; 824 cr.width = width; 825 cr.height = height; 826 } 827 Graphics cg = sg.create(cr.x, cr.y, cr.width, 828 cr.height); 829 cg.setColor(comp.getForeground()); 830 cg.setFont(comp.getFont()); 831 boolean shouldSetFlagBack = false; 832 try { 833 if(isJComponent) { 834 if(getFlag(ANCESTOR_USING_BUFFER)) { 835 ((JComponent )comp).setFlag(ANCESTOR_USING_BUFFER,true); 836 shouldSetFlagBack = true; 837 } 838 if(getFlag(IS_PAINTING_TILE)) { 839 ((JComponent )comp).setFlag(IS_PAINTING_TILE,true); 840 shouldSetFlagBack = true; 841 } 842 if(!printing) { 843 ((JComponent )comp).paint(cg); 844 } 845 else { 846 if (!getFlag(IS_PRINTING_ALL)) { 847 comp.print(cg); 848 } 849 else { 850 comp.printAll(cg); 851 } 852 } 853 } else { 854 if (!printing) { 855 comp.paint(cg); 856 } 857 else { 858 if (!getFlag(IS_PRINTING_ALL)) { 859 comp.print(cg); 860 } 861 else { 862 comp.printAll(cg); 863 } 864 } 865 } 866 } finally { 867 cg.dispose(); 868 if(shouldSetFlagBack) { 869 ((JComponent )comp).setFlag(ANCESTOR_USING_BUFFER,false); 870 ((JComponent )comp).setFlag(IS_PAINTING_TILE,false); 871 } 872 } 873 } 874 } 875 876 } 877 recycleRectangle(tmpRect); 878 } 879 } finally { 880 if (sg != null) { 881 sg.dispose(); 882 } 883 } 884 } 885 886 901 protected void paintBorder(Graphics g) { 902 Border border = getBorder(); 903 if (border != null) { 904 border.paintBorder(this, g, 0, 0, getWidth(), getHeight()); 905 } 906 } 907 908 909 919 public void update(Graphics g) { 920 paint(g); 921 } 922 923 924 949 public void paint(Graphics g) { 950 boolean shouldClearPaintFlags = false; 951 boolean paintCompleted = false; 952 953 if ((getWidth() <= 0) || (getHeight() <= 0)) { 954 return; 955 } 956 957 Graphics componentGraphics = getComponentGraphics(g); 958 Graphics co = (componentGraphics == null) ? null : 959 componentGraphics.create(); 960 try { 961 RepaintManager repaintManager = RepaintManager.currentManager(this); 962 Rectangle clipRect = co.getClipBounds(); 963 int clipX; 964 int clipY; 965 int clipW; 966 int clipH; 967 if (clipRect == null) { 968 clipX = clipY = 0; 969 clipW = getWidth(); 970 clipH = getHeight(); 971 } 972 else { 973 clipX = clipRect.x; 974 clipY = clipRect.y; 975 clipW = clipRect.width; 976 clipH = clipRect.height; 977 } 978 979 if(clipW > getWidth()) { 980 clipW = getWidth(); 981 } 982 if(clipH > getHeight()) { 983 clipH = getHeight(); 984 } 985 986 if(getParent() != null && !(getParent() instanceof JComponent )) { 987 adjustPaintFlags(); 988 shouldClearPaintFlags = true; 989 } 990 991 int bw,bh; 992 boolean printing = getFlag(IS_PRINTING); 993 if(!printing && repaintManager.isDoubleBufferingEnabled() && 994 !getFlag(ANCESTOR_USING_BUFFER) && isDoubleBuffered()) { 995 996 paintCompleted = paintDoubleBuffered(this, this, co, clipX, clipY, clipW, clipH); 997 } 998 if (!paintCompleted) { 999 if (clipRect == null) { 1001 co.setClip(clipX, clipY, clipW, clipH); 1002 } 1003 1004 if (!rectangleIsObscured(clipX,clipY,clipW,clipH)) { 1005 if (!printing) { 1006 paintComponent(co); 1007 paintBorder(co); 1008 } 1009 else { 1010 printComponent(co); 1011 printBorder(co); 1012 } 1013 } 1014 if (!printing) { 1015 paintChildren(co); 1016 } 1017 else { 1018 printChildren(co); 1019 } 1020 } 1021 } finally { 1022 co.dispose(); 1023 if(shouldClearPaintFlags) { 1024 setFlag(ANCESTOR_USING_BUFFER,false); 1025 setFlag(IS_PAINTING_TILE,false); 1026 setFlag(IS_PRINTING,false); 1027 setFlag(IS_PRINTING_ALL,false); 1028 } 1029 } 1030 } 1031 1032 1036 boolean isPainting() { 1037 Container component = this; 1038 while (component != null) { 1039 if (component instanceof JComponent && 1040 ((JComponent )component).getFlag(ANCESTOR_USING_BUFFER)) { 1041 return true; 1042 } 1043 component = component.getParent(); 1044 } 1045 return false; 1046 } 1047 1048 private void adjustPaintFlags() { 1049 JComponent jparent = null; 1050 Container parent; 1051 for(parent = getParent() ; parent != null ; parent = 1052 parent.getParent()) { 1053 if(parent instanceof JComponent ) { 1054 jparent = (JComponent ) parent; 1055 if(jparent.getFlag(ANCESTOR_USING_BUFFER)) 1056 setFlag(ANCESTOR_USING_BUFFER, true); 1057 if(jparent.getFlag(IS_PAINTING_TILE)) 1058 setFlag(IS_PAINTING_TILE, true); 1059 if(jparent.getFlag(IS_PRINTING)) 1060 setFlag(IS_PRINTING, true); 1061 if(jparent.getFlag(IS_PRINTING_ALL)) 1062 setFlag(IS_PRINTING_ALL, true); 1063 break; 1064 } 1065 } 1066 } 1067 1068 1078 public void printAll(Graphics g) { 1079 setFlag(IS_PRINTING_ALL, true); 1080 try { 1081 print(g); 1082 } 1083 finally { 1084 setFlag(IS_PRINTING_ALL, false); 1085 } 1086 } 1087 1088 1102 public void print(Graphics g) { 1103 setFlag(IS_PRINTING, true); 1104 try { 1105 paint(g); 1106 } 1107 finally { 1108 setFlag(IS_PRINTING, false); 1109 } 1110 } 1111 1112 1121 protected void printComponent(Graphics g) { 1122 paintComponent(g); 1123 } 1124 1125 1134 protected void printChildren(Graphics g) { 1135 paintChildren(g); 1136 } 1137 1138 1147 protected void printBorder(Graphics g) { 1148 paintBorder(g); 1149 } 1150 1151 1161 public boolean isPaintingTile() { 1162 return getFlag(IS_PAINTING_TILE); 1163 } 1164 1165 1183 @Deprecated 1184 public boolean isManagingFocus() { 1185 return false; 1186 } 1187 1188 private void registerNextFocusableComponent() { 1189 registerNextFocusableComponent(getNextFocusableComponent()); 1190 } 1191 1192 private void registerNextFocusableComponent(Component 1193 nextFocusableComponent) { 1194 if (nextFocusableComponent == null) { 1195 return; 1196 } 1197 1198 Container nearestRoot = 1199 (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor(); 1200 FocusTraversalPolicy policy = nearestRoot.getFocusTraversalPolicy(); 1201 if (!(policy instanceof LegacyGlueFocusTraversalPolicy )) { 1202 policy = new LegacyGlueFocusTraversalPolicy (policy); 1203 nearestRoot.setFocusTraversalPolicy(policy); 1204 } 1205 ((LegacyGlueFocusTraversalPolicy )policy). 1206 setNextFocusableComponent(this, nextFocusableComponent); 1207 } 1208 1209 private void deregisterNextFocusableComponent() { 1210 Component nextFocusableComponent = getNextFocusableComponent(); 1211 if (nextFocusableComponent == null) { 1212 return; 1213 } 1214 1215 Container nearestRoot = 1216 (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor(); 1217 if (nearestRoot == null) { 1218 return; 1219 } 1220 FocusTraversalPolicy policy = nearestRoot.getFocusTraversalPolicy(); 1221 if (policy instanceof LegacyGlueFocusTraversalPolicy ) { 1222 ((LegacyGlueFocusTraversalPolicy )policy). 1223 unsetNextFocusableComponent(this, nextFocusableComponent); 1224 } 1225 } 1226 1227 1248 @Deprecated 1249 public void setNextFocusableComponent(Component aComponent) { 1250 boolean displayable = isDisplayable(); 1251 if (displayable) { 1252 deregisterNextFocusableComponent(); 1253 } 1254 putClientProperty(NEXT_FOCUS, aComponent); 1255 if (displayable) { 1256 registerNextFocusableComponent(aComponent); 1257 } 1258 } 1259 1260 1278 @Deprecated 1279 public Component getNextFocusableComponent() { 1280 return (Component)getClientProperty(NEXT_FOCUS); 1281 } 1282 1283 1306 public void setRequestFocusEnabled(boolean requestFocusEnabled) { 1307 setFlag(REQUEST_FOCUS_DISABLED, !requestFocusEnabled); 1308 } 1309 1310 1327 public boolean isRequestFocusEnabled() { 1328 return !getFlag(REQUEST_FOCUS_DISABLED); 1329 } 1330 1331 private boolean runInputVerifier() { 1332 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 1333 boolean inActivation; 1334 try { 1335 AccessibleMethod accessibleMethod 1336 = new AccessibleMethod(KeyboardFocusManager.class, 1337 "isInActivation"); 1338 inActivation = (Boolean ) (accessibleMethod.invokeNoChecked(kfm)); 1339 } catch (NoSuchMethodException e) { 1340 throw new AssertionError ( 1341 "Couldn't locate method KeyboardFocusManager.isInActivation()"); 1342 } 1343 1344 if (inActivation) { 1345 return true; 1348 } 1349 1350 if (inInputVerifier) { 1351 return true; 1354 } 1355 Component focusOwner = kfm.getFocusOwner(); 1356 1357 if (focusOwner == null) { 1358 Window window = SwingUtilities.getWindowAncestor(this); 1365 if (window != null) { 1366 try { 1367 AccessibleMethod accessibleMethod 1368 = new AccessibleMethod(KeyboardFocusManager.class, 1369 "getMostRecentFocusOwner", 1370 Window.class); 1371 focusOwner = (Component) (accessibleMethod.invokeNoChecked(null, 1372 window)); 1373 } catch (NoSuchMethodException e) { 1374 throw new AssertionError ("Couldn't locate method " + 1375 "KeyboardFocusManager.getMostRecentFocusOwner()"); 1376 } 1377 } 1378 } 1379 1380 if (focusOwner == this) { 1381 return true; 1382 } 1383 1384 if (!getVerifyInputWhenFocusTarget()) { 1385 return true; 1386 } 1387 1388 if (focusOwner == null || !(focusOwner instanceof JComponent )) { 1389 return true; 1390 } 1391 1392 JComponent jFocusOwner = (JComponent )focusOwner; 1393 InputVerifier iv = jFocusOwner.getInputVerifier(); 1394 1395 if (iv == null) { 1396 return true; 1397 } else { 1398 inInputVerifier = true; 1399 try { 1400 return iv.shouldYieldFocus(jFocusOwner); 1401 } finally { 1402 inInputVerifier = false; 1403 } 1404 } 1405 } 1406 1407 1423 public void requestFocus() { 1424 if (runInputVerifier()) { 1425 super.requestFocus(); 1426 } 1427 } 1428 1429 1451 public boolean requestFocus(boolean temporary) { 1452 return (runInputVerifier()) 1453 ? super.requestFocus(temporary) 1454 : false; 1455 } 1456 1457 1474 public boolean requestFocusInWindow() { 1475 return (runInputVerifier()) 1476 ? super.requestFocusInWindow() 1477 : false; 1478 } 1479 1480 1496 protected boolean requestFocusInWindow(boolean temporary) { 1497 return (runInputVerifier()) 1498 ? super.requestFocusInWindow(temporary) 1499 : false; 1500 } 1501 1502 1514 public void grabFocus() { 1515 requestFocus(); 1516 } 1517 1518 1539 public void setVerifyInputWhenFocusTarget(boolean 1540 verifyInputWhenFocusTarget) { 1541 boolean oldVerifyInputWhenFocusTarget = 1542 this.verifyInputWhenFocusTarget; 1543 this.verifyInputWhenFocusTarget = verifyInputWhenFocusTarget; 1544 firePropertyChange("verifyInputWhenFocusTarget", 1545 oldVerifyInputWhenFocusTarget, 1546 verifyInputWhenFocusTarget); 1547 } 1548 1549 1563 public boolean getVerifyInputWhenFocusTarget() { 1564 return verifyInputWhenFocusTarget; 1565 } 1566 1567 1568 1577 public FontMetrics getFontMetrics(Font font) { 1578 if (font != null && SwingUtilities2.drawTextAntialiased(aaText)) { 1579 synchronized(aaFontMap) { 1580 FontMetrics aaMetrics = aaFontMap.get(font); 1581 if (aaMetrics == null) { 1582 aaMetrics = new FontDesignMetrics( 1583 font, SwingUtilities2.AA_FRC); 1584 aaFontMap.put(font, aaMetrics); 1585 } 1586 return aaMetrics; 1587 } 1588 } 1589 return super.getFontMetrics(font); 1590 } 1591 1592 1593 1602 public void setPreferredSize(Dimension preferredSize) { 1603 super.setPreferredSize(preferredSize); 1604 } 1605 1606 1607 1618 public Dimension getPreferredSize() { 1619 if (isPreferredSizeSet()) { 1620 return super.getPreferredSize(); 1621 } 1622 Dimension size = null; 1623 if (ui != null) { 1624 size = ui.getPreferredSize(this); 1625 } 1626 return (size != null) ? size : super.getPreferredSize(); 1627 } 1628 1629 1630 1644 public void setMaximumSize(Dimension maximumSize) { 1645 super.setMaximumSize(maximumSize); 1646 } 1647 1648 1649 1659 public Dimension getMaximumSize() { 1660 if (isMaximumSizeSet()) { 1661 return super.getMaximumSize(); 1662 } 1663 Dimension size = null; 1664 if (ui != null) { 1665 size = ui.getMaximumSize(this); 1666 } 1667 return (size != null) ? size : super.getMaximumSize(); 1668 } 1669 1670 1671 1684 public void setMinimumSize(Dimension minimumSize) { 1685 super.setMinimumSize(minimumSize); 1686 } 1687 1688 1698 public Dimension getMinimumSize() { 1699 if (isMinimumSizeSet()) { 1700 return super.getMinimumSize(); 1701 } 1702 Dimension size = null; 1703 if (ui != null) { 1704 size = ui.getMinimumSize(this); 1705 } 1706 return (size != null) ? size : super.getMinimumSize(); 1707 } 1708 1709 1717 public boolean contains(int x, int y) { 1718 return (ui != null) ? ui.contains(this, x, y) : super.contains(x, y); 1719 } 1720 1721 1752 public void setBorder(Border border) { 1753 Border oldBorder = this.border; 1754 1755 this.border = border; 1756 firePropertyChange("border", oldBorder, border); 1757 if (border != oldBorder) { 1758 if (border == null || oldBorder == null || 1759 !(border.getBorderInsets(this).equals(oldBorder.getBorderInsets(this)))) { 1760 revalidate(); 1761 } 1762 repaint(); 1763 } 1764 } 1765 1766 1773 public Border getBorder() { 1774 return border; 1775 } 1776 1777 1784 public Insets getInsets() { 1785 if (border != null) { 1786 return border.getBorderInsets(this); 1787 } 1788 return super.getInsets(); 1789 } 1790 1791 1805 public Insets getInsets(Insets insets) { 1806 if (insets == null) { 1807 insets = new Insets(0, 0, 0, 0); 1808 } 1809 if (border != null) { 1810 if (border instanceof AbstractBorder) { 1811 return ((AbstractBorder)border).getBorderInsets(this, insets); 1812 } else { 1813 return border.getBorderInsets(this); 1816 } 1817 } else { 1818 insets.left = insets.top = insets.right = insets.bottom = 0; 1821 return insets; 1822 } 1823 } 1824 1825 1833 public float getAlignmentY() { 1834 if (isAlignmentYSet) { 1835 return alignmentY; 1836 } 1837 return super.getAlignmentY(); 1838 } 1839 1840 1848 public void setAlignmentY(float alignmentY) { 1849 this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY; 1850 isAlignmentYSet = true; 1851 } 1852 1853 1854 1862 public float getAlignmentX() { 1863 if (isAlignmentXSet) { 1864 return alignmentX; 1865 } 1866 return super.getAlignmentX(); 1867 } 1868 1869 1877 public void setAlignmentX(float alignmentX) { 1878 this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX; 1879 isAlignmentXSet = true; 1880 } 1881 1882 1892 public void setInputVerifier(InputVerifier inputVerifier) { 1893 InputVerifier oldInputVerifier = (InputVerifier )getClientProperty( 1894 INPUT_VERIFIER_KEY); 1895 putClientProperty(INPUT_VERIFIER_KEY, inputVerifier); 1896 firePropertyChange("inputVerifier", oldInputVerifier, inputVerifier); 1897 } 1898 1899 1906 public InputVerifier getInputVerifier() { 1907 return (InputVerifier )getClientProperty(INPUT_VERIFIER_KEY); 1908 } 1909 1910 1916 public Graphics getGraphics() { 1917 if (DEBUG_GRAPHICS_LOADED && shouldDebugGraphics() != 0) { 1918 DebugGraphics graphics = new DebugGraphics (super.getGraphics(), 1919 this); 1920 return graphics; 1921 } 1922 return super.getGraphics(); 1923 } 1924 1925 1926 1951 public void setDebugGraphicsOptions(int debugOptions) { 1952 DebugGraphics.setDebugOptions(this, debugOptions); 1953 } 1954 1955 1970 public int getDebugGraphicsOptions() { 1971 return DebugGraphics.getDebugOptions(this); 1972 } 1973 1974 1975 1979 int shouldDebugGraphics() { 1980 return DebugGraphics.shouldComponentDebug(this); 1981 } 1982 1983 2047 public void registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition) { 2048 2049 InputMap inputMap = getInputMap(aCondition, true); 2050 2051 if (inputMap != null) { 2052 ActionMap actionMap = getActionMap(true); 2053 ActionStandin action = new ActionStandin(anAction, aCommand); 2054 inputMap.put(aKeyStroke, action); 2055 if (actionMap != null) { 2056 actionMap.put(action, action); 2057 } 2058 } 2059 } 2060 2061 2071 private void registerWithKeyboardManager(boolean onlyIfNew) { 2072 InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW, false); 2073 KeyStroke [] strokes; 2074 Hashtable registered = (Hashtable )getClientProperty 2075 (WHEN_IN_FOCUSED_WINDOW_BINDINGS); 2076 2077 if (inputMap != null) { 2078 strokes = inputMap.allKeys(); 2080 if (strokes != null) { 2081 for (int counter = strokes.length - 1; counter >= 0; 2082 counter--) { 2083 if (!onlyIfNew || registered == null || 2084 registered.get(strokes[counter]) == null) { 2085 registerWithKeyboardManager(strokes[counter]); 2086 } 2087 if (registered != null) { 2088 registered.remove(strokes[counter]); 2089 } 2090 } 2091 } 2092 } 2093 else { 2094 strokes = null; 2095 } 2096 if (registered != null && registered.size() > 0) { 2098 Enumeration keys = registered.keys(); 2099 2100 while (keys.hasMoreElements()) { 2101 KeyStroke ks = (KeyStroke )keys.nextElement(); 2102 unregisterWithKeyboardManager(ks); 2103 } 2104 registered.clear(); 2105 } 2106 if (strokes != null && strokes.length > 0) { 2108 if (registered == null) { 2109 registered = new Hashtable (strokes.length); 2110 putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, registered); 2111 } 2112 for (int counter = strokes.length - 1; counter >= 0; counter--) { 2113 registered.put(strokes[counter], strokes[counter]); 2114 } 2115 } 2116 else { 2117 putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, null); 2118 } 2119 } 2120 2121 2125 private void unregisterWithKeyboardManager() { 2126 Hashtable registered = (Hashtable )getClientProperty 2127 (WHEN_IN_FOCUSED_WINDOW_BINDINGS); 2128 2129 if (registered != null && registered.size() > 0) { 2130 Enumeration keys = registered.keys(); 2131 2132 while (keys.hasMoreElements()) { 2133 KeyStroke ks = (KeyStroke )keys.nextElement(); 2134 unregisterWithKeyboardManager(ks); 2135 } 2136 } 2137 putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, null); 2138 } 2139 2140 2148 void componentInputMapChanged(ComponentInputMap inputMap) { 2149 InputMap km = getInputMap(WHEN_IN_FOCUSED_WINDOW, false); 2150 2151 while (km != inputMap && km != null) { 2152 km = (ComponentInputMap )km.getParent(); 2153 } 2154 if (km != null) { 2155 registerWithKeyboardManager(false); 2156 } 2157 } 2158 2159 private void registerWithKeyboardManager(KeyStroke aKeyStroke) { 2160 KeyboardManager.getCurrentManager().registerKeyStroke(aKeyStroke,this); 2161 } 2162 2163 private void unregisterWithKeyboardManager(KeyStroke aKeyStroke) { 2164 KeyboardManager.getCurrentManager().unregisterKeyStroke(aKeyStroke, 2165 this); 2166 } 2167 2168 2173 public void registerKeyboardAction(ActionListener anAction,KeyStroke aKeyStroke,int aCondition) { 2174 registerKeyboardAction(anAction,null,aKeyStroke,aCondition); 2175 } 2176 2177 2191 public void unregisterKeyboardAction(KeyStroke aKeyStroke) { 2192 ActionMap am = getActionMap(false); 2193 for (int counter = 0; counter < 3; counter++) { 2194 InputMap km = getInputMap(counter, false); 2195 if (km != null) { 2196 Object actionID = km.get(aKeyStroke); 2197 2198 if (am != null && actionID != null) { 2199 am.remove(actionID); 2200 } 2201 km.remove(aKeyStroke); 2202 } 2203 } 2204 } 2205 2206 2213 public KeyStroke [] getRegisteredKeyStrokes() { 2214 int[] counts = new int[3]; 2215 KeyStroke [][] strokes = new KeyStroke [3][]; 2216 2217 for (int counter = 0; counter < 3; counter++) { 2218 InputMap km = getInputMap(counter, false); 2219 strokes[counter] = (km != null) ? km.allKeys() : null; 2220 counts[counter] = (strokes[counter] != null) ? 2221 strokes[counter].length : 0; 2222 } 2223 KeyStroke [] retValue = new KeyStroke [counts[0] + counts[1] + 2224 counts[2]]; 2225 for (int counter = 0, last = 0; counter < 3; counter++) { 2226 if (counts[counter] > 0) { 2227 System.arraycopy(strokes[counter], 0, retValue, last, 2228 counts[counter]); 2229 last += counts[counter]; 2230 } 2231 } 2232 return retValue; 2233 } 2234 2235 2247 public int getConditionForKeyStroke(KeyStroke aKeyStroke) { 2248 for (int counter = 0; counter < 3; counter++) { 2249 InputMap inputMap = getInputMap(counter, false); 2250 if (inputMap != null && inputMap.get(aKeyStroke) != null) { 2251 return counter; 2252 } 2253 } 2254 return UNDEFINED_CONDITION; 2255 } 2256 2257 2264 public ActionListener getActionForKeyStroke(KeyStroke aKeyStroke) { 2265 ActionMap am = getActionMap(false); 2266 2267 if (am == null) { 2268 return null; 2269 } 2270 for (int counter = 0; counter < 3; counter++) { 2271 InputMap inputMap = getInputMap(counter, false); 2272 if (inputMap != null) { 2273 Object actionBinding = inputMap.get(aKeyStroke); 2274 2275 if (actionBinding != null) { 2276 Action action = am.get(actionBinding); 2277 if (action instanceof ActionStandin) { 2278 return ((ActionStandin)action).actionListener; 2279 } 2280 return action; 2281 } 2282 } 2283 } 2284 return null; 2285 } 2286 2287 2294 public void resetKeyboardActions() { 2295 for (int counter = 0; counter < 3; counter++) { 2297 InputMap inputMap = getInputMap(counter, false); 2298 2299 if (inputMap != null) { 2300 inputMap.clear(); 2301 } 2302 } 2303 2304 ActionMap am = getActionMap(false); 2306 2307 if (am != null) { 2308 am.clear(); 2309 } 2310 } 2311 2312 2339 public final void setInputMap(int condition, InputMap map) { 2340 switch (condition) { 2341 case WHEN_IN_FOCUSED_WINDOW: 2342 if (map != null && !(map instanceof ComponentInputMap )) { 2343 throw new IllegalArgumentException ("WHEN_IN_FOCUSED_WINDOW InputMaps must be of type ComponentInputMap"); 2344 } 2345 windowInputMap = (ComponentInputMap )map; 2346 setFlag(WIF_INPUTMAP_CREATED, true); 2347 registerWithKeyboardManager(false); 2348 break; 2349 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: 2350 ancestorInputMap = map; 2351 setFlag(ANCESTOR_INPUTMAP_CREATED, true); 2352 break; 2353 case WHEN_FOCUSED: 2354 focusInputMap = map; 2355 setFlag(FOCUS_INPUTMAP_CREATED, true); 2356 break; 2357 default: 2358 throw new IllegalArgumentException ("condition must be one of JComponent.WHEN_IN_FOCUSED_WINDOW, JComponent.WHEN_FOCUSED or JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT"); 2359 } 2360 } 2361 2362 2372 public final InputMap getInputMap(int condition) { 2373 return getInputMap(condition, true); 2374 } 2375 2376 2384 public final InputMap getInputMap() { 2385 return getInputMap(WHEN_FOCUSED, true); 2386 } 2387 2388 2396 public final void setActionMap(ActionMap am) { 2397 actionMap = am; 2398 setFlag(ACTIONMAP_CREATED, true); 2399 } 2400 2401 2410 public final ActionMap getActionMap() { 2411 return getActionMap(true); 2412 } 2413 2414 2434 final InputMap getInputMap(int condition, boolean create) { 2435 switch (condition) { 2436 case WHEN_FOCUSED: 2437 if (getFlag(FOCUS_INPUTMAP_CREATED)) { 2438 return focusInputMap; 2439 } 2440 if (create) { 2442 InputMap km = new InputMap (); 2443 setInputMap(condition, km); 2444 return km; 2445 } 2446 break; 2447 case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: 2448 if (getFlag(ANCESTOR_INPUTMAP_CREATED)) { 2449 return ancestorInputMap; 2450 } 2451 if (create) { 2453 InputMap km = new InputMap (); 2454 setInputMap(condition, km); 2455 return km; 2456 } 2457 break; 2458 case WHEN_IN_FOCUSED_WINDOW: 2459 if (getFlag(WIF_INPUTMAP_CREATED)) { 2460 return windowInputMap; 2461 } 2462 if (create) { 2464 ComponentInputMap km = new ComponentInputMap (this); 2465 setInputMap(condition, km); 2466 return km; 2467 } 2468 break; 2469 default: 2470 throw new IllegalArgumentException ("condition must be one of JComponent.WHEN_IN_FOCUSED_WINDOW, JComponent.WHEN_FOCUSED or JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT"); 2471 } 2472 return null; 2473 } 2474 2475 2484 final ActionMap getActionMap(boolean create) { 2485 if (getFlag(ACTIONMAP_CREATED)) { 2486 return actionMap; 2487 } 2488 if (create) { 2490 ActionMap am = new ActionMap (); 2491 setActionMap(am); 2492 return am; 2493 } 2494 return null; 2495 } 2496 2497 2515 @Deprecated 2516 public boolean requestDefaultFocus() { 2517 Container nearestRoot = 2518 (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor(); 2519 if (nearestRoot == null) { 2520 return false; 2521 } 2522 Component comp = nearestRoot.getFocusTraversalPolicy(). 2523 getDefaultComponent(nearestRoot); 2524 if (comp != null) { 2525 comp.requestFocus(); 2526 return true; 2527 } else { 2528 return false; 2529 } 2530 } 2531 2532 2542 public void setVisible(boolean aFlag) { 2543 if(aFlag != isVisible()) { 2544 super.setVisible(aFlag); 2545 Container parent = getParent(); 2546 if(parent != null) { 2547 Rectangle r = getBounds(); 2548 parent.repaint(r.x,r.y,r.width,r.height); 2549 } 2550 revalidate(); 2554 } 2555 } 2556 2557 2579 public void setEnabled(boolean enabled) { 2580 boolean oldEnabled = isEnabled(); 2581 super.setEnabled(enabled); 2582 firePropertyChange("enabled", oldEnabled, enabled); 2583 if (enabled != oldEnabled) { 2584 repaint(); 2585 } 2586 } 2587 2588 2600 public void setForeground(Color fg) { 2601 Color oldFg = getForeground(); 2602 super.setForeground(fg); 2603 if ((oldFg != null) ? !oldFg.equals(fg) : ((fg != null) && !fg.equals(oldFg))) { 2604 repaint(); 2606 } 2607 } 2608 2609 2621 public void setBackground(Color bg) { 2622 Color oldBg = getBackground(); 2623 super.setBackground(bg); 2624 if ((oldBg != null) ? !oldBg.equals(bg) : ((bg != null) && !bg.equals(oldBg))) { 2625 repaint(); 2627 } 2628 } 2629 2630 2642 public void setFont(Font font) { 2643 Font oldFont = getFont(); 2644 super.setFont(font); 2645 if (font != oldFont) { 2647 revalidate(); 2648 repaint(); 2649 } 2650 } 2651 2652 2667 static public Locale getDefaultLocale() { 2668 Locale l = (Locale ) SwingUtilities.appContextGet(defaultLocale); 2669 if( l == null ) { 2670 l = Locale.getDefault(); 2673 JComponent.setDefaultLocale( l ); 2674 } 2675 return l; 2676 } 2677 2678 2679 2694 static public void setDefaultLocale( Locale l ) { 2695 SwingUtilities.appContextPut(defaultLocale, l); 2696 } 2697 2698 2699 2713 protected void processComponentKeyEvent(KeyEvent e) { 2714 } 2715 2716 2717 protected void processKeyEvent(KeyEvent e) { 2718 boolean result; 2719 boolean shouldProcessKey; 2720 2721 super.processKeyEvent(e); 2723 2724 if (! e.isConsumed()) { 2726 processComponentKeyEvent(e); 2727 } 2728 2729 shouldProcessKey = KeyboardState.shouldProcess(e); 2730 2731 if(e.isConsumed()) { 2732 return; 2733 } 2734 2735 if (shouldProcessKey && processKeyBindings(e, e.getID() == 2736 KeyEvent.KEY_PRESSED)) { 2737 e.consume(); 2738 } 2739 } 2740 2741 2763 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 2764 int condition, boolean pressed) { 2765 InputMap map = getInputMap(condition, false); 2766 ActionMap am = getActionMap(false); 2767 2768 if(map != null && am != null && isEnabled()) { 2769 Object binding = map.get(ks); 2770 Action action = (binding == null) ? null : am.get(binding); 2771 if (action != null) { 2772 return SwingUtilities.notifyAction(action, ks, e, this, 2773 e.getModifiers()); 2774 } 2775 } 2776 return false; 2777 } 2778 2779 2791 boolean processKeyBindings(KeyEvent e, boolean pressed) { 2792 if (!SwingUtilities.isValidKeyEventForKeyBindings(e)) { 2793 return false; 2794 } 2795 KeyStroke ks; 2797 2798 if (e.getID() == KeyEvent.KEY_TYPED) { 2799 ks = KeyStroke.getKeyStroke(e.getKeyChar()); 2800 } 2801 else { 2802 ks = KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(), 2803 (pressed ? false:true)); 2804 } 2805 2806 2807 if(processKeyBinding(ks, e, WHEN_FOCUSED, pressed)) 2808 return true; 2809 2810 2814 Container parent = this; 2815 while (parent != null && !(parent instanceof Window) && 2816 !(parent instanceof Applet )) { 2817 if(parent instanceof JComponent ) { 2818 if(((JComponent )parent).processKeyBinding(ks, e, 2819 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed)) 2820 return true; 2821 } 2822 if ((parent instanceof JInternalFrame ) && 2830 JComponent.processKeyBindingsForAllComponents(e,parent,pressed)){ 2831 return true; 2832 } 2833 parent = parent.getParent(); 2834 } 2835 2836 2840 if(parent != null) { 2841 return JComponent.processKeyBindingsForAllComponents(e,parent,pressed); 2842 } 2843 return false; 2844 } 2845 2846 static boolean processKeyBindingsForAllComponents(KeyEvent e, 2847 Container container, boolean pressed) { 2848 while (true) { 2849 if (KeyboardManager.getCurrentManager().fireKeyboardAction( 2850 e, pressed, container)) { 2851 return true; 2852 } 2853 if (container instanceof Popup.HeavyWeightWindow ) { 2854 container = ((Window)container).getOwner(); 2855 } 2856 else { 2857 return false; 2858 } 2859 } 2860 } 2861 2862 2877 public void setToolTipText(String text) { 2878 String oldText = getToolTipText(); 2879 putClientProperty(TOOL_TIP_TEXT_KEY, text); 2880 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 2881 if (text != null) { 2882 if (oldText == null) { 2883 toolTipManager.registerComponent(this); 2884 } 2885 } else { 2886 toolTipManager.unregisterComponent(this); 2887 } 2888 } 2889 2890 2897 public String getToolTipText() { 2898 return (String )getClientProperty(TOOL_TIP_TEXT_KEY); 2899 } 2900 2901 2902 2909 public String getToolTipText(MouseEvent event) { 2910 return getToolTipText(); 2911 } 2912 2913 2922 public Point getToolTipLocation(MouseEvent event) { 2923 return null; 2924 } 2925 2926 2938 public Point getPopupLocation(MouseEvent event) { 2939 return null; 2940 } 2941 2942 2943 2952 public JToolTip createToolTip() { 2953 JToolTip tip = new JToolTip (); 2954 tip.setComponent(this); 2955 return tip; 2956 } 2957 2958 2967 public void scrollRectToVisible(Rectangle aRect) { 2968 Container parent; 2969 int dx = getX(), dy = getY(); 2970 2971 for (parent = getParent(); 2972 !(parent == null) && 2973 !(parent instanceof JComponent ) && 2974 !(parent instanceof CellRendererPane ); 2975 parent = parent.getParent()) { 2976 Rectangle bounds = parent.getBounds(); 2977 2978 dx += bounds.x; 2979 dy += bounds.y; 2980 } 2981 2982 if (!(parent == null) && !(parent instanceof CellRendererPane )) { 2983 aRect.x += dx; 2984 aRect.y += dy; 2985 2986 ((JComponent )parent).scrollRectToVisible(aRect); 2987 aRect.x -= dx; 2988 aRect.y -= dy; 2989 } 2990 } 2991 2992 3036 public void setAutoscrolls(boolean autoscrolls) { 3037 setFlag(AUTOSCROLLS_SET, true); 3038 if (this.autoscrolls != autoscrolls) { 3039 this.autoscrolls = autoscrolls; 3040 if (autoscrolls) { 3041 enableEvents(AWTEvent.MOUSE_EVENT_MASK); 3042 enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); 3043 } 3044 else { 3045 Autoscroller.stop(this); 3046 } 3047 } 3048 } 3049 3050 3057 public boolean getAutoscrolls() { 3058 return autoscrolls; 3059 } 3060 3061 3089 public void setTransferHandler(TransferHandler newHandler) { 3090 TransferHandler oldHandler = (TransferHandler )getClientProperty( 3091 TRANSFER_HANDLER_KEY); 3092 putClientProperty(TRANSFER_HANDLER_KEY, newHandler); 3093 3094 if (! getSuppressDropTarget()) { 3095 DropTarget dropHandler = getDropTarget(); 3096 if ((dropHandler == null) || (dropHandler instanceof UIResource)) { 3097 if (newHandler == null) { 3098 setDropTarget(null); 3099 } else if (!GraphicsEnvironment.isHeadless()) { 3100 setDropTarget(new TransferHandler.SwingDropTarget (this)); 3101 } 3102 } 3103 } 3104 firePropertyChange("transferHandler", oldHandler, newHandler); 3105 } 3106 3107 3116 public TransferHandler getTransferHandler() { 3117 return (TransferHandler )getClientProperty(TRANSFER_HANDLER_KEY); 3118 } 3119 3120 3131 protected void processMouseEvent(MouseEvent e) { 3132 if (autoscrolls && e.getID() == MouseEvent.MOUSE_RELEASED) { 3133 Autoscroller.stop(this); 3134 } 3135 super.processMouseEvent(e); 3136 } 3137 3138 3144 protected void processMouseMotionEvent(MouseEvent e) { 3145 boolean dispatch = true; 3146 if (autoscrolls && e.getID() == MouseEvent.MOUSE_DRAGGED) { 3147 dispatch = !Autoscroller.isRunning(this); 3150 Autoscroller.processMouseDragged(e); 3151 } 3152 if (dispatch) { 3153 super.processMouseMotionEvent(e); 3154 } 3155 } 3156 3157 void superProcessMouseMotionEvent(MouseEvent e) { 3159 super.processMouseMotionEvent(e); 3160 } 3161 3162 3168 void setCreatedDoubleBuffer(boolean newValue) { 3169 setFlag(CREATED_DOUBLE_BUFFER, newValue); 3170 } 3171 3172 3178 boolean getCreatedDoubleBuffer() { 3179 return getFlag(CREATED_DOUBLE_BUFFER); 3180 } 3181 3182 3187 final class ActionStandin implements Action { 3188 private final ActionListener actionListener; 3189 private final String command; 3190 private final Action action; 3192 3193 ActionStandin(ActionListener actionListener, String command) { 3194 this.actionListener = actionListener; 3195 if (actionListener instanceof Action ) { 3196 this.action = (Action )actionListener; 3197 } 3198 else { 3199 this.action = null; 3200 } 3201 this.command = command; 3202 } 3203 3204 public Object getValue(String key) { 3205 if (key != null) { 3206 if (key.equals(Action.ACTION_COMMAND_KEY)) { 3207 return command; 3208 } 3209 if (action != null) { 3210 return action.getValue(key); 3211 } 3212 if (key.equals(NAME)) { 3213 return "ActionStandin"; 3214 } 3215 } 3216 return null; 3217 } 3218 3219 public boolean isEnabled() { 3220 if (actionListener == null) { 3221 return false; 3227 } 3228 if (action == null) { 3229 return true; 3230 } 3231 return action.isEnabled(); 3232 } 3233 3234 public void actionPerformed(ActionEvent ae) { 3235 if (actionListener != null) { 3236 actionListener.actionPerformed(ae); 3237 } 3238 } 3239 3240 public void putValue(String key, Object value) {} 3242 3243 public void setEnabled(boolean b) { } 3246 3247 public void addPropertyChangeListener 3248 (PropertyChangeListener listener) {} 3249 public void removePropertyChangeListener 3250 (PropertyChangeListener listener) {} 3251 } 3252 3253 3254 static final class IntVector { 3257 int array[] = null; 3258 int count = 0; 3259 int capacity = 0; 3260 3261 int size() { 3262 return count; 3263 } 3264 3265 int elementAt(int index) { 3266 return array[index]; 3267 } 3268 3269 void addElement(int value) { 3270 if (count == capacity) { 3271 capacity = (capacity + 2) * 2; 3272 int[] newarray = new int[capacity]; 3273 if (count > 0) { 3274 System.arraycopy(array, 0, newarray, 0, count); 3275 } 3276 array = newarray; 3277 } 3278 array[count++] = value; 3279 } 3280 3281 void setElementAt(int value, int index) { 3282 array[index] = value; 3283 } 3284 } 3285 3286 static class KeyboardState implements Serializable { 3287 private static final Object keyCodesKey = 3288 JComponent.KeyboardState .class; 3289 3290 static IntVector getKeyCodeArray() { 3292 IntVector iv = 3293 (IntVector)SwingUtilities.appContextGet(keyCodesKey); 3294 if (iv == null) { 3295 iv = new IntVector(); 3296 SwingUtilities.appContextPut(keyCodesKey, iv); 3297 } 3298 return iv; 3299 } 3300 3301 static void registerKeyPressed(int keyCode) { 3302 IntVector kca = getKeyCodeArray(); 3303 int count = kca.size(); 3304 int i; 3305 for(i=0;i<count;i++) { 3306 if(kca.elementAt(i) == -1){ 3307 kca.setElementAt(keyCode, i); 3308 return; 3309 } 3310 } 3311 kca.addElement(keyCode); 3312 } 3313 3314 static void registerKeyReleased(int keyCode) { 3315 IntVector kca = getKeyCodeArray(); 3316 int count = kca.size(); 3317 int i; 3318 for(i=0;i<count;i++) { 3319 if(kca.elementAt(i) == keyCode) { 3320 kca.setElementAt(-1, i); 3321 return; 3322 } 3323 } 3324 } 3325 3326 static boolean keyIsPressed(int keyCode) { 3327 IntVector kca = getKeyCodeArray(); 3328 int count = kca.size(); 3329 int i; 3330 for(i=0;i<count;i++) { 3331 if(kca.elementAt(i) == keyCode) { 3332 return true; 3333 } 3334 } 3335 return false; 3336 } 3337 3338 3342 static boolean shouldProcess(KeyEvent e) { 3343 switch (e.getID()) { 3344 case KeyEvent.KEY_PRESSED: 3345 if (!keyIsPressed(e.getKeyCode())) { 3346 registerKeyPressed(e.getKeyCode()); 3347 } 3348 return true; 3349 case KeyEvent.KEY_RELEASED: 3350 if (keyIsPressed(e.getKeyCode()) || e.getKeyCode()==KeyEvent.VK_PRINTSCREEN) { 3355 registerKeyReleased(e.getKeyCode()); 3356 return true; 3357 } 3358 return false; 3359 case KeyEvent.KEY_TYPED: 3360 return true; 3361 default: 3362 return false; 3364 } 3365 } 3366 } 3367 3368 3369 3372 3373 3377 @Deprecated 3378 public void enable() { 3379 if (isEnabled() != true) { 3380 super.enable(); 3381 if (accessibleContext != null) { 3382 accessibleContext.firePropertyChange( 3383 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 3384 null, AccessibleState.ENABLED); 3385 } 3386 } 3387 } 3388 3389 3393 @Deprecated 3394 public void disable() { 3395 if (isEnabled() != false) { 3396 super.disable(); 3397 if (accessibleContext != null) { 3398 accessibleContext.firePropertyChange( 3399 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 3400 AccessibleState.ENABLED, null); 3401 } 3402 } 3403 } 3404 3405 3409 protected AccessibleContext accessibleContext = null; 3410 3411 3421 public AccessibleContext getAccessibleContext() { 3422 return accessibleContext; 3423 } 3424 3425 3440 public abstract class AccessibleJComponent extends AccessibleAWTContainer 3441 implements AccessibleExtendedComponent 3442 { 3443 3447 protected AccessibleJComponent() { 3448 super(); 3449 } 3450 3451 protected ContainerListener accessibleContainerHandler = null; 3452 protected FocusListener accessibleFocusHandler = null; 3453 3454 3458 protected class AccessibleContainerHandler 3459 implements ContainerListener { 3460 public void componentAdded(ContainerEvent e) { 3461 Component c = e.getChild(); 3462 if (c != null && c instanceof Accessible) { 3463 AccessibleJComponent.this.firePropertyChange( 3464 AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, 3465 null, ((Accessible) c).getAccessibleContext()); 3466 } 3467 } 3468 public void componentRemoved(ContainerEvent e) { 3469 Component c = e.getChild(); 3470 if (c != null && c instanceof Accessible) { 3471 AccessibleJComponent.this.firePropertyChange( 3472 AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, 3473 ((Accessible) c).getAccessibleContext(), null); 3474 } 3475 } 3476 } 3477 3478 3482 protected class AccessibleFocusHandler implements FocusListener { 3483 public void focusGained(FocusEvent event) { 3484 if (accessibleContext != null) { 3485 accessibleContext.firePropertyChange( 3486 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 3487 null, AccessibleState.FOCUSED); 3488 } 3489 } 3490 public void focusLost(FocusEvent event) { 3491 if (accessibleContext != null) { 3492 accessibleContext.firePropertyChange( 3493 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 3494 AccessibleState.FOCUSED, null); 3495 } 3496 } 3497 } 3499 3500 3505 public void addPropertyChangeListener(PropertyChangeListener listener) { 3506 if (accessibleFocusHandler == null) { 3507 accessibleFocusHandler = new AccessibleFocusHandler(); 3508 JComponent.this.addFocusListener(accessibleFocusHandler); 3509 } 3510 if (accessibleContainerHandler == null) { 3511 accessibleContainerHandler = new AccessibleContainerHandler(); 3512 JComponent.this.addContainerListener(accessibleContainerHandler); 3513 } 3514 super.addPropertyChangeListener(listener); 3515 } 3516 3517 3524 public void removePropertyChangeListener(PropertyChangeListener listener) { 3525 if (accessibleFocusHandler != null) { 3526 JComponent.this.removeFocusListener(accessibleFocusHandler); 3527 accessibleFocusHandler = null; 3528 } 3529 super.removePropertyChangeListener(listener); 3530 } 3531 3532 3533 3534 3543 protected String getBorderTitle(Border b) { 3544 String s; 3545 if (b instanceof TitledBorder) { 3546 return ((TitledBorder) b).getTitle(); 3547 } else if (b instanceof CompoundBorder) { 3548 s = getBorderTitle(((CompoundBorder) b).getInsideBorder()); 3549 if (s == null) { 3550 s = getBorderTitle(((CompoundBorder) b).getOutsideBorder()); 3551 } 3552 return s; 3553 } else { 3554 return null; 3555 } 3556 } 3557 3558 3574 public String getAccessibleName() { 3575 String name = accessibleName; 3576 3577 if (name == null) { 3580 name = getBorderTitle(getBorder()); 3581 } 3582 3583 if (name == null) { 3586 Object o = getClientProperty(JLabel.LABELED_BY_PROPERTY); 3587 if (o instanceof Accessible) { 3588 AccessibleContext ac = ((Accessible) o).getAccessibleContext(); 3589 if (ac != null) { 3590 name = ac.getAccessibleName(); 3591 } 3592 } 3593 } 3594 return name; 3595 } 3596 3597 3612 public String getAccessibleDescription() { 3613 String description = accessibleDescription; 3614 3615 if (description == null) { 3618 try { 3619 description = getToolTipText(); 3620 } catch (Exception e) { 3621 } 3631 } 3632 3633 if (description == null) { 3636 Object o = getClientProperty(JLabel.LABELED_BY_PROPERTY); 3637 if (o instanceof Accessible) { 3638 AccessibleContext ac = ((Accessible) o).getAccessibleContext(); 3639 if (ac != null) { 3640 description = ac.getAccessibleDescription(); 3641 } 3642 } 3643 } 3644 3645 return description; 3646 } 3647 3648 3655 public AccessibleRole getAccessibleRole() { 3656 return AccessibleRole.SWING_COMPONENT; 3657 } 3658 3659 3666 public AccessibleStateSet getAccessibleStateSet() { 3667 AccessibleStateSet states = super.getAccessibleStateSet(); 3668 if (JComponent.this.isOpaque()) { 3669 states.add(AccessibleState.OPAQUE); 3670 } 3671 return states; 3672 } 3673 3674 3681 public int getAccessibleChildrenCount() { 3682 return super.getAccessibleChildrenCount(); 3683 } 3684 3685 3691 public Accessible getAccessibleChild(int i) { 3692 return super.getAccessibleChild(i); 3693 } 3694 3695 3697 3702 AccessibleExtendedComponent getAccessibleExtendedComponent() { 3703 return this; 3704 } 3705 3706 3712 public String getToolTipText() { 3713 return null; 3714 } 3715 3716 3722 public String getTitledBorderText() { 3723 Border border = JComponent.this.getBorder(); 3724 if (border instanceof TitledBorder) { 3725 return ((TitledBorder)border).getTitle(); 3726 } else { 3727 return null; 3728 } 3729 } 3730 3731 3738 public AccessibleKeyBinding getAccessibleKeyBinding() { 3739 return null; 3740 } 3741 } 3743 3744 3745 3755 private ArrayTable getClientProperties() { 3756 if (clientProperties == null) { 3757 clientProperties = new ArrayTable (); 3758 } 3759 return clientProperties; 3760 } 3761 3762 3763 3772 public final Object getClientProperty(Object key) { 3773 if (key == SwingUtilities2.AA_TEXT_PROPERTY_KEY) { 3774 return Boolean.valueOf(aaText); 3775 } 3776 if(clientProperties == null) { 3777 return null; 3778 } else { 3779 synchronized(clientProperties) { 3780 return clientProperties.get(key); 3781 } 3782 } 3783 } 3784 3785 3813 public final void putClientProperty(Object key, Object value) { 3814 if (value == null && clientProperties == null) { 3815 return; 3818 } 3819 if (key == SwingUtilities2.AA_TEXT_PROPERTY_KEY) { 3820 if (value instanceof Boolean ) { 3821 aaText = ((Boolean )value).booleanValue(); 3822 } 3823 return; 3824 } 3825 ArrayTable clientProperties = getClientProperties(); 3826 Object oldValue; 3827 synchronized(clientProperties) { 3828 oldValue = clientProperties.get(key); 3829 if (value != null) { 3830 clientProperties.put(key, value); 3831 } else if (oldValue != null) { 3832 clientProperties.remove(key); 3833 } else { 3834 return; 3836 } 3837 } 3838 firePropertyChange(key.toString(), oldValue, value); 3839 } 3840 3841 3842 3852 void setUIProperty(String propertyName, Object value) { 3853 if (propertyName == "opaque") { 3854 if (!getFlag(OPAQUE_SET)) { 3855 setOpaque(((Boolean )value).booleanValue()); 3856 setFlag(OPAQUE_SET, false); 3857 } 3858 } else if (propertyName == "autoscrolls") { 3859 if (!getFlag(AUTOSCROLLS_SET)) { 3860 setAutoscrolls(((Boolean )value).booleanValue()); 3861 setFlag(AUTOSCROLLS_SET, false); 3862 } 3863 } else if (propertyName == "focusTraversalKeysForward") { 3864 if (!getFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET)) { 3865 super.setFocusTraversalKeys(KeyboardFocusManager. 3866 FORWARD_TRAVERSAL_KEYS, 3867 (Set )value); 3868 } 3869 } else if (propertyName == "focusTraversalKeysBackward") { 3870 if (!getFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET)) { 3871 super.setFocusTraversalKeys(KeyboardFocusManager. 3872 BACKWARD_TRAVERSAL_KEYS, 3873 (Set )value); 3874 } 3875 } else { 3876 throw new IllegalArgumentException ("property \""+ 3877 propertyName+ "\" cannot be set using this method"); 3878 } 3879 } 3880 3881 3882 3908 public void 3909 setFocusTraversalKeys(int id, Set <? extends AWTKeyStroke> keystrokes) 3910 { 3911 if (id == KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) { 3912 setFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET,true); 3913 } else if (id == KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) { 3914 setFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET,true); 3915 } 3916 super.setFocusTraversalKeys(id,keystrokes); 3917 } 3918 3919 3923 3924 3930 public static boolean isLightweightComponent(Component c) { 3931 return c.getPeer() instanceof LightweightPeer; 3932 } 3933 3934 3935 3947 @Deprecated 3948 public void reshape(int x, int y, int w, int h) { 3949 super.reshape(x, y, w, h); 3950 } 3951 3952 3953 3966 public Rectangle getBounds(Rectangle rv) { 3967 if (rv == null) { 3968 return new Rectangle(getX(), getY(), getWidth(), getHeight()); 3969 } 3970 else { 3971 rv.setBounds(getX(), getY(), getWidth(), getHeight()); 3972 return rv; 3973 } 3974 } 3975 3976 3977 3988 public Dimension getSize(Dimension rv) { 3989 if (rv == null) { 3990 return new Dimension(getWidth(), getHeight()); 3991 } 3992 else { 3993 rv.setSize(getWidth(), getHeight()); 3994 return rv; 3995 } 3996 } 3997 3998 3999 4010 public Point getLocation(Point rv) { 4011 if (rv == null) { 4012 return new Point(getX(), getY()); 4013 } 4014 else { 4015 rv.setLocation(getX(), getY()); 4016 return rv; 4017 } 4018 } 4019 4020 4021 4030 public int getX() { return super.getX(); } 4031 4032 4033 4042 public int getY() { return super.getY(); } 4043 4044 4045 4054 public int getWidth() { return super.getWidth(); } 4055 4056 4057 4066 public int getHeight() { return super.getHeight(); } 4067 4068 4083 public boolean isOpaque() { 4084 return getFlag(IS_OPAQUE); 4085 } 4086 4087 4104 public void setOpaque(boolean isOpaque) { 4105 boolean oldValue = getFlag(IS_OPAQUE); 4106 setFlag(IS_OPAQUE, isOpaque); 4107 setFlag(OPAQUE_SET, true); 4108 firePropertyChange("opaque", oldValue, isOpaque); 4109 } 4110 4111 4112 4127 boolean rectangleIsObscured(int x,int y,int width,int height) 4128 { 4129 int numChildren = getComponentCount(); 4130 4131 for(int i = 0; i < numChildren; i++) { 4132 Component child = getComponent(i); 4133 int cx, cy, cw, ch; 4134 4135 cx = child.getX(); 4136 cy = child.getY(); 4137 cw = child.getWidth(); 4138 ch = child.getHeight(); 4139 4140 if (x >= cx && (x + width) <= (cx + cw) && 4141 y >= cy && (y + height) <= (cy + ch) && child.isVisible()) { 4142 4143 if(child instanceof JComponent ) { 4144 return ((JComponent )child).isOpaque(); 4148 } else { 4149 4152 return false; 4153 } 4154 } 4155 } 4156 4157 return false; 4158 } 4159 4160 4161 4174 static final void computeVisibleRect(Component c, Rectangle visibleRect) { 4175 Container p = c.getParent(); 4176 Rectangle bounds = c.getBounds(); 4177 4178 if (p == null || p instanceof Window || p instanceof Applet ) { 4179 visibleRect.setBounds(0, 0, bounds.width, bounds.height); 4180 } else { 4181 computeVisibleRect(p, visibleRect); 4182 visibleRect.x -= bounds.x; 4183 visibleRect.y -= bounds.y; 4184 SwingUtilities.computeIntersection(0,0,bounds.width,bounds.height,visibleRect); 4185 } 4186 } 4187 4188 4189 4201 public void computeVisibleRect(Rectangle visibleRect) { 4202 computeVisibleRect(this, visibleRect); 4203 } 4204 4205 4206 4214 public Rectangle getVisibleRect() { 4215 Rectangle visibleRect = new Rectangle(); 4216 4217 computeVisibleRect(visibleRect); 4218 return visibleRect; 4219 } 4220 4221 4231 public void firePropertyChange(String propertyName, 4232 boolean oldValue, boolean newValue) { 4233 super.firePropertyChange(propertyName, oldValue, newValue); 4234 } 4235 4236 4237 4247 public void firePropertyChange(String propertyName, 4248 int oldValue, int newValue) { 4249 super.firePropertyChange(propertyName, oldValue, newValue); 4250 } 4251 4252 public void firePropertyChange(String propertyName, char oldValue, char newValue) { 4255 super.firePropertyChange(propertyName, oldValue, newValue); 4256 } 4257 4258 4270 protected void fireVetoableChange(String propertyName, Object oldValue, Object newValue) 4271 throws java.beans.PropertyVetoException 4272 { 4273 if (vetoableChangeSupport == null) { 4274 return; 4275 } 4276 vetoableChangeSupport.fireVetoableChange(propertyName, oldValue, newValue); 4277 } 4278 4279 4280 4286 public synchronized void addVetoableChangeListener(VetoableChangeListener listener) { 4287 if (vetoableChangeSupport == null) { 4288 vetoableChangeSupport = new java.beans.VetoableChangeSupport (this); 4289 } 4290 vetoableChangeSupport.addVetoableChangeListener(listener); 4291 } 4292 4293 4294 4301 public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) { 4302 if (vetoableChangeSupport == null) { 4303 return; 4304 } 4305 vetoableChangeSupport.removeVetoableChangeListener(listener); 4306 } 4307 4308 4309 4322 public synchronized VetoableChangeListener[] getVetoableChangeListeners() { 4323 if (vetoableChangeSupport == null) { 4324 return new VetoableChangeListener[0]; 4325 } 4326 return vetoableChangeSupport.getVetoableChangeListeners(); 4327 } 4328 4329 4330 4339 public Container getTopLevelAncestor() { 4340 for(Container p = this; p != null; p = p.getParent()) { 4341 if(p instanceof Window || p instanceof Applet ) { 4342 return p; 4343 } 4344 } 4345 return null; 4346 } 4347 4348 private AncestorNotifier getAncestorNotifier() { 4349 return (AncestorNotifier )getClientProperty(ANCESTOR_NOTIFIER_KEY); 4350 } 4351 4352 4362 public void addAncestorListener(AncestorListener listener) { 4363 AncestorNotifier ancestorNotifier = getAncestorNotifier(); 4364 if (ancestorNotifier == null) { 4365 ancestorNotifier = new AncestorNotifier (this); 4366 putClientProperty(ANCESTOR_NOTIFIER_KEY, ancestorNotifier); 4367 } 4368 ancestorNotifier.addAncestorListener(listener); 4369 } 4370 4371 4378 public void removeAncestorListener(AncestorListener listener) { 4379 AncestorNotifier ancestorNotifier = getAncestorNotifier(); 4380 if (ancestorNotifier == null) { 4381 return; 4382 } 4383 ancestorNotifier.removeAncestorListener(listener); 4384 if (ancestorNotifier.listenerList.getListenerList().length == 0) { 4385 ancestorNotifier.removeAllListeners(); 4386 putClientProperty(ANCESTOR_NOTIFIER_KEY, null); 4387 } 4388 } 4389 4390 4403 public AncestorListener[] getAncestorListeners() { 4404 AncestorNotifier ancestorNotifier = getAncestorNotifier(); 4405 if (ancestorNotifier == null) { 4406 return new AncestorListener[0]; 4407 } 4408 return ancestorNotifier.getAncestorListeners(); 4409 } 4410 4411 4446 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 4447 T[] result; 4448 if (listenerType == AncestorListener.class) { 4449 result = (T[])getAncestorListeners(); 4451 } 4452 else if (listenerType == VetoableChangeListener.class) { 4453 result = (T[])getVetoableChangeListeners(); 4455 } 4456 else if (listenerType == PropertyChangeListener.class) { 4457 result = (T[])getPropertyChangeListeners(); 4459 } 4460 else { 4461 result = (T[])listenerList.getListeners(listenerType); 4462 } 4463 4464 if (result.length == 0) { 4465 return super.getListeners(listenerType); 4466 } 4467 return result; 4468 } 4469 4470 4477 public void addNotify() { 4478 super.addNotify(); 4479 firePropertyChange("ancestor", null, getParent()); 4480 4481 registerWithKeyboardManager(false); 4482 registerNextFocusableComponent(); 4483 } 4484 4485 4486 4493 public void removeNotify() { 4494 super.removeNotify(); 4495 firePropertyChange("ancestor", getParent(), null); 4499 4500 unregisterWithKeyboardManager(); 4501 deregisterNextFocusableComponent(); 4502 4503 if (getCreatedDoubleBuffer()) { 4504 RepaintManager.currentManager(this).resetDoubleBuffer(); 4505 setCreatedDoubleBuffer(false); 4506 } 4507 if (autoscrolls) { 4508 Autoscroller.stop(this); 4509 } 4510 } 4511 4512 4513 4526 public void repaint(long tm, int x, int y, int width, int height) { 4527 RepaintManager.currentManager(this).addDirtyRegion(this, x, y, width, height); 4528 } 4529 4530 4531 4540 public void repaint(Rectangle r) { 4541 repaint(0,r.x,r.y,r.width,r.height); 4542 } 4543 4544 4545 4571 public void revalidate() { 4572 if (getParent() == null) { 4573 return; 4580 } 4581 if (SwingUtilities.isEventDispatchThread()) { 4582 invalidate(); 4583 RepaintManager.currentManager(this).addInvalidComponent(this); 4584 } 4585 else { 4586 Runnable callRevalidate = new Runnable () { 4587 public void run() { 4588 revalidate(); 4589 } 4590 }; 4591 SwingUtilities.invokeLater(callRevalidate); 4592 } 4593 } 4594 4595 4607 public boolean isValidateRoot() { 4608 return false; 4609 } 4610 4611 4612 4622 public boolean isOptimizedDrawingEnabled() { 4623 return true; 4624 } 4625 4626 4633 boolean isPaintingOrigin() { 4634 return false; 4635 } 4636 4637 4653 public void paintImmediately(int x,int y,int w, int h) { 4654 Component c = this; 4655 Component parent; 4656 4657 if(!isShowing()) { 4658 return; 4659 } 4660 while(!((JComponent )c).isOpaque()) { 4661 parent = c.getParent(); 4662 if(parent != null) { 4663 x += c.getX(); 4664 y += c.getY(); 4665 c = parent; 4666 } else { 4667 break; 4668 } 4669 4670 if(!(c instanceof JComponent )) { 4671 break; 4672 } 4673 } 4674 if(c instanceof JComponent ) { 4675 ((JComponent )c)._paintImmediately(x,y,w,h); 4676 } else { 4677 c.repaint(x,y,w,h); 4678 } 4679 } 4680 4681 4686 public void paintImmediately(Rectangle r) { 4687 paintImmediately(r.x,r.y,r.width,r.height); 4688 } 4689 4690 4698 boolean alwaysOnTop() { 4700 return false; 4701 } 4702 4703 void setPaintingChild(Component paintingChild) { 4704 this.paintingChild = paintingChild; 4705 } 4706 4707 void _paintImmediately(int x, int y, int w, int h) { 4708 Graphics g; 4709 Container c; 4710 Rectangle b; 4711 4712 int tmpX, tmpY, tmpWidth, tmpHeight; 4713 int offsetX=0,offsetY=0; 4714 4715 boolean hasBuffer = false; 4716 4717 JComponent bufferedComponent = null; 4718 JComponent paintingComponent = this; 4719 4720 RepaintManager repaintManager = RepaintManager.currentManager(this); 4721 Vector path = new Vector (7); 4726 int pIndex = -1; 4727 int pCount = 0; 4728 4729 tmpX = tmpY = tmpWidth = tmpHeight = 0; 4730 4731 Rectangle paintImmediatelyClip = fetchRectangle(); 4732 paintImmediatelyClip.x = x; 4733 paintImmediatelyClip.y = y; 4734 paintImmediatelyClip.width = w; 4735 paintImmediatelyClip.height = h; 4736 4737 4738 4740 boolean ontop = alwaysOnTop() && isOpaque(); 4741 Component child; 4742 for (c = this, child = null; 4743 c != null && !(c instanceof Window) && !(c instanceof Applet ); 4744 child = c, c = c.getParent()) { 4745 JComponent jc = (c instanceof JComponent ) ? (JComponent )c : 4746 null; 4747 path.addElement(c); 4748 if(!ontop && jc != null && !jc.isOptimizedDrawingEnabled()) { 4749 boolean resetPC; 4750 4751 if (c != this) { 4760 if (jc.isPaintingOrigin()) { 4761 resetPC = true; 4762 } 4763 else { 4764 Component[] children = c.getComponents(); 4765 int i = 0; 4766 for (; i<children.length; i++) { 4767 if (children[i] == child) break; 4768 } 4769 switch (jc.getObscuredState(i, 4770 paintImmediatelyClip.x, 4771 paintImmediatelyClip.y, 4772 paintImmediatelyClip.width, 4773 paintImmediatelyClip.height)) { 4774 case NOT_OBSCURED: 4775 resetPC = false; 4776 break; 4777 case COMPLETELY_OBSCURED: 4778 recycleRectangle(paintImmediatelyClip); 4779 return; 4780 default: 4781 resetPC = true; 4782 break; 4783 } 4784 } 4785 } 4786 else { 4787 resetPC = false; 4788 } 4789 4790 if (resetPC) { 4791 paintingComponent = jc; 4794 pIndex = pCount; 4795 offsetX = offsetY = 0; 4796 hasBuffer = false; 4797 } 4798 } 4799 pCount++; 4800 4801 if(repaintManager.isDoubleBufferingEnabled() && jc != null && 4804 jc.isDoubleBuffered()) { 4805 hasBuffer = true; 4806 bufferedComponent = jc; 4807 } 4808 4809 if (!ontop) { 4811 int bx = c.getX(); 4812 int by = c.getY(); 4813 tmpWidth = c.getWidth(); 4814 tmpHeight = c.getHeight(); 4815 SwingUtilities.computeIntersection(tmpX,tmpY,tmpWidth,tmpHeight,paintImmediatelyClip); 4816 paintImmediatelyClip.x += bx; 4817 paintImmediatelyClip.y += by; 4818 offsetX += bx; 4819 offsetY += by; 4820 } 4821 } 4822 4823 if(c == null || c.getPeer() == null || 4825 paintImmediatelyClip.width <= 0 || 4826 paintImmediatelyClip.height <= 0) { 4827 recycleRectangle(paintImmediatelyClip); 4828 return; 4829 } 4830 4831 paintingComponent.setFlag(IS_REPAINTING, true); 4832 4833 paintImmediatelyClip.x -= offsetX; 4834 paintImmediatelyClip.y -= offsetY; 4835 4836 if(paintingComponent != this) { 4839 Component comp; 4840 int i = pIndex; 4841 for(; i > 0 ; i--) { 4842 comp = (Component) path.elementAt(i); 4843 if(comp instanceof JComponent ) { 4844 ((JComponent )comp).setPaintingChild 4845 ((Component)path.elementAt(i-1)); 4846 } 4847 } 4848 } 4849 4850 try { 4851 try { 4852 Graphics pcg = paintingComponent.getGraphics(); 4853 g = (pcg == null) ? null : pcg.create(); 4854 pcg.dispose(); 4855 } catch(NullPointerException e) { 4856 g = null; 4857 e.printStackTrace(); 4858 } 4859 4860 if (g == null) { 4861 System.err.println("In paintImmediately null graphics"); 4862 return; 4863 } 4864 4865 try { 4866 boolean paintCompleted = false; 4867 if (hasBuffer) { 4868 paintCompleted = paintDoubleBuffered(paintingComponent, bufferedComponent, g, 4869 paintImmediatelyClip.x, 4870 paintImmediatelyClip.y, 4871 paintImmediatelyClip.width, 4872 paintImmediatelyClip.height); 4873 } 4874 if (!paintCompleted) { 4875 g.setClip(paintImmediatelyClip.x,paintImmediatelyClip.y, 4877 paintImmediatelyClip.width,paintImmediatelyClip.height); 4878 paintingComponent.paint(g); 4879 } 4880 } finally { 4881 g.dispose(); 4882 } 4883 } 4884 finally { 4885 if(paintingComponent != this) { 4887 Component comp; 4888 int i = pIndex; 4889 for(; i > 0 ; i--) { 4890 comp = (Component) path.elementAt(i); 4891 if(comp instanceof JComponent ) { 4892 ((JComponent )comp).setPaintingChild(null); 4893 } 4894 } 4895 } 4896 path.removeAllElements(); 4897 paintingComponent.setFlag(IS_REPAINTING, false); 4898 } 4899 recycleRectangle(paintImmediatelyClip); 4900 } 4901 4902 private boolean paintDoubleBuffered(JComponent paintingComponent, Component bufferComponent, 4903 Graphics g, int clipX, int clipY, int clipW, int clipH) { 4904 RepaintManager repaintManager = RepaintManager.currentManager(paintingComponent); 4905 boolean paintCompleted = false; 4906 Image offscreen = null; 4907 4908 if (repaintManager.useVolatileDoubleBuffer() && 4912 (offscreen = repaintManager.getVolatileOffscreenBuffer( 4913 bufferComponent,clipW,clipH)) != null && 4914 (offscreen.getWidth(null) > 0 && offscreen.getHeight(null) > 0)) { 4915 4916 VolatileImage vImage = (java.awt.image.VolatileImage )offscreen; 4917 GraphicsConfiguration gc = bufferComponent. 4918 getGraphicsConfiguration(); 4919 for (int i = 0; !paintCompleted && i < RepaintManager.VOLATILE_LOOP_MAX; i++) { 4920 if (vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) { 4921 repaintManager.resetVolatileDoubleBuffer(gc); 4922 offscreen = repaintManager.getVolatileOffscreenBuffer(bufferComponent,clipW, clipH); 4923 vImage = (java.awt.image.VolatileImage )offscreen; 4924 } 4925 paintWithOffscreenBuffer(paintingComponent, g, clipX, clipY, clipW, clipH, offscreen); 4926 paintCompleted = !vImage.contentsLost(); 4927 } 4928 } 4929 if (!paintCompleted) { 4930 if ((offscreen = 4932 repaintManager.getOffscreenBuffer(bufferComponent, clipW, clipH)) != null && 4933 (offscreen.getWidth(null) > 0 && offscreen.getHeight(null) > 0)) { 4934 4935 paintWithOffscreenBuffer(paintingComponent, g, clipX, clipY, clipW, clipH, offscreen); 4936 paintCompleted = true; 4937 } 4938 } 4939 return paintCompleted; 4940 } 4941 4942 private void paintWithOffscreenBuffer(JComponent paintingComponent, Graphics g, 4943 int clipX, int clipY, int clipW, int clipH, 4944 Image offscreen) { 4945 Graphics og = offscreen.getGraphics(); 4946 Graphics osg = (og == null) ? null : og.create(); 4947 og.dispose(); 4948 int bw = offscreen.getWidth(null); 4949 int bh = offscreen.getHeight(null); 4950 int x,y,maxx,maxy; 4951 4952 if (bw > clipW) { 4953 bw = clipW; 4954 } 4955 if (bh > clipH) { 4956 bh = clipH; 4957 } 4958 try { 4959 paintingComponent.setFlag(ANCESTOR_USING_BUFFER,true); 4960 paintingComponent.setFlag(IS_PAINTING_TILE,true); 4961 for(x = clipX, maxx = clipX+clipW; x < maxx ; x += bw ) { 4962 for(y=clipY, maxy = clipY + clipH; y < maxy ; y += bh) { 4963 if ((y+bh) >= maxy && (x+bw) >= maxx) { 4964 paintingComponent.setFlag(IS_PAINTING_TILE,false); 4965 } 4966 osg.translate(-x,-y); 4967 osg.setClip(x,y,bw,bh); 4968 4969 if (paintingComponent.getFlag(IS_REPAINTING)) { 4970 paintingComponent.paint(osg); 4973 } else { 4974 if(!paintingComponent.rectangleIsObscured(clipX,clipY,bw,bh)) { 4976 paintingComponent.paintComponent(osg); 4977 paintingComponent.paintBorder(osg); 4978 } 4979 paintingComponent.paintChildren(osg); 4980 } 4981 g.setClip(x,y,bw,bh); 4982 g.drawImage(offscreen,x,y,paintingComponent); 4983 osg.translate(x,y); 4984 } 4985 } 4986 } finally { 4987 paintingComponent.setFlag(ANCESTOR_USING_BUFFER,false); 4988 paintingComponent.setFlag(IS_PAINTING_TILE,false); 4989 osg.dispose(); 4990 } 4991 } 4992 4993 5002 private int getObscuredState(int compIndex, int x, int y, int width, 5003 int height) { 5004 int retValue = NOT_OBSCURED; 5005 Rectangle tmpRect = fetchRectangle(); 5006 5007 for (int i = compIndex - 1 ; i >= 0 ; i--) { 5008 Component sibling = getComponent(i); 5009 if (!sibling.isVisible()) { 5010 continue; 5011 } 5012 Rectangle siblingRect; 5013 boolean opaque; 5014 if (sibling instanceof JComponent ) { 5015 opaque = ((JComponent )sibling).isOpaque(); 5016 if (!opaque) { 5017 if (retValue == PARTIALLY_OBSCURED) { 5018 continue; 5019 } 5020 } 5021 } 5022 else { 5023 opaque = true; 5024 } 5025 siblingRect = sibling.getBounds(tmpRect); 5026 if (opaque && x >= siblingRect.x && (x + width) <= 5027 (siblingRect.x + siblingRect.width) && 5028 y >= siblingRect.y && (y + height) <= 5029 (siblingRect.y + siblingRect.height)) { 5030 recycleRectangle(tmpRect); 5031 return COMPLETELY_OBSCURED; 5032 } 5033 else if (retValue == NOT_OBSCURED && 5034 !((x + width <= siblingRect.x) || 5035 (y + height <= siblingRect.y) || 5036 (x >= siblingRect.x + siblingRect.width) || 5037 (y >= siblingRect.y + siblingRect.height))) { 5038 retValue = PARTIALLY_OBSCURED; 5039 } 5040 } 5041 recycleRectangle(tmpRect); 5042 return retValue; 5043 } 5044 5045 5053 boolean checkIfChildObscuredBySibling() { 5054 return true; 5055 } 5056 5057 5058 private void setFlag(int aFlag, boolean aValue) { 5059 if(aValue) { 5060 flags |= (1 << aFlag); 5061 } else { 5062 flags &= ~(1 << aFlag); 5063 } 5064 } 5065 private boolean getFlag(int aFlag) { 5066 int mask = (1 << aFlag); 5067 return ((flags & mask) == mask); 5068 } 5069 static void setWriteObjCounter(JComponent comp, byte count) { 5073 comp.flags = (comp.flags & ~(0xFF << WRITE_OBJ_COUNTER_FIRST)) | 5074 (count << WRITE_OBJ_COUNTER_FIRST); 5075 } 5076 static byte getWriteObjCounter(JComponent comp) { 5077 return (byte)((comp.flags >> WRITE_OBJ_COUNTER_FIRST) & 0xFF); 5078 } 5079 5080 5081 5082 5093 public void setDoubleBuffered(boolean aFlag) { 5094 setFlag(IS_DOUBLE_BUFFERED,aFlag); 5095 } 5096 5097 5102 public boolean isDoubleBuffered() { 5103 return getFlag(IS_DOUBLE_BUFFERED); 5104 } 5105 5106 5112 public JRootPane getRootPane() { 5113 return SwingUtilities.getRootPane(this); 5114 } 5115 5116 5117 5118 5119 5123 void compWriteObjectNotify() { 5124 byte count = JComponent.getWriteObjCounter(this); 5125 JComponent.setWriteObjCounter(this, (byte)(count + 1)); 5126 if (count != 0) { 5127 return; 5128 } 5129 5130 if (ui != null) { 5131 ui.uninstallUI(this); 5132 } 5133 5140 if (getToolTipText() != null || 5141 this instanceof javax.swing.table.JTableHeader ) { 5142 ToolTipManager.sharedInstance().unregisterComponent(JComponent.this); 5143 } 5144 } 5145 5146 5162 private class ReadObjectCallback implements ObjectInputValidation 5163 { 5164 private final Vector roots = new Vector (1); 5165 private final ObjectInputStream inputStream; 5166 5167 ReadObjectCallback(ObjectInputStream s) throws Exception { 5168 inputStream = s; 5169 s.registerValidation(this, 0); 5170 } 5171 5172 5178 public void validateObject() throws InvalidObjectException { 5179 try { 5180 for(int i = 0; i < roots.size(); i++) { 5181 JComponent root = (JComponent )(roots.elementAt(i)); 5182 SwingUtilities.updateComponentTreeUI(root); 5183 } 5184 } 5185 finally { 5186 readObjectCallbacks.remove(inputStream); 5187 } 5188 } 5189 5190 5196 private void registerComponent(JComponent c) 5197 { 5198 5201 for(int i = 0; i < roots.size(); i++) { 5202 JComponent root = (JComponent )roots.elementAt(i); 5203 for(Component p = c; p != null; p = p.getParent()) { 5204 if (p == root) { 5205 return; 5206 } 5207 } 5208 } 5209 5210 5214 for(int i = 0; i < roots.size(); i++) { 5215 JComponent root = (JComponent )roots.elementAt(i); 5216 for(Component p = root.getParent(); p != null; p = p.getParent()) { 5217 if (p == c) { 5218 roots.removeElementAt(i--); break; 5220 } 5221 } 5222 } 5223 5224 roots.addElement(c); 5225 } 5226 } 5227 5228 5229 5236 private void readObject(ObjectInputStream s) 5237 throws IOException , ClassNotFoundException 5238 { 5239 s.defaultReadObject(); 5240 5241 5247 ReadObjectCallback cb = (ReadObjectCallback)(readObjectCallbacks.get(s)); 5248 if (cb == null) { 5249 try { 5250 readObjectCallbacks.put(s, cb = new ReadObjectCallback(s)); 5251 } 5252 catch (Exception e) { 5253 throw new IOException (e.toString()); 5254 } 5255 } 5256 cb.registerComponent(this); 5257 5258 if (getToolTipText() != null) { 5259 ToolTipManager.sharedInstance().registerComponent(this); 5260 } 5261 5262 int cpCount = s.readInt(); 5264 if (cpCount > 0) { 5265 clientProperties = new ArrayTable (); 5266 for (int counter = 0; counter < cpCount; counter++) { 5267 clientProperties.put(s.readObject(), 5268 s.readObject()); 5269 } 5270 } 5271 5272 setWriteObjCounter(this, (byte)0); 5273 } 5274 5275 5276 5287 private void writeObject(ObjectOutputStream s) throws IOException { 5288 s.defaultWriteObject(); 5289 if (getUIClassID().equals(uiClassID)) { 5290 byte count = JComponent.getWriteObjCounter(this); 5291 JComponent.setWriteObjCounter(this, --count); 5292 if (count == 0 && ui != null) { 5293 ui.installUI(this); 5294 } 5295 } 5296 ArrayTable.writeArrayTable(s, clientProperties); 5297 } 5298 5299 5300 5310 protected String paramString() { 5311 String preferredSizeString = (isPreferredSizeSet() ? 5312 getPreferredSize().toString() : ""); 5313 String minimumSizeString = (isMinimumSizeSet() ? 5314 getMinimumSize().toString() : ""); 5315 String maximumSizeString = (isMaximumSizeSet() ? 5316 getMaximumSize().toString() : ""); 5317 String borderString = (border != null ? 5318 border.toString() : ""); 5319 5320 return super.paramString() + 5321 ",alignmentX=" + alignmentX + 5322 ",alignmentY=" + alignmentY + 5323 ",border=" + borderString + 5324 ",flags=" + flags + ",maximumSize=" + maximumSizeString + 5326 ",minimumSize=" + minimumSizeString + 5327 ",preferredSize=" + preferredSizeString; 5328 } 5329 5330} 5331 | Popular Tags |