package cuisinart;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import org.apache.commons.io.FileUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class RecipePage extends ContentPanel {
private Recipe recipe;
private MainWindow parent;
private JButton image;
private Image shareBkgrd;
private JTextField title;
private JTextArea servings;
private JTextArea tagline;
private JTextArea ingredients;
private JTextArea directions;
private static JButton addToFavorites;
private static JButton addToShoppingList;
private static JButton printRecipe;
private static JButton shareRecipe;
private static JButton editRecipe;
private JButton saveButton;
private static final int SM_IMG_WIDTH = 200;
private static final int SM_IMG_HEIGHT = 200;
private static final int LG_IMG_WIDTH = 500;
private static final int LG_IMG_HEIGHT = 500;
static {
addToFavorites = new BlueButton("/res/images/addToFavoritesBtn.png");
addToShoppingList = new BlueButton("/res/images/addToShoppingListBtn.png");
printRecipe = new BlueButton("/res/images/printRecipeBtn.png");
shareRecipe = new BlueButton("/res/images/shareRecipeBtn.png");
editRecipe = new BlueButton("/res/images/editRecipeBtn.png");
}
public RecipePage(Recipe rec, MainWindow p)
{
super(p);
parent = p;
recipe = rec;
setBackground(Color.WHITE);
addToFavorites.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
parent.addFavorite(recipe.getNumber());
}
});
addToShoppingList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
parent.addToShoppingList(recipe);
}
});
printRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doPrint();
}
});
shareRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JDialog shareDialog = new JDialog();
try {
shareBkgrd = ImageIO.read(RecipePage.class.getResource("/res/images/share_background.png"));
} catch (IOException e1) {
shareBkgrd = recipe.getImage();
e1.printStackTrace();
//TODO: handle properly
}
JPanel sharePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Dimension d = getSize();
g.drawImage(shareBkgrd, 0, 0, null);
}
};
shareDialog.setUndecorated(true);
shareDialog.setBounds(new Rectangle(root.getFrame().getLocationOnScreen().x+200,
root.getFrame().getLocationOnScreen().y+100,
shareBkgrd.getWidth(null), shareBkgrd.getHeight(null)));
JButton closeBtn = new JButton("Close");
closeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shareDialog.dispose();
shareDialog.setVisible(false);
}
});
closeBtn.setBounds(300,300,100,20); //TODO: Temporary
sharePanel.setLayout(null);
sharePanel.add(closeBtn);
sharePanel.setOpaque(false);
shareDialog.setModal(true);
shareDialog.getRootPane().setOpaque(false);
shareDialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
shareDialog.setBackground(new Color(0, 0, 0, 0));
shareDialog.add(sharePanel);
shareDialog.setVisible(true);
//TODO: Add FB/Twitter buttons, finish and fix
}
});
editRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
title.setEditable(true);
title.setBorder(new LineBorder(Color.LIGHT_GRAY,1));
servings.setEditable(true);
servings.setBorder(new LineBorder(Color.LIGHT_GRAY,1));
tagline.setEditable(true);
tagline.setBorder(new LineBorder(Color.LIGHT_GRAY,1));
ingredients.setEditable(true);
ingredients.setBorder(new LineBorder(Color.LIGHT_GRAY,1));
directions.setEditable(false);
directions.setBorder(new LineBorder(Color.LIGHT_GRAY,1));
saveButton = new JButton("SAVE");
saveButton.setBackground(buttonBackground);
saveButton.setFont(myriadPro.deriveFont(16));
saveButton.setForeground(Color.WHITE);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
title.setEditable(false);
title.setBorder(null);
servings.setEditable(false);
servings.setBorder(null);
tagline.setEditable(false);
tagline.setBorder(null);
ingredients.setEditable(false);
ingredients.setBorder(null);
directions.setEditable(false);
directions.setBorder(null);
recipe.setTitle(title.getText());
recipe.setServings(servings.getText());
recipe.setTagline(tagline.getText());
recipe.setIngredients(ingredients.getText());
recipe.setDirections(directions.getText());
File userRecipes = getFileByCategory(recipe.getCategory());
FileOutputStream stream = null;
BufferedOutputStream out = null;
try {
stream = new FileOutputStream(userRecipes);
out = new BufferedOutputStream(stream);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return;
}
XStream xstream = new XStream(new DomDriver());
xstream.alias("recipe", Recipe.class);
xstream.alias("recipes", List.class);
xstream.omitField(Recipe.class, "image");
xstream.omitField(Recipe.class, "catNum");
List<Recipe> recipes = root.getRecipesByCategory(recipe.getCategory());
String xml = xstream.toXML(recipes);
System.out.print("Generated XML String: " + xml);
try {
out.write(xml.getBytes("UTF-8"));
out.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
GridBagConstraints gbc_saveBtn = new GridBagConstraints();
gbc_saveBtn.gridx = 0;
gbc_saveBtn.gridy = 6;
gbc_saveBtn.anchor = GridBagConstraints.WEST;
gbc_saveBtn.insets = new Insets(20, 30, 0, 0);
add(saveButton,gbc_saveBtn);
}
});
//TODO: Implement Edit button
GridBagLayout gbl = new GridBagLayout();
gbl.columnWidths = new int[]{0, 0};
gbl.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl.columnWeights = new double[]{0.0, 1.0};
gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
setLayout(gbl);
final Image recipeImage = recipe.getImage();
image = new ImageButton(recipeImage, SM_IMG_WIDTH, SM_IMG_HEIGHT);
image.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JDialog imgDialog = new JDialog(root.getFrame(),recipe.getTitle(),true);
JButton lgImg = new ImageButton(recipeImage, LG_IMG_WIDTH, LG_IMG_HEIGHT);
imgDialog.add(lgImg);
lgImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
imgDialog.dispose();
imgDialog.setVisible(false);
}
});
imgDialog.setBounds(new Rectangle(root.getFrame().getLocationOnScreen().x+100,
root.getFrame().getLocationOnScreen().y+100,
LG_IMG_WIDTH, LG_IMG_HEIGHT));
imgDialog.setUndecorated(true);
imgDialog.setVisible(true);
imgDialog.setLayout(new BorderLayout());
}
});
GridBagConstraints gbc_image = new GridBagConstraints();
gbc_image.fill = GridBagConstraints.BOTH;
gbc_image.insets = new Insets(20,30,20,20);
gbc_image.gridx = 0;
gbc_image.gridy = 0;
add(image,gbc_image);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
GridBagLayout gbl_button = new GridBagLayout();
gbl_button.columnWidths = new int[]{0};
gbl_button.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl_button.columnWeights = new double[]{0.0};
gbl_button.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
buttonPanel.setLayout(gbl_button);
Insets buttonInsets = new Insets(1,1,1,1);
GridBagConstraints gbc_favorites = new GridBagConstraints();
gbc_favorites.anchor = GridBagConstraints.WEST;
gbc_favorites.fill = GridBagConstraints.VERTICAL;
gbc_favorites.insets = buttonInsets;
gbc_favorites.gridx = 0;
gbc_favorites.gridy = 1;
buttonPanel.add(addToFavorites, gbc_favorites);
GridBagConstraints gbc_shopping = new GridBagConstraints();
gbc_shopping.anchor = GridBagConstraints.WEST;
gbc_shopping.fill = GridBagConstraints.VERTICAL;
gbc_shopping.insets = buttonInsets;
gbc_shopping.gridx = 0;
gbc_shopping.gridy = 2;
buttonPanel.add(addToShoppingList, gbc_shopping);
GridBagConstraints gbc_print = new GridBagConstraints();
gbc_print.anchor = GridBagConstraints.WEST;
gbc_print.fill = GridBagConstraints.VERTICAL;
gbc_print.insets = buttonInsets;
gbc_print.gridx = 0;
gbc_print.gridy = 3;
buttonPanel.add(printRecipe, gbc_print);
GridBagConstraints gbc_share = new GridBagConstraints();
gbc_share.anchor = GridBagConstraints.WEST;
gbc_share.fill = GridBagConstraints.VERTICAL;
gbc_share.insets = buttonInsets;
gbc_share.gridx = 0;
gbc_share.gridy = 4;
buttonPanel.add(shareRecipe, gbc_share);
GridBagConstraints gbc_edit = new GridBagConstraints();
gbc_edit.anchor = GridBagConstraints.WEST;
gbc_edit.fill = GridBagConstraints.VERTICAL;
gbc_edit.insets = buttonInsets;
gbc_edit.gridx = 0;
gbc_edit.gridy = 5;
buttonPanel.add(editRecipe, gbc_edit);
GridBagConstraints gbc_buttons = new GridBagConstraints();
gbc_buttons.anchor = GridBagConstraints.WEST;
gbc_buttons.fill = GridBagConstraints.VERTICAL;
gbc_buttons.gridx = 1;
gbc_buttons.gridy = 0;
add(buttonPanel,gbc_buttons);
title = new JTextField();
title.setText(recipe.getTitle());
title.setEditable(false);
title.setBorder(null);
title.setBackground(Color.WHITE);
title.setForeground(titleColor);
title.setFont(minionPro.deriveFont((float)31.5));
GridBagConstraints gbc_title = new GridBagConstraints();
gbc_title.anchor = GridBagConstraints.SOUTH;
gbc_title.fill = GridBagConstraints.HORIZONTAL;
gbc_title.insets = new Insets(0,LEFT_INSET,0,0);
gbc_title.gridx = 0;
gbc_title.gridy = 1;
gbc_title.gridwidth = 2;
add(title,gbc_title);
servings = new JTextArea();
servings.setText(recipe.getServings());
servings.setEditable(false);
servings.setBorder(null);
servings.setBackground(Color.WHITE);
servings.setFont(minionPro.deriveFont((float)15));
servings.setLineWrap(true);
servings.setWrapStyleWord(true);
GridBagConstraints gbc_servings = new GridBagConstraints();
gbc_servings.fill = GridBagConstraints.BOTH;
gbc_servings.insets = new Insets(0,30,30,0);
gbc_servings.gridx = 0;
gbc_servings.gridy = 2;
gbc_servings.gridwidth = 2;
add(servings,gbc_servings);
tagline = new JTextArea();
tagline.setText(recipe.getTagline());
tagline.setEditable(false);
tagline.setBorder(null);
tagline.setBackground(Color.WHITE);
tagline.setFont(minionProIt.deriveFont((float)16));
tagline.setLineWrap(true);
tagline.setWrapStyleWord(true);
GridBagConstraints gbc_tagline = new GridBagConstraints();
gbc_tagline.fill = GridBagConstraints.BOTH;
gbc_tagline.insets = new Insets(0,30,30,0);
gbc_tagline.gridx = 0;
gbc_tagline.gridy = 3;
gbc_tagline.gridwidth = 2;
add(tagline,gbc_tagline);
ingredients = new JTextArea();
ingredients.setText(recipe.getIngredients());
ingredients.setEditable(false);
ingredients.setBorder(null);
ingredients.setBackground(Color.WHITE);
ingredients.setFont(minionProSemi.deriveFont((float)16));
ingredients.setLineWrap(true);
ingredients.setWrapStyleWord(true);
GridBagConstraints gbc_ingredients = new GridBagConstraints();
gbc_ingredients.fill = GridBagConstraints.BOTH;
gbc_ingredients.insets = new Insets(0,30,30,0);
gbc_ingredients.gridx = 0;
gbc_ingredients.gridy = 4;
gbc_ingredients.gridwidth = 2;
add(ingredients,gbc_ingredients);
directions = new JTextArea();
directions.setText(recipe.getDirections());
directions.setEditable(false);
directions.setBorder(null);
directions.setBackground(Color.WHITE);
directions.setFont(minionPro.deriveFont((float)16));
directions.setLineWrap(true);
directions.setWrapStyleWord(true);
GridBagConstraints gbc_directions = new GridBagConstraints();
gbc_directions.fill = GridBagConstraints.BOTH;
gbc_directions.insets = new Insets(0,30,0,0);
gbc_directions.gridx = 0;
gbc_directions.gridy = 5;
gbc_directions.gridwidth = 2;
add(directions,gbc_directions);
}
protected File getFileByCategory(int selectedIndex) {
if(selectedIndex == Recipe.STARTERS)
return new File(root.getRecDir(),"Starters.xml");
else if(selectedIndex == Recipe.BEVERAGES)
return new File(root.getRecDir(),"Beverages.xml");
else if(selectedIndex == Recipe.PASTAS)
return new File(root.getRecDir(),"Pasta.xml");
else if(selectedIndex == Recipe.BREADS)
return new File(root.getRecDir(),"Breads.xml");
else if(selectedIndex == Recipe.VEGETABLES)
return new File(root.getRecDir(),"Vegetables.xml");
else if(selectedIndex == Recipe.MAINDISHES)
return new File(root.getRecDir(),"Entrees.xml");
else if(selectedIndex == Recipe.DESSERTS)
return new File(root.getRecDir(),"Desserts.xml");
return null;
}
private void doPrint() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName("Print Recipe");
pj.setPrintable(this);
if(pj.printDialog()) {
try {
pj.print();
} catch(PrinterException ex) {
//put error dialog here
}
}
}
}