/* Auteur: Nicolas JANEY          */
/* nicolas.janey@univ-fcomte.fr   */
/* Fevrier 2005                   */

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.print.attribute.*;
import java.awt.font.*;
import java.awt.geom.*;

public class CanvasConsole extends Canvas
                           implements Printable {
  public static boolean open = false;
  public Vector lignes;
  public int maxl;
  public int lc;
  private Font font = new Font("Courier",Font.PLAIN,12);
  private CurseurCanvasConsole ccc;
  public boolean curseur = true;
  private FrameConsole fc;
  private boolean top = true;
  private int size = 12;
  private int width;
  private int height;
  
  public CanvasConsole(FrameConsole fc,int maxl) {
    lignes = new Vector();
    this.maxl = maxl;
    this.fc = fc;
    lc = 0;
    lignes.add("");
    ccc = new CurseurCanvasConsole(this,700);
    ccc.start();
  }
  
  public void effacer() {
    lc = 0;
    lignes = new Vector();
    lignes.add("");
  }
  
  public void sauve(PrintWriter dis) {
    for ( int i = 0 ; i <= lc ; i++ )
      dis.println((String) lignes.elementAt(i));
  }
  
  public void print(String s) {
    StringTokenizer st = new StringTokenizer(s,"\n",true);
    while ( st.hasMoreTokens() ) {
      String tk = st.nextToken();
      if ( ( tk.length() == 1 ) && ( tk.charAt(0) == '\n' ) ) {
        tk = new String("");
        lignes.add(tk);
        lc++; }
        else {
        String str = format((String) lignes.elementAt(lc));
        str = str.concat(tk);
        lignes.setElementAt(str,lc); } }
    top = true;
    repaint();
  }
  
  public String format(String s) {
    return(s);
  }
  
  public void paint(Graphics g) {
    Insets insFc = fc.getInsets();
    Insets insSp = fc.sp.getInsets();
    setSize(fc.sp.getViewportSize().width,maxl*14+5);
    fc.validate();
    Graphics2D g2 =(Graphics2D) g;
    g2.setFont(font);
    int deb = ( lc+1 > maxl ) ? lc-maxl+1 : 0;
    for ( int i = deb ; i <= lc ; i++ )
      g2.drawString(format((String) lignes.elementAt(i)),4,13+(i-deb)*14);
    String str = format((String) lignes.elementAt(lc));
    int l = 0;
    if ( Console.line != null ) {
      g2.drawString(Console.line,4+str.length()*7,13+(lc-deb)*14);
      l = Console.line.length(); }
    if ( curseur ) {
      int posx = (str.length()+l)*7+4;
      g2.drawLine(posx,13+(lc-deb)*14,posx+5,13+(lc-deb)*14); }
    if ( top == true ) {
      if ( lc < fc.sp.getViewportSize().height/14-1 )
        fc.sp.setScrollPosition(0,0);
        else
        fc.sp.setScrollPosition(0,(lc+1-fc.sp.getViewportSize().height/14)*14);
      top = false; }
  }

  public int print(Graphics g,PageFormat pageFormat,int page) {
    Graphics2D g2 =(Graphics2D) g;
    Font f = new Font("Courier",Font.PLAIN,size);
    g2.setFont(f);
    { FontRenderContext frc = new FontRenderContext(g2.getTransform(),false,false);
      Rectangle2D r = f.getStringBounds("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",frc);
      width =(int) (pageFormat.getImageableWidth()/r.getWidth()*100);
      height =(int) (pageFormat.getImageableHeight()/size); }
    if ( page*height > lc+1 )
      return(Printable.NO_SUCH_PAGE);
    int cx =(int) pageFormat.getImageableX();
    int cy =(int) pageFormat.getImageableY();
    int pg = 0;
    int li = page*height;
    int lf = (page+1)*height-1;
    if ( lf > lc )
      lf = lc;
    for ( int i = li ; i <= lf ; i++ ) {
      g2.drawString(format((String) lignes.elementAt(i)),cx,cy+(i-li+1)*size); }
    return(Printable.PAGE_EXISTS);
  }
}