package examples.zac.update;

import java.awt.*;
import java.util.Enumeration;
import java.util.Vector;

/**
 * Used by the ZAC-driven applet in this example.
 *
 * @author                    Copyright (c) 1998-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class ObjectListPanel extends Canvas {
  
  char[][] vals;
  int W, H, fh;
  Font f;
  static final int PAD = 10;
  
  public ObjectListPanel(Enumeration enum) {
    this(enum, new Font("Serif", Font.PLAIN, 10));
  }
  
  public ObjectListPanel(Enumeration enum, Font f) {
    this.f = f;
    parse(enum);
  }
  
  private void parse(Enumeration enum) {
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
    fh = fm.getHeight();
    W = H = 0;
    Vector v = new Vector();
    while (enum.hasMoreElements()) {
      char[] ca = enum.nextElement().toString().toCharArray();
      W = Math.max(W, fm.charsWidth(ca, 0, ca.length));
      H += fh;
      v.addElement(ca);
    }
    H += fh * 3;
    W += (PAD<<2);
    vals = new char[v.size()][];
    v.copyInto(vals);
  }
  
  public Dimension preferredSize() {
    return getPreferredSize();
  }
  
  public Dimension minimumSize() {
    return getPreferredSize();
  }
  
  public Dimension getPreferredSize() {
    return new Dimension(W,H);
  }
  
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
  
  public void paintBorder(Graphics g) {
    g.drawLine(PAD,PAD,W-PAD,PAD);
    g.drawLine(PAD,PAD,PAD,H-PAD);
    g.drawLine(PAD,H-PAD,W-PAD,H-PAD);
    g.drawLine(W-PAD,PAD,W-PAD,H-PAD);
  }
  
  public void paint(Graphics g) {
    paintBorder(g);
    g.setFont(f);
    int i = 0;
    for (int j = (fh<<1); i < vals.length; i++, j += fh) {
      g.drawChars(vals[i], 0, vals[i].length, PAD<<1, j);
    }
  }
}
