package examples.zac.update;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;

/**
 * Class used by the ZAC-driven applet in this example.
 *
 * @author                    Copyright (c) 1998-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class ImageCanvas extends Canvas implements ImageObserver {
  
  Image logo;
  private static final int W = 40;
  private static final int H = 40;
  
  private int h, w;
  private boolean haveImage;
  private char[] badMsg;
  
  static void p(String s) {
    System.out.println("ImgCanvas: " + s);
  }
  
  public ImageCanvas(String s) {
    getLogo(s);
  }
  
  /* We must provide preferredSize()/minimumSize() methods
   * since the GridBagLayout in the MS VM at the time of
   * this writing (4/98) does not query components using
   * the 1.1 methods.
   */
  public Dimension preferredSize() {
    return getPreferredSize();
  }
  
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
  
  public Dimension minimumSize() {
    return getPreferredSize();
  }
  
  public Dimension getPreferredSize() {
    Dimension d;
    if (w != -1 && h != -1) {
      d = new Dimension(w, h);
    }
    else {
      d = new Dimension(W,H);
    }
    return d;
  }
  
  private void createBadMsg(String s) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    badMsg = s.toCharArray();
    Font f = getFont();
    if (f == null) {
      f = new Font("SansSerif", Font.PLAIN, 12);
      setFont(f);
    }
    FontMetrics fm = tk.getFontMetrics(f);
    w = fm.charsWidth(badMsg, 0, badMsg.length) + 20;
    h = fm.getHeight() + 20;
  }
  
  private void getLogo(String img) {
    try {
      badMsg = null;
      Toolkit tk = Toolkit.getDefaultToolkit();
      URL u = getClass().getResource(img);
      if (u == null) {
        createBadMsg("Image not found: " + img);
        return;
      }
      
      InputStream i = u.openStream();
      byte[] buf = new byte[1024];
      int nread = 0;
      int r;
      while ((r = i.read(buf, nread, buf.length - nread)) != -1) {
        nread += r;
        if (buf.length == nread) {
          byte[] tmp = new byte[nread << 1];
          System.arraycopy(buf, 0, tmp, 0, nread);
          buf = tmp;
        }
      }
      
      logo = tk.createImage(buf, 0, nread);
      w = logo.getWidth(this);
      h = logo.getHeight(this);
      if (w != -1 && h != -1) {
        setSize(w, h);
        Container c = getParent();
        if (c != null)
          c.doLayout();
      }
    }
    catch (Throwable t) {
      t.printStackTrace();
      createBadMsg("image load failed: " + t.toString());
    }
  }
  
  public String toString() {
    return super.toString() + " w="+w+", h="+h;
  }
  
  public void paint(Graphics g) {
    if (badMsg != null) {
      g.drawChars(badMsg, 0, badMsg.length,
                  10, h - 10);
      return;
    }
    
    if (h == -1 || w == -1) {
      //	    p("paint: no dimensions");
      w = logo.getWidth(this);
      h = logo.getHeight(this);
      if (w != -1 && h != -1) {
        setSize(w, h);
        Container c = getParent();
        if (c != null) {
          c.doLayout();
        }
      }
    }
    if (w != -1 && h != -1) {
      g.drawImage(logo, 0, 0, w, h, this);
    }
  }
}
