
package gui;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

//import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

/**
 * Main class to send GUI actions to the TSP model.
 * @author Heinz Kredel.
 */
public class TSPguiActionControl implements ActionListener {

    TSPguiModel model;

/**
 * @param model the TSPguiModel.
 */
    public TSPguiActionControl(TSPguiModel model) {
        this.model = model;
    }

    public void actionPerformed(ActionEvent event) {
        Object s = event.getSource();
        //System.out.println("source = " + s);
        if ( s instanceof JMenuItem ) {
           JMenuItem source = (JMenuItem)s;
           String was = source.getActionCommand();
           if ( was == null ) {
               return;
           }
           if ( was.equals("generate") ) {
              model.generateProblem();
              model.doUpdate();
              return;
           }
           if ( was.equals("solve") ) {
              model.solveProblem();
              model.doUpdate();
              return;
           }
           if ( was.equals("stop") ) {
              model.stopProblem();
              model.doUpdate();
              return;
           }
           if ( was.equals("sequential") ) {
              model.setAlgorithm( was );
              model.doUpdate();
              return;
           }
           if ( was.equals("parallel") ) {
              model.setAlgorithm( was );
              model.doUpdate();
              return;
           }
           if ( was.equals("distributed") ) {
              model.setAlgorithm( was );
              model.doUpdate();
              return;
           }
           if ( was.equals("about") ) {
              TSPguiMain.doAboutDialog();
              model.doUpdate();
              return;
           }
           if ( was.equals("standAlone") ) {
              try {
                  model.setStandAlone( ! model.getStandAlone() );
              } catch (NumberFormatException ignored) {
                  //ignored.printStackTrace();
              }
              model.doUpdate();
              return;
           }
           if ( was.equals("exit") ) {
              if ( ! model.isDone() ) {
                 if ( ! TSPguiMain.doExitDialog() ) {
                    return;
                 }
                 model.setDone();
              }
              return;
           }
           return;
        }
        if ( s instanceof JTextField ) {
           JTextField source = (JTextField)s;
           System.out.println("source = " + event.getActionCommand() );
           String was = event.getActionCommand();
           if ( was == null ) {
               return;
           }
           if ( was.equals("size") ) {
              try {
                  int i = Integer.parseInt( source.getText() );
                  model.setProbSize(i);
              } catch (NumberFormatException ignored) {
              }
              model.doUpdate();
              return;
           }
           if ( was.equals("file") ) {
              // not implemented
              model.doUpdate();
              return;
           }
           if ( was.equals("algorithm") ) {
              model.setAlgorithm( source.getText() );
              model.doUpdate();
              return;
           }
           if ( was.equals("threads") ) {
              try {
                  int i = Integer.parseInt( source.getText() );
                  model.setThreads(i);
              } catch (NumberFormatException ignored) {
              }
              model.doUpdate();
              return;
           }
           if ( was.equals("maxiter") ) {
              try {
                  long i;
                  if ( source.getText().equals("max") ) {
                      i = Long.MAX_VALUE;
                  } else {
                      i = Long.parseLong( source.getText() );
                  }
                  model.setMaxIterations(i);
              } catch (NumberFormatException ignored) {
                  //ignored.printStackTrace();
              }
              model.doUpdate();
              return;
           }
           if ( was.equals("server") ) {
              model.setServer( source.getText() );
              model.doUpdate();
              return;
           }
           if ( was.equals("port") ) {
              try {
                  int i = Integer.parseInt( source.getText() );
                  model.setPort(i);
              } catch (NumberFormatException ignored) {
                  //ignored.printStackTrace();
              }
              model.doUpdate();
              return;
           }
           return;
        }
        if ( s instanceof JButton ) {
           JButton source = (JButton)s;
           //System.out.println("source = " + event.getActionCommand() );
           String was = event.getActionCommand();
           if ( was == null ) {
               return;
           }
           return;
        }
    }

}
