/Users/e1818782/NetBeansProjects/IA Stuff/RecipeDatabase_Nicolas/src/nicolas/IngredientsBuilder.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package nicolas;
  7 
  8 import javax.swing.JOptionPane;
  9 import java.awt.BorderLayout;
 10 import java.util.ArrayList;
 11 import javax.swing.*;
 12 import javax.swing.event.DocumentEvent;
 13 import javax.swing.event.DocumentListener;
 14 /**
 15  *
 16  * @author 18782
 17  */
 18 public class IngredientsBuilder extends javax.swing.JFrame {
 19    
 20     /**
 21      * Creates new form IngredientsBuilder
 22      */
 23     public IngredientsBuilder() {
 24         initComponents();
 25         for(int c=0; c<ingList.getModel().getSize();c++)
 26         {listModel.add(c, ingList.getModel().getElementAt(c));}//Creates new list model to be manipulated
 27     }//end of constructor
 28     
 29     public IngredientsBuilder( int pos, ArrayList<Recipe> recipeList)
 30     {
 31      //pos=get latest   
 32         this.recipeList=recipeList;//same pointer as the GUIproto class
 33       
 34         this.pos=pos;
 35      
 36         initComponents();
 37           for(int c=0; c<ingList.getModel().getSize();c++){
 38         listModel.add(c, ingList.getModel().getElementAt(c));//create new list model to be manipulated
 39        }
 40           ingList.setModel(listModel);
 41         if(!recipeList.get(pos).getIng().isEmpty())
 42         {
 43             for(int i=0; i<recipeList.get(pos).getIng().size(); i++)
 44             {       
 45              ingToString= recipeList.get(pos).getIng().get(i).getIngName()//Reformat the ing
 46             +"    "+recipeList.get(pos).getIng().get(i).getQuantity()
 47             +recipeList.get(pos).getIng().get(i).getUnit();
 48            
 49              
 50              ingModel.add(i,ingToString );
 51             }
 52             listofIngs.setModel(ingModel);
 53         }
 54     }
 55 
 56 public void refreshArea(){
 57     
 58          
 59              ingToString= recipeList.get(pos).getIng().get(ingCount).getIngName()
 60             +"    "+recipeList.get(pos).getIng().get(ingCount).getQuantity()
 61             +recipeList.get(pos).getIng().get(ingCount).getUnit();
 62          
 63              
 64              ingModel.add(ingCount,ingToString );
 65         
 66         // setModel.add(recipeList.get(recipeList.size()-1).getIng().size()-1, );
 67         //Uopdate the model for the ingredients list so as to add them to the ingredients list
 68 
 69         listofIngs.setModel(ingModel);
 70      
 71      
 72    // super.setVisible(false);
 73     
 74             
 75 }
 76 
 77 
 78     /**
 79      * This method is called from within the constructor to initialize the form.
 80      * WARNING: Do NOT modify this code. The content of this method is always
 81      * regenerated by the Form Editor.
 82      */
 83     @SuppressWarnings("unchecked")
 84     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
 85     private void initComponents() {
 86 
 87         unitButs = new javax.swing.ButtonGroup();
 88         ingScroll = new javax.swing.JScrollPane();
 89         ingList = new javax.swing.JList<>();
 90         quantityLab = new javax.swing.JLabel();
 91         cupsBut = new javax.swing.JRadioButton();
 92         gramsBut = new javax.swing.JRadioButton();
 93         teaspoonBut = new javax.swing.JRadioButton();
 94         mlBut = new javax.swing.JRadioButton();
 95         tablespoonBut = new javax.swing.JRadioButton();
 96         quantityField = new javax.swing.JTextField();
 97         removeIng = new javax.swing.JButton();
 98         addIng = new javax.swing.JButton();
 99         jScrollPane1 = new javax.swing.JScrollPane();
100         listofIngs = new javax.swing.JList<>();
101         saveBut = new javax.swing.JButton();
102         jLabel1 = new javax.swing.JLabel();
103         costField = new javax.swing.JTextField();
104 
105         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
106 
107         ingList.setModel(new javax.swing.AbstractListModel<String>() {
108             String[] strings = { "Margarine", "Butter", "Oil", "Cocoa", "Eggs", "Sugar", "Confectioners sugar", "Brown Sugar", "Flour", "Baking Powder", "Baking Soda", "Vanilla", "Salt", "Cinnamon", "Chocolate Chips Dark", "Chocolate Chips white", "Oatmeal", "Cream Cheese", "Sweet Condensed Milk", "Heavy Cream", "Apple", "Carrot", "Lime", "Lemon", " " };
109             public int getSize() { return strings.length; }
110             public String getElementAt(int i) { return strings[i]; }
111         });
112         ingScroll.setViewportView(ingList);
113 
114         quantityLab.setText("Quantity: ");
115 
116         unitButs.add(cupsBut);
117         cupsBut.setText("Cups");
118         cupsBut.addActionListener(new java.awt.event.ActionListener() {
119             public void actionPerformed(java.awt.event.ActionEvent evt) {
120                 cupsButActionPerformed(evt);
121             }
122         });
123 
124         unitButs.add(gramsBut);
125         gramsBut.setText("Grams");
126         gramsBut.addActionListener(new java.awt.event.ActionListener() {
127             public void actionPerformed(java.awt.event.ActionEvent evt) {
128                 gramsButActionPerformed(evt);
129             }
130         });
131 
132         unitButs.add(teaspoonBut);
133         teaspoonBut.setText("Teaspoon");
134         teaspoonBut.addActionListener(new java.awt.event.ActionListener() {
135             public void actionPerformed(java.awt.event.ActionEvent evt) {
136                 teaspoonButActionPerformed(evt);
137             }
138         });
139 
140         unitButs.add(mlBut);
141         mlBut.setText("mL");
142         mlBut.addActionListener(new java.awt.event.ActionListener() {
143             public void actionPerformed(java.awt.event.ActionEvent evt) {
144                 mlButActionPerformed(evt);
145             }
146         });
147 
148         unitButs.add(tablespoonBut);
149         tablespoonBut.setText("Tablespoon");
150         tablespoonBut.addActionListener(new java.awt.event.ActionListener() {
151             public void actionPerformed(java.awt.event.ActionEvent evt) {
152                 tablespoonButActionPerformed(evt);
153             }
154         });
155 
156         quantityField.addActionListener(new java.awt.event.ActionListener() {
157             public void actionPerformed(java.awt.event.ActionEvent evt) {
158                 quantityFieldActionPerformed(evt);
159             }
160         });
161 
162         removeIng.setText("Remove");
163         removeIng.addActionListener(new java.awt.event.ActionListener() {
164             public void actionPerformed(java.awt.event.ActionEvent evt) {
165                 removeIngActionPerformed(evt);
166             }
167         });
168 
169         addIng.setText("Add");
170         addIng.addActionListener(new java.awt.event.ActionListener() {
171             public void actionPerformed(java.awt.event.ActionEvent evt) {
172                 addIngActionPerformed(evt);
173             }
174         });
175 
176         jScrollPane1.setViewportView(listofIngs);
177 
178         saveBut.setText("Save");
179         saveBut.addActionListener(new java.awt.event.ActionListener() {
180             public void actionPerformed(java.awt.event.ActionEvent evt) {
181                 saveButActionPerformed(evt);
182             }
183         });
184 
185         jLabel1.setText("Price/Unit: ");
186 
187         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
188         getContentPane().setLayout(layout);
189         layout.setHorizontalGroup(
190             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
191             .addGroup(layout.createSequentialGroup()
192                 .addContainerGap()
193                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
194                     .addComponent(jScrollPane1)
195                     .addComponent(ingScroll))
196                 .addGap(18, 18, 18)
197                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
198                     .addGroup(layout.createSequentialGroup()
199                         .addComponent(quantityLab)
200                         .addGap(18, 18, 18)
201                         .addComponent(quantityField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
202                     .addGroup(layout.createSequentialGroup()
203                         .addComponent(gramsBut)
204                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
205                         .addComponent(teaspoonBut))
206                     .addComponent(addIng)
207                     .addGroup(layout.createSequentialGroup()
208                         .addComponent(removeIng)
209                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
210                         .addComponent(saveBut))
211                     .addGroup(layout.createSequentialGroup()
212                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
213                             .addComponent(cupsBut)
214                             .addComponent(jLabel1))
215                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
216                             .addGroup(layout.createSequentialGroup()
217                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
218                                 .addComponent(mlBut)
219                                 .addGap(18, 18, 18)
220                                 .addComponent(tablespoonBut))
221                             .addGroup(layout.createSequentialGroup()
222                                 .addGap(16, 16, 16)
223                                 .addComponent(costField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))))
224                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
225         );
226         layout.setVerticalGroup(
227             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
228             .addGroup(layout.createSequentialGroup()
229                 .addContainerGap()
230                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
231                     .addGroup(layout.createSequentialGroup()
232                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
233                             .addComponent(quantityLab)
234                             .addComponent(quantityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
235                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
236                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
237                             .addComponent(jLabel1)
238                             .addComponent(costField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
239                         .addGap(18, 18, 18)
240                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
241                             .addComponent(cupsBut)
242                             .addComponent(mlBut)
243                             .addComponent(tablespoonBut))
244                         .addGap(14, 14, 14)
245                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
246                             .addComponent(gramsBut)
247                             .addComponent(teaspoonBut))
248                         .addGap(18, 18, 18)
249                         .addComponent(addIng))
250                     .addComponent(ingScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
251                 .addGap(18, 18, 18)
252                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
253                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
254                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
255                         .addComponent(removeIng)
256                         .addComponent(saveBut)))
257                 .addContainerGap(16, Short.MAX_VALUE))
258         );
259 
260         pack();
261     }// </editor-fold>                        
262 
263     private void removeIngActionPerformed(java.awt.event.ActionEvent evt) {                                          
264 if (!listofIngs.isSelectionEmpty()){
265           ing.remove(listofIngs.getSelectedIndex());
266         ingModel.remove(listofIngs.getSelectedIndex());
267   
268        
269            recipeList.get(pos).setIng(ing);//reset the list of ings in recipe list
270 }
271 else
272     JOptionPane.showMessageDialog(null, "Select an Ingredient to remove");
273 
274 
275         // TODO add your handling code here:
276     }                                         
277 
278     private void addIngActionPerformed(java.awt.event.ActionEvent evt) {                                       
279     //    ref();
280     if(quantityField.getText().matches("[a-zA-Z]+")&&costField.getText().matches("[a-zA-Z]+")) {///erro trap no strings in doubles
281         JOptionPane.showMessageDialog(null, "Input only numbers please");
282     quantityField.setText("");   
283 }
284     else{
285     ing.add(new Ingredients());
286     ing.get(ingCount).setQuantity(Double.parseDouble(quantityField.getText()));
287     ing.get(ingCount).setName(ingList.getModel().getElementAt(ingList.getSelectedIndex()));
288     ing.get(ingCount).setCost(Double.parseDouble(costField.getText()));
289     //buttons: 0:grams, 1:cups 2:teaspoon 3:tablespoon 4:ml
290     String unit="";
291     switch (butSel)
292     {
293         case(0):
294         {
295          unit="grams";
296          break;
297         }
298         case(1):
299         {
300             unit="cups";
301             break;
302         }
303         case(2):
304         {
305             unit="teaspoons";
306             break;
307         }
308         case(3):
309         {
310             unit="tablespoons";
311             break;
312         }
313         case(4):
314         {
315             unit="ml";
316             break;
317         }
318         
319     }
320     ing.get(ingCount).setUnit(unit);
321     
322     
323     recipeList.get(pos).setIng(ing);//Reset the list of ings
324     refreshArea();
325     quantityField.setText("");
326     costField.setText("");
327 ingCount++;
328     }
329 //listModel.remove(ingList.getSelectedIndex());
330     
331         // TODO add your handling code here:
332     }                                      
333 
334     private void gramsButActionPerformed(java.awt.event.ActionEvent evt) {                                         
335 butSel=0;        // TODO add your handling code here:
336     }                                        
337 
338     private void cupsButActionPerformed(java.awt.event.ActionEvent evt) {                                        
339 butSel=1;        // TODO add your handling code here:
340     }                                       
341 
342     private void teaspoonButActionPerformed(java.awt.event.ActionEvent evt) {                                            
343 butSel=2;        // TODO add your handling code here:
344     }                                           
345 
346     private void tablespoonButActionPerformed(java.awt.event.ActionEvent evt) {                                              
347 butSel=3;        // TODO add your handling code here:
348     }                                             
349 
350     private void mlButActionPerformed(java.awt.event.ActionEvent evt) {                                      
351     butSel=4;    // TODO add your handling code here:
352     }                                     
353 
354     private void saveButActionPerformed(java.awt.event.ActionEvent evt) {                                        
355         setVisible(false);
356         // TODO add your handling code here:
357     }                                       
358 
359     private void quantityFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
360         // TODO add your handling code here:
361     }                                             
362 
363     /**
364      * @param args the command line arguments
365      */
366     public static void main(String args[]) {
367         /* Set the Nimbus look and feel */
368         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
369         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
370          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
371          */
372         try {
373             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
374                 if ("Nimbus".equals(info.getName())) {
375                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
376                     break;
377                 }
378             }
379         } catch (ClassNotFoundException ex) {
380             java.util.logging.Logger.getLogger(IngredientsBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
381         } catch (InstantiationException ex) {
382             java.util.logging.Logger.getLogger(IngredientsBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
383         } catch (IllegalAccessException ex) {
384             java.util.logging.Logger.getLogger(IngredientsBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
385         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
386             java.util.logging.Logger.getLogger(IngredientsBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
387         }
388         //</editor-fold>
389 
390         /* Create and display the form */
391         java.awt.EventQueue.invokeLater(new Runnable() {
392             public void run() {
393                 new IngredientsBuilder().setVisible(true);
394             }
395         });
396     }
397 
398     // Variables declaration - do not modify                     
399     private javax.swing.JButton addIng;
400     private javax.swing.JTextField costField;
401     private javax.swing.JRadioButton cupsBut;
402     private javax.swing.JRadioButton gramsBut;
403     private javax.swing.JList<String> ingList;
404     private javax.swing.JScrollPane ingScroll;
405     private javax.swing.JLabel jLabel1;
406     private javax.swing.JScrollPane jScrollPane1;
407     private javax.swing.JList<String> listofIngs;
408     private javax.swing.JRadioButton mlBut;
409     private javax.swing.JTextField quantityField;
410     private javax.swing.JLabel quantityLab;
411     private javax.swing.JButton removeIng;
412     private javax.swing.JButton saveBut;
413     private javax.swing.JRadioButton tablespoonBut;
414     private javax.swing.JRadioButton teaspoonBut;
415     private javax.swing.ButtonGroup unitButs;
416     // End of variables declaration                   
417 DefaultListModel<String> mainMod= new DefaultListModel<>();
418     ArrayList<Recipe>recipeList= new ArrayList<>();
419     int ingCount=0;
420   String ingToString="";
421     ArrayList<Ingredients> ing = new ArrayList<>();
422     DefaultListModel ingModel= new DefaultListModel();
423     DefaultListModel listModel= new DefaultListModel();
424     
425     int pos=0;
426     int butSel=-1;
427 }
428