Package com.ledruide.druidecave.gui

Source Code of com.ledruide.druidecave.gui.BottleSearch

/**
* Copyright (C) 2007 Julien Revault d'Allonnes
*
* This file is part of DruideCave.
*
* DruideCave is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DruideCave is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DruideCave; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.ledruide.druidecave.gui;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.ledruide.druidecave.business.CombosProxy;
import com.ledruide.druidecave.business.lists.ColorProxy;
import com.ledruide.druidecave.business.lists.CountryProxy;
import com.ledruide.druidecave.business.lists.DenominationProxy;
import com.ledruide.druidecave.business.lists.FormatProxy;
import com.ledruide.druidecave.business.lists.RegionProxy;
import com.ledruide.druidecave.dao.Bottle;
import com.ledruide.druidecave.dao.BottlePeer;
import com.ledruide.druidecave.helpers.I18nScreens;
import com.ledruide.druidecave.model.TableBouteilles;
import com.ledruide.druidecave.model.TableSorter;
import com.ledruide.druidegui.Stop;
import com.ledruide.druidegui.SwapFrame;
import com.ledruide.druidegui.helpers.Constants;
import com.ledruide.druidegui.helpers.Session;
import com.ledruide.druidegui.helpers.TextTools;
import com.ledruide.druidegui.tools.ComponentTools;
import com.ledruide.druidegui.tools.DruideJPanel;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;


public class BottleSearch extends DruideJPanel
{
   JLabel labelTitle = new JLabel();
   JLabel labelCountry = new JLabel();
   JComboBox comboCountry = new JComboBox();
   JLabel labelRegion = new JLabel();
   JComboBox comboRegion = new JComboBox();
   JButton buttonQuit = new JButton();
   JButton buttonHome = new JButton();
   JLabel labelDenomination = new JLabel();
   JComboBox comboDenomination = new JComboBox();
   JLabel labelYear = new JLabel();
   JTextField fieldYear = new JTextField();
   JLabel labelColor = new JLabel();
   JComboBox comboColor = new JComboBox();
   JLabel labelFormat = new JLabel();
   JComboBox comboFormat = new JComboBox();
   JLabel labelName = new JLabel();
   JTextField fieldName = new JTextField();
   JLabel labelAwakening = new JLabel();
   JTextField fieldAwakening = new JTextField();
   JButton buttonDetail = new JButton();
   JButton buttonAdd = new JButton();

    // ****************************************/
    // **                                    **/
    // FAIRE ATTENTION ICI AVEC LE MERGE
    TableBouteilles tableModel = new TableBouteilles();
    TableSorter sorter = new TableSorter(tableModel);
    JTable tableResultats = new JTable(sorter);
    JScrollPane resultatsScrollPane = new JScrollPane();
    // **                                    **/
    // ****************************************/

   /**
    * Default constructor
    */
   public BottleSearch()
   {
      initializePanel();
   }

   public JPanel createPanel()
   {
      JPanel jpanel1 = new JPanel();
      FormLayout formlayout1 = new FormLayout("FILL:10PX:NONE,FILL:75PX:NONE,FILL:35PX:NONE,FILL:12PX:NONE,FILL:62PX:NONE,FILL:35PX:NONE,FILL:12PX:NONE,FILL:28PX:NONE,FILL:68PX:NONE,FILL:25PX:NONE,FILL:54PX:NONE,FILL:40PX:NONE,FILL:75PX:NONE,FILL:10PX:NONE","CENTER:17PX:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:200PX:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
      CellConstraints cc = new CellConstraints();
      jpanel1.setLayout(formlayout1);

      labelTitle.setFont(new Font("Tahoma", Font.BOLD,12));
      labelTitle.setForeground(new Color(204,0,51));
      labelTitle.setName("labelTitle");
      labelTitle.setText("screen.bottle.search.title");
      labelTitle.setHorizontalAlignment(JLabel.CENTER);
      jpanel1.add(labelTitle,cc.xywh(3,1,10,2));

      labelCountry.setName("labelCountry");
      labelCountry.setText("screen.bottle.search.country");
      labelCountry.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelCountry,cc.xy(2,5));

      comboCountry.setName("comboCountry");
      jpanel1.add(comboCountry,cc.xywh(3,5,6,1));

      labelRegion.setName("labelRegion");
      labelRegion.setText("screen.bottle.search.region");
      labelRegion.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelRegion,cc.xy(9,5));

      comboRegion.setName("comboRegion");
      jpanel1.add(comboRegion,cc.xywh(10,5,4,1));

      buttonQuit.setActionCommand("Quitter");
      buttonQuit.setFont(new Font("Dialog",Font.BOLD,10));
      buttonQuit.setName("buttonQuit");
      buttonQuit.setOpaque(false);
      buttonQuit.setText("screen.button.quit");
      jpanel1.add(buttonQuit,cc.xy(2,1));

      buttonHome.setActionCommand("Quitter");
      buttonHome.setFont(new Font("Dialog",Font.BOLD,10));
      buttonHome.setName("buttonHome");
      buttonHome.setOpaque(false);
      buttonHome.setText("screen.button.home");
      jpanel1.add(buttonHome,cc.xy(13,1));

      labelDenomination.setName("labelDenomination");
      labelDenomination.setText("screen.bottle.search.denomination");
      labelDenomination.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelDenomination,cc.xy(2,7));

      comboDenomination.setName("comboDenomination");
      jpanel1.add(comboDenomination,cc.xywh(3,7,7,1));

      labelYear.setName("labelYear");
      labelYear.setText("screen.bottle.search.year");
      labelYear.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelYear,cc.xywh(11,7,2,1));

      fieldYear.setName("fieldYear");
      jpanel1.add(fieldYear,cc.xy(13,7));

      labelColor.setName("labelColor");
      labelColor.setText("screen.bottle.search.color");
      labelColor.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelColor,cc.xy(2,9));

      comboColor.setName("comboColor");
      jpanel1.add(comboColor,cc.xywh(3,9,6,1));

      labelFormat.setName("labelFormat");
      labelFormat.setText("screen.bottle.search.format");
      labelFormat.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelFormat,cc.xy(9,9));

      comboFormat.setName("comboFormat");
      jpanel1.add(comboFormat,cc.xywh(10,9,4,1));

      labelName.setName("labelName");
      labelName.setText("screen.bottle.search.name");
      labelName.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelName,cc.xy(2,11));

      fieldName.setName("fieldName");
      jpanel1.add(fieldName,cc.xywh(3,11,8,1));

      labelAwakening.setName("labelAwakening");
      labelAwakening.setText("screen.bottle.search.awakening");
      labelAwakening.setHorizontalAlignment(JLabel.RIGHT);
      jpanel1.add(labelAwakening,cc.xywh(11,11,2,1));

      fieldAwakening.setName("fieldAwakening");
      jpanel1.add(fieldAwakening,cc.xy(13,11));

      buttonDetail.setActionCommand("Acc�der � la fiche");
      buttonDetail.setFont(new Font("Tahoma",Font.BOLD,11));
      buttonDetail.setName("buttonDetail");
      buttonDetail.setOpaque(false);
      buttonDetail.setText("screen.bottle.search.button.detail");
      jpanel1.add(buttonDetail,cc.xywh(11,15,3,1));

       /* ******************************* */
       /* ATTENTION AVEC LE MERGE !       */
       tableResultats.setName("tableResultats");
       tableResultats.setRowMargin(2);
       tableResultats.setCellSelectionEnabled(false);
       tableResultats.setColumnSelectionAllowed(false);
       tableResultats.setRowSelectionAllowed(true);
       resultatsScrollPane.setViewportView(tableResultats);
       resultatsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
       resultatsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
       jpanel1.add(resultatsScrollPane,cc.xywh(2,13,12,1));
       /* ******************************* */
      JScrollPane jscrollpane1 = new JScrollPane();
      jscrollpane1.setViewportView(tableResultats);
      jscrollpane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      jscrollpane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      jpanel1.add(jscrollpane1,cc.xywh(2,13,12,1));

      buttonAdd.setActionCommand("Ajouter des bouteilles");
      buttonAdd.setFont(new Font("Tahoma",Font.BOLD,11));
      buttonAdd.setName("buttonAdd");
      buttonAdd.setText("screen.bottle.search.button.add");
      jpanel1.add(buttonAdd,cc.xywh(2,15,4,1));

      addFillComponents(jpanel1,new int[]{ 1,4,5,6,7,8,9,10,11,12,14 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 });
      return jpanel1;
   }

   /**
    * Initializer
    */
   protected void initializePanel()
   {
      setLayout(new BorderLayout());
      add(createPanel(), BorderLayout.CENTER);
   }

    /* _____________________________________________________________________
      |                                                                     |
      |             IMPLEMENTATION DES METHODES QUI PERMETTENT              |
      |         DE S'INSERER FACILEMENT DANS L'ARCHITECTURE GLOBALE         |
      |_____________________________________________________________________|
    */
    public void initFirst() throws Exception {
        buttonQuit.addActionListener(this);
        buttonHome.addActionListener(this);
        buttonDetail.addActionListener(this);
        buttonAdd.addActionListener(this);

        comboCountry.addActionListener(this);
        comboRegion.addActionListener(this);
        comboDenomination.addActionListener(this);
        comboFormat.addActionListener(this);
        comboColor.addActionListener(this);

        fieldName.addKeyListener(this);
        fieldYear.addKeyListener(this);
        fieldAwakening.addKeyListener(this);

    }

    public void initI18n() {
        labelTitle.setText(I18nScreens.getInstance().get(labelTitle.getText()));
        buttonQuit.setText(I18nScreens.getInstance().get(buttonQuit.getText()));
        buttonHome.setText(I18nScreens.getInstance().get(buttonHome.getText()));
        labelCountry.setText(I18nScreens.getInstance().get(labelCountry.getText()));
        labelRegion.setText(I18nScreens.getInstance().get(labelRegion.getText()));
        labelDenomination.setText(I18nScreens.getInstance().get(labelDenomination.getText()));
        labelName.setText(I18nScreens.getInstance().get(labelName.getText()));
        labelYear.setText(I18nScreens.getInstance().get(labelYear.getText()));
        labelAwakening.setText(I18nScreens.getInstance().get(labelAwakening.getText()));
        labelColor.setText(I18nScreens.getInstance().get(labelColor.getText()));
        labelFormat.setText(I18nScreens.getInstance().get(labelFormat.getText()));
        buttonAdd.setText(I18nScreens.getInstance().get(buttonAdd.getText()));
        buttonDetail.setText(I18nScreens.getInstance().get(buttonDetail.getText()));
    }


    public void initEach() throws Exception {
        Session.getInstance().removeAttribute("BOUTEILLE_ID");

        comboCountry.removeAllItems();
        comboRegion.removeAllItems();
        comboDenomination.removeAllItems();
        comboFormat.removeAllItems();
        comboColor.removeAllItems();

        CombosProxy.add(comboCountry, CountryProxy.getKeyValueList(), true);
        CombosProxy.add(comboFormat, FormatProxy.getKeyValueList(), true);
        CombosProxy.add(comboColor, ColorProxy.getKeyValueList(), true);
        comboCountry.setSelectedIndex(0);
        comboRegion.setSelectedIndex(0);
        comboDenomination.setSelectedIndex(0);
        comboFormat.setSelectedIndex(0);
        comboColor.setSelectedIndex(0);

        fieldName.setText(Constants.BLANK);
        fieldYear.setText(Constants.BLANK);
        fieldAwakening.setText(Constants.BLANK);

        tableModel.vider();
        // on fait ue recherche afin de pr�afficher tous les r�sultats...
        recherche();
    }

    public void keyReleased(KeyEvent e) {
        if (e.getSource().equals(fieldName)
         || e.getSource().equals(fieldYear)
         || e.getSource().equals(fieldAwakening)) {
            recherche();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(buttonQuit)) {
            Stop.close();
        }
        else if (e.getSource().equals(buttonHome)) {
            SwapFrame.getInstance().init(Home.class);
        }
        else if (e.getSource().equals(buttonAdd)) {
            SwapFrame.getInstance().init(BottleAdd.class);
        }
        else if (e.getSource().equals(comboCountry)) {
            // La recherche est effectu�e car pays met � jour les r�gions
            // qui met � jour les appellations qui fait la recherche

            // ici on met � jour les r�gions
            try {
                comboRegion.removeAllItems();
                CombosProxy.add(comboRegion, RegionProxy.getKeyValueList(ComponentTools.getClefFromCombo(comboCountry)), true);
            } catch (Exception ex) { ex.printStackTrace(); }
        }
        else if (e.getSource().equals(comboRegion)) {
            // La recherche est effectu�e car r�gion met � jour les
            // appellations qui fait la recherche

            // ici on met � jour les appellations
            try {
                comboDenomination.removeAllItems();
                CombosProxy.add(comboDenomination, DenominationProxy.getKeyValueList(ComponentTools.getClefFromCombo(comboCountry), ComponentTools.getClefFromCombo(comboRegion)), true);
            } catch (Exception ex) { ex.printStackTrace(); }
        }
        else if (e.getSource().equals(comboDenomination)
              || e.getSource().equals(comboColor)
              || e.getSource().equals(comboFormat)) {
            recherche();
        }
        else if (e.getSource().equals(buttonDetail)) {
            // ici on part sur le d�tail d'une bouteille
            Bottle bout = (Bottle) tableModel.getSelection(tableResultats.getSelectedRow());
            if (bout != null) {
                Session.getInstance().setAttribute("BOUTEILLE_ID", bout.getId());
                SwapFrame.getInstance().init(BottleDetail.class);
            }
        }
        else {
            System.out.println("Ce boutton n'existe pas : " + e.getSource().getClass().toString());
        }
    }

    private void recherche() {
        // On commence par vider le tableau des r�sultats
        tableModel.vider();

        String lePays   = ComponentTools.getClefFromCombo(comboCountry);
        String laRegion = ComponentTools.getClefFromCombo(comboRegion);
        String lAppellation = ComponentTools.getClefFromCombo(comboDenomination);
        String leFormat = ComponentTools.getClefFromCombo(comboFormat);
        String leType   = ComponentTools.getClefFromCombo(comboColor);
        String leNom  = fieldName.getText();
        String lAnnee = fieldYear.getText();
        String lEveil = fieldAwakening.getText();

        Bottle recherche = new Bottle();
        // Le nom n'est pas ajout� car on en fait un startWith
        if (!TextTools.isNullOrEmpty(lePays))   recherche.setCountry_id(lePays);
        if (!TextTools.isNullOrEmpty(laRegion)) recherche.setRegion_id(laRegion);
        if (!TextTools.isNullOrEmpty(lAppellation)) recherche.setDenomination_id(lAppellation);
        if (!TextTools.isNullOrEmpty(leFormat)) recherche.setFormat_id(leFormat);
        if (!TextTools.isNullOrEmpty(leType)) recherche.setColor_id(leType);
        if (!TextTools.isNullOrEmpty(lAnnee)) recherche.setYear(lAnnee);
        if (!TextTools.isNullOrEmpty(lEveil)) recherche.setAwakening(lEveil);

        try {
            List results = BottlePeer.find(recherche);
            // Si le nom pass� en argument n'est pas nul ou vide
            if (!TextTools.isNullOrEmpty(leNom)) {
                // Nous faisons une recherche sur chaque �l�ment qui peut contenir
                // une partie du nom pass� en argument
                Iterator liste = results.iterator();
                while (liste.hasNext()) {
                    Bottle bout = (Bottle) liste.next();
                    if (bout != null && bout.getName() != null) {
                        if (bout.getName().indexOf(leNom) == -1) {
                            liste.remove();
                        }
                    }
                }
            }
            tableModel.addRows(results);
        }
        catch (Exception e1) {
            System.out.println("Impossible de trouver les bouteilles li�e � cette recherche : " + recherche.toString());
            e1.printStackTrace();
        }
    }

    public int getPanelHeight() {
        return 450;
    }
}
TOP

Related Classes of com.ledruide.druidecave.gui.BottleSearch

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.