/*---------------------------------------------------------------------------
File:         			DialogCanvas.java
Package:                        JFLAP Version 3.1
Author:			        Eric Gramond 
Modified:		        Ted Hung V3.1 7/21/99
                                
Description of Contents:	Contains class DialogCanvas.
				
--------------------------------------------------------------------------*/

/* 
 * 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.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.util.*;



/**
 * this is a little canvas that will draw the given message onto itself.
 * used by dialog boxes. Best way to get allth e text nicely centered.
 */
class DialogCanvas extends Canvas {


    Vector myLines;
    String myText;
    int x, y;

    public DialogCanvas(String text)
    {
	myText = text;
	myLines = new Vector();

	StringReader sr = new StringReader(text);
	BufferedReader br = new BufferedReader(sr);
	String temp;
	try {
	    temp =br.readLine();
	    while(temp != null) {
		myLines.addElement(temp.trim());
		temp = br.readLine();
	    }
	}
	catch(IOException e) {}
    }

    public void paint(Graphics g) {
	
	FontMetrics myFM = g.getFontMetrics();
	int longest = this.getSize().width;
	String temp;

	y = 10;
	String aString;

	for(Enumeration e = myLines.elements(); e.hasMoreElements(); ) {
	    aString = (String) e.nextElement();
	    x = (longest - myFM.stringWidth(aString)) /2;
	    g.drawString(aString, x, y);
	    y+= myFM.getAscent()+ 5;
	}	
    }

    /**
     *calculate the size of the canvas given a minimum
     *  width and height
     */
    public void calcSize(int width, int height)
    {
       FontMetrics myFM = getGraphics().getFontMetrics();
       String aString;
       int lines = 0;
       for(Enumeration e = myLines.elements(); e.hasMoreElements(); )
       {
	  aString = (String) e.nextElement();
	  if(myFM.stringWidth(aString) + 10 > width)
	     width = myFM.stringWidth(aString) + 10;
	  ++lines;
       }
       if(height < ((lines + 1) * (myFM.getAscent() + 5)))
	  height = (lines + 1) * (myFM.getAscent()+ 5);

       setSize(width, height);
    }
}
