/**
  *
  * SYSTEMSAMPLE Device
  * PROGRAMMING SAMPLE
  *
  * @author Peter Beck
  */

package de.p_b_e.provisu.devices.systemsample;


import de.p_b_e.provisu.devices.systemsample.panels.*;

import java.awt.*;
import java.util.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.concurrent.TimeUnit;


public class SYSTEMSAMPLE
implements de.p_b_e.provisu.interfaces.ProVisuDevice
{

  /* ********  Fields  *********** */
  private final static String name = "SYSTEMSAMPLE";
  private de.p_b_e.utilities.config.Settings settings;
  private LinkedList<de.p_b_e.provisu.interfaces.StateListener> stateListeners;


  /* ********************************************************************* */
  /* **************      constructor    ********************************** */
  /* ********************************************************************* */
  /**
   *  constructor of this device
   */
  public SYSTEMSAMPLE(de.p_b_e.utilities.config.Settings sets) {
    settings = sets;
    stateListeners = new LinkedList<de.p_b_e.provisu.interfaces.StateListener>();
  }


  /* ********************************************************************* */
  /* **************      getName    ************************************** */
  /* ********************************************************************* */
  /**
   *  returns the name of this device
   */
  public String getName(){
      return name;
  }
  
  
  /* ********************************************************************* */
  /* **************      addStateListener    ***************************** */
  /* ********************************************************************* */
  /**
   *  added StateListener Component from list
   */
  public void addStateListener(de.p_b_e.provisu.interfaces.StateListener sl){
    stateListeners.add(sl);
  }


  /* ********************************************************************* */
  /* **************      removeStateListener    ************************** */
  /* ********************************************************************* */
  /**
   *  remove StateListeners Component from list
   */
  public void removeStateListener(de.p_b_e.provisu.interfaces.StateListener sl){
    stateListeners.remove(sl);
  }



  /* ********************************************************************* */
  /* **************      informStateListener    ************************** */
  /* ********************************************************************* */
  private void informStateListener(de.p_b_e.provisu.interfaces.StateEvent stateEvent){
  /**
   *  inform all added StateListeners for StateEvents
   */
    Iterator iter = stateListeners.iterator();
    try{
      while (iter.hasNext()){
        de.p_b_e.provisu.interfaces.StateListener stateListener = (de.p_b_e.provisu.interfaces.StateListener)iter.next();
        stateListener.statePerformed(stateEvent);
      }
    }
    catch (Exception e){}
  }

  /* ********************************************************************* */
  /* **************      finishedInit    ********************************* */
  /* ********************************************************************* */
  /**
   *  called after main frame ist visible an hole application is init
   *  for auto connecting or e.g.
   */
  public void finishedInit(){

  }



  /* ********************************************************************* */
  /* **************      commandPerformed    ***************************** */
  /* ********************************************************************* */
  /**
   *  called from any componente to perform a command
   */
  public void commandPerformed(String command){
    if (!command.equals("")) {
      String[] values = command.split(" ");
      if (values[0].equals("MINIMIZE")) settings.getFrame().setExtendedState(Frame.ICONIFIED);

      else if (values[0].equals("DELAY")){
        int delay = 0;
        if  (values[1].equals("")) return;
        else delay=Integer.parseInt(values[1]);
        
        try{
          TimeUnit.MILLISECONDS.sleep(delay );
        }
        catch(Exception e){}
      }
      
    }
  }



  /* ********************************************************************* */
  /* **************      getCommandPanel    ****************************** */
  /* ********************************************************************* */
  /**
   *  called from any componente to get CommandPanel
   *  returns new instance of CommandPanel
   */
  public de.p_b_e.provisu.interfaces.DeviceCommandPanel getCommandPanel(){
    return new CommandPanel(settings);
  }
  


  /* ********************************************************************* */
  /* **************      getStatePanel    ****************************** */
  /* ********************************************************************* */
  /**
   *  called from any componente to get StatePanel
   *  returns new instance of StatePanel
   */
  public de.p_b_e.provisu.interfaces.DeviceStatePanel getStatePanel(){
    return new StatePanel(settings);
  }



   /* ********************************************************************* */
  /* **************      getSetupTree    ******************************** */
  /* ********************************************************************* */
  /**
   *  called from main setup Panel to get setup panels
   *  returns instance DefaultMutableTreeNode
   */
  public DefaultMutableTreeNode getSetupTree(){
    return null;
    
    /*
    example:
    DefaultMutableTreeNode firstLevel = new DefaultMutableTreeNode( new de.p_b_e.utilities.config.SetupTreeItem( new JPanel(), getName() ));
    DefaultMutableTreeNode secondLevel = new DefaultMutableTreeNode( new de.p_b_e.utilities.config.SetupTreeItem( new ConnectionPanel(this, settings),messages.getString("Setup.Connection.Title")));
    firstLevel.add(secondLevel);
    secondLevel = new DefaultMutableTreeNode( new de.p_b_e.utilities.config.SetupTreeItem( new CheckConnectionPanel(this, settings),messages.getString("Setup.CheckConnection.Title")));
    firstLevel.add(secondLevel);
    secondLevel = new DefaultMutableTreeNode( new de.p_b_e.utilities.config.SetupTreeItem( new AutoConnectPanel(this, settings), messages.getString("Setup.AutoConnect.Title")));
    firstLevel.add(secondLevel);
    secondLevel = new DefaultMutableTreeNode( new de.p_b_e.utilities.config.SetupTreeItem( new ComConsolePanel(this, settings), messages.getString("Setup.ComConsole.Title")));
    firstLevel.add(secondLevel);

    return firstLevel;
    */


  }

}