package examples.zac.update;

import java.io.*;
import java.awt.*;
import java.util.*;

import weblogic.zac.ZACLog;
/* for exceptions */
import weblogic.gui.ExceptionPanel;

/**
 * Displays a log from a ZAC update.
 *
 * @author                    Copyright (c) 1998-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class ZACLogPanel extends Panel {
  
  ZACLog zl;
  
  static final Font BIG =  new Font("SansSerif", Font.BOLD, 16);
  static final Font SMALL = new Font("SansSerif", Font.PLAIN, 12);
  static final Font SMALLBOLD = new Font("SansSerif", Font.BOLD, 12);
  static final Font SMALLITALIC = new Font("SansSerif", Font.ITALIC, 12);
  static final Font TINYITALIC = new Font("SansSerif", Font.ITALIC, 10);

  /**
   * Sets up the displayable parts related to ZAC, including
   * ZAC name, host and port of the publishing server, local
   * ZAC directory location, and the status of the update.
   */
  public ZACLogPanel(ZACLog zl) {
    if (zl == null)
      throw new NullPointerException("Null ZACLog object!");
    
    this.zl = zl;
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = gbc.gridy = 0;
    gbc.insets = new Insets(4,1,4,1);
    
    /* BIG LABEL */
    gbc.gridwidth = gbc.REMAINDER;
    gbc.fill = gbc.HORIZONTAL;
    Label l = new Label("Update status for " + zl.getZACName());
    l.setFont(BIG);
    add(l, gbc);
    
    /* host/port */
    gbc.weightx = gbc.weighty = 0.0;
    gbc.gridx = 0;
    gbc.gridy++;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.fill = gbc.NONE;
    gbc.anchor = gbc.EAST;
    l = new Label("ZAC Host:");
    l.setFont(SMALLBOLD);
    add(l, gbc);
    gbc.gridx++;
    gbc.anchor = gbc.WEST;
    l = new Label(zl.getZACHost());
    l.setFont(SMALLITALIC);
    add(l, gbc);
    gbc.gridx++;
    gbc.anchor = gbc.EAST;
    l = new Label("ZAC Port:");
    l.setFont(SMALLBOLD);
    add(l, gbc);
    gbc.gridx++;
    gbc.anchor = gbc.WEST;
    l = new Label(String.valueOf(zl.getZACPort()));
    l.setFont(SMALLITALIC);
    add(l, gbc);
    
    /* local directory */
    gbc.gridx=0;
    gbc.gridy++;
    gbc.anchor = gbc.EAST;
    l = new Label("Local Directory:");
    l.setFont(SMALLBOLD);
    add(l, gbc);
    gbc.gridwidth = 3;
    gbc.anchor = gbc.WEST;
    gbc.fill = gbc.BOTH;
    gbc.gridx++;
    l = new Label(zl.getLocalDirectory().getAbsolutePath());
    l.setFont(SMALLITALIC);
    add(l, gbc);
    
    /* success/failure/noop */
    gbc.fill = gbc.NONE;
    gbc.gridy++;
    gbc.gridx = 0;
    gbc.gridwidth = 4;
    
    switch (zl.getUpdateStatus()) {
    case ZACLog.UPDATE_NONE:
      l = new Label("No Automatic Update Performed");
      l.setFont(SMALLBOLD);
      add(l, gbc);
      return;
    case ZACLog.UPDATE_FAILURE:
      gbc.anchor = gbc.CENTER;
      l = new Label("Update Failed:");
      l.setFont(SMALLBOLD);
      l.setForeground(Color.red);
      add(l, gbc);
      gbc.gridy++;
      ExceptionPanel c =
        new ExceptionPanel(zl.getUpdateFailureString(), TINYITALIC);
      c.setSeen(true);
      gbc.fill = gbc.BOTH;
      add(c, gbc);
      return;
    }
    
    /* otherwise there was a successful update */
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.HORIZONTAL;
    int cnt = zl.getUpdateFileCount();
    long binsz = zl.getUpdateByteCount();
    
    String lab;
    
    if (cnt == 0)
      lab = "Update Success, 0 files updated, 0 bytes transferred.";
    else {
      if (cnt == 1) 
        lab = "Update Success, 1 file updated, ";
      else
        lab = "Update Success, " + cnt + " files updated, ";
      
      if (binsz > 1000)
        lab += (binsz / 1000) + " KBytes transferred.";
      else
        lab += (binsz) + " bytes transferred.";
    }
    l = new Label(lab);
    l.setFont(SMALLBOLD);
    add(l, gbc);
    
    if (cnt == 0)
      return;
    
    gbc.gridx = 0;
    gbc.gridy++;
    gbc.fill = gbc.BOTH;
    Component comp = new ObjectListPanel(zl.getUpdateFileList(), SMALLITALIC);
    add(comp, gbc);
  }
  
  //    public void paint(Graphics g) {
  //	super.paint(g);
  //	System.err.println(zl.getZACName() + ": list:");
  //	list(System.out);
  //    }
}







