package pipe.gui;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import pipe.dataLayer.Transition;


/**
 * Action object that can be used to add a rate to a transition.
 */
public class EditRateAction extends AbstractAction {

  private Container contentPane;
  private Transition selected;


  public EditRateAction(Container contentPane, Component component) {
    this.contentPane = contentPane;
    selected = (Transition)component;

  }

  /** Actions deleting the component */
  public void actionPerformed(ActionEvent e) {

    double currentRate = selected.getRate();
    String input = JOptionPane.showInputDialog("Rate:", String.valueOf(currentRate));

    try {
      double newRate = Double.parseDouble(input);
      if (newRate < 0)
        JOptionPane.showMessageDialog(contentPane,"Please enter a real number.");
      else
        selected.setRate(newRate);

    } catch (Exception exc) {
      if (input != null)
        JOptionPane.showMessageDialog(contentPane,"Please enter a real number.","Invalid entry",JOptionPane.ERROR_MESSAGE);
      System.err.println(exc.toString());
    }


  }

}
