All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jfree.util.WaitingImageObserver Maven / Gradle / Ivy

Go to download

jtstand-common is a library derived from jcommon, used by jtstand-chart, which is derived from jfreechart

There is a newer version: 1.5.9
Show newest version
/*
 * Copyright (c) 2009 Albert Kurucz. 
 *
 * This file, WaitingImageObserver.java is part of JTStand.
 *
 * JTStand is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JTStand 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GTStand.  If not, see .
 */

package org.jfree.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.Serializable;

/**
 * This image observer blocks until the image is completely loaded. AWT
 * defers the loading of images until they are painted on a graphic.
 *
 * While printing reports it is not very nice, not to know whether a image
 * was completely loaded, so this observer forces the loading of the image
 * until a final state (either ALLBITS, ABORT or ERROR) is reached.
 *
 * @author Thomas Morgner
 */
public class WaitingImageObserver implements ImageObserver, Serializable,
                                             Cloneable
{
  /** For serialization. */
  static final long serialVersionUID = -807204410581383550L;

  /** The lock. */
  private boolean lock;

  /** The image. */
  private Image image;

  /** A flag that signals an error. */
  private boolean error;

  /**
   * Creates a new ImageObserver for the given Image.
   * The observer has to be started by an external thread.
   *
   * @param image  the image to observe (null not permitted).
   */
  public WaitingImageObserver(final Image image) {
    if (image == null) {
      throw new NullPointerException();
    }
    this.image = image;
    this.lock = true;
  }

  /**
   * Callback function used by AWT to inform that more data is available. The
   * observer waits until either all data is loaded or AWT signals that the
   * image cannot be loaded.
   *
   * @param     img   the image being observed.
   * @param     infoflags   the bitwise inclusive OR of the following
   *               flags:  WIDTH, HEIGHT,
   *               PROPERTIES, SOMEBITS,
   *               FRAMEBITS, ALLBITS,
   *               ERROR, ABORT.
   * @param     x   the x coordinate.
   * @param     y   the y coordinate.
   * @param     width    the width.
   * @param     height   the height.
   *
   * @return    false if the infoflags indicate that the
   *            image is completely loaded; true otherwise.
   */
  public synchronized boolean imageUpdate(
      final Image img,
      final int infoflags,
      final int x,
      final int y,
      final int width,
      final int height) {
    if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) {
        this.lock = false;
        this.error = false;
        notifyAll();
        return false;
    }
    else if ((infoflags & ImageObserver.ABORT) == ImageObserver.ABORT
        || (infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) {
        this.lock = false;
        this.error = true;
        notifyAll();
        return false;
    }
    //notifyAll();
    return true;
  }

  /**
   * The workerthread. Simply draws the image to a BufferedImage's
   * Graphics-Object and waits for the AWT to load the image.
   */
  public synchronized void waitImageLoaded() {

    if (this.lock == false)
    {
      return;
    }

    final BufferedImage img = new BufferedImage(
        1, 1, BufferedImage.TYPE_INT_RGB
    );
    final Graphics g = img.getGraphics();

    while (this.lock) {
      if (g.drawImage(this.image, 0, 0, img.getWidth(this),
            img.getHeight(this), this)) {
        return;
      }

      try {
        wait(500);
      }
      catch (InterruptedException e) {
        Log.info(
          "WaitingImageObserver.waitImageLoaded(): InterruptedException thrown",
          e
        );
      }
    }
  }

  /**
   * Clones this WaitingImageObserver.
   *
   * @return a clone.
   *
   * @throws CloneNotSupportedException this should never happen.
   * @deprecated cloning may lock down the observer
   */
  public Object clone() throws CloneNotSupportedException {
    return (WaitingImageObserver) super.clone();
  }

  /**
   * Returns true if loading is complete, and false
   * otherwise.
   *
   * @return A boolean.
   */
  public boolean isLoadingComplete() {
    return this.lock == false;
  }

  /**
   * Returns true if there is an error condition, and false otherwise.
   *
   * @return A boolean.
   */
  public boolean isError() {
    return this.error;
  }
}