package examples.zac.simple;

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

/**
 * This simple application demonstrates how you can build
 * automatic updating into your WebLogic apps with the ZAC
 * API.
 *
 * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class ZSimple implements WindowListener, ActionListener {
  
  private Frame frame;
  private static Button restart;

  /**
   * Checks action events. If the action is restart, it calls a
   * system exit with the argument ZAC_EXIT_RESTART from the ZAC
   * class, which causes the current application to be relaunched
   * identically to the current runtime.
   *
   * @param ev                Action event
   */
  public void actionPerformed(ActionEvent ev) {
    Object o = ev.getSource();
    if (o == restart)
      System.exit(ZAC.ZAC_EXIT_RESTART);
  }
  
  /* WindowListener Methods */
  /**
   * WindowListener for an open event.
   */
  public void windowOpened(WindowEvent e) {}
  
  /**
   * WindowListener for a closing event. 
   */
  public void windowClosing(WindowEvent e) {
    frame.setVisible(false);
    frame.dispose();
    System.exit(0);
  }
  
  /**
   * WindowListener for a closed event. 
   */
  public void windowClosed(WindowEvent e) { }

  /**
   * WindowListener for an iconify event.
   */
  public void windowIconified(WindowEvent e) {}

  /**
   * WindowListener for a deiconified event.
   */
  public void windowDeiconified(WindowEvent e) {}

  /**
   * WindowListener for an activated event. 
   */
  public void windowActivated(WindowEvent e) {}
  
  /**
   * WindowListener for a deactivated event. 
   */
  public void windowDeactivated(WindowEvent e) {}

  /**
   * Runs this application. This simple application prints out
   * information about your Java environment. It has a ZAC RESTART
   * button to demonstrate how server-side republishing and
   * client-side updating work.
   */
    public static void main(String[] a) throws Exception {
        //	sort(a);
        if (a == null) {
            System.err.println("argv NULL!");
        }
        else {
            for (int i = 0; i < a.length; i++)
                System.err.println("argv[" + i + "] = '" + a[i] + "'");
        }
        
        Frame f = new Frame("Simple ZAC Example");
        System.out.println("Simple ZAC Example: Entered 'main'");
        GridBagLayout gb = new GridBagLayout();
        Panel pan = new Panel();
        pan.setBackground(Color.lightGray);
        pan.setLayout(gb);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2,2,2,2);
        gbc.gridwidth = gbc.REMAINDER;
        
        ZSimple zs = new ZSimple();
        
        zs.frame = f;
        
        Label l = new Label("Congratulations!");
        l.setFont(new Font("Courier", Font.BOLD, 20));
        l.setForeground(new Color(0xFF0000));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        l = new Label("This is the Simple ZAC Client");
        l.setFont(new Font("Courier", Font.ITALIC, 18));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        l = new Label("Installed and running!");
        l.setFont(new Font("Courier", Font.ITALIC, 18));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        
        l = new Label("Java version is " + System.getProperty("java.version"));
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        l = new Label("Java vendor is " + System.getProperty("java.vendor"));
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        /* classpath */
        l = new Label("Java CLASSPATH is:");
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        TextArea ta = new TextArea("", 10, 40);
        ta.setEditable(false);
        ta.setBackground(Color.white);
        StringTokenizer st = new StringTokenizer(System.getProperty("java.class.path"),
                                                 File.pathSeparator,
                                                 false);
        while (st.hasMoreTokens()) {
            ta.append(st.nextToken() + "\n");
        }
        gb.setConstraints(ta, gbc);
        pan.add(ta);
        
        /* argv */
        l = new Label("ARGV[]:");
        l.setFont(new Font("Courier", Font.PLAIN, 18));
        pan.add(l, gbc);
        ta = new TextArea("", 10 , 40);
        ta.setEditable(false);
        ta.setBackground(Color.white);
        int iter = 0;
        for (iter = 0; iter < a.length; iter++) {
            ta.append("argv["+iter+"]='" + a[iter] + "'\n");
        }
        pan.add(ta, gbc);
        
        /* sys props */
        l = new Label("Java System props are:");
        l.setFont(new Font("Courier", Font.PLAIN, 18));
        pan.add(l, gbc);
        ta = new TextArea("", 10 , 40);
        ta.setEditable(false);
        ta.setBackground(Color.white);
        Properties sysprops = System.getProperties();
        int size = sysprops.size();
        String[] propsstr = new String[size];
        Enumeration enum = sysprops.keys();
        iter = 0;
        while (enum.hasMoreElements()) {
            propsstr[iter++] = (String)enum.nextElement();
        }
        sort(propsstr);
        for (iter = 0; iter < size; iter++) {
            ta.append('\'' + propsstr[iter] + "'='" +
                      sysprops.getProperty(propsstr[iter]) +
                      "'\n");
        }
        pan.add(ta, gbc);
        
        l = new Label("Java home  is " + System.getProperty("java.home"));
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        l = new Label("Pwd is " + (new File(".").getAbsolutePath()));
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        String str = "<NULL>";
        if (zs.getClass().getClassLoader() != null) {
            str = zs.getClass().getClassLoader().getClass().getName();
        }
        l = new Label("ClassLoader type " + str);
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        str = "<NULL>";
        if (System.getSecurityManager() != null) {
            str = System.getSecurityManager().getClass().getName();
        }
        l = new Label("SecurityMgr type " + str);
        l.setFont(new Font("Courier", Font.PLAIN, 12));
        gb.setConstraints(l, gbc);
        pan.add(l);
        
        restart = new Button("ZAC RESTART");
        restart.setFont(new Font("Courier", Font.BOLD, 12));
        restart.setForeground(new Color(0xff0000));
        restart.addActionListener(zs);
        gb.setConstraints(restart, gbc);
        pan.add(restart);
        
        ScrollPane scrollp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
        scrollp.add(pan);
        
        f.setLayout(new GridBagLayout());
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.gridwidth = gbc2.REMAINDER;
        gbc2.gridheight = gbc2.REMAINDER;
        gbc2.fill = gbc2.BOTH;
        gbc2.weightx = gbc2.weighty = 1.0;
        f.add(scrollp, gbc2);
        f.addWindowListener(zs);
        f.pack();
        Dimension dim = f.getSize();
        int minw = Math.max(400, dim.width);
        int minh = Math.max(400, dim.height);
        f.setSize(minw, minh);
        frontAndCenter(f);
        dim = f.getSize();
        f.show();
  }
  
  /**
   * Simple bubble sort algorithm.
   */
  public static void sort(String[] a) {
    int len = a.length;
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len - i - 1; j++) {
        if (a[j].compareTo(a[j+1]) > 0) {
          // swap
          String tmp = a[j];
          a[j] = a[j+1];
          a[j+1] = tmp;
        }
      }
    }
  }
  

  /**
   * Moves the window to front and center of the screen.
   */
  public static void frontAndCenter(Window w) {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension sz = w.getSize();
    int xt = (screen.width - sz.width)/2;
    int yt = (screen.height - sz.height)/2;
    w.setLocation(new Point(xt, yt));
  }
}


