Package omnom.newRecipes

Source Code of omnom.newRecipes.AddRecipe

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* AddRecipe.java
*
* Created on Jan 24, 2011, 5:12:11 PM
*/
package omnom.newRecipes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import omnom.database.DatabaseHandler;
import omnom.RecipesApp;
import omnom.RecipesView;
import omnom.jaxb.IngredientType;
import omnom.jaxb.RecipeType;

/**
*
* @author ashton
*/
public class AddRecipe extends javax.swing.JDialog implements ActionListener {

    private JPopupMenu menu;
    private DefaultListModel ingredientModel;
    private int lastClicked = -1;
    private ArrayList<IngredientType> ingredients;
    private AddIngredient addIngredient;
    private DatabaseHandler db;
    private RecipesView recParent;

    /** Creates new form AddRecipe */
    public AddRecipe(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void clearFields() {
        if (ingredients != null) {
            ingredients.clear();
        }
        refreshIngredientList();
        nameTextField.setText("");
        servesTextField.setText("");
        timeTextField.setText("");
    }

    public AddRecipe(java.awt.Frame parent, RecipesView recipeView) {
        super(parent);
        recParent = recipeView;
        initComponents();
        JMenuItem temp = new JMenuItem("Delete ingredient");
        temp.addActionListener(this);
        menu = new JPopupMenu();
        menu.add(temp);
        temp = new JMenuItem("Add ingredient");
        menu.add(temp);
        temp.addActionListener(this);
        ingredientModel = new DefaultListModel();
        ingredientList.setModel(ingredientModel);
        ingredientModel.clear();
        ingredients = new ArrayList<IngredientType>();

    }

    void addIngredient(IngredientType ing) {
        ingredients.add(ing);
        refreshIngredientList();
    }

    private void refreshIngredientList() {
        ingredientModel.clear();
        for (IngredientType ing : ingredients) {
            ingredientModel.addElement(ing);

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        servesTextField = new javax.swing.JTextField();
        Serves = new javax.swing.JLabel();
        timeTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ingredientList = new IngredientList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(omnom.RecipesApp.class).getContext().getResourceMap(AddRecipe.class);
        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        acceptButton.setText(resourceMap.getString("acceptButton.text")); // NOI18N
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        servesTextField.setText(resourceMap.getString("servesTextField.text")); // NOI18N
        servesTextField.setName("servesTextField"); // NOI18N

        Serves.setText(resourceMap.getString("Serves.text")); // NOI18N
        Serves.setName("Serves"); // NOI18N

        timeTextField.setText(resourceMap.getString("timeTextField.text")); // NOI18N
        timeTextField.setName("timeTextField"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        ingredientList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        ingredientList.setName("ingredientList"); // NOI18N
        ingredientList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ingredientListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ingredientList);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setToolTipText(resourceMap.getString("jTextArea1.toolTipText")); // NOI18N
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(Serves)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(servesTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                            .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                            .addComponent(timeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(acceptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                            .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(servesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Serves))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(acceptButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_cancelButtonMouseClicked

    private void ingredientListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ingredientListMouseClicked
        if (evt.getButton() == 3) {
            menu.show(evt.getComponent(), evt.getX(), evt.getY());
            lastClicked = ingredientList.locationToIndex(evt.getPoint());
            ingredientList.setSelectedIndex(lastClicked);
        }
    }//GEN-LAST:event_ingredientListMouseClicked

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        if (nameTextField.getText() == null ? "" == null : nameTextField.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You need a name for a recipe");
        } else if (servesTextField.getText() == null ? "" == null : servesTextField.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You need to specify how many people this serves.");
        } else if (timeTextField.getText() == null ? "" == null : timeTextField.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You need to specify how long this recipe takes to prepare");
        } else {
            try {
                Integer.parseInt(servesTextField.getText());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "You can't add anything that isn't a number in the 'Serves' Field");
                return;
            }
            try {
                Integer.parseInt(timeTextField.getText());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "You can't add anything that isn't a number in the 'Preparation Time' Field");
                return;
            }
            RecipeType recipe = new RecipeType();
            recipe.setName(nameTextField.getText());
            recipe.setServes(Integer.parseInt(servesTextField.getText()));
            recipe.setPrepareTime(Integer.parseInt(timeTextField.getText()));
            List<IngredientType> temp = recipe.getIngredients();
            temp.addAll(ingredients);
            List<String> steps = recipe.getSteps();
            String[] raw = jTextArea1.getText().replaceAll("\t", "").split("\n");
            for (String s : raw) {
                if (!s.equals("")) {
                    steps.add(s);
                }
            }
            DatabaseHandler.addRecipe(recipe);
            recParent.refreshRecipes();
            dispose();
        }
    }//GEN-LAST:event_acceptButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AddRecipe dialog = new AddRecipe(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Serves;
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JList ingredientList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField servesTextField;
    private javax.swing.JTextField timeTextField;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {
        JMenuItem temp = (JMenuItem) e.getSource();
        if (temp.getText().equals("Delete ingredient") && lastClicked >= 0) {
            ingredientModel.remove(lastClicked);
        } else if (temp.getText().equals("Add ingredient")) {
            if (addIngredient == null) {
                JFrame mainFrame = RecipesApp.getApplication().getMainFrame();
                addIngredient = new AddIngredient(mainFrame, this);
                addIngredient.setLocationRelativeTo(mainFrame);
            }
            RecipesApp.getApplication().show(addIngredient);
            addIngredient.clearFields();
        }
    }
}

class IngredientList extends JList {

    @Override
    public String getToolTipText(MouseEvent event) {
        int index = locationToIndex(event.getPoint());
        if (index < 0) {
            System.err.println("Index below 0");
            return null;
        }
        Object item = getModel().getElementAt(index);
        if (item == null) {
            System.err.println("Null object");
            return null;
        }
        try {
            IngredientType ingredient = (IngredientType) item;
            String result = ingredient.getName();
            if (ingredient.getNumerator() == 0) {
                result += " " + ingredient.getUnit();
            } else if (ingredient.getDenominator() == 0) {
                result += " " + ingredient.getNumerator() + " " + ingredient.getUnit();
            } else {
                result += " " + ingredient.getNumerator() + "/" + ingredient.getDenominator() + " " + ingredient.getUnit();
            }
            return result;
        } catch (ClassCastException ex) {
            System.err.println("Class cast exception");
            return null;
        }
    }
}
TOP

Related Classes of omnom.newRecipes.AddRecipe

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.