// $Id: Comics.java 3865 2012-10-16 14:21:29Z fabien $ import java.sql.*; import java.awt.*; import java.awt.event.*; import java.util.GregorianCalendar; import java.util.Calendar; /** basic GUI to add books to the database. * @author Fabien Coelho */ public class Comics extends Frame implements ActionListener, ItemListener, TextListener { // CONSTANTS static public final String NOTHING = "*"; // DATABASE ATTRIBUTES protected Connection dbc; protected ComicsDB db; // AWT ATTRIBUTES protected TextField titre; protected Choice annee; protected ChoiceId editeur, collection, auteur, langue; protected Button add, quit, autre_auteur; protected Label status; public Comics(Connection dbc) throws SQLException, ClassNotFoundException, Exception { super("Comics..."); this.dbc = dbc; this.db = new ComicsAwtDB(dbc); GridBagLayout gbl = new GridBagLayout(); this.setLayout(gbl); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; titre = new TextField("", 20); addRow("Titre", titre, gbl, c); annee = new Choice(); int cette_annee = new GregorianCalendar().get(Calendar.YEAR); for (int i=cette_annee; i>=1930; i--) annee.add(Integer.toString(i)); annee.select(0); addRow("Annee", annee, gbl, c); editeur = (ChoiceId) db.getEditeurs(); addRow("Editeur", editeur, gbl, c); collection = (ChoiceId) db.getCollections(); addRow("Collection", collection, gbl, c); langue = (ChoiceId) db.getLangues(); addRow("Langue", langue, gbl, c); auteur = (ChoiceId) db.getAuteurs(); addRow("Auteur", auteur, gbl, c); Panel boutons = new Panel(new GridLayout(1,3)); add = new Button("Ajouter"); autre_auteur = new Button("Auteurs..."); autre_auteur.setEnabled(false); quit = new Button("Quitter"); boutons.add(add); boutons.add(autre_auteur); boutons.add(quit); c.gridwidth = GridBagConstraints.REMAINDER; add(boutons, gbl, c); status = new Label(NOTHING, Label.CENTER); addRow("status", status, gbl, c); this.pack(); // desactivation de autre_auteur... titre.addTextListener(this); editeur.addItemListener(this); annee.addItemListener(this); collection.addItemListener(this); langue.addItemListener(this); auteur.addItemListener(this); add.addActionListener(this); autre_auteur.addActionListener(this); quit.addActionListener(this); } protected void add(Component cp, GridBagLayout gbl, GridBagConstraints gbc) { gbl.setConstraints(cp, gbc); this.add(cp); } protected void addRow(String label, Component cp, GridBagLayout gbl, GridBagConstraints gbc) { gbc.gridwidth = GridBagConstraints.RELATIVE; add(new Label(label, Label.RIGHT), gbl, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; add(cp, gbl, gbc); } public void itemStateChanged(ItemEvent e) { Object src = e.getSource(); if (src!=auteur) autre_auteur.setEnabled(false); status.setText(NOTHING); } public void textValueChanged(TextEvent e) { autre_auteur.setEnabled(false); status.setText(NOTHING); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if (src==add) { try { if (titre.getText().trim().equals("")) throw new Exception("pas de titre !"); db.addOuvrage(titre.getText(), Integer.parseInt(annee.getSelectedItem()), editeur.getSelectedId(), collection.getSelectedId(), auteur.getSelectedId(), langue.getSelectedId()); autre_auteur.setEnabled(true); status.setText("done"); } catch (Exception ex) { System.err.println(ex); status.setText("error"); ex.printStackTrace(); } } // on pourrait aussi re-utiliser le bouton "add" + un etat particulier. else if (src==autre_auteur) { try { if (titre.getText().trim().equals("")) throw new Exception("pas de titre !"); db.addAuteur(titre.getText(), Integer.parseInt(annee.getSelectedItem()), collection.getSelectedId(), auteur.getSelectedId(), langue.getSelectedId()); status.setText("done"); } catch (Exception ex) { System.err.println(ex); status.setText("error"); ex.printStackTrace(); } } else if (src==quit) { // should I close the connection here? try { dbc.close(); } catch (Exception ex) { System.err.println(ex); ex.printStackTrace(); } this.dispose(); System.exit(0); } else { System.err.println("unexpected action source: " + e); } } // TEST static public void main(String[] args) { try { DatabaseChoser cd = new DatabaseChoser(); cd.setVisible(true); if (! cd.isOkay()) throw new Exception("database choice canceled"); // else Connection dbc; // À compléter en exploitant les réponses de l'utilisateur : // - chargement du driver // - initialisation de la Connection cd.dispose(); Frame f = new Comics(dbc); f.setVisible(true); } catch(Exception e) { System.err.println(e); e.printStackTrace(); System.exit(0); } } }