/**
  *
  * SYSTEMSAMPLE Device
  *
  * CommandPanel
  *
  * @author Peter Beck
  */

package de.p_b_e.provisu.devices.systemsample.panels;


import de.p_b_e.provisu.interfaces.*;
import de.p_b_e.utilities.config.Settings;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class CommandPanel extends JPanel
  implements ActionListener, DeviceCommandPanel {


  private JComboBox command;
  private JSpinner delay;
  private JLabel commandLabel;
  private JLabel delayLabel;

  private Settings settings;

  /* ********************************************************************* */
  /* **************      constructor    ********************************** */
  /* ********************************************************************* */
  /**
   *  constructor for new CommandPanel
   */
  public CommandPanel(Settings sets) {

    settings = sets;
    ResourceBundle messages = settings.getMessages();

    setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

    command = new JComboBox();
    command.addItem("DELAY");
    command.addItem("MINIMIZE");

    delay = new  JSpinner(new SpinnerNumberModel(1000,0,1000000,1));

    commandLabel=new JLabel(messages.getString("System.Command.Label.Command"));
    delayLabel=new JLabel(messages.getString("System.Command.Label.Delay"));

    command.setMaximumSize(new Dimension(command.getMaximumSize().width,22));
    delay.setMaximumSize(new Dimension(delay.getMaximumSize().width,22));

    commandLabel.setAlignmentX(0.0f);
    command.setAlignmentX(0.0f);
    delayLabel.setAlignmentX(0.0f);
    delay.setAlignmentX(0.0f);

    add(commandLabel);
    add(command);
    add(delayLabel);
    add(delay);
    add(Box.createVerticalGlue());

    command.setMaximumSize(new Dimension((int)command.getMaximumSize().getWidth(),22));
    delay.setMaximumSize(new Dimension((int)delay.getMaximumSize().getWidth(),22));

    command.addActionListener(this);

  } // end constructor


  /* ********************************************************************* */
  /* **************      getCommand    *********************************** */
  /* ********************************************************************* */
  /**
   *  return command as String like chooses
   */
  public String getCommand(){

    String selection = (String)command.getSelectedItem();
    String commandString = (String)command.getSelectedItem();
    String delayString = String.valueOf(delay.getValue());

    if (selection.equals("MINIMIZE") ){
      return commandString;
    }
    else {
      return commandString+" "+delayString;
    }

  } // end getCommand()
  

  /* ********************************************************************* */
  /* **************      setCommand    *********************************** */
  /* ********************************************************************* */
  /**
   *  set CommandPanel like command String
   */
  public void setCommand(String value){
    String[] commandString = value.split(" ");
    command.setSelectedItem(commandString[0]);
    if (commandString.length > 1 ) {
      try{
        delay.setValue(Integer.parseInt(commandString[1]));
      }
      catch(Exception e){}
    }
    
  } // end setCommand()
  

  /* ********************************************************************* */
  /* **************      getParamList    *********************************** */
  /* ********************************************************************* */
  /**
   *  return String array of commands parameter labels
   */
  public String[] getParamList(){

    ResourceBundle messages = settings.getMessages();

    String selection = (String)command.getSelectedItem();
    if (selection.equals("MINIMIZE") ){
      String[] paramList = new String[1];
      paramList[0]=messages.getString("System.Command.Label.Command");
      return paramList;
    }
    else {
      String[] paramList = new String[2];
      paramList[0]=messages.getString("System.Command.Label.Command");
      paramList[1]=messages.getString("System.Command.Label.Delay");
      return paramList;
    }
    
  } // end getParamList()


  
  /* ********************************************************************* */
  /* **************      actionPerformed    ****************************** */
  /* ********************************************************************* */
  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source == command) {
      String selection = (String)command.getSelectedItem();

      if (selection.equals("MINIMIZE") ){

         delayLabel.setVisible(false);
         delay.setVisible(false);

      }
      else {
         delayLabel.setVisible(true);
         delay.setVisible(true);
      }
    }
  } // end actionPerformed()


}  // end class CommandPanel


