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

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

import pipe.dataLayer.Place;


/**
 * Action object that can be used to effect adding/removing tokens from a place
 */
public class EditTokenAction extends AbstractAction {

  private String label;
  private Container contentPane;
  private Place selected;


  public EditTokenAction(Container contentPane, Place place) {
    this.contentPane = contentPane;
    selected = place;
  }

  /** Prompts the user for the number of tokens they want the place to have */
  public void actionPerformed(ActionEvent e) {
    int currentMarking = selected.getCurrentMarking();

    String input = JOptionPane.showInputDialog("Number of tokens:", String.valueOf(currentMarking));

    try {
      int newMarking = Integer.parseInt(input);
      if (newMarking < 0)
        JOptionPane.showMessageDialog(contentPane,"Please enter a positive number.");
      else
        selected.setCurrentMarking(newMarking);

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

}




