//****************************************************************************
// CLASS NAME:	Token.java
//
// AUTHOR:	Rick Brink
//		rick@mail.csh.rit.edu
//		http://www.csh.rit.edu/~rick
//
// VERSION:	1.0
//
// HISTORY:	4/16/96		Initial Version
//
// COPYRIGHT INFORMATION:
//
// This program and the Java source is in the public domain.
// Permission to use, copy, modify, and distribute this software
// and its documentation for NON-COMMERCIAL purposes and
// without fee is hereby granted.
//
//    Copyright 1996
//
//    Rick Brink
//    1266 Brighton-Henrietta Townline Rd.
//    Rochester, NY 14623
//
// DISCLAIMER:
//
// The author claims no responsibility for any damage, direct or indirect,
// to any harware or software as a result of using this program.
//****************************************************************************

import java.io.PrintStream;
import java.util.StringTokenizer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;

/**
  * A class representing a Token in a Petri Net diagram.
  *
  * @see PetriComponent
  * @see java.awt.Graphics
  * @see java.awt.Color
  * @see java.awt.Font
  * @see java.io.PrintStream
  * @see java.util.StringTokenizer
  *
  * @version 1.0 July 3, 1996
  *
  * @author  Rick Brink
**/
class Token extends PetriComponent {

    /** The number of Petri Net Tokens this Token Object represents. **/
    private protected int tokensRepresented_;

    /**
      * Construct a new Token, with coordinates of
      * (x,y) which are the absolute coordinates divided by
      * gridStep_.  Also note, all coordinates represent the
      * upper left hand corner of a grid square.  Also set the
      * tokensRperesented_, allowing a single Token object to
      * represent a number of actual Petri Net Tokens.
    **/
    public Token (int x, int y, int numTokens) {
        xCoordinate_ = x;
        yCoordinate_ = y;
        tokensRepresented_ = numTokens;
    }

    /**
      * Construct a new Token without specifying any parameters.
    **/
    public Token() {
    }

    /**
      * Create a copy of the current Token Object
    **/
    public Object clone() {
        Token t = new Token(xCoordinate_, yCoordinate_,
                            tokensRepresented_);

        return t;
    }

    /**
      * Return the number of Tokens this Token object represents.
    **/
    public int getTokensRepresented() {
        return (tokensRepresented_);
    }

    /**
      * Set the number of Tokens this Token object represents.
    **/
    public void setTokensRepresented(int numTokens) {
        tokensRepresented_ = numTokens;
    }


    /**
      * Draw the Token to the screen.
    **/
    public void draw(Graphics g, int gridStep, Color tokenColor) {
        if (tokensRepresented_ > 0) {
            g.setColor(tokenColor);
            g.fillOval(xCoordinate_ * gridStep + gridStep / 4,
                       yCoordinate_ * gridStep + gridStep / 4,
                       gridStep / 2, gridStep / 2);
            if (selected_) {
                int x__ = xCoordinate_ * gridStep + gridStep / 4;
                int y__ = yCoordinate_ * gridStep + gridStep / 4;
                int tokenSize__ = gridStep / 2;
                g.setColor(Color.green);
                g.fillRect(x__ - 2, y__ - 2, SELECT_SIZE, SELECT_SIZE);
                g.fillRect(x__ + tokenSize__ - 2, y__ - 2, SELECT_SIZE, SELECT_SIZE);
                g.fillRect(x__ - 2, y__ + tokenSize__ - 2, SELECT_SIZE, SELECT_SIZE);
                g.fillRect(x__ + tokenSize__ - 2, y__ + tokenSize__ - 2, SELECT_SIZE, SELECT_SIZE);
            }
            g.setColor(Color.magenta);
            Font labelFont__ = new Font("Dialog", Font.PLAIN, gridStep / 2);
            g.setFont(labelFont__);
            g.drawString("T = " + tokensRepresented_, xCoordinate_ * gridStep,
                         (yCoordinate_ + 2) * gridStep);
        }
    }

    /**
      * Save the necessary Token information to a PrintStream
    **/
    public void saveToFile(PrintStream printStream) {
        printStream.println("Token: " + xCoordinate_ + "," +
                            yCoordinate_ + "," + tokensRepresented_);
    }

    /**
      * Given a StringTokenizer, set the Token values.  Useful
      * for restoring a Token from a file.
    **/
    public void loadFromFile(StringTokenizer tokenizer) {
        xCoordinate_ = Integer.parseInt(tokenizer.nextToken());
        yCoordinate_ = Integer.parseInt(tokenizer.nextToken());
        tokensRepresented_ = Integer.parseInt(tokenizer.nextToken());
        return;
    }

}
