File: WaitingImageObserver.java

package info (click to toggle)
libbase 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: java: 8,709; xml: 1,522; makefile: 18
file content (202 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (4)
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
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program 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.
 *
 * Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.base.util;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This image observer blocks until the image is completely loaded. AWT defers the loading of images until they are
 * painted on a graphic.
 * <p/>
 * 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
{
  /** A logger. */
  private static final Log LOGGER = LogFactory.getLog(WaitingImageObserver.class);

  /**
   * For serialization.
   */
  private static final long serialVersionUID = -807204410581383550L;

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

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

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

  private long lastUpdate;
  
  // we better dont wait longer than two seconds for the image. This denotes the maximum time between two
  // updates, not the total loading time.
  private static final long MAX_LOADTIME_DEFAULT = 2000;
  private long maxLoadTime;

  /**
   * Creates a new <code>ImageObserver<code> for the given <code>Image<code>. The observer has to be started by an
   * external thread.
   *
   * @param image the image to observe (<code>null</code> not permitted).
   */
  public WaitingImageObserver(final Image image)
  {
    this(image, MAX_LOADTIME_DEFAULT);
  }

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

  /**
   * 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:  <code>WIDTH</code>, <code>HEIGHT</code>,
   *                  <code>PROPERTIES</code>, <code>SOMEBITS</code>, <code>FRAMEBITS</code>, <code>ALLBITS</code>,
   *                  <code>ERROR</code>, <code>ABORT</code>.
   * @param x         the <i>x</i> coordinate.
   * @param y         the <i>y</i> coordinate.
   * @param width     the width.
   * @param height    the height.
   * @return <code>false</code> if the infoflags indicate that the image is completely loaded; <code>true</code>
   *         otherwise.
   */
  public synchronized boolean imageUpdate(final Image img,
                                          final int infoflags,
                                          final int x,
                                          final int y,
                                          final int width,
                                          final int height)
  {
    if (img == null)
    {
      throw new NullPointerException();
    }
    lastUpdate = System.currentTimeMillis();
    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 && error == false)
    {
      if (g.drawImage(this.image, 0, 0, img.getWidth(this), img.getHeight(this), this))
      {
        return;
      }

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

      if (maxLoadTime > 0 && lastUpdate < (System.currentTimeMillis() - maxLoadTime))
      {
        error = true;
        lock = false;
        LOGGER.info("WaitingImageObserver.waitImageLoaded(): Image loading reached timeout.");
        return;
      }
    }
  }

  /**
   * Checks whether the loading is complete.
   * @return true, if the loading is complete, false otherwise.
   */
  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;
  }
}