package com.sun.sysid.panels;

import java.awt.*;
import java.util.*;
import com.sun.wizards.awt.*;
import com.sun.wizards.panels.*;
import com.sun.wizards.core.*;
import com.sun.sysid.core.*;

public class EStarPanel extends WizardLeaf
{
  Panel mainPanel = new Panel(new VerticalLayout());
  CheckboxGroup cgroup1 = new CheckboxGroup();
  CheckboxGroup cgroup2 = new CheckboxGroup();
  
  static String eStarString;
  static String eStarString2;
  
  static String EStarOnlabel;
  static String EStarOfflabel;
  
  static String ask;
  static String dontAsk;
  
  Boolean eStarOn = new Boolean(true);
  Boolean askAtReboot = new Boolean(false);
  
  public EStarPanel()
    {
    }
  
  public EStarPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public EStarPanel(WizardState wizardState)
    {
      super(wizardState, "Power Management");
    }
  
  public EStarPanel(String name, Route route, 
		    WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }
  
  
  public void createUI()
    {
      WizardTreeManager wizardManager = getTreeManager();
      super.createUI();

      /*
       * Set the header text.
       */
      HeaderPanel headerPanel = getHeaderPanel();

      String heading = MsgCat.getString("com.sun.sysid.Sysid", 
					  "Power Management");

      if(headerPanel != null) {
        headerPanel.setText(heading);
      }

      eStarOn = 
	(Boolean) wizardManager.callServerObjectMethod(getRoute(),
                                             "getData",
                                             new String[] { "java.lang.String.class" },
                                             new Object[] { "eStarOn" });
      askAtReboot = 
	(Boolean) wizardManager.callServerObjectMethod(getRoute(),
                                             "getData",
                                             new String[] { "java.lang.String.class" },
                                             new Object[] { "askAtReboot" });

      mainPanel.add(new FlowLabel(eStarString = MsgCat.getString("com.sun.sysid.Sysid", 
						   "eStarString")), "1");
     
      
      // Set up the Estar cb
      EStarOnlabel = MsgCat.getString("com.sun.sysid.Sysid", "EStarOn");
      EStarOfflabel = MsgCat.getString("com.sun.sysid.Sysid", "EStarOff");
    
      Panel p = new Panel(new FlowLayout(FlowLayout.LEFT));
      Checkbox cb = new Checkbox(EStarOnlabel, cgroup1, false);
      if (eStarOn.booleanValue())
	cb.setState(true);

      p.add(cb);
      mainPanel.add(p,"2");

      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      cb = new Checkbox(EStarOfflabel, cgroup1, false);
      if (!eStarOn.booleanValue())
	cb.setState(true);
      p.add(cb);
      mainPanel.add(p,"3");
      
      // Set up the Ask at reboot
      
      mainPanel.add(new FlowLabel(eStarString2 = MsgCat.getString("com.sun.sysid.Sysid", 
						   "eStarString2")),"4");
      
      ask = MsgCat.getString("com.sun.sysid.Sysid", "ask");
      dontAsk = MsgCat.getString("com.sun.sysid.Sysid", "dontAsk");

      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      cb = new Checkbox(ask, cgroup2, false);
      if (askAtReboot.booleanValue())
	cb.setState(true);

      p.add(cb);
      mainPanel.add(p,"5");

      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      cb = new Checkbox(dontAsk, cgroup2, false);
      if (!askAtReboot.booleanValue())
	cb.setState(true);
      p.add(cb);
      mainPanel.add(p,"6");
      add(mainPanel,"Center");
      
    }
  
  public void consoleInteraction()
     {
         TTYDisplay.showNewline();
 
       String prompt1 = MsgCat.getString("com.sun.sysid.Sysid", "cli_estarPrompt1");
       String prompt2 = MsgCat.getString("com.sun.sysid.Sysid", "cli_estarPrompt2");
       
       String yval = MsgCat.getString("com.sun.sysid.Sysid", "y");
       String nval = MsgCat.getString("com.sun.sysid.Sysid", "n");
 
       String estar = TTYDisplay.queryValue(prompt1, 
                                            eStarOn.booleanValue()?yval:nval, 
                                            new String[]{ yval,nval}, "");
       eStarOn = new Boolean(estar.equalsIgnoreCase(yval));
       
       String reboot = TTYDisplay.queryValue(prompt2, 
                                             askAtReboot.booleanValue()?yval:nval, 
                                             new String[]{ yval,nval}, eStarString2);
       askAtReboot = new Boolean(reboot.equalsIgnoreCase(yval));
       
     }

  public int getButtonMask() {
    return (BACK_BUTTON | NEXT_BUTTON);
  }

  public boolean isDisplayComplete()
    {
      WizardTreeManager wizardManager = getTreeManager();
      if (wizardManager.hasGUI()) 
      {
	 eStarOn = new Boolean(
	    cgroup1.getSelectedCheckbox().getLabel().equals(EStarOnlabel));
      }
    
      wizardManager.callServerObjectMethod(getRoute(), "setData",
					   new String[] { "java.lang.String.class", 
							    "java.lang.Object.class" },
					   new Object[] { "eStarOn", eStarOn });
      if (wizardManager.hasGUI())
      {
	 askAtReboot = new Boolean(
	    cgroup2.getSelectedCheckbox().getLabel().equals(ask));
      }
      wizardManager.callServerObjectMethod(getRoute(), "setData",
					   new String[] { "java.lang.String.class", 
							    "java.lang.Object.class" },
					   new Object[] { "askAtReboot", askAtReboot });
      
      return(true);
      
    }
  public void addRuntimeResources(Vector resourceVector)
    {
      super.addRuntimeResources(resourceVector);
      resourceVector.addElement(new String[] {null, "com.sun.sysid.panels.EStarPanel"});
      resourceVector.addElement(new String[] {null, "com.sun.sysid.SysidResources"});
      
      resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.InsetsPanel"});
      
      resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.FullWidthPanel"});
      resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.FlowLabel"});
      resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.VerticalLayout"});
      resourceVector.addElement(new String[] {null,
                        "com.sun.wizards.awt.Separator"});
    }
}
