/* Generated By:JJTree: Do not edit this line. SimpleNode.java */

/**************************************************************

File				: SimpleNode.java
Auther				: M. Spiller
Registration #		: Y0259667
Date				: Thursday 13th november 2003
Unit				: CMP-2A25 Programming - Software Development Techniques
Description			: 

**************************************************************/

import java.io.*;

public class SimpleNode implements Node 
{
	protected Node parent;
	protected Node[] children;
	protected int id;
	protected DrawPhase3 parser;

	public SimpleNode(int i) 
	{
		id = i;
	}

	public SimpleNode(DrawPhase3 p, int i) 
	{
		this(i);
		parser = p;
	}

	public void jjtOpen(){}
	public void jjtClose(){}
	public void jjtSetParent(Node n) 
	{ 
		parent = n; 
	}
	public Node jjtGetParent() 
	{ 
		return parent; 
	}

	public void jjtAddChild(Node n, int i) 
	{
		if (children == null) 
		{
			children = new Node[i + 1];
		} 
		else if (i >= children.length) 
		{
			Node c[] = new Node[i + 1];
			System.arraycopy(children, 0, c, 0, children.length);
			children = c;
		}
		children[i] = n;
	}

	public Node jjtGetChild(int i) 
	{
		return children[i];
	}

	public int jjtGetNumChildren() 
	{
		return (children == null) ? 0 : children.length;
	}

  /* You can override these two methods in subclasses of SimpleNode to
     customize the way the node appears when the tree is dumped.  If
     your output uses more than one line you should override
     toString(String), otherwise overriding toString() is probably all
     you need to do. */

	public String toString() 
	{ 
		return DrawPhase3TreeConstants.jjtNodeName[id]; 
	}
	public String toString(String prefix) 
	{ 
		return prefix + toString(); 
	}

  /* Override this method if you want to customize how the node dumps
     out its children. */

	public void dump(String prefix) 
	{
		System.out.println(toString(prefix));
		if (children != null) 
		{
			for (int i = 0; i < children.length; ++i)
			{
				SimpleNode n = (SimpleNode)children[i];
				if (n != null) 
				{
					n.dump(prefix + " ");
				}
			}
		}
	}

	public void generate(FileWriter SVGFile,int Xsc, int Ysc, int X, int Y, String colour, int brush){}

	public void Error()
	{
		System.out.println("Error writing to file");
	}

	//write the top of the svg file stating the picture size
	public void FileHeader(FileWriter SVGFile)
	{
		StartItem(SVGFile,"svg");
        NumberLabel(SVGFile,"width", ((ASTInt)jjtGetChild(0)).val);
		NumberLabel(SVGFile,"height", ((ASTInt)jjtGetChild(1)).val);
		EndItem(SVGFile);
	}

	public void FileComplete(FileWriter SVGFile)
	{
		Enclose(SVGFile,"svg");
	}

	//opens an SVG statement
    public void StartItem(FileWriter SVGFile, String text)
    {
		try{
       		System.out.print("<" + text);//prints to screen for testing
        	SVGFile.write("<" + text);//prints to file
		}
		catch(Exception e){
			Error();
		}
    }

	//labels values accordingly
    public void NumberLabel(FileWriter SVGFile, String text, int value)
    {
		try{
    	    System.out.print(" " + text + "=\"" + value + "\"");
			SVGFile.write(" " + text + "=\"" + value + "\"");
		}
		catch(Exception e){
			Error();
		}
    }

	//labels Strings accordingly
    public void TextLabel(FileWriter SVGFile, String text, String text2)
    {
		try{
  	      System.out.print(" " + text + "=\"" + text2 + "\"");
			SVGFile.write(" " + text + "=\"" + text2 + "\"");
		}
		catch(Exception e){
			Error();
		}
    }

	//ends and SVG statement
    public void EndItem(FileWriter SVGFile)
    {
		try{
			System.out.print(">\n");
			SVGFile.write(">\n");
		}
		catch(Exception e){
			Error();
		}
    }

	//ends the SVG process
    public void Stop(FileWriter SVGFile)
    {
		try{
			System.out.println("/>");
			SVGFile.write("/>\n");
		}
		catch(Exception e){
			Error();
		}
    }

	//encloses text
    public void Enclose(FileWriter SVGFile, String text)
    {
		try{
			System.out.println("</" + text + ">");
			SVGFile.write("</" + text + ">");
		}
		catch(Exception e){
			Error();
		}
    }

	//Draw line method to
    public void DrawLine(FileWriter SVGFile, int Xsc, int Ysc, int X, int  Y, String colour,
						 int brush, int x1, int y1, int x2,int y2)
    {
		//sends labels and values to be printed to file
        StartItem(SVGFile,"line");
        NumberLabel(SVGFile,"x1", x1 * Xsc + X);
        NumberLabel(SVGFile,"y1", y1 * Ysc + Y);
        NumberLabel(SVGFile,"x2", x2 * Xsc + X);
        NumberLabel(SVGFile,"y2", y2 * Ysc + Y);
		TextLabel(SVGFile,"stroke", colour);
        NumberLabel(SVGFile,"stroke-width", brush);
        Stop(SVGFile);

    }

	//Draw rectangle method
    public void DrawRectangle(FileWriter SVGFile, int Xsc, int Ysc, int X, int  Y, String colour,
							 int brush, int x1, int x2, int x3, int x4)
    {
		//calls draw line method 4 times to generate 4 sides
        DrawLine(SVGFile, Xsc, Ysc, X, Y, colour, brush, x1, x2, x3, x2);
        DrawLine(SVGFile, Xsc, Ysc, X, Y, colour, brush, x3, x2, x3, x4);
        DrawLine(SVGFile, Xsc, Ysc, X, Y, colour, brush, x3, x4, x1, x4);
        DrawLine(SVGFile, Xsc, Ysc, X, Y, colour, brush, x1, x4, x1, x2);
    }

	//draw ellipse/circle method
    public void DrawEllipse(FileWriter SVGFile, int Xsc, int Ysc, int X, int  Y, String colour,
							 int brush, int x1, int x2, int x3, int x4)
    {
		//calculates new Y side
		int CalcY = ((x3-x2)*Xsc)/2 >= 0 ? ((x3-x2)*Xsc/2) : -((x3-x2)*Xsc)/2;
		//calculates new X side
		int CalcX = ((x4-x2)*Ysc)/2 >= 0 ? ((x4-x2)*Ysc/2) : -((x4-x2)*Ysc)/2;
		//sends labels and values to be printed to file
        StartItem(SVGFile, "ellipse");
        NumberLabel(SVGFile, "cx", ((x1 + x3) * Xsc) / 2 + X);//left
        NumberLabel(SVGFile, "cy", ((x2 + x4) * Ysc) / 2 + Y);//right
        NumberLabel(SVGFile, "rx", CalcX);//top
        NumberLabel(SVGFile, "ry", CalcY);//bottom
        TextLabel(SVGFile, "fill", "none");
		TextLabel(SVGFile,"stroke", colour);
        NumberLabel(SVGFile,"stroke-width", brush);
        Stop(SVGFile);
    }

	//draw text method
    public void DrawText(FileWriter SVGFile, int Xsc, int Ysc, int X, int  Y, String colour,
							 int brush, int x1, int y1, int size, String text1)
    {
		try{
		//sends labels and values to be printed to file
        StartItem(SVGFile, "text");
        NumberLabel(SVGFile, "x", x1 * Xsc + X);//applied X scale and offset
        NumberLabel(SVGFile, "y", y1 * Ysc + Y);//applied Y scale and offset
        NumberLabel(SVGFile, "font-size", size);
        TextLabel(SVGFile, "stroke", "none");
        NumberLabel(SVGFile, "stroke-width", 1);
        EndItem(SVGFile);
        System.out.print(text1.substring(1, text1.length() - 1));
        SVGFile.write(text1.substring(1, text1.length() - 1));
        Enclose(SVGFile, "text");
		}
		catch(Exception e){
			Error();
		}
    }
}

