1 7 8 package java.awt; 9 10 import java.awt.Component ; 11 import java.awt.Image ; 12 import java.awt.Graphics ; 13 import java.awt.image.ImageObserver ; 14 15 152 public class MediaTracker implements java.io.Serializable { 153 154 162 Component target; 163 171 MediaEntry head; 172 173 176 private static final long serialVersionUID = -483174189758638095L; 177 178 183 public MediaTracker(Component comp) { 184 target = comp; 185 } 186 187 194 public void addImage(Image image, int id) { 195 addImage(image, id, -1, -1); 196 } 197 198 208 public synchronized void addImage(Image image, int id, int w, int h) { 209 head = MediaEntry.insert(head, 210 new ImageMediaEntry(this, image, id, w, h)); 211 } 212 213 218 public static final int LOADING = 1; 219 220 225 public static final int ABORTED = 2; 226 227 233 public static final int ERRORED = 4; 234 235 241 public static final int COMPLETE = 8; 242 243 static final int DONE = (ABORTED | ERRORED | COMPLETE); 244 245 264 public boolean checkAll() { 265 return checkAll(false, true); 266 } 267 268 290 public boolean checkAll(boolean load) { 291 return checkAll(load, true); 292 } 293 294 private synchronized boolean checkAll(boolean load, boolean verify) { 295 MediaEntry cur = head; 296 boolean done = true; 297 while (cur != null) { 298 if ((cur.getStatus(load, verify) & DONE) == 0) { 299 done = false; 300 } 301 cur = cur.next; 302 } 303 return done; 304 } 305 306 314 public synchronized boolean isErrorAny() { 315 MediaEntry cur = head; 316 while (cur != null) { 317 if ((cur.getStatus(false, true) & ERRORED) != 0) { 318 return true; 319 } 320 cur = cur.next; 321 } 322 return false; 323 } 324 325 334 public synchronized Object [] getErrorsAny() { 335 MediaEntry cur = head; 336 int numerrors = 0; 337 while (cur != null) { 338 if ((cur.getStatus(false, true) & ERRORED) != 0) { 339 numerrors++; 340 } 341 cur = cur.next; 342 } 343 if (numerrors == 0) { 344 return null; 345 } 346 Object errors[] = new Object [numerrors]; 347 cur = head; 348 numerrors = 0; 349 while (cur != null) { 350 if ((cur.getStatus(false, false) & ERRORED) != 0) { 351 errors[numerrors++] = cur.getMedia(); 352 } 353 cur = cur.next; 354 } 355 return errors; 356 } 357 358 374 public void waitForAll() throws InterruptedException { 375 waitForAll(0); 376 } 377 378 399 public synchronized boolean waitForAll(long ms) 400 throws InterruptedException 401 { 402 long end = System.currentTimeMillis() + ms; 403 boolean first = true; 404 while (true) { 405 int status = statusAll(first, first); 406 if ((status & LOADING) == 0) { 407 return (status == COMPLETE); 408 } 409 first = false; 410 long timeout; 411 if (ms == 0) { 412 timeout = 0; 413 } else { 414 timeout = end - System.currentTimeMillis(); 415 if (timeout <= 0) { 416 return false; 417 } 418 } 419 wait(timeout); 420 } 421 } 422 423 446 public int statusAll(boolean load) { 447 return statusAll(load, true); 448 } 449 450 private synchronized int statusAll(boolean load, boolean verify) { 451 MediaEntry cur = head; 452 int status = 0; 453 while (cur != null) { 454 status = status | cur.getStatus(load, verify); 455 cur = cur.next; 456 } 457 return status; 458 } 459 460 480 public boolean checkID(int id) { 481 return checkID(id, false, true); 482 } 483 484 507 public boolean checkID(int id, boolean load) { 508 return checkID(id, load, true); 509 } 510 511 private synchronized boolean checkID(int id, boolean load, boolean verify) 512 { 513 MediaEntry cur = head; 514 boolean done = true; 515 while (cur != null) { 516 if (cur.getID() == id 517 && (cur.getStatus(load, verify) & DONE) == 0) 518 { 519 done = false; 520 } 521 cur = cur.next; 522 } 523 return done; 524 } 525 526 536 public synchronized boolean isErrorID(int id) { 537 MediaEntry cur = head; 538 while (cur != null) { 539 if (cur.getID() == id 540 && (cur.getStatus(false, true) & ERRORED) != 0) 541 { 542 return true; 543 } 544 cur = cur.next; 545 } 546 return false; 547 } 548 549 561 public synchronized Object [] getErrorsID(int id) { 562 MediaEntry cur = head; 563 int numerrors = 0; 564 while (cur != null) { 565 if (cur.getID() == id 566 && (cur.getStatus(false, true) & ERRORED) != 0) 567 { 568 numerrors++; 569 } 570 cur = cur.next; 571 } 572 if (numerrors == 0) { 573 return null; 574 } 575 Object errors[] = new Object [numerrors]; 576 cur = head; 577 numerrors = 0; 578 while (cur != null) { 579 if (cur.getID() == id 580 && (cur.getStatus(false, false) & ERRORED) != 0) 581 { 582 errors[numerrors++] = cur.getMedia(); 583 } 584 cur = cur.next; 585 } 586 return errors; 587 } 588 589 605 public void waitForID(int id) throws InterruptedException { 606 waitForID(id, 0); 607 } 608 609 631 public synchronized boolean waitForID(int id, long ms) 632 throws InterruptedException 633 { 634 long end = System.currentTimeMillis() + ms; 635 boolean first = true; 636 while (true) { 637 int status = statusID(id, first, first); 638 if ((status & LOADING) == 0) { 639 return (status == COMPLETE); 640 } 641 first = false; 642 long timeout; 643 if (ms == 0) { 644 timeout = 0; 645 } else { 646 timeout = end - System.currentTimeMillis(); 647 if (timeout <= 0) { 648 return false; 649 } 650 } 651 wait(timeout); 652 } 653 } 654 655 680 public int statusID(int id, boolean load) { 681 return statusID(id, load, true); 682 } 683 684 private synchronized int statusID(int id, boolean load, boolean verify) { 685 MediaEntry cur = head; 686 int status = 0; 687 while (cur != null) { 688 if (cur.getID() == id) { 689 status = status | cur.getStatus(load, verify); 690 } 691 cur = cur.next; 692 } 693 return status; 694 } 695 696 705 public synchronized void removeImage(Image image) { 706 MediaEntry cur = head; 707 MediaEntry prev = null; 708 while (cur != null) { 709 MediaEntry next = cur.next; 710 if (cur.getMedia() == image) { 711 if (prev == null) { 712 head = next; 713 } else { 714 prev.next = next; 715 } 716 cur.cancel(); 717 } else { 718 prev = cur; 719 } 720 cur = next; 721 } 722 notifyAll(); } 724 725 736 public synchronized void removeImage(Image image, int id) { 737 MediaEntry cur = head; 738 MediaEntry prev = null; 739 while (cur != null) { 740 MediaEntry next = cur.next; 741 if (cur.getID() == id && cur.getMedia() == image) { 742 if (prev == null) { 743 head = next; 744 } else { 745 prev.next = next; 746 } 747 cur.cancel(); 748 } else { 749 prev = cur; 750 } 751 cur = next; 752 } 753 notifyAll(); } 755 756 768 public synchronized void removeImage(Image image, int id, 769 int width, int height) { 770 MediaEntry cur = head; 771 MediaEntry prev = null; 772 while (cur != null) { 773 MediaEntry next = cur.next; 774 if (cur.getID() == id && cur instanceof ImageMediaEntry 775 && ((ImageMediaEntry) cur).matches(image, width, height)) 776 { 777 if (prev == null) { 778 head = next; 779 } else { 780 prev.next = next; 781 } 782 cur.cancel(); 783 } else { 784 prev = cur; 785 } 786 cur = next; 787 } 788 notifyAll(); } 790 791 synchronized void setDone() { 792 notifyAll(); 793 } 794 } 795 796 abstract class MediaEntry { 797 MediaTracker tracker; 798 int ID; 799 MediaEntry next; 800 801 int status; 802 boolean cancelled; 803 804 MediaEntry(MediaTracker mt, int id) { 805 tracker = mt; 806 ID = id; 807 } 808 809 abstract Object getMedia(); 810 811 static MediaEntry insert(MediaEntry head, MediaEntry me) { 812 MediaEntry cur = head; 813 MediaEntry prev = null; 814 while (cur != null) { 815 if (cur.ID > me.ID) { 816 break; 817 } 818 prev = cur; 819 cur = cur.next; 820 } 821 me.next = cur; 822 if (prev == null) { 823 head = me; 824 } else { 825 prev.next = me; 826 } 827 return head; 828 } 829 830 int getID() { 831 return ID; 832 } 833 834 abstract void startLoad(); 835 836 void cancel() { 837 cancelled = true; 838 } 839 840 static final int LOADING = MediaTracker.LOADING; 841 static final int ABORTED = MediaTracker.ABORTED; 842 static final int ERRORED = MediaTracker.ERRORED; 843 static final int COMPLETE = MediaTracker.COMPLETE; 844 845 static final int LOADSTARTED = (LOADING | ERRORED | COMPLETE); 846 static final int DONE = (ABORTED | ERRORED | COMPLETE); 847 848 synchronized int getStatus(boolean doLoad, boolean doVerify) { 849 if (doLoad && ((status & LOADSTARTED) == 0)) { 850 status = (status & ~ABORTED) | LOADING; 851 startLoad(); 852 } 853 return status; 854 } 855 856 void setStatus(int flag) { 857 synchronized (this) { 858 status = flag; 859 } 860 tracker.setDone(); 861 } 862 } 863 864 class ImageMediaEntry extends MediaEntry implements ImageObserver , 865 java.io.Serializable { 866 Image image; 867 int width; 868 int height; 869 870 873 private static final long serialVersionUID = 4739377000350280650L; 874 875 ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) { 876 super(mt, c); 877 image = img; 878 width = w; 879 height = h; 880 } 881 882 boolean matches(Image img, int w, int h) { 883 return (image == img && width == w && height == h); 884 } 885 886 Object getMedia() { 887 return image; 888 } 889 890 int getStatus(boolean doLoad, boolean doVerify) { 891 if (doVerify) { 892 int flags = tracker.target.checkImage(image, width, height, null); 893 int s = parseflags(flags); 894 if (s == 0) { 895 if ((status & (ERRORED | COMPLETE)) != 0) { 896 setStatus(ABORTED); 897 } 898 } else if (s != status) { 899 setStatus(s); 900 } 901 } 902 return super.getStatus(doLoad, doVerify); 903 } 904 905 void startLoad() { 906 if (tracker.target.prepareImage(image, width, height, this)) { 907 setStatus(COMPLETE); 908 } 909 } 910 911 int parseflags(int infoflags) { 912 if ((infoflags & ERROR) != 0) { 913 return ERRORED; 914 } else if ((infoflags & ABORT) != 0) { 915 return ABORTED; 916 } else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) { 917 return COMPLETE; 918 } 919 return 0; 920 } 921 922 public boolean imageUpdate(Image img, int infoflags, 923 int x, int y, int w, int h) { 924 if (cancelled) { 925 return false; 926 } 927 int s = parseflags(infoflags); 928 if (s != 0 && s != status) { 929 setStatus(s); 930 } 931 return ((status & LOADING) != 0); 932 } 933 } 934 | Popular Tags |