summaryrefslogtreecommitdiff
path: root/libjava/classpath/java/awt/MediaTracker.java
blob: e69d23e8b7ea3725c975e33afdebe0e0cd6d6ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/* MediaTracker.java -- Class used for keeping track of images
   Copyright (C) 1999, 2002, 2004, 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt;

import java.awt.image.ImageObserver;
import java.util.ArrayList;

/**
  * This class is used for keeping track of the status of various media
  * objects.
  *
  * Media objects are tracked by assigning them an ID. It is possible
  * to assign the same ID to mutliple objects, effectivly grouping them
  * together. In this case the status flags ({@link #statusID}) and error flag
  * (@link #isErrorID} and {@link #getErrorsID}) are ORed together. This
  * means that you cannot say exactly which media object has which status,
  * at most you can say that there <em>are</em> certain media objects with
  * some certain status.
  *
  * At the moment only images are supported by this class.
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  * @author Bryce McKinlay
  */
public class MediaTracker implements java.io.Serializable
{
  /** Indicates that the media is still loading. */
  public static final int LOADING = 1 << 0;

  /** Indicates that the loading operation has been aborted. */
  public static final int ABORTED = 1 << 1;

  /** Indicates that an error has occured during loading of the media. */
  public static final int ERRORED = 1 << 2;

  /** Indicates that the media has been successfully and completely loaded. */
  public static final int COMPLETE = 1 << 3;

  /** The component on which the media is eventually been drawn. */
  Component target;

  /** The head of the linked list of tracked media objects. */
  MediaEntry head;

  /** Our serialVersionUID for serialization. */
  static final long serialVersionUID = -483174189758638095L;

  /**
   * This represents a media object that is tracked by a MediaTracker.
   * It also implements a simple linked list.
   */
  // FIXME: The serialized form documentation says MediaEntry is a
  // serializable field, but the serialized form of MediaEntry itself
  // doesn't appear to be documented.
  class MediaEntry implements ImageObserver
  {
    /** The ID of the media object. */
    int id;

    /** The media object. (only images are supported ATM). */
    Image image;

    /** The link to the next entry in the list. */
    MediaEntry next;

    /** The tracking status. */
    int status;

    /** The width of the image. */
    int width;

    /** The height of the image. */
    int height;

    /**
     * Receives notification from an {@link java.awt.image.ImageProducer}
     * that more data of the image is available.
     *
     * @param img the image that is updated
     * @param flags flags from the ImageProducer that indicate the status
     *        of the loading process
     * @param x the X coordinate of the upper left corner of the image
     * @param y the Y coordinate of the upper left corner of the image
     * @param width the width of the image
     * @param height the height of the image
     *
     * @return <code>true</code> if more data is needed, <code>false</code>
     *         otherwise
     *
     * @see java.awt.image.ImageObserver
     */
    public boolean imageUpdate(Image img, int flags, int x, int y,
                               int width, int height)
    {
      if ((flags & ABORT) != 0)
        status = ABORTED;
      else if ((flags & ERROR) != 0)
        status = ERRORED;
      else if ((flags & ALLBITS) != 0)
        status = COMPLETE;
      else
        status = 0;

      synchronized (MediaTracker.this)
        {
          MediaTracker.this.notifyAll();
        }

      // If status is not COMPLETE then we need more updates.
      return ((status & (COMPLETE | ERRORED | ABORTED)) == 0);
    }
  }

  /**
   * Constructs a new MediaTracker for the component <code>c</code>. The
   * component should be the component that uses the media (i.e. draws it).
   *
   * @param c the Component that wants to use the media
   */
  public MediaTracker(Component c)
  {
    target = c;
  }

  /**
   * Adds an image to the tracker with the specified <code>ID</code>.
   *
   * @param image the image to be added
   * @param id the ID of the tracker list to which the image is added
   */
  public void addImage(Image image, int id)
  {
    MediaEntry e = new MediaEntry();
    e.id = id;
    e.image = image;
    synchronized(this)
      {
        e.next = head;
        head = e;
      }
  }

  /**
   * Adds an image to the tracker with the specified <code>ID</code>.
   * The image is expected to be rendered with the specified width and
   * height.
   *
   * @param image the image to be added
   * @param id the ID of the tracker list to which the image is added
   * @param width the width of the image
   * @param height the height of the image
   */
  public void addImage(Image image, int id, int width, int height)
  {
    MediaEntry e = new MediaEntry();
    e.id = id;
    e.image = image;
    e.width = width;
    e.height = height;
    synchronized(this)
      {
        e.next = head;
        head = e;
      }
  }

  /**
   * Checks if all media objects have finished loading, i.e. are
   * {@link #COMPLETE}, {@link #ABORTED} or {@link #ERRORED}.
   *
   * If the media objects are not already loading, a call to this
   * method does <em>not</em> start loading. This is equivalent to
   * a call to <code>checkAll(false)</code>.
   *
   * @return if all media objects have finished loading either by beeing
   *         complete, have been aborted or errored.
   */
  public boolean checkAll()
  {
    return checkAll(false);
  }

  /**
   * Checks if all media objects have finished loading, i.e. are
   * {@link #COMPLETE}, {@link #ABORTED} or {@link #ERRORED}.
   *
   * If the media objects are not already loading, and <code>load</code>
   * is <code>true</code> then a call to this
   * method starts loading the media objects.
   *
   * @param load if <code>true</code> this method starts loading objects
   *        that are not already loading
   *
   * @return if all media objects have finished loading either by beeing
   *         complete, have been aborted or errored.
   */
  public boolean checkAll(boolean load)
  {
    MediaEntry e = head;
    boolean result = true;

    while (e != null)
      {
        if ((e.status & (COMPLETE | ERRORED | ABORTED)) == 0)
          {
            if (load && ((e.status & LOADING) == 0))
              {
                if (target.prepareImage(e.image, e))
                  e.status = COMPLETE;
                else
                  {
                    e.status = LOADING;
                    int flags = target.checkImage(e.image, e);
                    if ((flags & ImageObserver.ABORT) != 0)
                      e.status = ABORTED;
                    else if ((flags & ImageObserver.ERROR) != 0)
                      e.status = ERRORED;
                    else if ((flags & ImageObserver.ALLBITS) != 0)
                      e.status = COMPLETE;
                  }
                boolean complete = (e.status
                                    & (COMPLETE | ABORTED | ERRORED)) != 0;
                if (!complete)
                  result = false;
              }
            else
              result = false;
          }
        e = e.next;
      }
    return result;
  }

  /**
   * Checks if any of the registered media objects has encountered an error
   * during loading.
   *
   * @return <code>true</code> if at least one media object has encountered
   *         an error during loading, <code>false</code> otherwise
   *
   */
  public boolean isErrorAny()
  {
    MediaEntry e = head;
    while (e != null)
      {
        if ((e.status & ERRORED) != 0)
          return true;
        e = e.next;
      }
    return false;
  }

  /**
   * Returns all media objects that have encountered errors during loading.
   *
   * @return an array of all media objects that have encountered errors
   *         or <code>null</code> if there were no errors at all
   */
  public Object[] getErrorsAny()
  {
    MediaEntry e = head;
    ArrayList result = null;
    while (e != null)
      {
        if ((e.status & ERRORED) != 0)
          {
            if (result == null)
              result = new ArrayList();
            result.add(e.image);
          }
        e = e.next;
      }
    if (result == null)
      return null;
    else
      return result.toArray();
  }

  /**
   * Waits for all media objects to finish loading, either by completing
   * successfully or by aborting or encountering an error.
   *
   * @throws InterruptedException if another thread interrupted the
   *         current thread while waiting
   */
  public void waitForAll() throws InterruptedException
  {
    synchronized (this)
    {
      while (checkAll(true) == false)
        wait();
    }
  }

  /**
   * Waits for all media objects to finish loading, either by completing
   * successfully or by aborting or encountering an error.
   *
   * This method waits at most <code>ms</code> milliseconds. If the
   * media objects have not completed loading within this timeframe, this
   * method returns <code>false</code>, otherwise <code>true</code>.
   *
   * @param ms timeframe in milliseconds to wait for the media objects to
   *        finish
   *
   * @return <code>true</code> if all media objects have successfully loaded
   *         within the timeframe, <code>false</code> otherwise
   *
   * @throws InterruptedException if another thread interrupted the
   *         current thread while waiting
   */
  public boolean waitForAll(long ms) throws InterruptedException
  {
    long start = System.currentTimeMillis();
    boolean result = checkAll(true);
    synchronized (this)
    {
      while (result == false)
        {
          wait(ms);
          result = checkAll(true);
          if ((System.currentTimeMillis() - start) > ms)
            break;
        }
    }

    return result;
  }

  /**
   * Returns the status flags of all registered media objects ORed together.
   * If <code>load</code> is <code>true</code> then media objects that
   * are not already loading will be started to load.
   *
   * @param load if set to <code>true</code> then media objects that are
   *        not already loading are started
   *
   * @return the status flags of all tracked media objects ORed together
   */
  public int statusAll(boolean load)
  {
    int result = 0;
    MediaEntry e = head;
    while (e != null)
      {
        if (load && e.status == 0)
          {
            if (target.prepareImage(e.image, e))
              e.status = COMPLETE;
            else
              {
                e.status = LOADING;
                int flags = target.checkImage(e.image, e);
                if ((flags & ImageObserver.ABORT) != 0)
                  e.status = ABORTED;
                else if ((flags & ImageObserver.ERROR) != 0)
                  e.status = ERRORED;
                else if ((flags & ImageObserver.ALLBITS) != 0)
                  e.status = COMPLETE;
              }
          }
        result |= e.status;
        e = e.next;
      }
    return result;
  }

  /**
   * Checks if the media objects with <code>ID</code> have completed loading.
   *
   * @param id the ID of the media objects to check
   *
   * @return <code>true</code> if all media objects with <code>ID</code>
   *         have successfully finished
   */
  public boolean checkID(int id)
  {
    return checkID(id, false);
  }

  /**
   * Checks if the media objects with <code>ID</code> have completed loading.
   * If <code>load</code> is <code>true</code> then media objects that
   * are not already loading will be started to load.
   *
   * @param id the ID of the media objects to check
   * @param load if set to <code>true</code> then media objects that are
   *        not already loading are started
   *
   * @return <code>true</code> if all media objects with <code>ID</code>
   *         have successfully finished
   */
  public boolean checkID(int id, boolean load)
  {
    MediaEntry e = head;
    boolean result = true;

    while (e != null)
      {
        if (e.id == id && ((e.status & (COMPLETE | ABORTED | ERRORED)) == 0))
          {
            if (load && ((e.status & LOADING) == 0))
              {
                e.status = LOADING;
                if (target.prepareImage(e.image, e))
                  e.status = COMPLETE;
                else
                  {
                    int flags = target.checkImage(e.image, e);
                    if ((flags & ImageObserver.ABORT) != 0)
                      e.status = ABORTED;
                    else if ((flags & ImageObserver.ERROR) != 0)
                      e.status = ERRORED;
                    else if ((flags & ImageObserver.ALLBITS) != 0)
                      e.status = COMPLETE;
                  }
                boolean complete = (e.status
                                    & (COMPLETE | ABORTED | ERRORED)) != 0;
                if (!complete)
                  result = false;
              }
            else
              result = false;
          }
        e = e.next;
      }
    return result;
  }

  /**
   * Returns <code>true</code> if any of the media objects with <code>ID</code>
   * have encountered errors during loading, false otherwise.
   *
   * @param id the ID of the media objects to check
   *
   * @return <code>true</code> if any of the media objects with <code>ID</code>
   *         have encountered errors during loading, false otherwise
   */
  public boolean isErrorID(int id)
  {
    MediaEntry e = head;
    while (e != null)
      {
        if (e.id == id && ((e.status & ERRORED) != 0))
          return true;
        e = e.next;
      }
    return false;
  }

  /**
   * Returns all media objects with the specified ID that have encountered
   * an error.
   *
   * @param id the ID of the media objects to check
   *
   * @return an array of all media objects  with the specified ID that
   *         have encountered an error
   */
  public Object[] getErrorsID(int id)
  {
    MediaEntry e = head;
    ArrayList result = null;
    while (e != null)
      {
        if (e.id == id && ((e.status & ERRORED) != 0))
          {
            if (result == null)
              result = new ArrayList();
            result.add(e.image);
          }
        e = e.next;
      }
    if (result == null)
      return null;
    else
      return result.toArray();
  }

  /**
   * Waits for all media objects with the specified ID to finish loading,
   * either by completing successfully or by aborting or encountering an error.
   *
   * @param id the ID of the media objects to wait for
   *
   * @throws InterruptedException if another thread interrupted the
   *         current thread while waiting
   */
  public void waitForID(int id) throws InterruptedException
  {
    MediaEntry e = head;
    synchronized (this)
    {
      while (checkID (id, true) == false)
        wait();
    }
  }

  /**
   * Waits for all media objects with the specified ID to finish loading,
   * either by completing successfully or by aborting or encountering an error.
   *
   * This method waits at most <code>ms</code> milliseconds. If the
   * media objects have not completed loading within this timeframe, this
   * method returns <code>false</code>, otherwise <code>true</code>.
   *
   * @param id the ID of the media objects to wait for
   * @param ms timeframe in milliseconds to wait for the media objects to
   *        finish
   *
   * @return <code>true</code> if all media objects have successfully loaded
   *         within the timeframe, <code>false</code> otherwise
   *
   * @throws InterruptedException if another thread interrupted the
   *         current thread while waiting
   */
  public boolean waitForID(int id, long ms) throws InterruptedException
  {
    MediaEntry e = head;
    long start = System.currentTimeMillis();
    boolean result = checkID(id, true);

    synchronized (this)
    {
      while (result == false)
        {
          wait(ms);
          result = checkID(id, true);
          if ((System.currentTimeMillis() - start) > ms)
            break;
        }
    }

    return result;
  }

  /**
   * Returns the status flags of the media objects with the specified ID
   * ORed together.
   *
   * If <code>load</code> is <code>true</code> then media objects that
   * are not already loading will be started to load.
   *
   * @param load if set to <code>true</code> then media objects that are
   *        not already loading are started
   *
   * @return the status flags of all tracked media objects ORed together
   */
  public int statusID(int id, boolean load)
  {
    int result = 0;
    MediaEntry e = head;
    while (e != null)
      {
        if (e.id == id)
          {
            if (load && e.status == 0)
              {
                if (target.prepareImage(e.image, e))
                  e.status = COMPLETE;
                else
                  {
                    e.status = LOADING;
                    int flags = target.checkImage(e.image, e);
                    if ((flags & ImageObserver.ABORT) != 0)
                      e.status = ABORTED;
                    else if ((flags & ImageObserver.ERROR) != 0)
                      e.status = ERRORED;
                    else if ((flags & ImageObserver.ALLBITS) != 0)
                      e.status = COMPLETE;
                  }
              }
            result |= e.status;
          }
        e = e.next;
      }
    return result;
  }

  /**
   * Removes an image from this MediaTracker.
   *
   * @param image the image to be removed
   */
  public void removeImage(Image image)
  {
    synchronized (this)
      {
        MediaEntry e = head;
        MediaEntry prev = null;
        while (e != null)
          {
            if (e.image == image)
              {
                if (prev == null)
                  head = e.next;
                else
                  prev.next = e.next;
              }
            else
              prev = e;
            e = e.next;
          }
      }
  }

  /**
   * Removes an image with the specified ID from this MediaTracker.
   *
   * @param image the image to be removed
   */
  public void removeImage(Image image, int id)
  {
    synchronized (this)
      {
        MediaEntry e = head;
        MediaEntry prev = null;
        while (e != null)
          {
            if (e.id == id && e.image == image)
              {
                if (prev == null)
                  head = e.next;
                else
                  prev.next = e.next;
              }
            else
              prev = e;
            e = e.next;
          }
      }
  }

  /**
   * Removes an image with the specified ID and scale from this MediaTracker.
   *
   * @param image the image to be removed
   */
  public void removeImage(Image image, int id, int width, int height)
  {
    synchronized (this)
      {
        MediaEntry e = head;
        MediaEntry prev = null;
        while (e != null)
          {
            if (e.id == id && e.image == image
                && e.width == width && e.height == height)
              {
                if (prev == null)
                  head = e.next;
                else
                  prev.next = e.next;
              }
            else
              prev = e;
            e = e.next;
          }
      }
  }
}