import rock.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.URL;

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

/**
 * Creates a popup dialog box to display thermodynamic properties of a selected phase.
 * @version 1.0 (August 2007)
 * @author Mark S. Ghiorso, OFM-Research Inc.
 */
public class PhasePropertiesDialog {

  private rock.dRock rockRef;
  private JFrame frame;
  private String phase;

  /**
   * Creates the GUI and initializes the class.
   * @param rockRef Instance of server-side rock object.
   * @param phase Name of phase whose thermodynamic properties are to be displayed.
   */
  public PhasePropertiesDialog (rock.dRock rockRef, String phase) {
    this.rockRef = rockRef;
    this.phase   = phase;
    
    if (rockRef != null && !phase.equals("")) {    
      Border etchedBdr = BorderFactory.createEtchedBorder();
      
      frame = new JFrame("Physical and Chemical Properties");
      frame.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent e) {
            Frame f = (Frame) e.getSource();
            f.setVisible(false);
            f.dispose();
          }
        });
      Container cp = frame.getContentPane();
    	  	cp.setLayout(new BorderLayout());

      JPanel panel1 = new JPanel();
             panel1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      JLabel topLabel = new JLabel("Specific properties of:");
      panel1.add(topLabel, BorderLayout.NORTH);
      cp.add(panel1, BorderLayout.NORTH);

      JTabbedPane tabbedPane = new JTabbedPane();
      
      try {
        double Gphase,  Hphase,  Sphase,  Vphase,  CPphase,  RHOphase,  VISphase;
        double Gsystem, Hsystem, Ssystem, Vsystem, CPsystem, RHOsystem, VISsystem;
        
        if (phase.equals("liquid")) {
          double mass = rockRef.getLiquidMass();
      	  if (mass == 0.0) mass = 1.0;
          Gphase   = rockRef.getLiquidGibbsFreeEnergy()/(1000.0*mass); // kJ/g  
      	  Hphase   = rockRef.getLiquidEnthalpy()/(1000.0*mass);        // kJ/g
      	  Sphase   = rockRef.getLiquidEntropy()/mass;		       // J/K-g
      	  Vphase   = rockRef.getLiquidVolume()*1.0e6/mass;	       // cc/g
      	  CPphase  = rockRef.getLiquidHeatCapacity()/mass;	       // J/K-g
      	  RHOphase = (Vphase == 0.0) ? 0.0 : 1.0/Vphase;	       // g/cc
      	  VISphase = 0.0; 
        } else {
          String[] phases = phase.split(":");
          short pIndex = rockRef.getPhaseNo(phases[0]);
      	  short pInstance = 0;
      	  if (phases[0].length() != phase.length()) {
      	    Integer x = new Integer(phases[1]);
      	    pInstance = (short) x.intValue();
      	  }
          double mass = rockRef.getSolPhaseMass(pIndex, pInstance);
      	  if (mass == 0.0) mass = 1.0;
          Gphase   = rockRef.getSolidGibbsFreeEnergy(pIndex, pInstance)/(1000.0*mass); // kJ/g  
      	  Hphase   = rockRef.getSolidEnthalpy(pIndex, pInstance)/(1000.0*mass);        // kJ/g
      	  Sphase   = rockRef.getSolidEntropy(pIndex, pInstance)/mass;		       // J/K-g
      	  Vphase   = rockRef.getSolidVolume(pIndex, pInstance)*1.0e6/mass;	       // cc/g
      	  CPphase  = rockRef.getSolidHeatCapacity(pIndex, pInstance)/mass;	       // J/K-g
      	  RHOphase = (Vphase == 0.0) ? 0.0 : 1.0/Vphase;			       // g/cc
      	  VISphase = 0.0; 
        }

        double mass = rockRef.getLiquidMass() + rockRef.getSolidMass();
        if (mass == 0.0) mass = 1.0;
        Gsystem   = (rockRef.getLiquidGibbsFreeEnergy() + rockRef.getSolidGibbsFreeEnergyTotal())/(1000.0*mass);
        Hsystem   = (rockRef.getLiquidEnthalpy() + rockRef.getSolidEnthalpyTotal())/(1000.0*mass);
        Ssystem   = (rockRef.getLiquidEntropy() + rockRef.getSolidEntropyTotal())/mass;
        Vsystem   = (rockRef.getLiquidVolume() + rockRef.getSolidVolumeTotal())*1.0e6/mass;
        CPsystem  = (rockRef.getLiquidHeatCapacity() + rockRef.getSolidHeatCapacityTotal())/mass;
        RHOsystem = (Vsystem == 0.0) ? 0.0 : 1.0/Vsystem;		
        VISsystem = 0.0;
        
        java.lang.Object columnNames[] = { "", "" };
        java.lang.Object phaseData[][] = { 
          { "G (kJ/g)", 		     new Double(Gphase  ) },
          { "H (kJ/g)", 		     new Double(Hphase  ) },
          { "S (J/g)",  		     new Double(Sphase  ) },
          { "V (cc/g)", 		     new Double(Vphase  ) },
          { "Cp (J/g)", 		     new Double(CPphase ) },
          { "\u03C1" + "(g/cc)",	     new Double(RHOphase) },
          { "log(" + "\u03B7" + ") (poise)", new Double(VISphase) }
        };    
        JTable phaseTable = new JTable(phaseData, columnNames);
        JPanel panel2 = new JPanel();
               panel2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      	       panel2.add(phaseTable);
        tabbedPane.addTab(phase, panel2); 
      	       
        java.lang.Object systemData[][] = { 
          { "G (kJ/g)", 		     new Double(Gsystem  ) },
          { "H (kJ/g)", 		     new Double(Hsystem  ) },
          { "S (J/g)",  		     new Double(Ssystem  ) },
          { "V (cc/g)", 		     new Double(Vsystem  ) },
          { "Cp (J/g)", 		     new Double(CPsystem ) },
          { "\u03C1" + "(g/cc)",	     new Double(RHOsystem) },
          { "log(" + "\u03B7" + ") (poise)", new Double(VISsystem) }
        };    
        JTable systemTable = new JTable(systemData, columnNames);
        JPanel panel3 = new JPanel();
               panel3.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      	       panel3.add(systemTable);
        tabbedPane.addTab("system", panel3); 
      } catch (org.omg.CORBA.COMM_FAILURE     noServer) {
        JOptionPane.showMessageDialog(null, 
          "Lost contact with server. Server is most likely down.\nPlease wait a few minutes and reload applet or restart application.",
          "CORBA Server Error", JOptionPane.ERROR_MESSAGE);
      } catch (org.omg.CORBA.OBJECT_NOT_EXIST noObject) {
        JOptionPane.showMessageDialog(null, 
          "Lost contact with server. Server has deleted your process.\nPlease reload applet or restart application.",
          "CORBA Server Error", JOptionPane.ERROR_MESSAGE);
      }

      cp.add(tabbedPane, BorderLayout.CENTER);
      
      tabbedPane.setSelectedIndex(0);

      JButton tb1 = new JButton("Dismiss");
    	      tb1.addActionListener(new DismissHandler());
      cp.add(tb1, BorderLayout.SOUTH);
       
      frame.pack();
      frame.setLocation(50, 50);
      frame.setVisible(true);
    }
  }
  
  private class DismissHandler implements ActionListener {
    public void actionPerformed(ActionEvent e){
      System.out.println("Entering the Dismiss ActionListener.");      
      frame.setVisible(false);
      frame.dispose();
    }
  }
  
}

