/**
* 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.ListsProxy;
import com.ledruide.druidecave.business.VigneronProxy;
import com.ledruide.druidecave.business.lists.CountryProxy;
import com.ledruide.druidecave.dao.Vigneron;
import com.ledruide.druidecave.helpers.I18nScreens;
import com.ledruide.druidegui.Stop;
import com.ledruide.druidegui.SwapFrame;
import com.ledruide.druidegui.helpers.TextTools;
import com.ledruide.druidegui.tools.ComponentTools;
import com.ledruide.druidegui.tools.DruideJPanel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.List;
public class VigneronDetailRegions extends DruideJPanel
{
JLabel labelTitle = new JLabel();
JButton buttonQuit = new JButton();
JButton buttonHome = new JButton();
JLabel labelName = new JLabel();
/* *** NE PAS SUPPRIMER *** */
DefaultListModel modelExiste = new DefaultListModel();
DefaultListModel modelRecoltant = new DefaultListModel();
JList listRegionsAll = new JList(modelExiste);
JList listRegionsVigneron = new JList(modelRecoltant);
/* *** NE PAS SUPPRIMER *** */
JLabel labelRegionsAll = new JLabel();
JLabel labelRegionsVigneron = new JLabel();
JLabel labelCountry = new JLabel();
JButton buttonRemove = new JButton();
JButton buttonAdd = new JButton();
JButton buttonBack = new JButton();
JButton buttonFinish = new JButton();
JTextField fieldName = new JTextField();
JComboBox comboCountry = new JComboBox();
/**
* Default constructor
*/
public VigneronDetailRegions()
{
initializePanel();
}
public JPanel createPanel()
{
JPanel jpanel1 = new JPanel();
FormLayout formlayout1 = new FormLayout("FILL:10PX:NONE,FILL:75PX:NONE,FILL:37PX:NONE,FILL:37PX:NONE,FILL:37PX:NONE,FILL:37PX:NONE,FILL:DEFAULT:NONE,FILL:53PX:NONE,FILL:14PX:NONE,FILL:37PX:NONE,FILL:37PX:NONE,FILL:37PX:NONE,FILL:37PX: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: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:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
CellConstraints cc = new CellConstraints();
jpanel1.setLayout(formlayout1);
labelTitle.setFont(new Font("Tahoma",Font.BOLD,11));
labelTitle.setForeground(new Color(204,0,51));
labelTitle.setName("labelTitle");
labelTitle.setText("screen.vigneron.detail.regions.title");
labelTitle.setHorizontalAlignment(JLabel.CENTER);
jpanel1.add(labelTitle,cc.xywh(3,1,11,2));
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(14,1));
labelName.setName("labelName");
labelName.setText("screen.vigneron.detail.regions.name");
labelName.setHorizontalAlignment(JLabel.RIGHT);
jpanel1.add(labelName,cc.xywh(3,4,4,1));
listRegionsAll.setName("listRegionsAll");
JScrollPane jscrollpane1 = new JScrollPane();
jscrollpane1.setViewportView(listRegionsAll);
jscrollpane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jscrollpane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpanel1.add(jscrollpane1,cc.xywh(2,10,5,17));
listRegionsVigneron.setName("listRegionsVigneron");
JScrollPane jscrollpane2 = new JScrollPane();
jscrollpane2.setViewportView(listRegionsVigneron);
jscrollpane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jscrollpane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpanel1.add(jscrollpane2,cc.xywh(10,10,5,17));
labelRegionsAll.setFont(new Font("Tahoma",Font.BOLD,11));
labelRegionsAll.setName("labelRegionsAll");
labelRegionsAll.setText("screen.vigneron.detail.regions.list.all");
labelRegionsAll.setHorizontalAlignment(JLabel.CENTER);
jpanel1.add(labelRegionsAll,cc.xywh(2,8,4,1));
labelRegionsVigneron.setFont(new Font("Tahoma",Font.BOLD,11));
labelRegionsVigneron.setName("labelRegionsVigneron");
labelRegionsVigneron.setText("screen.vigneron.detail.regions.list.vigneron");
labelRegionsVigneron.setHorizontalAlignment(JLabel.CENTER);
jpanel1.add(labelRegionsVigneron,cc.xywh(10,8,5,1));
labelCountry.setName("labelCountry");
labelCountry.setText("screen.vigneron.detail.regions.country");
labelCountry.setHorizontalAlignment(JLabel.RIGHT);
jpanel1.add(labelCountry,cc.xywh(3,6,4,1));
buttonRemove.setIcon(loadImage("img/gauche.gif"));
buttonRemove.setName("buttonRemove");
buttonRemove.setOpaque(false);
buttonRemove.setToolTipText("Retirer de la liste...");
jpanel1.add(buttonRemove,cc.xy(8,18));
buttonAdd.setIcon(loadImage("img/droite.gif"));
buttonAdd.setName("buttonAdd");
buttonAdd.setOpaque(false);
buttonAdd.setToolTipText("Ajouter � la liste...");
jpanel1.add(buttonAdd,cc.xy(8,16));
buttonBack.setActionCommand("Valider");
buttonBack.setFont(new Font("Tahoma",Font.BOLD,11));
buttonBack.setName("buttonBack");
buttonBack.setOpaque(false);
buttonBack.setText("screen.vigneron.detail.regions.button.back");
jpanel1.add(buttonBack,cc.xywh(2,28,5,1));
buttonFinish.setActionCommand("Valider");
buttonFinish.setFont(new Font("Tahoma",Font.BOLD,11));
buttonFinish.setName("buttonFinish");
buttonFinish.setOpaque(false);
buttonFinish.setText("screen.vigneron.detail.regions.button.finish");
jpanel1.add(buttonFinish,cc.xywh(10,28,5,1));
fieldName.setBackground(new Color(240,245,250));
fieldName.setEditable(false);
fieldName.setName("fieldName");
jpanel1.add(fieldName,cc.xywh(7,4,7,1));
comboCountry.setName("comboCountry");
jpanel1.add(comboCountry,cc.xywh(7,6,7,1));
addFillComponents(jpanel1,new int[]{ 1,4,5,6,7,8,9,10,11,12,13,15 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 });
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);
buttonAdd.addActionListener(this);
buttonRemove.addActionListener(this);
buttonFinish.addActionListener(this);
buttonBack.addActionListener(this);
comboCountry.addActionListener(this);
}
public void initI18n() {
buttonQuit.setText(I18nScreens.getInstance().get(buttonQuit.getText()));
buttonHome.setText(I18nScreens.getInstance().get(buttonHome.getText()));
buttonBack.setText(I18nScreens.getInstance().get(buttonBack.getText()));
buttonRemove.setText(I18nScreens.getInstance().get(buttonRemove.getText()));
buttonFinish.setText(I18nScreens.getInstance().get(buttonFinish.getText()));
buttonAdd.setText(I18nScreens.getInstance().get(buttonAdd.getText()));
labelTitle.setText(I18nScreens.getInstance().get(labelTitle.getText()));
labelName.setText(I18nScreens.getInstance().get(labelName.getText()));
labelRegionsAll.setText(I18nScreens.getInstance().get(labelRegionsAll.getText()));
labelRegionsVigneron.setText(I18nScreens.getInstance().get(labelRegionsVigneron.getText()));
labelCountry.setText(I18nScreens.getInstance().get(labelCountry.getText()));
}
public void initEach() throws Exception {
comboCountry.removeAllItems();
CombosProxy.add(comboCountry, CountryProxy.getKeyValueList(), true);
modelExiste.removeAllElements();
modelRecoltant.removeAllElements();
Vigneron recoltant = VigneronProxy.getSessionRecoltantFromID();
fieldName.setText(recoltant.getFirstname() + " " + recoltant.getLastname());
// Nous allons charger la liste des r�gion du r�coltant.
List vignes = VigneronProxy.getRegions(recoltant.getId());
ListsProxy.ajoutRegions(modelRecoltant, vignes);
// On se place par d�faut sur le pays du r�coltant
// ce qui charge automatiquement la liste des r�gions :)
ComponentTools.positionnerCombo(comboCountry, recoltant.getCountry_id());
}
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)) {
// Il faut arriver � trier automatiquement la JList
Object[] valeurs = listRegionsAll.getSelectedValues();
for (int i = 0; i < valeurs.length; i++) {
Object valeur = valeurs[i];
// On ajoute l'�l�ment que s'il n'existe pas d�j�.
//noinspection PointlessBooleanExpression
if (modelRecoltant.contains(valeur) == false) {
modelRecoltant.addElement(valeur);
modelExiste.removeElement(valeur);
}
}
}
else if (e.getSource().equals(buttonRemove)) {
Object[] valeurs = listRegionsVigneron.getSelectedValues();
for (int i = 0; i < valeurs.length; i++) {
Object valeur = valeurs[i];
// On retire l'�l�ment qu'il existe ou non de l'autre c�t� !
modelRecoltant.removeElement(valeur);
// ensuite on l'ajoute s'il n'existe pas...
if (!modelExiste.contains(valeur)) {
modelExiste.addElement(valeur);
}
}
}
else if (e.getSource().equals(comboCountry)) {
// ici on met � jour les r�gions
String pays_id = ComponentTools.getClefFromCombo(comboCountry);
modelExiste.removeAllElements();
if (!TextTools.isNullOrEmpty(pays_id)) {
try {
ListsProxy.ajoutRegionInexistante(modelExiste, modelRecoltant, ComponentTools.getClefFromCombo(comboCountry));
//ListsProxy.ajoutRegionDansJList(modelExiste, ComponentTools.getClefFromCombo(comboPays));
// KLOUGY : Il faut ensuite supprimer de la 1ere combo les choses qui sont d�j� dans la seconde...
} catch (Exception ex) { ex.printStackTrace(); }
}
}
else if (e.getSource().equals(buttonBack)) {
enregistrer(VigneronDetail.class);
}
else if (e.getSource().equals(buttonFinish)) {
// On repart vers la page de recherche des r�coltants ?
enregistrer(VigneronSearch.class);
}
else {
System.out.println("Ce boutton n'existe pas !");
}
}
private void enregistrer(Class sortie) {
try {
Vigneron recoltant = VigneronProxy.getSessionRecoltantFromID();
if (VigneronProxy.ajoutRegions(recoltant.getId(), modelRecoltant.elements())) {
SwapFrame.getInstance().init(sortie);
}
else {
System.out.println("Impossible d'enregistrer ce r�coltant.");
}
}
catch (Exception e1) {
System.out.println("Impossible d'enregistrer ce r�coltant : ");
e1.printStackTrace();
}
}
public int getPanelHeight() {
return 420;
}
}