Package components.robot

Source Code of components.robot.RobotGUI

package components.robot;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import resources.SWTResourceManager;
import resources.digesters.ClassManager;
import resources.digesters.Plugin;
import utils.ErrorMessage;
import utils.defines.Defines;

import components.Component;
import components.ComponentGUI;
import components.ComponentGUIException;
import components.commchannel.CommunicationChannelGUI;
import components.features.Feature;
import components.features.FeatureGUI;
import components.neuralnetwork.NeuralNetworkGUI;


/**
* Esta clase implementa la interfaz gr�fica que permite configurar
* un robot. <br><br>
*
* Esta clase no puede ser instanciada (necesariamente, deber� extenderse).
*/
public abstract class RobotGUI extends ComponentGUI
{

  private Composite    backComposite;
  private CTabItem    cTabItem1;
  private CTabFolder    configTabs;
  private CTabFolder    featureTabs;
  private Composite    configPannelFeature;
  private Vector      configElements;
  private Composite    featureStatus;
  private Composite    sensorComposite;
  private CTabItem     neuralNet;
  private CTabItem     commChannel;
 
 
  /**
   * Crea una nueva interfaz gr�fica (GUI) para configuraci�n de un
   * robot.
   * @param robot    robot a configurar.
   */
  public RobotGUI(Component robot)
  {
    super(robot);
    configElements = new Vector();
  }
 
  /**
   * Este metodo devuelve un composite con en estado del robot. Este metodo
   * pide a cada uno de los componentes del robot sus respectivos paneles de estado.
   * @param parent es el composite del padre
   * @param style para el composite
   * @return composite con el estado del robot
   */
  public Composite getStatusPannel(Composite parent, int style) throws ComponentGUIException
  {
    CTabFolder tabs;
    CTabItem sensors;
    CTabItem robot;
    Composite statusPannel = new Composite(parent,style);
    GridLayout robotStatusLayout = new GridLayout();
    statusPannel.setLayout(robotStatusLayout);

    try
    {

      tabs = new CTabFolder(statusPannel, SWT.FLAT | SWT.TOP | SWT.BORDER);
      {
        sensors = new CTabItem(tabs,SWT.NONE);
        sensors.setText("Sensores");
        sensors.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/System-ColorSync.png"));
        {
          sensorComposite = new Composite(tabs,SWT.NONE);
          GridData sensorCompositeLData = new GridData();
          sensorCompositeLData.horizontalAlignment = GridData.BEGINNING;
          sensorCompositeLData.verticalAlignment = GridData.BEGINNING;
          sensorCompositeLData.grabExcessHorizontalSpace = true;
          sensorCompositeLData.grabExcessVerticalSpace = false;
          GridLayout sensorCompositeLayout = new GridLayout();
          sensorCompositeLayout.numColumns = 2;
          sensorCompositeLayout.marginWidth = 0;
          sensorCompositeLayout.marginHeight = 0;
          sensorCompositeLayout.verticalSpacing = 1;
          sensorCompositeLayout.marginLeft = 3;
          sensorCompositeLayout.marginTop = 2;
          sensors.setControl(sensorComposite);
          sensorComposite.setLayout(sensorCompositeLayout);
          sensorComposite.setLayoutData(sensorCompositeLData);
          {
            for (int i=0; i<((Robot)component).getFeatures().length;i++)
            {
              Plugin plugin = ClassManager.getInstance().getPluginByName(((Robot)component).getFeatures()[i].getClass().getCanonicalName());

              if (plugin != null)
              {
                FeatureGUI featureGUI = (FeatureGUI)plugin.makeGUInstance(((Robot)component).getFeatures()[i]);
                featureStatus =  featureGUI.getStatusPannel( sensorComposite, sensorComposite.getStyle());
                 
                //Fuerzo layout
                GridData featureStatusLData = new GridData();
                featureStatusLData.horizontalAlignment = GridData.FILL;
                featureStatusLData.verticalAlignment = GridData.FILL;
                featureStatusLData.grabExcessHorizontalSpace = true;
                featureStatusLData.grabExcessVerticalSpace = true;
                featureStatus.setLayoutData(featureStatusLData);
              }
            }
          }       
          sensorComposite.layout(true);
        }
        neuralNet = new CTabItem(tabs,SWT.NONE);
        neuralNet.setText("Red Neuronal");
        neuralNet.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/neuron.png"));
        {
          Plugin neuralNetPlugin = ClassManager.getInstance().getPluginByName(((Robot)component).getNeuralNetwork().getClass().getCanonicalName());
          if (neuralNetPlugin != null)
          {
            NeuralNetworkGUI nnGUI = (NeuralNetworkGUI) neuralNetPlugin.makeGUInstance(((Robot)component).getNeuralNetwork());
            Composite neuralNetComposite = nnGUI.getStatusPannel(tabs,SWT.NONE);
            neuralNet.setControl(neuralNetComposite);
            GridData neuralNetCompositeLData = new GridData();
            neuralNetCompositeLData.horizontalAlignment = GridData.BEGINNING;
            neuralNetCompositeLData.verticalAlignment = GridData.BEGINNING;
            neuralNetCompositeLData.grabExcessHorizontalSpace = true;
            neuralNetCompositeLData.grabExcessVerticalSpace = false;
            GridLayout neuralNetCompositeLayout = new GridLayout();
            neuralNetComposite.setLayout(neuralNetCompositeLayout);
            neuralNetComposite.setLayoutData(neuralNetCompositeLData);
            neuralNetComposite.layout(true);
          }
         
        }
        commChannel = new CTabItem(tabs,SWT.NONE);
        commChannel.setText("Canal de Comunicaciones");
        commChannel.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/Connection.png"));
        {
          Plugin ccPlugin = ClassManager.getInstance().getPluginByName(((Robot)component).getCommChannel().getClass().getCanonicalName());
          if (ccPlugin != null)
          {
            CommunicationChannelGUI ccGUI = (CommunicationChannelGUI) ccPlugin.makeGUInstance(((Robot)component).getCommChannel());
            Composite ccComposite = ccGUI.getStatusPannel(tabs,SWT.NONE);
            commChannel.setControl(ccComposite);
            GridData ccCompositeLData = new GridData();
            ccCompositeLData.horizontalAlignment = GridData.BEGINNING;
            ccCompositeLData.verticalAlignment = GridData.BEGINNING;
            ccCompositeLData.grabExcessHorizontalSpace = true;
            ccCompositeLData.grabExcessVerticalSpace = false;
            GridLayout ccCompositeLayout = new GridLayout();
            ccComposite.setLayout(ccCompositeLayout);
            ccComposite.setLayoutData(ccCompositeLData);
            ccComposite.layout(true);
          }
        }
        {
          robot = new CTabItem(tabs, SWT.NONE);
          robot.setText("Robot");
          robot.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/AIBO210.png"));
          {
            Label roboText = new Label(tabs,SWT.NONE);
            roboText.setText"Robot Resuelve Laberintos v1.0\n" +
                      "Desarrollado por:\n" +
                      "Augusto Vega (ajvega@fi.uba.ar)\n" +
                      "Juan Caracoche (jmcaracoche@gmail.com)");
            robot.setControl(roboText);
          }
        }
      }
     
      tabs.layout(true);
      GridData cTabFolder1LData = new GridData();
      cTabFolder1LData.grabExcessHorizontalSpace = true;
      cTabFolder1LData.grabExcessVerticalSpace = true;
      cTabFolder1LData.horizontalAlignment = GridData.FILL;
      cTabFolder1LData.verticalAlignment = GridData.FILL;
      tabs.setLayoutData(cTabFolder1LData);
      tabs.setSelection(0);
     
    }
    catch (Exception e)
    {
      throw new ComponentGUIException( e.getMessage() );
    }
   
    return statusPannel; 
  }

  /**
   * Este metodo devuelve un composite con la configuracion. Este metodo
   * pide a cada uno de los componentes del robot sus respectivos paneles de configuracion.
   * @param parent es el composite del padre
   * @param style para el composite
   * @return composite con el panel de configuracion del robot
   */
  public Composite getConfigPannel(Composite parent, int style) throws ComponentGUIException
  {
   
    backComposite = new Composite(parent, style);
    GridLayout backCompositeLayout = new GridLayout();
    backCompositeLayout.makeColumnsEqualWidth = true;
    backCompositeLayout.horizontalSpacing = 0;
    backCompositeLayout.marginHeight = 0;
    backCompositeLayout.marginWidth = 0;
    backCompositeLayout.numColumns = 1;
    backComposite.setLayout(backCompositeLayout);
     
   
    try
    {
      configTabs = new CTabFolder(backComposite, SWT.NONE);
       
      {
        CTabItem cTabFeatures = new CTabItem(configTabs, SWT.NONE);
        cTabFeatures.setText("Features");
        cTabFeatures.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/System-ColorSync.png"));
        {
          featureTabs = new CTabFolder(configTabs,SWT.NONE);
          cTabFeatures.setControl(featureTabs);
         
          for (int i=0; i<((Robot)component).getFeatures().length; i++)
          {
            Plugin feature = null;
            if (((Robot)component).getFeatures() != null)
              feature = ClassManager.getInstance().getPluginByName( ((Robot)component).getFeatures()[i].getClass().getCanonicalName() );
            if (feature != null)
            {
              cTabItem1 = new CTabItem(featureTabs, SWT.NONE);
              cTabItem1.setText( ((Robot)component).getFeatures()[i].getDescription() );
              {
                FeatureGUI featureGUI = (FeatureGUI)feature.makeGUInstance(((Robot)component).getFeatures()[i]);
                configPannelFeature = featureGUI.getConfigPannel( featureTabs, featureTabs.getStyle());
               
                if (configPannelFeature != null)
                {
                  //Fuerzo el layout que trae el feature para qeu se ajuste al diaogo
                  GridData configPannelFeatureLData = new GridData();
                  configPannelFeatureLData.horizontalAlignment = GridData.FILL;
                  configPannelFeatureLData.verticalAlignment = GridData.FILL;
                  configPannelFeatureLData.grabExcessHorizontalSpace = true;
                  configPannelFeatureLData.grabExcessVerticalSpace = true;
                  configPannelFeature.setLayoutData(configPannelFeatureLData);
                 
                  cTabItem1.setControl(configPannelFeature);
                }
                 
                configElements.add(featureGUI);
       
              }
            }
            featureTabs.setSelection(0);
            featureTabs.layout(true);
           
          }
        }
        Plugin neuralNet = null;
        if ( ((Robot)component).getNeuralNetwork() != null )
          neuralNet = ClassManager.getInstance().getPluginByName( ((Robot)component).getNeuralNetwork().getClass().getCanonicalName() );
        if (neuralNet != null)
        {
          CTabItem cTabNeuralNetwork = new CTabItem(configTabs, SWT.NONE);
          cTabNeuralNetwork.setImage(SWTResourceManager.getImage("resources/icons/icon24x24/enable/neuron.png"));
          cTabNeuralNetwork.setText("Red Neuronal");
          {
            NeuralNetworkGUI neuralNetworkGUI = (NeuralNetworkGUI)neuralNet.makeGUInstance(((Robot)component).getNeuralNetwork());
            Composite configPannelNeuralNet = neuralNetworkGUI.getConfigPannel( configTabs,SWT.NONE);
            if (configPannelNeuralNet != null)
            {
              //Fuerzo el layout para que se adapte al dialogo
              GridData configPannelNeuralNetLData = new GridData();
              configPannelNeuralNetLData.horizontalAlignment = GridData.FILL;
              configPannelNeuralNetLData.verticalAlignment = GridData.FILL;
              configPannelNeuralNetLData.grabExcessHorizontalSpace = true;
              configPannelNeuralNetLData.grabExcessVerticalSpace = true;
              configPannelNeuralNet.setLayoutData(configPannelNeuralNetLData);
             
              cTabNeuralNetwork.setControl(configPannelNeuralNet);
              configPannelNeuralNet.layout(true);
             
            }
            configElements.add(neuralNetworkGUI);           
           
          }
        }
        Plugin commChannel = null;
        if ( ((Robot)component).getCommChannel() != null )
          commChannel = ClassManager.getInstance().getPluginByName( ((Robot)component).getCommChannel().getClass().getCanonicalName() );
        if (commChannel != null)
        {
          CTabItem cTabCommChannel = new CTabItem(configTabs,SWT.NONE);
          cTabCommChannel.setText("Canal de Comunicaciones");
          cTabCommChannel.setImage(SWTResourceManager.getImage("resources/icons/icon32x32/enable/Connection Configuration.png"));
          {
            CommunicationChannelGUI communicationChannelGUI= (CommunicationChannelGUI)commChannel.makeGUInstance(((Robot)component).getCommChannel());
            Composite configPannelcommChannel =  communicationChannelGUI.getConfigPannel(configTabs, SWT.NONE);
            if (configPannelcommChannel != null)
            {
              // Fuerzo el layout para que se ajuste al dialogo
              GridData configPannelcommChannelLData = new GridData();
              configPannelcommChannelLData.horizontalAlignment = GridData.FILL;
              configPannelcommChannelLData.verticalAlignment = GridData.FILL;
              configPannelcommChannelLData.grabExcessHorizontalSpace = true;
              configPannelcommChannelLData.grabExcessVerticalSpace = true;
              configPannelcommChannel.setLayoutData(configPannelcommChannelLData);
             
              cTabCommChannel.setControl(configPannelcommChannel);
              configPannelcommChannel.layout(true);
            }
            configElements.add(communicationChannelGUI);
           
          }
        }

      }
      configTabs.layout(true);
      GridData configTabsLData = new GridData();
      configTabsLData.horizontalAlignment = GridData.FILL;
      configTabsLData.verticalAlignment = GridData.FILL;
      configTabsLData.grabExcessHorizontalSpace = true;
      configTabsLData.grabExcessVerticalSpace = true;
      configTabs.setLayoutData(configTabsLData);
      configTabs.setSelection(0);
     
    }
    catch (Exception e)
    {
      throw new ComponentGUIException( e.getMessage() );
    }
   
    return backComposite;
  }

 
  public void saveConfig()
  {
    ((Robot)component).setXMLConfigFile( ((Robot)component).getName() + ".xml" );
  }

 
  public void applyChanges() throws ComponentGUIException
  {
    if (component != null)
    {
      for (int i=0 ; i<configElements.size(); i++)
        ((ComponentGUI) configElements.get(i)).applyChanges();
      checkRobotConfig();
    }
    /*else
    {
      for (int i=0 ; i<configElements.size(); i++)
        ((ComponentGUI) configElements.get(i)).applyChanges();
      Simulation.getCurrent().addRobot( component );
    }*/
   
 
 
  private void checkRobotConfig()
  {
    String errorString = new String();
    boolean error = false;
    Feature[] features = ((Robot)component).getFeatures();
    for (int i=0; i<features.length ; i++)
    {
      if(features[i].isAllOK()==false){
        errorString=errorString.concat("El feature " + features[i].getName() + " no est� configurado correctamente." + Defines.CR);
        error =true;
      }
    }
   
    if (((Robot)component).getCommChannel().isAllOK()== false)
    {
      errorString=errorString.concat("El canal de comunicaciones no est� configurado correctamente." + Defines.CR);
      error =true;
    }
    if (((Robot)component).getNeuralNetwork().isAllOK() == false)
    {
      errorString=errorString.concat("La red neuronal no est� configurada correctamente." + Defines.CR);
      error =true;
    }
    if (error == true)
      ErrorMessage.customMessage(errorString,ErrorMessage.ERROR_MESSAGE,this.backComposite.getParent());
    else
      this.component.setAllOK(true);
  }
 
 

}
TOP

Related Classes of components.robot.RobotGUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.