1 7 package javax.swing; 8 9 import com.sun.java.swing.SwingUtilities2; 10 import sun.swing.UIAction; 11 12 import java.applet.*; 13 14 import java.awt.*; 15 import java.awt.event.*; 16 17 import java.util.Vector ; 18 import java.util.Hashtable ; 19 20 import java.lang.reflect.*; 21 22 import javax.accessibility.*; 23 import javax.swing.event.MenuDragMouseEvent ; 24 import javax.swing.plaf.UIResource ; 25 import javax.swing.text.View ; 26 27 import sun.awt.AppContext; 28 29 35 public class SwingUtilities implements SwingConstants 36 { 37 private static boolean canAccessEventQueue = false; 39 private static boolean eventQueueTested = false; 40 41 42 45 public static final boolean isRectangleContainingRectangle(Rectangle a,Rectangle b) { 46 if (b.x >= a.x && (b.x + b.width) <= (a.x + a.width) && 47 b.y >= a.y && (b.y + b.height) <= (a.y + a.height)) { 48 return true; 49 } 50 return false; 51 } 52 53 56 public static Rectangle getLocalBounds(Component aComponent) { 57 Rectangle b = new Rectangle(aComponent.getBounds()); 58 b.x = b.y = 0; 59 return b; 60 } 61 62 63 73 public static Window getWindowAncestor(Component c) { 74 for(Container p = c.getParent(); p != null; p = p.getParent()) { 75 if (p instanceof Window) { 76 return (Window)p; 77 } 78 } 79 return null; 80 } 81 82 86 static Point convertScreenLocationToParent(Container parent,int x, int y) { 87 for (Container p = parent; p != null; p = p.getParent()) { 88 if (p instanceof Window) { 89 Point point = new Point(x, y); 90 91 SwingUtilities.convertPointFromScreen(point, parent); 92 return point; 93 } 94 } 95 throw new Error ("convertScreenLocationToParent: no window ancestor"); 96 } 97 98 108 public static Point convertPoint(Component source,Point aPoint,Component destination) { 109 Point p; 110 111 if(source == null && destination == null) 112 return aPoint; 113 if(source == null) { 114 source = getWindowAncestor(destination); 115 if(source == null) 116 throw new Error ("Source component not connected to component tree hierarchy"); 117 } 118 p = new Point(aPoint); 119 convertPointToScreen(p,source); 120 if(destination == null) { 121 destination = getWindowAncestor(source); 122 if(destination == null) 123 throw new Error ("Destination component not connected to component tree hierarchy"); 124 } 125 convertPointFromScreen(p,destination); 126 return p; 127 } 128 129 139 public static Point convertPoint(Component source,int x, int y,Component destination) { 140 Point point = new Point(x,y); 141 return convertPoint(source,point,destination); 142 } 143 144 154 public static Rectangle convertRectangle(Component source,Rectangle aRectangle,Component destination) { 155 Point point = new Point(aRectangle.x,aRectangle.y); 156 point = convertPoint(source,point,destination); 157 return new Rectangle(point.x,point.y,aRectangle.width,aRectangle.height); 158 } 159 160 165 public static Container getAncestorOfClass(Class <?> c, Component comp) 166 { 167 if(comp == null || c == null) 168 return null; 169 170 Container parent = comp.getParent(); 171 while(parent != null && !(c.isInstance(parent))) 172 parent = parent.getParent(); 173 return parent; 174 } 175 176 181 public static Container getAncestorNamed(String name, Component comp) { 182 if(comp == null || name == null) 183 return null; 184 185 Container parent = comp.getParent(); 186 while(parent != null && !(name.equals(parent.getName()))) 187 parent = parent.getParent(); 188 return parent; 189 } 190 191 203 public static Component getDeepestComponentAt(Component parent, int x, int y) { 204 if (!parent.contains(x, y)) { 205 return null; 206 } 207 if (parent instanceof Container) { 208 Component components[] = ((Container)parent).getComponents(); 209 for (int i = 0 ; i < components.length ; i++) { 210 Component comp = components[i]; 211 if (comp != null && comp.isVisible()) { 212 Point loc = comp.getLocation(); 213 if (comp instanceof Container) { 214 comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y); 215 } else { 216 comp = comp.getComponentAt(x - loc.x, y - loc.y); 217 } 218 if (comp != null && comp.isVisible()) { 219 return comp; 220 } 221 } 222 } 223 } 224 return parent; 225 } 226 227 228 241 public static MouseEvent convertMouseEvent(Component source, 242 MouseEvent sourceEvent, 243 Component destination) { 244 Point p = convertPoint(source,new Point(sourceEvent.getX(), 245 sourceEvent.getY()), 246 destination); 247 Component newSource; 248 249 if(destination != null) 250 newSource = destination; 251 else 252 newSource = source; 253 254 MouseEvent newEvent; 255 if (sourceEvent instanceof MouseWheelEvent) { 256 MouseWheelEvent sourceWheelEvent = (MouseWheelEvent)sourceEvent; 257 newEvent = new MouseWheelEvent(newSource, 258 sourceWheelEvent.getID(), 259 sourceWheelEvent.getWhen(), 260 sourceWheelEvent.getModifiers(), 261 p.x,p.y, 262 sourceWheelEvent.getClickCount(), 263 sourceWheelEvent.isPopupTrigger(), 264 sourceWheelEvent.getScrollType(), 265 sourceWheelEvent.getScrollAmount(), 266 sourceWheelEvent.getWheelRotation()); 267 } 268 else if (sourceEvent instanceof MenuDragMouseEvent ) { 269 MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent )sourceEvent; 270 newEvent = new MenuDragMouseEvent (newSource, 271 sourceMenuDragEvent.getID(), 272 sourceMenuDragEvent.getWhen(), 273 sourceMenuDragEvent.getModifiers(), 274 p.x,p.y, 275 sourceMenuDragEvent.getClickCount(), 276 sourceMenuDragEvent.isPopupTrigger(), 277 sourceMenuDragEvent.getPath(), 278 sourceMenuDragEvent.getMenuSelectionManager()); 279 } 280 else { 281 newEvent = new MouseEvent (newSource, 282 sourceEvent.getID(), 283 sourceEvent.getWhen(), 284 sourceEvent.getModifiers(), 285 p.x,p.y, 286 sourceEvent.getClickCount(), 287 sourceEvent.isPopupTrigger()); 288 } 289 return newEvent; 290 } 291 292 293 300 public static void convertPointToScreen(Point p,Component c) { 301 Rectangle b; 302 int x,y; 303 304 do { 305 if(c instanceof JComponent ) { 306 x = ((JComponent )c).getX(); 307 y = ((JComponent )c).getY(); 308 } else if(c instanceof java.applet.Applet || 309 c instanceof java.awt.Window ) { 310 try { 311 Point pp = c.getLocationOnScreen(); 312 x = pp.x; 313 y = pp.y; 314 } catch (IllegalComponentStateException icse) { 315 x = c.getX(); 316 y = c.getY(); 317 } 318 } else { 319 x = c.getX(); 320 y = c.getY(); 321 } 322 323 p.x += x; 324 p.y += y; 325 326 if(c instanceof java.awt.Window || c instanceof java.applet.Applet ) 327 break; 328 c = c.getParent(); 329 } while(c != null); 330 } 331 332 339 public static void convertPointFromScreen(Point p,Component c) { 340 Rectangle b; 341 int x,y; 342 343 do { 344 if(c instanceof JComponent ) { 345 x = ((JComponent )c).getX(); 346 y = ((JComponent )c).getY(); 347 } else if(c instanceof java.applet.Applet || 348 c instanceof java.awt.Window ) { 349 try { 350 Point pp = c.getLocationOnScreen(); 351 x = pp.x; 352 y = pp.y; 353 } catch (IllegalComponentStateException icse) { 354 x = c.getX(); 355 y = c.getY(); 356 } 357 } else { 358 x = c.getX(); 359 y = c.getY(); 360 } 361 362 p.x -= x; 363 p.y -= y; 364 365 if(c instanceof java.awt.Window || c instanceof java.applet.Applet ) 366 break; 367 c = c.getParent(); 368 } while(c != null); 369 } 370 371 384 public static Window windowForComponent(Component c) { 385 return getWindowAncestor(c); 386 } 387 388 391 public static boolean isDescendingFrom(Component a,Component b) { 392 if(a == b) 393 return true; 394 for(Container p = a.getParent();p!=null;p=p.getParent()) 395 if(p == b) 396 return true; 397 return false; 398 } 399 400 401 416 public static Rectangle computeIntersection(int x,int y,int width,int height,Rectangle dest) { 417 int x1 = (x > dest.x) ? x : dest.x; 418 int x2 = ((x+width) < (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width); 419 int y1 = (y > dest.y) ? y : dest.y; 420 int y2 = ((y + height) < (dest.y + dest.height) ? (y+height) : (dest.y + dest.height)); 421 422 dest.x = x1; 423 dest.y = y1; 424 dest.width = x2 - x1; 425 dest.height = y2 - y1; 426 427 if (dest.width < 0 || dest.height < 0) { 429 dest.x = dest.y = dest.width = dest.height = 0; 430 } 431 432 return dest; 433 } 434 435 447 public static Rectangle computeUnion(int x,int y,int width,int height,Rectangle dest) { 448 int x1 = (x < dest.x) ? x : dest.x; 449 int x2 = ((x+width) > (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width); 450 int y1 = (y < dest.y) ? y : dest.y; 451 int y2 = ((y+height) > (dest.y + dest.height)) ? (y+height) : (dest.y + dest.height); 452 453 dest.x = x1; 454 dest.y = y1; 455 dest.width = (x2 - x1); 456 dest.height= (y2 - y1); 457 return dest; 458 } 459 460 465 public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) { 466 if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) { 467 return new Rectangle[0]; 468 } 469 470 Rectangle t = new Rectangle(); 471 Rectangle a=null,b=null,c=null,d=null; 472 Rectangle result[]; 473 int rectCount = 0; 474 475 476 if (isRectangleContainingRectangle(rectA,rectB)) { 477 t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height; 478 if(t.width > 0 && t.height > 0) { 479 a = new Rectangle(t); 480 rectCount++; 481 } 482 483 t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y; 484 if(t.width > 0 && t.height > 0) { 485 b = new Rectangle(t); 486 rectCount++; 487 } 488 489 t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width; 490 t.height = rectA.y + rectA.height - (rectB.y + rectB.height); 491 if(t.width > 0 && t.height > 0) { 492 c = new Rectangle(t); 493 rectCount++; 494 } 495 496 t.x = rectB.x + rectB.width; t.y = rectA.y; t.width = rectA.x + rectA.width - (rectB.x + rectB.width); 497 t.height = rectA.height; 498 if(t.width > 0 && t.height > 0) { 499 d = new Rectangle(t); 500 rectCount++; 501 } 502 } else { 503 504 if (rectB.x <= rectA.x && rectB.y <= rectA.y) { 505 if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) { 506 507 t.x = rectA.x; t.y = rectB.y + rectB.height; 508 t.width = rectA.width; t.height = rectA.y + rectA.height - (rectB.y + rectB.height); 509 if(t.width > 0 && t.height > 0) { 510 a = t; 511 rectCount++; 512 } 513 } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) { 514 t.setBounds((rectB.x + rectB.width), rectA.y, 515 (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height); 516 if(t.width > 0 && t.height > 0) { 517 a = t; 518 rectCount++; 519 } 520 } else { 521 t.setBounds((rectB.x + rectB.width), rectA.y, 522 (rectA.x + rectA.width) - (rectB.x + rectB.width), 523 (rectB.y + rectB.height) - rectA.y); 524 if(t.width > 0 && t.height > 0) { 525 a = new Rectangle(t); 526 rectCount++; 527 } 528 529 t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width, 530 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 531 if(t.width > 0 && t.height > 0) { 532 b = new Rectangle(t); 533 rectCount++; 534 } 535 } 536 } else if (rectB.x <= rectA.x && (rectB.y + rectB.height) >= (rectA.y + rectA.height)) { 537 if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) { 538 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 539 if(t.width > 0 && t.height > 0) { 540 a = t; 541 rectCount++; 542 } 543 } else { 544 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 545 if(t.width > 0 && t.height > 0) { 546 a = new Rectangle(t); 547 rectCount++; 548 } 549 t.setBounds((rectB.x + rectB.width), rectB.y, 550 (rectA.x + rectA.width) - (rectB.x + rectB.width), 551 (rectA.y + rectA.height) - rectB.y); 552 if(t.width > 0 && t.height > 0) { 553 b = new Rectangle(t); 554 rectCount++; 555 } 556 } 557 } else if (rectB.x <= rectA.x) { 558 if ((rectB.x + rectB.width) >= (rectA.x + rectA.width)) { 559 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 560 if(t.width>0 && t.height > 0) { 561 a = new Rectangle(t); 562 rectCount++; 563 } 564 565 t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width, 566 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 567 if(t.width > 0 && t.height > 0) { 568 b = new Rectangle(t); 569 rectCount++; 570 } 571 } else { 572 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 573 if(t.width > 0 && t.height > 0) { 574 a = new Rectangle(t); 575 rectCount++; 576 } 577 578 t.setBounds((rectB.x + rectB.width), rectB.y, 579 (rectA.x + rectA.width) - (rectB.x + rectB.width), 580 rectB.height); 581 if(t.width > 0 && t.height > 0) { 582 b = new Rectangle(t); 583 rectCount++; 584 } 585 586 t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width, 587 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 588 if(t.width > 0 && t.height > 0) { 589 c = new Rectangle(t); 590 rectCount++; 591 } 592 } 593 } else if (rectB.x <= (rectA.x + rectA.width) && (rectB.x + rectB.width) > (rectA.x + rectA.width)) { 594 if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) { 595 t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height); 596 if(t.width > 0 && t.height > 0) { 597 a = t; 598 rectCount++; 599 } 600 } else if (rectB.y <= rectA.y) { 601 t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, 602 (rectB.y + rectB.height) - rectA.y); 603 if(t.width > 0 && t.height > 0) { 604 a = new Rectangle(t); 605 rectCount++; 606 } 607 608 t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width, 609 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 610 if(t.width > 0 && t.height > 0) { 611 b = new Rectangle(t); 612 rectCount++; 613 } 614 } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) { 615 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 616 if(t.width > 0 && t.height > 0) { 617 a = new Rectangle(t); 618 rectCount++; 619 } 620 621 t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x, 622 (rectA.y + rectA.height) - rectB.y); 623 if(t.width > 0 && t.height > 0) { 624 b = new Rectangle(t); 625 rectCount++; 626 } 627 } else { 628 t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y); 629 if(t.width > 0 && t.height > 0) { 630 a = new Rectangle(t); 631 rectCount++; 632 } 633 634 t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x, 635 rectB.height); 636 if(t.width > 0 && t.height > 0) { 637 b = new Rectangle(t); 638 rectCount++; 639 } 640 641 t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width, 642 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 643 if(t.width > 0 && t.height > 0) { 644 c = new Rectangle(t); 645 rectCount++; 646 } 647 } 648 } else if (rectB.x >= rectA.x && (rectB.x + rectB.width) <= (rectA.x + rectA.width)) { 649 if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) { 650 t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height); 651 if(t.width > 0 && t.height > 0) { 652 a = new Rectangle(t); 653 rectCount++; 654 } 655 t.setBounds((rectB.x + rectB.width), rectA.y, 656 (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height); 657 if(t.width > 0 && t.height > 0) { 658 b = new Rectangle(t); 659 rectCount++; 660 } 661 } else if (rectB.y <= rectA.y) { 662 t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height); 663 if(t.width > 0 && t.height > 0) { 664 a = new Rectangle(t); 665 rectCount++; 666 } 667 668 t.setBounds(rectB.x, (rectB.y + rectB.height), 669 rectB.width, 670 (rectA.y + rectA.height) - (rectB.y + rectB.height)); 671 if(t.width > 0 && t.height > 0) { 672 b = new Rectangle(t); 673 rectCount++; 674 } 675 676 t.setBounds((rectB.x + rectB.width), rectA.y, 677 (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height); 678 if(t.width > 0 && t.height > 0) { 679 c = new Rectangle(t); 680 rectCount++; 681 } 682 } else { 683 t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height); 684 if(t.width > 0 && t.height > 0) { 685 a = new Rectangle(t); 686 rectCount++; 687 } 688 689 t.setBounds(rectB.x, rectA.y, rectB.width, 690 rectB.y - rectA.y); 691 if(t.width > 0 && t.height > 0) { 692 b = new Rectangle(t); 693 rectCount++; 694 } 695 696 t.setBounds((rectB.x + rectB.width), rectA.y, 697 (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height); 698 if(t.width > 0 && t.height > 0) { 699 c = new Rectangle(t); 700 rectCount++; 701 } 702 } 703 } 704 } 705 706 result = new Rectangle[rectCount]; 707 rectCount = 0; 708 if(a != null) 709 result[rectCount++] = a; 710 if(b != null) 711 result[rectCount++] = b; 712 if(c != null) 713 result[rectCount++] = c; 714 if(d != null) 715 result[rectCount++] = d; 716 return result; 717 } 718 719 725 public static boolean isLeftMouseButton(MouseEvent anEvent) { 726 return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0); 727 } 728 729 735 public static boolean isMiddleMouseButton(MouseEvent anEvent) { 736 return ((anEvent.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK); 737 } 738 739 745 public static boolean isRightMouseButton(MouseEvent anEvent) { 746 return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK); 747 } 748 749 757 public static int computeStringWidth(FontMetrics fm,String str) { 758 return SwingUtilities2.stringWidth(null, fm, str); 762 } 763 764 772 public static String layoutCompoundLabel(JComponent c, 773 FontMetrics fm, 774 String text, 775 Icon icon, 776 int verticalAlignment, 777 int horizontalAlignment, 778 int verticalTextPosition, 779 int horizontalTextPosition, 780 Rectangle viewR, 781 Rectangle iconR, 782 Rectangle textR, 783 int textIconGap) 784 { 785 boolean orientationIsLeftToRight = true; 786 int hAlign = horizontalAlignment; 787 int hTextPos = horizontalTextPosition; 788 789 if (c != null) { 790 if (!(c.getComponentOrientation().isLeftToRight())) { 791 orientationIsLeftToRight = false; 792 } 793 } 794 795 switch (horizontalAlignment) { 798 case LEADING: 799 hAlign = (orientationIsLeftToRight) ? LEFT : RIGHT; 800 break; 801 case TRAILING: 802 hAlign = (orientationIsLeftToRight) ? RIGHT : LEFT; 803 break; 804 } 805 806 switch (horizontalTextPosition) { 809 case LEADING: 810 hTextPos = (orientationIsLeftToRight) ? LEFT : RIGHT; 811 break; 812 case TRAILING: 813 hTextPos = (orientationIsLeftToRight) ? RIGHT : LEFT; 814 break; 815 } 816 817 return layoutCompoundLabelImpl(c, 818 fm, 819 text, 820 icon, 821 verticalAlignment, 822 hAlign, 823 verticalTextPosition, 824 hTextPos, 825 viewR, 826 iconR, 827 textR, 828 textIconGap); 829 } 830 831 841 public static String layoutCompoundLabel( 842 FontMetrics fm, 843 String text, 844 Icon icon, 845 int verticalAlignment, 846 int horizontalAlignment, 847 int verticalTextPosition, 848 int horizontalTextPosition, 849 Rectangle viewR, 850 Rectangle iconR, 851 Rectangle textR, 852 int textIconGap) 853 { 854 return layoutCompoundLabelImpl(null, fm, text, icon, 855 verticalAlignment, 856 horizontalAlignment, 857 verticalTextPosition, 858 horizontalTextPosition, 859 viewR, iconR, textR, textIconGap); 860 } 861 862 872 private static String layoutCompoundLabelImpl( 873 JComponent c, 874 FontMetrics fm, 875 String text, 876 Icon icon, 877 int verticalAlignment, 878 int horizontalAlignment, 879 int verticalTextPosition, 880 int horizontalTextPosition, 881 Rectangle viewR, 882 Rectangle iconR, 883 Rectangle textR, 884 int textIconGap) 885 { 886 888 889 if (icon != null) { 890 iconR.width = icon.getIconWidth(); 891 iconR.height = icon.getIconHeight(); 892 } 893 else { 894 iconR.width = iconR.height = 0; 895 } 896 897 901 902 boolean textIsEmpty = (text == null) || text.equals(""); 903 int lsb = 0; 904 907 int gap; 908 909 View v = null; 910 if (textIsEmpty) { 911 textR.width = textR.height = 0; 912 text = ""; 913 gap = 0; 914 } 915 else { 916 int availTextWidth; 917 gap = (icon == null) ? 0 : textIconGap; 918 919 if (horizontalTextPosition == CENTER) { 920 availTextWidth = viewR.width; 921 } 922 else { 923 availTextWidth = viewR.width - (iconR.width + gap); 924 } 925 v = (c != null) ? (View ) c.getClientProperty("html") : null; 926 if (v != null) { 927 textR.width = Math.min(availTextWidth, 928 (int) v.getPreferredSpan(View.X_AXIS)); 929 textR.height = (int) v.getPreferredSpan(View.Y_AXIS); 930 } else { 931 textR.width = SwingUtilities2.stringWidth(c, fm, text); 932 lsb = SwingUtilities2.getLeftSideBearing(c, fm, text); 933 if (lsb < 0) { 934 textR.width -= lsb; 945 } 946 if (textR.width > availTextWidth) { 947 text = SwingUtilities2.clipString(c, fm, text, 948 availTextWidth); 949 textR.width = SwingUtilities2.stringWidth(c, fm, text); 950 } 951 textR.height = fm.getHeight(); 952 } 953 } 954 955 956 959 960 if (verticalTextPosition == TOP) { 961 if (horizontalTextPosition != CENTER) { 962 textR.y = 0; 963 } 964 else { 965 textR.y = -(textR.height + gap); 966 } 967 } 968 else if (verticalTextPosition == CENTER) { 969 textR.y = (iconR.height / 2) - (textR.height / 2); 970 } 971 else { if (horizontalTextPosition != CENTER) { 973 textR.y = iconR.height - textR.height; 974 } 975 else { 976 textR.y = (iconR.height + gap); 977 } 978 } 979 980 if (horizontalTextPosition == LEFT) { 981 textR.x = -(textR.width + gap); 982 } 983 else if (horizontalTextPosition == CENTER) { 984 textR.x = (iconR.width / 2) - (textR.width / 2); 985 } 986 else { textR.x = (iconR.width + gap); 988 } 989 990 997 int labelR_x = Math.min(iconR.x, textR.x); 998 int labelR_width = Math.max(iconR.x + iconR.width, 999 textR.x + textR.width) - labelR_x; 1000 int labelR_y = Math.min(iconR.y, textR.y); 1001 int labelR_height = Math.max(iconR.y + iconR.height, 1002 textR.y + textR.height) - labelR_y; 1003 1004 int dx, dy; 1005 1006 if (verticalAlignment == TOP) { 1007 dy = viewR.y - labelR_y; 1008 } 1009 else if (verticalAlignment == CENTER) { 1010 dy = (viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2)); 1011 } 1012 else { dy = (viewR.y + viewR.height) - (labelR_y + labelR_height); 1014 } 1015 1016 if (horizontalAlignment == LEFT) { 1017 dx = viewR.x - labelR_x; 1018 } 1019 else if (horizontalAlignment == RIGHT) { 1020 dx = (viewR.x + viewR.width) - (labelR_x + labelR_width); 1021 } 1022 else { dx = (viewR.x + (viewR.width / 2)) - 1024 (labelR_x + (labelR_width / 2)); 1025 } 1026 1027 1029 1030 textR.x += dx; 1031 textR.y += dy; 1032 1033 iconR.x += dx; 1034 iconR.y += dy; 1035 1036 if (lsb < 0) { 1037 textR.x -= lsb; 1040 } 1041 1042 return text; 1043 } 1044 1045 1046 1078 public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) { 1079 getCellRendererPane(c, p).paintComponent(g, c, p, x, y, w, h,false); 1080 } 1081 1082 1108 public static void paintComponent(Graphics g, Component c, Container p, Rectangle r) { 1109 paintComponent(g, c, p, r.x, r.y, r.width, r.height); 1110 } 1111 1112 1113 1118 private static CellRendererPane getCellRendererPane(Component c, Container p) { 1119 Container shell = c.getParent(); 1120 if (shell instanceof CellRendererPane ) { 1121 if (shell.getParent() != p) { 1122 p.add(shell); 1123 } 1124 } else { 1125 shell = new CellRendererPane (); 1126 shell.add(c); 1127 p.add(shell); 1128 } 1129 return (CellRendererPane )shell; 1130 } 1131 1132 1137 public static void updateComponentTreeUI(Component c) { 1138 updateComponentTreeUI0(c); 1139 c.invalidate(); 1140 c.validate(); 1141 c.repaint(); 1142 } 1143 1144 private static void updateComponentTreeUI0(Component c) { 1145 if (c instanceof JComponent ) { 1146 ((JComponent ) c).updateUI(); 1147 } 1148 Component[] children = null; 1149 if (c instanceof JMenu ) { 1150 children = ((JMenu )c).getMenuComponents(); 1151 } 1152 else if (c instanceof Container) { 1153 children = ((Container)c).getComponents(); 1154 } 1155 if (children != null) { 1156 for(int i = 0; i < children.length; i++) { 1157 updateComponentTreeUI0(children[i]); 1158 } 1159 } 1160 } 1161 1162 1163 1197 public static void invokeLater(Runnable doRun) { 1198 EventQueue.invokeLater(doRun); 1199 } 1200 1201 1202 1254 public static void invokeAndWait(final Runnable doRun) 1255 throws InterruptedException , InvocationTargetException 1256 { 1257 EventQueue.invokeAndWait(doRun); 1258 } 1259 1260 1268 public static boolean isEventDispatchThread() 1269 { 1270 return EventQueue.isDispatchThread(); 1271 } 1272 1273 1274 1278 1279 1289 public static int getAccessibleIndexInParent(Component c) { 1290 return c.getAccessibleContext().getAccessibleIndexInParent(); 1291 } 1292 1293 1301 public static Accessible getAccessibleAt(Component c, Point p) { 1302 if (c instanceof Container) { 1303 return c.getAccessibleContext().getAccessibleComponent().getAccessibleAt(p); 1304 } else if (c instanceof Accessible) { 1305 Accessible a = (Accessible) c; 1306 if (a != null) { 1307 AccessibleContext ac = a.getAccessibleContext(); 1308 if (ac != null) { 1309 AccessibleComponent acmp; 1310 Point location; 1311 int nchildren = ac.getAccessibleChildrenCount(); 1312 for (int i=0; i < nchildren; i++) { 1313 a = ac.getAccessibleChild(i); 1314 if ((a != null)) { 1315 ac = a.getAccessibleContext(); 1316 if (ac != null) { 1317 acmp = ac.getAccessibleComponent(); 1318 if ((acmp != null) && (acmp.isShowing())) { 1319 location = acmp.getLocation(); 1320 Point np = new Point(p.x-location.x, 1321 p.y-location.y); 1322 if (acmp.contains(np)){ 1323 return a; 1324 } 1325 } 1326 } 1327 } 1328 } 1329 } 1330 } 1331 return (Accessible) c; 1332 } 1333 return null; 1334 } 1335 1336 1347 public static AccessibleStateSet getAccessibleStateSet(Component c) { 1348 return c.getAccessibleContext().getAccessibleStateSet(); 1349 } 1350 1351 1362 public static int getAccessibleChildrenCount(Component c) { 1363 return c.getAccessibleContext().getAccessibleChildrenCount(); 1364 } 1365 1366 1376 public static Accessible getAccessibleChild(Component c, int i) { 1377 return c.getAccessibleContext().getAccessibleChild(i); 1378 } 1379 1380 1394 @Deprecated 1395 public static Component findFocusOwner(Component c) { 1396 Component focusOwner = KeyboardFocusManager. 1397 getCurrentKeyboardFocusManager().getFocusOwner(); 1398 1399 for (Component temp = focusOwner; temp != null; 1401 temp = (temp instanceof Window) ? null : temp.getParent()) 1402 { 1403 if (temp == c) { 1404 return focusOwner; 1405 } 1406 } 1407 1408 return null; 1409 } 1410 1411 1416 public static JRootPane getRootPane(Component c) { 1417 if (c instanceof RootPaneContainer ) { 1418 return ((RootPaneContainer )c).getRootPane(); 1419 } 1420 for( ; c != null; c = c.getParent()) { 1421 if (c instanceof JRootPane ) { 1422 return (JRootPane )c; 1423 } 1424 } 1425 return null; 1426 } 1427 1428 1429 1433 public static Component getRoot(Component c) { 1434 Component applet = null; 1435 for(Component p = c; p != null; p = p.getParent()) { 1436 if (p instanceof Window) { 1437 return p; 1438 } 1439 if (p instanceof Applet) { 1440 applet = p; 1441 } 1442 } 1443 return applet; 1444 } 1445 1446 1462 public static boolean processKeyBindings(KeyEvent event) { 1463 if (event != null) { 1464 if (event.isConsumed()) { 1465 return false; 1466 } 1467 1468 Component component = event.getComponent(); 1469 Component last = component; 1470 boolean pressed = (event.getID() == KeyEvent.KEY_PRESSED); 1471 1472 if (!isValidKeyEventForKeyBindings(event)) { 1473 return false; 1474 } 1475 while (component != null) { 1478 if (component instanceof JComponent ) { 1479 return ((JComponent )component).processKeyBindings( 1480 event, pressed); 1481 } 1482 last = component; 1483 component = component.getParent(); 1484 } 1485 if ((last instanceof Applet) || (last instanceof Window)) { 1488 return JComponent.processKeyBindingsForAllComponents( 1489 event, (Container)last, pressed); 1490 } 1491 } 1492 return false; 1493 } 1494 1495 1499 static boolean isValidKeyEventForKeyBindings(KeyEvent e) { 1500 if (e.getID() == KeyEvent.KEY_TYPED) { 1501 int mod = e.getModifiers(); 1502 if (((mod & ActionEvent.ALT_MASK) != 0) && 1503 ((mod & ActionEvent.CTRL_MASK) == 0)) { 1504 return false; 1507 } 1508 } 1509 return true; 1510 } 1511 1512 1529 public static boolean notifyAction(Action action, KeyStroke ks, 1530 KeyEvent event, Object sender, 1531 int modifiers) { 1532 if (action == null) { 1533 return false; 1534 } 1535 if (action instanceof UIAction) { 1536 if (!((UIAction)action).isEnabled(sender)) { 1537 return false; 1538 } 1539 } 1540 else if (!action.isEnabled()) { 1541 return false; 1542 } 1543 Object commandO; 1544 boolean stayNull; 1545 1546 commandO = action.getValue(Action.ACTION_COMMAND_KEY); 1548 if (commandO == null && (action instanceof JComponent.ActionStandin )) { 1549 stayNull = true; 1552 } 1553 else { 1554 stayNull = false; 1555 } 1556 1557 String command; 1559 1560 if (commandO != null) { 1561 command = commandO.toString(); 1562 } 1563 else if (!stayNull && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 1564 command = String.valueOf(event.getKeyChar()); 1565 } 1566 else { 1567 command = null; 1570 } 1571 action.actionPerformed(new ActionEvent(sender, 1572 ActionEvent.ACTION_PERFORMED, command, event.getWhen(), 1573 modifiers)); 1574 return true; 1575 } 1576 1577 1578 1585 public static void replaceUIInputMap(JComponent component, int type, 1586 InputMap uiInputMap) { 1587 InputMap map = component.getInputMap(type, (uiInputMap != null)); 1588 1589 while (map != null) { 1590 InputMap parent = map.getParent(); 1591 if (parent == null || (parent instanceof UIResource )) { 1592 map.setParent(uiInputMap); 1593 return; 1594 } 1595 map = parent; 1596 } 1597 } 1598 1599 1600 1607 public static void replaceUIActionMap(JComponent component, 1608 ActionMap uiActionMap) { 1609 ActionMap map = component.getActionMap((uiActionMap != null));; 1610 1611 while (map != null) { 1612 ActionMap parent = map.getParent(); 1613 if (parent == null || (parent instanceof UIResource )) { 1614 map.setParent(uiActionMap); 1615 return; 1616 } 1617 map = parent; 1618 } 1619 } 1620 1621 1622 1630 public static InputMap getUIInputMap(JComponent component, int condition) { 1631 InputMap map = component.getInputMap(condition, false); 1632 while (map != null) { 1633 InputMap parent = map.getParent(); 1634 if (parent instanceof UIResource ) { 1635 return parent; 1636 } 1637 map = parent; 1638 } 1639 return null; 1640 } 1641 1642 1649 public static ActionMap getUIActionMap(JComponent component) { 1650 ActionMap map = component.getActionMap(false); 1651 while (map != null) { 1652 ActionMap parent = map.getParent(); 1653 if (parent instanceof UIResource ) { 1654 return parent; 1655 } 1656 map = parent; 1657 } 1658 return null; 1659 } 1660 1661 1662 private static final Object sharedOwnerFrameKey = 1664 new StringBuffer ("SwingUtilities.sharedOwnerFrame"); 1665 1666 static class SharedOwnerFrame extends Frame implements WindowListener { 1667 public void addNotify() { 1668 super.addNotify(); 1669 installListeners(); 1670 } 1671 1672 1675 void installListeners() { 1676 Window[] windows = getOwnedWindows(); 1677 for (int ind = 0; ind < windows.length; ind++){ 1678 Window window = windows[ind]; 1679 if (window != null) { 1680 window.removeWindowListener(this); 1681 window.addWindowListener(this); 1682 } 1683 } 1684 } 1685 1686 1690 public void windowClosed(WindowEvent e) { 1691 synchronized(getTreeLock()) { 1692 Window[] windows = getOwnedWindows(); 1693 for (int ind = 0; ind < windows.length; ind++) { 1694 Window window = windows[ind]; 1695 if (window != null) { 1696 if (window.isDisplayable()) { 1697 return; 1698 } 1699 window.removeWindowListener(this); 1700 } 1701 } 1702 dispose(); 1703 } 1704 } 1705 public void windowOpened(WindowEvent e) { 1706 } 1707 public void windowClosing(WindowEvent e) { 1708 } 1709 public void windowIconified(WindowEvent e) { 1710 } 1711 public void windowDeiconified(WindowEvent e) { 1712 } 1713 public void windowActivated(WindowEvent e) { 1714 } 1715 public void windowDeactivated(WindowEvent e) { 1716 } 1717 1718 public void show() { 1719 } 1721 public void dispose() { 1722 try { 1723 getToolkit().getSystemEventQueue(); 1724 super.dispose(); 1725 } catch (Exception e) { 1726 } 1728 } 1729 } 1730 1731 1739 static Frame getSharedOwnerFrame() throws HeadlessException { 1740 Frame sharedOwnerFrame = 1741 (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey); 1742 if (sharedOwnerFrame == null) { 1743 sharedOwnerFrame = new SharedOwnerFrame(); 1744 SwingUtilities.appContextPut(sharedOwnerFrameKey, 1745 sharedOwnerFrame); 1746 } 1747 return sharedOwnerFrame; 1748 } 1749 1750 1757 static WindowListener getSharedOwnerFrameShutdownListener() throws HeadlessException { 1758 Frame sharedOwnerFrame = getSharedOwnerFrame(); 1759 return (WindowListener)sharedOwnerFrame; 1760 } 1761 1762 1766 1769 static Object appContextGet(Object key) { 1770 return AppContext.getAppContext().get(key); 1771 } 1772 1773 static void appContextPut(Object key, Object value) { 1774 AppContext.getAppContext().put(key, value); 1775 } 1776 1777 static void appContextRemove(Object key) { 1778 AppContext.getAppContext().remove(key); 1779 } 1780 1781 1782 static Class loadSystemClass(String className) throws ClassNotFoundException { 1783 return Class.forName(className, true, Thread.currentThread(). 1784 getContextClassLoader()); 1785 } 1786 1787 1788 1792 static boolean isLeftToRight( Component c ) { 1793 return c.getComponentOrientation().isLeftToRight(); 1794 } 1795 private SwingUtilities() { 1796 throw new Error ("SwingUtilities is just a container for static methods"); 1797 } 1798 1799 1803 static boolean doesIconReferenceImage(Icon icon, Image image) { 1804 Image iconImage = (icon != null && (icon instanceof ImageIcon )) ? 1805 ((ImageIcon )icon).getImage() : null; 1806 return (iconImage == image); 1807 } 1808 1809 1818 static int findDisplayedMnemonicIndex(String text, int mnemonic) { 1819 if (text == null || mnemonic == '\0') { 1820 return -1; 1821 } 1822 1823 char uc = Character.toUpperCase((char)mnemonic); 1824 char lc = Character.toLowerCase((char)mnemonic); 1825 1826 int uci = text.indexOf(uc); 1827 int lci = text.indexOf(lc); 1828 1829 if (uci == -1) { 1830 return lci; 1831 } else if(lci == -1) { 1832 return uci; 1833 } else { 1834 return (lci < uci) ? lci : uci; 1835 } 1836 } 1837 1838 1856 public static Rectangle calculateInnerArea(JComponent c, Rectangle r) { 1857 if (c == null) { 1858 return null; 1859 } 1860 Rectangle rect = r; 1861 Insets insets = c.getInsets(); 1862 1863 if (rect == null) { 1864 rect = new Rectangle(); 1865 } 1866 1867 rect.x = insets.left; 1868 rect.y = insets.top; 1869 rect.width = c.getWidth() - insets.left - insets.right; 1870 rect.height = c.getHeight() - insets.top - insets.bottom; 1871 1872 return rect; 1873 } 1874} 1875 | Popular Tags |