Cook book written in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

240 lines
6.4 KiB

package ksiazka.kucharska;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import db.SearchRecipeAll;
import db.Cookbook;
import db.SearchRecipe;
import db.SearchRecipeNameTag;
public class CB_Controller {
private static CB_View theView;
private static CB_Model theModel;
public CB_Controller(CB_View theView, CB_Model theModel) {
CB_Controller.theView = theView;
CB_Controller.theModel = theModel;
CB_Controller.theView.addListener(new AddListener());
CB_Controller.theView.delListener(new DelListener());
CB_Controller.theView.loadListener(new LoadListener());
CB_Controller.theView.cancelListener(new CancelListener());
CB_Controller.theView.saveListener(new SaveListener());
CB_Controller.theView.listSelectListener(new SelectionListener());
CB_Controller.theView.searchTypeListener(new SearchListener());
}
public void fillList(){
Cookbook myCb = new Cookbook();
SearchRecipe sr;
if (theView.getSearchValue() != "") {
sr = new SearchRecipeNameTag(theView);
}
else
{
sr = new SearchRecipeAll(theView);
}
List<String> names = sr.obtainRecipeNames();
theView.setRecipesList(names);
myCb.closeConnection();
}
public static void fillComponents(String name){
Cookbook myCb = new Cookbook();
theModel = myCb.selectRecipe(name);
theView.setRecipeTitle(theModel.getName());
theView.setRecipeIngredients(theModel.getIngredients());
theView.setRecipeDescription(theModel.getDescription());
theView.setRecipeTags(theModel.getTags());
theView.setRecipeIcon(theModel.getImage());
theView.resetCaretPositions();
myCb.closeConnection();
}
public static void addNewRecipe(){
Cookbook myCb = new Cookbook();
RecipeManager rm = new RecipeManager();
rm.setRecipeBuilder(new NewRecipeBuilder(theView));
rm.constructRecipe();
CB_Model obj = rm.getRecipe();
myCb.insertRecipe(obj);
myCb.closeConnection();
}
public static void removeRecipe(String name){
Cookbook myCb = new Cookbook();
myCb.deleteRecipe(name);
myCb.closeConnection();
}
public ImageIcon resizeImage(File f) {
//File file = f;
ImageIcon iconLogo=null;
try {
BufferedImage srcImg = ImageIO.read(f);
int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*theView.getImgRecipe().getHeight());
BufferedImage resizedImg = new BufferedImage(w, theView.getImgRecipe().getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, theView.getImgRecipe().getHeight(), null);
g2.dispose();
iconLogo = new ImageIcon(resizedImg);
} catch (IOException e1) {
e1.printStackTrace();
}
return iconLogo;
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
theView.ActivateAddMenu();
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class DelListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JList recipesList = theView.getRecipesList();
DefaultListModel model = (DefaultListModel) recipesList.getModel();
if (recipesList.getSelectedIndex() != -1)
{
int dialogResult = JOptionPane.showConfirmDialog (null, "Czy na pewno chcesz usunąć przepis?", "Pytanie", JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
removeRecipe(recipesList.getSelectedValue().toString());
model.remove(recipesList.getSelectedIndex());
if (recipesList.getModel().getSize()>0)
{
recipesList.setSelectedIndex(0);
fillComponents(recipesList.getSelectedValue().toString());
}
else
{
theView.setRecipeTitle("Nazwa przepisu");
theView.setRecipeIngredients("");
theView.setRecipeDescription("");
theView.setRecipeTags("");
}
}
}
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class LoadListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JFileChooser fcImg = theView.getFileChooser();
int returnVal = fcImg.showOpenDialog(theView);
if (returnVal == JFileChooser.APPROVE_OPTION) {
theView.getImgRecipe().setIcon(resizeImage(fcImg.getSelectedFile()));
}
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class CancelListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
theView.DeactivateAddMenu();
JList recipesList = theView.getRecipesList();
if(recipesList.getSelectedIndex() != -1)
fillComponents(recipesList.getSelectedValue().toString());
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
addNewRecipe();
theView.setRecipesListItem(theView.getRecipeEdTitle());
theView.DeactivateAddMenu();
theView.getRecipesList().setSelectedIndex(theView.getRecipesList().getModel().getSize()-1);
fillComponents(theView.getRecipesList().getSelectedValue().toString());
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class SelectionListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
try{
JList recipesList = theView.getRecipesList();
if(recipesList.getSelectedIndex() != -1)
fillComponents(recipesList.getSelectedValue().toString());
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class SearchListener implements DocumentListener{
public void changedUpdate(DocumentEvent e) {
fillList();
}
public void insertUpdate(DocumentEvent e) {
fillList();
}
public void removeUpdate(DocumentEvent e) {
fillList();
}
}
}