package com.sun.solarisinstall.panels;

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

import com.sun.wizards.core.*;
import com.sun.solarisinstall.*;

public class DiskConfPanel extends Panel implements DiskTableListener, ActionListener
{
  private Table t;
  private DiskTableManager tableManager;
  private Image image;
  private Button modButton;
  private Hashtable frames;

  public DiskConfPanel( DiskTableManager dtm, Image img )
    {
      tableManager = dtm;
      image = img;
      dtm.addTableListener( this );
      dtm.setDTMParent(this);
      
      frames = new Hashtable();

      createUI();
      update();
    }

  public void createUI()
    {
       setLayout(new BorderLayout(0, 0));

       Panel tPanel = new Panel();
       tPanel.setLayout(new BorderLayout());
       add("Center", tPanel);

       t = new Table(8);
       t.headerArea().setBackground(Color.blue);
       t.setNumColumns(5);
       
       t.showHorizontalScrollbar(false);
       // Disk/File System col heading
       t.setColumnLabel(1,
		  MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
				   SolarisInstallResources.MSG_DISKFILESYS));
       
       // Size col heading
       t.setColumnLabel(2,
		MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
				 SolarisInstallResources.MSG_SIZE_LOWERCASE) +
				"(" + 
		MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
				 SolarisInstallResources.MSG_MB_NOSPACES) +
				 ")");
       t.setColumnLabel(3, 
		MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
				 SolarisInstallResources.MSG_SIZE_LOWERCASE) +
			"(" + 
		MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
				 SolarisInstallResources.MSG_NOSPACE_CYLS) +
				 ")");
       
       t.setColumnWidthInChars(0, 3, ALIGNMENT.LEFT);
       t.setColumnWidthInChars(1, 20, ALIGNMENT.LEFT);
       t.setColumnWidthInChars(2, 10, ALIGNMENT.RIGHT);
       t.setColumnWidthInChars(3, 10, ALIGNMENT.RIGHT);
       t.setColumnWidthInChars(4, 2, ALIGNMENT.LEFT);
       
       /*  Add some dummy data so that the vertical 
	* scrollbar gets created. Otherwise, the peer
	* scrollbar is not created and never appears.
	*/
       for (int i=0; i < 16; i++) {
	 Vector v = new Vector();
	 v.addElement(image);
	 v.addElement("foo"+i);
	 v.addElement("27 MB");
	 v.addElement("27 MB");
	 v.addElement(" ");
	 t.addRow(v);
       }
       
       t.showRowDividers(true);
       t.setColumnsResizable(false);
       t.showHorizontalScrollbar(false);
       t.showVerticalScrollbar(true);
       
       tPanel.add("Center",t);
       
       Panel newPanel = new Panel();
       // Modify... button label
       modButton =
	 new Button(MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			      SolarisInstallResources.MSG_MODIFY_LOWERCASE));
       
       modButton.addActionListener(this);
       newPanel.add(modButton);
       add("South", newPanel);
    }
  

  public synchronized void update()
    {
      
      t.clear();
      t.showVerticalScrollbar(true);
       
      String disks[] = tableManager.getDiskNames();
      Vector endDisks = new Vector();
      int row = 0;

      for(int i = 0; i < disks.length; i++ ) {
	if( Def.DEBUG ) System.out.println("Disks["+i+"] is " + disks[i]); 
	if( tableManager.useDisk( disks[i] ) ) {
	  Vector v = new Vector();
	  v.addElement(image);
	  v.addElement(disks[i]);
	  v.addElement("");
	  v.addElement("");
	  t.addRow(v);
	  row++;
	 
	  for( int k = 0; k < tableManager.getNumSlices( disks[i] ); k++ ) { 
	    String sliceName = tableManager.getSliceName(disks[i], k);
	    
	    if( !sliceName.equals("") && !sliceName.equals("overlap") ) {
	      v = new Vector();
	      v.addElement(" ");
	      v.addElement(sliceName);
	      int sizeInMb = tableManager.getSliceSize(disks[i], k);
	      v.addElement(String.valueOf(sizeInMb) +
		     MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			         SolarisInstallResources.MSG_SINGLESPACE_MB));
	      int sizeInCyls = 0;
	      if (tableManager.isDefaultSwapDisk(disks[i]) && 
		  tableManager.isDefaultSwapSlice(disks[i], k)) {
		sizeInCyls = tableManager.getDefaultSwapSizeInCyls();
	      } else {
		sizeInCyls = tableManager.convertMbToCyls(disks[i], sizeInMb);
	      }
	      
	      v.addElement(String.valueOf(sizeInCyls));
	      t.addRow(v);
	      row++;
	      if ((t.getSelectedIndex() == -1) &&
		  disks[i].equals(tableManager.getRootDisk())) {
		t.select(row);
	      }
	    }
	  }
	} else {
	  endDisks.addElement( disks[i] );
	}
      }
      
      for (int i = 0; i < endDisks.size(); i++) {
	Vector v = new Vector();
	v.addElement(image);
	v.addElement( (String)endDisks.elementAt(i) );
	v.addElement(MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			  SolarisInstallResources.MSG_NOTSELECTED_LOWERCASE));
	v.addElement("");
	t.addRow(v);
      }
      validate();
    }
  
  public void tableUpdate( DiskTableEvent dte )
    {
      update();
      
    }
  
  public void actionPerformed( ActionEvent ae )
    {
      int selDisk = t.getSelectedIndex();
      if( selDisk != -1 ) {
	
    	Vector v = null;
	do {
	  v = t.getRow( selDisk-- ); 
	} while( v.size() != 4 );
	
	String name = (String) v.elementAt(1);

	if( !tableManager.useDisk(name) ) return;

	DiskTableFrame dtf = null;
	if( frames.containsKey(name) ) {
	  dtf = (DiskTableFrame) frames.get(name);
	} else {
	  dtf = new DiskTableFrame( tableManager, name );
	  
	  class DtfWindowAdapter extends WindowAdapter {
	    private String name;
	    private Hashtable ht;
	    public DtfWindowAdapter( String n, Hashtable h ) {
	      name = n;
	      ht = h;
	    }
	    public void windowClosed( WindowEvent we ) {
	      ht.remove(name);
	    }
	  }
	  dtf.addWindowListener( new DtfWindowAdapter( name, frames) );
	  frames.put( name, dtf );
	}
	dtf.setVisible(true);
	dtf.toFront();
      }
    }
  
}
