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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package fenetres;
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import objetProduit.GroupeProduit;
import objetProduit.Produit;
public class PanelSaisieProduit {
Color coul2 = new Color(190,234,251);
GroupeProduit gprod;
JFrame FenSaisieProduit;
JPanel OptionProd ;
JPanel ImageProd ;
JPanel jPanelSaisieProduit;
JComboBox categorie;
JComboBox menuImage;
JTextField nameProd,urlprod;
JButton annuleProd,valideProd;
JLabel cadreIMG;
BufferedImage buffImg;
File f;
int indiceOption;
String mois,annee,concatDate;
//////////// ici j'ai essayé de récupérer la variable "jour" de la classe fille via son getter mais ça veut pas :(
String jour = getJour();
// constructeur
public PanelSaisieProduit(GroupeProduit gprod, JFrame fenprincipale,JPanel onglet2) {
this.gprod = gprod;
jPanelSaisieProduit=onglet2;
FenSaisieProduit = fenprincipale;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// JPanel qui va contenir les champs de saisie
jPanelSaisieProduit.setLayout(new GridBagLayout());
jPanelSaisieProduit.setBackground(coul2);
// initialisation des options de produit + menu de selection image sur "vide" (invisible)
OptionProd = new JPanel();
OptionProd.removeAll();
OptionNone su = new OptionNone();
OptionProd.add(su.panelBlank);
OptionProd.revalidate();
OptionProd.setBackground(coul2);
ImageProd = new JPanel();
ImageProd.removeAll();
OptionNone su2 = new OptionNone();
ImageProd.add(su2.panelBlank);
ImageProd.revalidate();
ImageProd.setBackground(coul2);
// composants
JLabel categ = new JLabel("Categorie de produit");
categorie = new JComboBox(new String[]{" ","produit laitier","produit surgele","produit viande"});
JLabel noprod = new JLabel("Nom du produit");
nameProd = new JTextField();
urlprod = new JTextField();
valideProd = new JButton("Valider");
annuleProd = new JButton("Annuler");
// affichage image avec ImageIO.read
f = new File("src/Images/IMGvierge.jpg");
try {
buffImg = ImageIO.read(f);
cadreIMG = new JLabel(new ImageIcon(buffImg) );
}catch (IOException e) {
e.printStackTrace();
}
////////////////////////// contraintes et placement des composants dans la fenetre //////////////////////////////
GridBagConstraints gbc2 = new GridBagConstraints();
// ligne 1 - menu deroulant categorie
gbc2.gridx = gbc2.gridy = 0;
gbc2.gridheight = gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.anchor = GridBagConstraints.FIRST_LINE_START;
gbc2.insets = new Insets(0, 10, 0, 0);
jPanelSaisieProduit.add(categ, gbc2);
gbc2.gridx = 1;
gbc2.gridwidth = GridBagConstraints.REMAINDER;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.insets = new Insets(0, 10, 0, 10);
jPanelSaisieProduit.add(categorie, gbc2);
SelecTypeProduit SelTypeProd = new SelecTypeProduit();
categorie.addActionListener(SelTypeProd);
// ligne 2 - options du produit
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.gridheight = 1;
gbc2.gridwidth = 3;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.NONE;
gbc2.insets = new Insets(0, 0, 0, 0);
jPanelSaisieProduit.add(OptionProd, gbc2);
// ligne 3 - nom du produit
gbc2.gridx = 0;
gbc2.gridy = 2;
gbc2.gridheight = gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.NONE;
gbc2.insets = new Insets(0, 10, 0, 0);
jPanelSaisieProduit.add(noprod, gbc2);
gbc2.gridx = 1;
gbc2.gridwidth = GridBagConstraints.REMAINDER;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.insets = new Insets(5, 10, 0, 10);
jPanelSaisieProduit.add(nameProd, gbc2);
// new ligne 4 - menu image produit
gbc2.gridx = 0;
gbc2.gridy = 3;
gbc2.gridheight = gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.NONE;
gbc2.insets = new Insets(0, 10, 0, 0);
gbc2.gridx = 1;
gbc2.gridwidth = GridBagConstraints.REMAINDER;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.insets = new Insets(5, 10, 0, 10);
jPanelSaisieProduit.add(ImageProd, gbc2);
// SelecImage SelecImage = new SelecImage();
// ImageProd.addActionListener(SelecImage);
// ligne 5 - champs d'affichage de l'image
gbc2.gridx = 0;
gbc2.gridy = 4;
gbc2.gridheight = 1;
gbc2.gridwidth = 3;
gbc2.anchor = GridBagConstraints.BASELINE_LEADING;
gbc2.anchor = GridBagConstraints.LINE_START;
gbc2.insets = new Insets(10, 0, 10, 0);
cadreIMG.setPreferredSize(new Dimension(340,215));
jPanelSaisieProduit.add(cadreIMG, gbc2);
// ligne 6 - Boutons Annule et Valide
gbc2.gridx = 1;
gbc2.gridy = 5;
gbc2.gridheight = gbc2.gridwidth = 1;
gbc2.anchor = GridBagConstraints.BASELINE_TRAILING;
gbc2.weightx = 1.;
gbc2.fill = GridBagConstraints.NONE;
gbc2.insets = new Insets(0, 0, 5, 5);
jPanelSaisieProduit.add(annuleProd, gbc2);
actAnnuler annul = new actAnnuler();
annuleProd.addActionListener(annul);
gbc2.gridx = 2;
gbc2.weightx = 0.;
gbc2.insets = new Insets(5, 0, 10, 10);
jPanelSaisieProduit.add(valideProd, gbc2);
actionNewFicheProd nfprod = new actionNewFicheProd();
valideProd.addActionListener(nfprod);
}
////////////////////////////////////////////////// ECOUTEURS //////////////////////////////////////////////////
// Ecouteur sur le menu "type de produit" qui affiche les options de produit + menu image correspondant
public class SelecTypeProduit implements ActionListener{
public void actionPerformed(ActionEvent e) {
String type = (String) categorie.getSelectedItem();
//System.out.println("le type de produit selectionne est : "+ type)
if (type.equals(" ")){
indiceOption = 0;
OptionProd.removeAll();
OptionNone su = new OptionNone();
OptionProd.add(su.panelBlank);
OptionProd.revalidate();
ImageProd.removeAll();
OptionNone su2 = new OptionNone();
ImageProd.add(su2.panelBlank);
ImageProd.revalidate();
}
if (type.equals("produit laitier")){
indiceOption = 1;
OptionProd.removeAll();
OptionLait p = new OptionLait();
OptionProd.add(p.panelBoxPeremp);
OptionProd.revalidate();
ImageProd.removeAll();
MenuImageLait imLai = new MenuImageLait();
ImageProd.add(imLai.panelBoxImageLait);
ImageProd.revalidate();
}
if (type.equals("produit viande")){
indiceOption = 2;
OptionProd.removeAll();
OptionViande prov = new OptionViande();
OptionProd.add(prov.panelProvenance);
OptionProd.revalidate();
ImageProd.removeAll();
MenuImageViande imLai = new MenuImageViande();
ImageProd.add(imLai.panelBoxImageViande);
ImageProd.revalidate();
}
if (type.equals("produit surgele")){
indiceOption = 0;
OptionProd.removeAll();
OptionNone su = new OptionNone();
OptionProd.add(su.panelBlank);
OptionProd.revalidate();
ImageProd.removeAll();
MenuImageSurgele imSurgel = new MenuImageSurgele();
ImageProd.add(imSurgel.panelBoxImageSurgele);
ImageProd.revalidate();
}
}
}
// ajouter un nouveau produit
public class actionNewFicheProd implements ActionListener{
public void actionPerformed(ActionEvent e) {
/////////// test de validite des infos saisies ////////////
boolean verif = true;
String errorMessage = "";
// pas de Categorie selectionnee
if(((String) categorie.getSelectedItem()).length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : vous l'avez pas selectionne de categorie de produit \n"; }
// pas de nom
if(nameProd.getText().length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : pas de nom de produit \n"; }
// pas d'options
/* if (indiceOption==1){
if(((String) XXXXXXXXX.length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : vous l'avez pas selectionne le jour de peremption \n"; }
if(((String) XXXXXXXXX.length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : vous l'avez pas selectionne le mois de peremption \n"; }
if(((String) XXXXXXXXX.length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : vous l'avez pas selectionne l'annee de peremption \n"; }
}
if (indiceOption==2){
if(xxxxxxxPROVENANCExxxxxx .getText().length()==0){
verif = false;
errorMessage = errorMessage +"ERREUR : pas de provenance \n"; }
}
*/ // pas d'image
if(((String) menuImage.getSelectedItem()).equals(" ")){
verif = false;
errorMessage = errorMessage +"ERREUR : pas d'image selectionnee"; }
// le produit existe deja dans la liste
// Ã* faire quand on aura cree la arraylist des produits
// si toutes les conditions de verifications sont remplies, on cree le produit
if( verif){
creaProduit();
}
else{ JOptionPane.showMessageDialog(null,errorMessage); }
}
}
// creation de l'objet PRODUIT
public void creaProduit(){
if(((String) categorie.getSelectedItem()).equals("produit surgele") || ((String) categorie.getSelectedItem()).equals(" ")){
Produit prd1 = new Produit (
(String) categorie.getSelectedItem(),
nameProd.getText(),
(String) menuImage.getSelectedItem()
);
// ajouts du nouveau produit au GroupeProduit
gprod.ajoute(prd1);
}
if(((String) categorie.getSelectedItem()).equals("produit laitier")){
Produit prd1 = new Produit (
(String) categorie.getSelectedItem(),
nameProd.getText(),
/// récupération de la concatenation date de peremption /////////// A FAIRE ///////////////
(String) menuImage.getSelectedItem()
);
// ajours du nouveau produit au GroupeProduit
gprod.ajoute(prd1);
}
if(((String) categorie.getSelectedItem()).equals("produit viande")){
Produit prd1 = new Produit (
(String) categorie.getSelectedItem(),
nameProd.getText(),
// provenance.getText(), /////////// A FAIRE ///////////////
(String) menuImage.getSelectedItem()
);
// ajours du nouveau produit au GroupeProduit
gprod.ajoute(prd1);
}
// affichage d'une boite de dialogue qui indique que le nouveau produit a bien ete cree
JOptionPane.showMessageDialog(null,
"Nouveau "+(String) categorie.getSelectedItem()+" '"+nameProd.getText()+"' cree avec succes");
FenSaisieProduit.setVisible(false); // fermeture de la fenetre
new Fenetre(null, gprod); // retour a la fenetre d'accueil
}
// Ecouteur bouton annule = retour a la fenetre de depart
public class actAnnuler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("annule et retour a l'accueil");
FenSaisieProduit.setVisible(false); // fermeture de la fenetre de saisie
new Fenetre(null, gprod);
}
// test sur les images
// ...... j'ai coupé pour alléger......
} |
Partager