/*---------------------------------------------------------------------------
File:         			ToGrammarCanvas.java
Package:                        JFLAP Version 3.1
Author:			        Ted Hung
                                
Description of Contents:	Contains class ToGrammarCanvas.
				
--------------------------------------------------------------------------*/

/* 
 * Susan H. Rodger, Magda Procopiuc, Octavian Procopiuc, Eric Gramond, Ted Hung
 * Computer Science Department
 * Duke University
 * August 1999
 * Supported by National Science Foundation DUE-9596002, DUE-9555084,
 *  and DUE-9752583
 *
 * Copyright (c) 1999
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the author.  The name of the author may not be used to
 * endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

package flap;

import java.awt.*;
import java.util.*;


/**
 * this is the canvas used when drawing the rules for the conversion
 * from FSA or PDA to a Grammar
 */
class ToGrammarCanvas extends Canvas
{
    Vector myRules;
    String myText;
    int x, y;

    public ToGrammarCanvas()
    {
       super();
    }

    public void calcSize()
    {
       if(myRules == null)
	  return;

       FontMetrics fm = getGraphics().getFontMetrics();
       
       Rule aRule;
       int width = 20;
       int height = fm.getHeight() * myRules.size();
       for(Enumeration e = myRules.elements(); e.hasMoreElements(); )
       {
	 aRule = (Rule) e.nextElement();
	 if(fm.stringWidth(aRule.toString()) > width)
	    width = fm.stringWidth(aRule.toString());
       }
       setBackground(Color.white);
       setSize(width + 40, height + 40);
    }

    public void setRules(Vector rules)
    {
       myRules = rules;
       calcSize();
    }

    public void paint(Graphics g)
    {
	FontMetrics fm = g.getFontMetrics();
	Rule aRule;
	Vector right;
	String temp;
	int xpos = 20;
	int ypos = 20;
	GrammarRightHandToken t;
	
	for(Enumeration e = myRules.elements(); e.hasMoreElements(); )
	{
	   xpos = 20;
	   aRule = (Rule) e.nextElement();
	   temp = aRule.getLeftHand().getContents();
	   g.setColor(Color.blue);
	   g.drawString(temp, xpos, ypos);
	   xpos += fm.stringWidth(temp);

	   g.setColor(Color.black);
	   g.drawString(" -> ", xpos, ypos);
	   xpos += fm.stringWidth(" -> ");
	   
	   right = aRule.getRightHand();
	   for(Enumeration e2 = right.elements(); e2.hasMoreElements(); )
	   {
	      t = (GrammarRightHandToken) e2.nextElement();
	      if(t.getType().equals("Variable"))
	      {
		 g.setColor(Color.blue);
	      }
	      else
	      {
		 g.setColor(Color.red);
	      }

	      g.drawString(t.getContents() + " ", xpos, ypos);
	      xpos += fm.stringWidth(t.getContents() + " "); 
	   }

	   ypos += fm.getHeight();
	}	
    }
}
