import phases.*;
import phases.PhaseManagerPackage.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

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

import javax.naming.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import javax.swing.table.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;

public class PhaseManagerApplet extends JApplet {
  public boolean isStandalone = false; // Standalone application or an Applet
  public String args[];
  public PhaseManager pManager;
  public int ptSelectedRow, ptSelectedCol;
  
  private JFrame frame;
  private Container cp;
  private PhaseTableModel  pTableModel;
  private UpdatePhaseTable updatePT;
  
//private static final String nameServer = "localhost";      // local nameserver (development)
  private static final String nameServer = "216.231.56.114"; // OFM Research Inc.

  public PhaseManagerApplet() { }
  
  public PhaseManagerApplet(String args[]) {
    this.isStandalone = true;
    this.args = args;
  }
  
  public void init() {

    String laf = UIManager.getCrossPlatformLookAndFeelClassName();
    try {
      UIManager.setLookAndFeel(laf);
    } catch(Exception ce) {
      System.err.println("Error loading" + laf + ":" +ce);
    }
    
    // ORB Call
    try {
      Connection c1 = new Connection();
      ORB orb;
      
      if (isStandalone) {
        java.util.Properties props = new java.util.Properties();
        props.put("org.omg.CORBA.ORBInitialPort", "2809");
        props.put("org.omg.CORBA.ORBInitialHost", nameServer);
        orb = c1.getORB(args, props);
      
      } else orb = c1.getORB(this); 
      pManager = c1.getdPhaseManager(orb);
    } catch (Exception e) {
      System.out.println("ERROR in ORB initialization: " + e);
    }
    // ... End ORB Call

    //***************************
    // Create the Applet Instance
    //***************************
    if (isStandalone) {
      frame = new JFrame("CORBA Phase Manager Applet");
      frame.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
    	  System.exit(0);
        }
      });
      cp = frame.getContentPane();
    } else cp = getContentPane();
    
    JApplet applet = new PhaseManagerApplet();
    
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    cp.setLayout(gbl);
    if (isStandalone) frame.setVisible(true);
    
    Border etchedBdr = BorderFactory.createEtchedBorder();

    //*************************
    // Create the GUI interface
    //*************************
    JLabel label1 = new JLabel("Phase Manager Applet, v 1.0");
           label1.setFont(new Font("Helvetica", Font.BOLD, 24));
           label1.setForeground(Color.red);
    addComponent(cp, label1, 0, 0, 4, 1, GridBagConstraints.BOTH);

    pManager.setGracePeriod( (short) (24*3600) );
    
    pTableModel = new PhaseTableModel();
    updatePT    = new UpdatePhaseTable(pManager, pTableModel);
    
    JTable pTable = new JTable(pTableModel);
           pTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ListSelectionModel rowSM = pTable.getSelectionModel();
    rowSM.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
	if (lsm.isSelectionEmpty()) {
	  System.out.println("No rows selected.");
	} else {
	  ptSelectedRow = lsm.getMinSelectionIndex();
	}
      }
    });
    pTable.setCellSelectionEnabled(true);
    ListSelectionModel colSM = pTable.getColumnModel().getSelectionModel();
    colSM.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
	if (lsm.isSelectionEmpty()) {
	  System.out.println("No columns selected.");
	} else {
	  ptSelectedCol = lsm.getMinSelectionIndex();
	}
      }
    });
    pTable.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
	if (e.getClickCount() > 1 && ptSelectedCol == 0) {
	  String name = (String) pTableModel.getValueAt(ptSelectedRow, ptSelectedCol);
	}
      }
    });
    
    pTable.setPreferredScrollableViewportSize(new Dimension(800,500));
    JPanel panel = new JPanel();
           panel.setLayout(new BorderLayout());
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    JScrollPane pane = new JScrollPane(pTable, v, h);
                pane.setBorder(etchedBdr);
	   panel.add(pane);
	   panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
	   
    System.out.println(System.getProperty("java.version"));
    System.out.println(System.getProperty("java.vendor"));
    System.out.println(System.getProperty("java.vm.specification.version"));
    System.out.println(System.getProperty("java.vm.specification.vendor"));
    System.out.println(System.getProperty("java.vm.specification.name"));
    System.out.println(System.getProperty("java.specification.version"));
    System.out.println(System.getProperty("java.specification.vendor"));
    System.out.println(System.getProperty("java.specification.name"));
    System.out.println(System.getProperty("os.name"));
    System.out.println(System.getProperty("os.arch"));
    System.out.println(System.getProperty("os.version"));
    System.out.println(System.getProperty("user.name"));
    System.out.println(Locale.getDefault().getDisplayName());
    System.out.println(TimeZone.getDefault().getDisplayName());
    try {
      System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
      System.out.println(InetAddress.getLocalHost().getHostAddress());
      System.out.println(InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) { ; }
    
    addComponent(cp, panel, 0, 1, 4, 20, GridBagConstraints.BOTH);
 
    cp.add(applet);
    cp.validate();
    if (isStandalone) {
      frame.setSize(850, 650);
      frame.setLocation(200, 200);
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
  }

  //*********************
  // Class public methods
  //*********************
  
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) 
                        : (getParameter(key) != null ? getParameter(key) : def);
  }
  
  public String[][] getParameterInfo() {
    return null;
  }
  
  public String getAppletInfo() {
    return "CORBA Phase Manager Applet. v.1.0, March 2006";
  }
  
  public void start() {
    Thread worker = new Thread() {
      public void run() {
        for (;;) {
          try {
	    SwingUtilities.invokeLater(new Runnable() {
              public void run() { updatePT.update(); }
	    });
	    Thread.sleep(10000);
          } catch (Exception e) {} 
	}
      }
    };
    worker.start();
  }
  
  public void stop() { }
  
  public void destroy() { }
  
  public static void main(String[] args) {
    PhaseManagerApplet applet = new PhaseManagerApplet(args);
    applet.init();
    applet.start();
  }
 
  //**********************
  // Class private methods
  //**********************
  
  private static void addComponent(Container container, Component component, 
    int gx, int gy, int gw, int gh, int fill) {
    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx      = gx;
    gbc.gridy      = gy;
    gbc.gridwidth  = gw;
    gbc.gridheight = gh;
    gbc.fill       = fill;
    container.add(component, gbc);
  }

}

