import rock.*;
import rock.RockManagerPackage.*;
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 RockManagerApplet extends JApplet {
  public boolean isStandalone = false; // Standalone application or an Applet
  public String args[];
  public RockManager rManager;
  public int ptSelectedRow, ptSelectedCol;
  
  private JFrame frame;
  private Container cp;
  private JTable rTable, dTable;
  private RockTableModel  rTableModel, dTableModel;
  private UpdateRockTable updatePT;
  private javax.swing.Timer timer = null; 
   
  private final static int TEN_SECONDS = 10000;
//private static final String nameServer = "localhost";      // local nameserver (development)
  private static final String nameServer = "216.231.56.114"; // OFM Research Inc.

  public RockManagerApplet() { }
  
  public RockManagerApplet(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); 
      rManager = c1.getdRockManager(orb);
    } catch (Exception e) {
      System.out.println("RockManagerApplet::init: ERROR in ORB initialization. Exiting. ");
      if (isStandalone) System.exit(0);  else rManager = null;
    }
    // ... End ORB Call

    //***************************
    // Create the Applet Instance
    //***************************
    if (isStandalone) {
      frame = new JFrame("CORBA Rock 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 RockManagerApplet();
    
    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("Rock 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);

    if (rManager != null) {
      try {
        rManager.setGracePeriod( (short) (24*3600) );
      } catch (Exception e) {
        System.out.println("RockManagerApplet::init: dRockServer is non running. Exiting. ");
	if (isStandalone) System.exit(0);
      }
    }
    
    rTableModel = new RockTableModel();
    dTableModel = new RockTableModel();
    rTable      = new JTable(rTableModel);
    dTable      = new JTable(dTableModel);
    rTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    updatePT    = new UpdateRockTable(rManager, rTableModel, dTableModel, rTable, dTable);
    
    ListSelectionModel rowSM = rTable.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();
	}
      }
    });
    rTable.setCellSelectionEnabled(true);
    ListSelectionModel colSM = rTable.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();
	}
      }
    });
    rTable.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
	if (e.getClickCount() > 1) {
	  updatePT.popupDetails(ptSelectedRow, ptSelectedCol);
	}
      }
    });
    
    rTable.setPreferredScrollableViewportSize(new Dimension(800,250));
    dTable.setPreferredScrollableViewportSize(new Dimension(800,250));
    
    rTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    dTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	       
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

    JPanel panelr = new JPanel();
           panelr.setLayout(new BorderLayout());
    JScrollPane paner = new JScrollPane(rTable, v, h);
                paner.setBorder(etchedBdr);
	   panelr.add(paner);
	   panelr.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

    JPanel paneld = new JPanel();
           paneld.setLayout(new BorderLayout());
    JScrollPane paned = new JScrollPane(dTable, v, h);
                paned.setBorder(etchedBdr);
	   paneld.add(paned);
	   paneld.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

    addComponent(cp, panelr, 0,  1, 4, 10, GridBagConstraints.BOTH);
    addComponent(cp, paneld, 0, 11, 4, 10, 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 Rock Manager Applet. v.1.0, March 2006";
  }
  
  public void start() {
    timer = new javax.swing.Timer(TEN_SECONDS, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
	try { 
          updatePT.update();
        } catch (Exception e) { timer.stop(); } 
      }
    });
    timer.start();
  }
  
  public void stop() { }
  
  public void destroy() { }
  
  public static void main(String[] args) {
    RockManagerApplet applet = new RockManagerApplet(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);
  }

}

