import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

public class Exo1 extends Frame implements ActionListener{
    String titre = new String("Lecture de fichier");
    
    MenuBar barre = new MenuBar();
    Menu file = new Menu("Fichier");
    MenuShortcut aff = new MenuShortcut(KeyEvent.VK_A, true);
    MenuShortcut q = new MenuShortcut(KeyEvent.VK_Q, true);
    MenuItem affich = new MenuItem("Afficher");
    MenuItem quit = new MenuItem("Quitter");

    TextArea a = new TextArea("", 20, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);

    public Exo1(){
	setTitle(titre);
	
	affich.setShortcut(aff);
	quit.setShortcut(q);

	affich.setActionCommand("Afficher");
	affich.addActionListener(this);
	quit.setActionCommand("Quitter");
	quit.addActionListener(this);

	file.add(affich);
	file.add(quit);
	barre.add(file);

	setMenuBar(barre);
	add(a);
	
	pack();
	show();
    }	
 
    public void Affichage(){
	FileDialog fd = new FileDialog(this, null, FileDialog.LOAD);
	fd.show();
	File f =new File(fd.getDirectory() + fd.getFile());
	try {
	    FileInputStream fi = new FileInputStream(f);
	    a.setText("");
	    int c = fi.read();
	    while (c > -1) {
		a.append((char) c + "");
		c = fi.read();
	    }
	    fi.close();
	} catch (FileNotFoundException e) {
	    System.err.println("error");
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }	
	
    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof MenuItem) {
		    MenuItem c = (MenuItem)evt.getSource();
		    String x = c.getLabel();
		    if (x.equals("Quitter")) {
			System.exit(0);
		    }
		    else
			{ 
			Affichage();
			}
		}
    }

    public static void main(String args[]){
	new Exo1();
    }

}


public class Exo3 extends Frame implements ActionListener{
    String titre = new String("Lecture de fichier");
    
    MenuBar barre = new MenuBar();
    Menu file = new Menu("Fichier");
    MenuShortcut aff = new MenuShortcut(KeyEvent.VK_A, true);
    MenuShortcut cha = new MenuShortcut(KeyEvent.VK_C, true);
    MenuShortcut q = new MenuShortcut(KeyEvent.VK_Q, true);
    MenuItem affich = new MenuItem("Afficher");
    MenuItem charger = new MenuItem("Charger");
    MenuItem quit = new MenuItem("Quitter");

    TextArea a = new TextArea("", 20, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);

    Label l = new Label("Message : ");
    TextField t = new TextField(20);

    public Exo3(){
	setTitle(titre);
	
	affich.setShortcut(aff);
	quit.setShortcut(q);
	charger.setShortcut(cha);

	affich.setActionCommand("Afficher");
	affich.addActionListener(this);
	charger.setActionCommand("Charger");
	charger.addActionListener(this);	
	quit.setActionCommand("Quitter");
	quit.addActionListener(this);

	file.add(affich);
	file.add(charger);
	file.add(quit);
	barre.add(file);

	a.addMouseListener(new mouseHandler());

	setMenuBar(barre);
	add(a, BorderLayout.NORTH);
	add(l, BorderLayout.WEST);
	add(t, BorderLayout.CENTER);

	pack();
	show();
    }	
 
    public void Affichage(){
	FileDialog fd = new FileDialog(this, null, FileDialog.LOAD);
	fd.show();
	File f =new File(fd.getDirectory() + fd.getFile());
	try {
	    FileInputStream fi = new FileInputStream(f);
	    a.setText("");
	    int c = fi.read();
	    while (c > -1) {
		a.append((char) c + "");
		c = fi.read();
	    }
	    fi.close();
	} catch (FileNotFoundException e) {
	    System.err.println("error");
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }	

    public mouseHandler extends MouseAdapter{
	public void mouseClicked(MouseEvent evt){
	    a.insert(t.getText(), a.getCaretPosition());
	}
    }
	
    public void actionPerformed (ActionEvent evt){
		if (evt.getSource() instanceof MenuItem) {
		    MenuItem c = (MenuItem)evt.getSource();
		    String x = c.getLabel();
		    if (x.equals("Quitter")) {
			System.exit(0);
		    }
		    else
			{ if (x.equals("Charger"))
			    {
				Affichage();
				a.insert("toto",a.getCaretPosition());
			    }
			else 
			    { System.out.println(a.getText());}
				
			}   
		}
    }

    public static void main(String args[]){
	new Exo3();
    }

}







