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

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class FrameConsole extends Frame
                          implements WindowListener,
			                               ActionListener,
                                     KeyListener{
  public static boolean open = false;
  public CanvasConsole cc;
  public ScrollPane sp;
  private MenuBar mb = new MenuBar();
  private Menu m1 = new Menu("Fichier");
  private MenuItem mfs = new MenuItem("Sauver");
  private MenuItem mfe = new MenuItem("Effacer");
  private MenuItem mfmp = new MenuItem("Mise en page");
  private MenuItem mfi = new MenuItem("Imprimer");
  private MenuItem mff = new MenuItem("Fermer");
  private Menu m2 = new Menu("Propriétés");
  private MenuItem mpp = new MenuItem("Police");
  private MenuItem mpcf = new MenuItem("Configuration");
  private MenuItem mpc = new MenuItem("Couleur");
  public PageFormat pf = null;

  public void ouvre() {
    open = true;
    setMenuBar(mb);
    mb.add(m1);
    m1.add(mfs);
    m1.add(mfe);
    m1.addSeparator();
    m1.add(mfmp);
    m1.add(mfi);
    m1.addSeparator();
    m1.add(mff);
    mb.add(m2);
    m2.add(mpp);
    m2.add(mpcf);
    m2.add(mpc);
    mfmp.addActionListener(this);
    mfi.addActionListener(this);
    mfs.addActionListener(this);
    mfe.addActionListener(this);
    mff.addActionListener(this);
    mpp.addActionListener(this);
    mpcf.addActionListener(this);
    mpc.addActionListener(this);
    addWindowListener(this);
    setTitle("Console");
    setSize(320,240);
    sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
    sp.add(cc = new CanvasConsole(this,80));
    add(sp);
    setVisible(true);
  }

  public void setKeyListeners() {
    addKeyListener(Console.in);
    cc.addKeyListener(Console.in);
  }

  public void print(String s) {
    cc.print(s);
  }
  
  public void sauver() {
    FileDialog fd = new FileDialog(this,"Sauver",FileDialog.SAVE);
    fd.setVisible(true);
    String dire = fd.getDirectory();
    String file = fd.getFile();
    if ( dire != null ) {
      try {
        File f = new File(dire+file);
        OutputStream out = new FileOutputStream(f);
        PrintWriter dis = new PrintWriter(new OutputStreamWriter(out));
        cc.sauve(dis);
        dis.close(); }
      catch ( IOException e ) { } }
  }
  
  public void imprimer() {
	  PrinterJob pj = PrinterJob.getPrinterJob();
    if ( pf == null )
      miseEnPageInitiale();
    pj.setPrintable(cc,pf);
    if ( pj.printDialog() )
      try {
        pj.print(); }
      catch(PrinterException pe) {
        System.out.println("Erreur d'impression"); }
  }
  
  public void miseEnPageInitiale() {
    pf = new PageFormat();
    Paper p = new Paper();
    double tx = 210.0/25.4*72.0;
    double ty = 297.0/25.4*72.0;
    p.setSize(tx,ty);
    p.setImageableArea(28.346,28.346,tx-56.693,ty-56.693);
    pf.setPaper(p);
  }
  
  public void miseEnPage() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    if ( pf == null ) {
      miseEnPageInitiale(); }
    pf = pj.pageDialog(pf);
  }
  
  public void effacer() {
    cc.effacer();
  }
  
  public void windowClosed(WindowEvent e) {
  }
  
  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }
  
  public void windowActivated(WindowEvent e) {
  }
  
  public void windowDeactivated(WindowEvent e) {
  }
  
  public void windowDeiconified(WindowEvent e) {
  }
  
  public void windowIconified(WindowEvent e) {
  }
  
  public void windowOpened(WindowEvent e) {
  }
  
  public void actionPerformed(ActionEvent e) {
    if ( e.getSource() == mff ) {
      System.exit(0); }
    if ( e.getSource() == mfmp ) {
      miseEnPage(); }
    if ( e.getSource() == mfi ) {
      imprimer(); }
    if ( e.getSource() == mfs ) {
      sauver(); }
    if ( e.getSource() == mfe ) {
      effacer(); }
  }

  public void keyPressed(KeyEvent e) {
  }
  
  public void keyReleased(KeyEvent e) {
  }
  
  public void keyTyped(KeyEvent e) {
    int code =(int) e.getKeyChar();
    System.out.println(code); 
  }
}