package visualization;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import odor.Odor;
/**
* Controls visualization options during runtime
* @author pedro
*
*/
public class ControlWidget extends JFrame implements KeyListener
{
private static final long serialVersionUID = -3693537596454819343L;
private final RunsSimulation runner;
private JButton startButton;
private JButton newOdorButton;
private JComboBox odorComboBox; //TODO ComboBox
public final boolean SINGLE_ODOR;
/**
* Set default frame options
*/
public void setFrameOptions()
{
this.setSize(new Dimension(225,130));
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setTitle(main.Main.PROGRAM_NAME);
this.setLayout(new FlowLayout());
}
public Odor getSelectedOdor()
{
return (Odor) odorComboBox.getSelectedItem();
}
private void runSimulation()
{
this.runner.runSimulation(null);
}
private JNetwork getJNetwork()
{
return this.runner.getJNetwork();
}
private void setOdor(Odor i)
{
this.runner.setOdor(i);
}
public ControlWidget(RunsSimulation runner ,Odor[] battery)
{
this.setFrameOptions();
this.runner = runner;
startButton = new JButton("Start Simulation");
odorComboBox = new JComboBox(battery); //TODO Fix display (toString method)
odorComboBox.addKeyListener(this);
//Add ActionListeners
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
runSimulation();
}
});
odorComboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
JComboBox source = (JComboBox)arg0.getSource();
Odor odr = (Odor)source.getSelectedItem();
if (getJNetwork() != null)
{
getJNetwork().setOdor(odr);
getJNetwork().repaint();
}
}
});
this.add(startButton);
this.add(odorComboBox);
this.validate();
this.SINGLE_ODOR = false;
}
public ControlWidget(RunsSimulation runner)
{
this.setFrameOptions();
this.runner = runner;
startButton = new JButton("Start Simulation");
newOdorButton = new JButton("New Odor");
//Add ActionListeners
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
runSimulation();
}
});
/*
newOdorButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
setOdor(new Odor(getJNetwork().getNumCols(), (int)main.Main.ODOR_INPUT_SUM));
}
});
*/
this.add(startButton);
this.add(newOdorButton);
this.validate();
this.SINGLE_ODOR = true;
}
public void keyPressed(KeyEvent arg0)
{
//DO NOTHING
if (this.odorComboBox.getSelectedItem() != null)
{
if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
{
this.startButton.doClick();
}
}
}
public void keyReleased(KeyEvent arg0)
{
//DO NOTHING
}
public void keyTyped(KeyEvent arg0)
{
if (this.odorComboBox.getSelectedItem() != null)
{
if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
{
this.startButton.doClick();
}
}
}
}