package examples.zac.update;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import weblogic.zac.ZACLog;

/**
 * Creates the frame for the ZACLogPanel.
 * 
 * @author                    Copyright (c) 1998-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class ZACLogDialog extends Dialog implements ActionListener, WindowListener {
  
  Hashtable buttons2panels = new Hashtable(10);
  ScrollPane view;
  
  public ZACLogDialog(Frame f, Enumeration zaclogs) {
    super(f, "ZAC Updates", false);
    addWindowListener(this);
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5,5,5,5);
    
    gbc.gridx = gbc.gridy = 0;
    gbc.fill = gbc.BOTH;
    gbc.weightx = gbc.weighty = 1.0;
    gbc.gridwidth = gbc.REMAINDER;
    view = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
    add(view, gbc);
    
    boolean first = true;
    while (zaclogs.hasMoreElements()) {
      ZACLog zl = (ZACLog)zaclogs.nextElement();
      ZACLogPanel zlp = new ZACLogPanel(zl);
      if (first) {
        view.add(zlp);
        first = false;
      }
      Button b = new Button(zl.getZACName());
      b.addActionListener(this);
      buttons2panels.put(b, zlp);
    }
    
    gbc.fill = gbc.HORIZONTAL;
    gbc.gridy++;
    gbc.weightx = gbc.weighty = 0.1;
    add(makeButtonPanel(), gbc);
    setSize(400,300);
    /* front & center boy */
    Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Dimension sz = getSize();
    Point p = new Point(0,0);
    p.x = (screen.width - sz.width)/2;
    p.y = (screen.height - sz.height)/2;
    setLocation(p);
  }
  
  private Component makeButtonPanel() {
    
    Panel ret = new Panel();
    int sz = buttons2panels.size();
    int cols = 3;
    int rows;
    if (sz % cols == 0) {
      rows = sz / cols;
    } else {
      rows = sz / cols + 1;
    }
    ret.setLayout(new GridLayout(rows, cols, 3, 4));
    Enumeration e = buttons2panels.keys();
    while (e.hasMoreElements()) {
      Button b = (Button)e.nextElement();
      ret.add(b);
    }
    return ret;
  }
  
  /* ActionListener interface */
  public void actionPerformed(ActionEvent ev) {
    Object o = ev.getSource();
    Container c = (Container)buttons2panels.get(o);
    if (c == null) return;
    view.add(c);
    /* I don't know yet why this hack is necessary */
    for (int i = 0; i < 3; i++) {
      c.doLayout();
      view.doLayout();
      doLayout();
    }
  }
  
  /** WindowListener methods */
  public void windowActivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowClosing(WindowEvent e) {
    setVisible(false);
    dispose();
  }
  public void windowDeactivated(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowOpened(WindowEvent e) {}
}

