/**Applet de manipulation de la bibliothèque*/
//Importation des librairies
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import biblio.*;

/**@author RIGHETTO Dominique
@version 1.1
@see Bibliotheque.java Documents.java Livre.java Revue.java*/

/**
Codes-sources utilisables selon 
la licence GNU General Public Licence
(voir fichier HTML joint*/


/**
<APPLET	code="ManipBiblio.class" width="500" height="300">
Votre navigateur ne supporte pas le java !
</APPLET>
*/



public class ManipBiblio extends Applet implements ActionListener
{
	Bibliotheque B;
	//déclaration des éléments graphiques
	Label auteurLabel= new Label("Auteur : ");
	Label titreLabel=  new Label("Titre  : ");
	Label editionLabel=new Label("Edition: ");
	Label specialiteLabel=new Label("Specialité : ");
	TextField champAuteur = new TextField(10);
	TextField champTitre = new TextField(10);
	TextField champEdition = new TextField(10);
	TextField champSpecialite= new TextField(10);				
	Button boutonAjouter = new Button("AJOUTER");
	Button boutonRechercher = new Button("RECHERCHER");
	Choice choixDocument = new Choice();
	java.awt.List tousLesDocuments=new java.awt.List(4);
	
	
	/**Méthode pour initialiser l'Applet*/
	public void init()
	{
	String Nom = JOptionPane.showInputDialog("Nom de la bibliothèque : ");
	B=new Bibliotheque(Nom);
	showStatus(Nom);
	//choix du gestionnaire graphique
	setLayout(new GridLayout(14,1));
	//ajout des Elts au gestionnaire
	add(auteurLabel);
	add(champAuteur);
	add(titreLabel);
	add(champTitre);
	add(editionLabel);
	add(champEdition);
	add(specialiteLabel);
	add(champSpecialite);
	choixDocument.add("Livre");
	choixDocument.add("Revue");
	choixDocument.select("Livre");
	add(choixDocument);
	add(boutonAjouter);
	add(boutonRechercher);
	add(tousLesDocuments);
	//ajout des ecouteurs d'evenement
	boutonAjouter.addActionListener(this);
	boutonRechercher.addActionListener(this);
	}
	
	/**Constructeur*/
	public ManipBiblio()
	{}
	
	/**Méthode de dessin*/
	public void paint(Graphics g)
	{
	Enumeration leVecteur;
	Documents temp;
	
	showStatus("Bibliothèque : "+B.retournerNom());
	leVecteur=B.retournerVecteur();
	tousLesDocuments.removeAll();
	while(leVecteur.hasMoreElements())
		{
			temp=(Documents)leVecteur.nextElement();
			tousLesDocuments.add(temp.affiche());
		}
	}	

 /**Méthode d'action si on clique sur le bouton*/
 public void actionPerformed(ActionEvent evt)
 {
	int choix;
	String nomDuBouton;
	String auteurRecherche;
	Object[] reponsePossible= { "Livre", "Revue"};
	
	
	nomDuBouton=evt.getActionCommand();
	if(nomDuBouton.equals("AJOUTER"))
	{
		choix=choixDocument.getSelectedIndex();

 		switch(choix)
 		{
 			case 0 :{
 				    B.ajout(new Livre(champTitre.getText(),champAuteur.getText(),champEdition.getText()));
 			    	champTitre.setText("");
 			    	champAuteur.setText("");
	 			    champEdition.setText("");
 				    JOptionPane.showMessageDialog(null, "Livre ajouté !","Ajout de document", JOptionPane.INFORMATION_MESSAGE);
 				    repaint();
 				    break;
 			    	}
	 		case 1 :{	
 				    B.ajout(new Revue(champTitre.getText(),champSpecialite.getText()));
 				    champTitre.setText("");
 				    champSpecialite.setText("");
 			    	JOptionPane.showMessageDialog(null, "Revue ajoutée !","Ajout de document", JOptionPane.INFORMATION_MESSAGE);
 			    	repaint();
 			    	break;
 			    	}
 		}		            
 	}
 	
 	if(nomDuBouton.equals("RECHERCHER"))
 	{
 		
   Object reponseChoisie = JOptionPane.showInputDialog(null, "Choix du document à rechercher", "Input",JOptionPane.INFORMATION_MESSAGE, null,reponsePossible, reponsePossible[0]);
   
   
  if (reponseChoisie=="Livre")
     {
	      auteurRecherche = JOptionPane.showInputDialog("Nom de l'auteur recherché : ");
 	      if(B.rechercheParAuteur(auteurRecherche))
 		       JOptionPane.showMessageDialog(null, "Ouvrage trouvé !","Recherche de "+auteurRecherche, JOptionPane.INFORMATION_MESSAGE);
	      else
		       JOptionPane.showMessageDialog(null, "Ouvrage absent !","Recherche de "+auteurRecherche, JOptionPane.INFORMATION_MESSAGE);
 	  }
 	 else
 	 
 	  {
 	  
 	  	   auteurRecherche = JOptionPane.showInputDialog("Titre de la revue recherchée : ");
 	      if(B.presentRevue(auteurRecherche))
 		       JOptionPane.showMessageDialog(null, "Revue trouvée !","Recherche de "+auteurRecherche, JOptionPane.INFORMATION_MESSAGE);
	      else
		       JOptionPane.showMessageDialog(null, "Revue absente !","Recherche de "+auteurRecherche, JOptionPane.INFORMATION_MESSAGE);  	
 	  }	 
 	
 	}	
 		            
 }
 
} 		