kolejne poprawki w UI (m.in. wyśrodkowanie okna przy starcie, ustawianie przepisu z listy)
support dla grafiki w przepisie filtrowanie plików w wyborze grafik potwierdzenie usunięcia przepisu
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package db;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import db.Recipe;
|
||||
|
||||
public class Cookbook {
|
||||
@@ -35,7 +41,7 @@ public class Cookbook {
|
||||
}
|
||||
|
||||
public boolean createTables() {
|
||||
String createRecipes = "CREATE TABLE IF NOT EXISTS recipes (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar, ingredients varchar, description varchar, tags varchar)";
|
||||
String createRecipes = "CREATE TABLE IF NOT EXISTS recipes (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar, ingredients varchar, description varchar, tags varchar, img blob)";
|
||||
|
||||
try {
|
||||
stat.execute(createRecipes);
|
||||
@@ -47,20 +53,40 @@ public class Cookbook {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean insertRecipe(String name, String ingredients, String description, String tags) {
|
||||
public boolean insertRecipe(String name, String ingredients, String description, String tags, ImageIcon img) {
|
||||
try {
|
||||
//przekształcam ImageIcon -> BuforowanyImg
|
||||
BufferedImage bImg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
bImg.getGraphics().drawImage(img.getImage(), 0,0, null);
|
||||
//tworzę strumień bajtowy
|
||||
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
||||
//zapisuję do strumienia bufor obrazka
|
||||
ImageIO.write( bImg, "png", bStream );
|
||||
bStream.flush();
|
||||
|
||||
|
||||
PreparedStatement prepStmt = conn.prepareStatement(
|
||||
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?);");
|
||||
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
|
||||
prepStmt.setString(1, name);
|
||||
prepStmt.setString(2, ingredients);
|
||||
prepStmt.setString(3, description);
|
||||
prepStmt.setString(4, tags);
|
||||
prepStmt.setBytes(5, bStream.toByteArray());
|
||||
prepStmt.execute();
|
||||
} catch (SQLException e) {
|
||||
bStream.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
System.err.println("Blad przy wstawianiu przepisu");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("Blad przy wstawianiu przepisu");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -72,11 +98,13 @@ public class Cookbook {
|
||||
ResultSet result = query.executeQuery();
|
||||
int id;
|
||||
String ingredients, description, tags;
|
||||
ImageIcon pict;
|
||||
id = result.getInt("id");
|
||||
ingredients = result.getString("ingredients");
|
||||
description = result.getString("description");
|
||||
tags = result.getString("tags");
|
||||
selectedRecipe = new Recipe(id, name, ingredients, description, tags);
|
||||
pict = new ImageIcon(result.getBytes("img"));
|
||||
selectedRecipe = new Recipe(id, name, ingredients, description, tags, pict);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -89,7 +117,6 @@ public class Cookbook {
|
||||
try {
|
||||
PreparedStatement query = conn.prepareStatement("DELETE FROM recipes WHERE name=?");
|
||||
query.setString(1, name);
|
||||
System.out.println(name);
|
||||
query.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package db;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Recipe {
|
||||
private int id;
|
||||
private String name;
|
||||
private String ingredients;
|
||||
private String description;
|
||||
private String tags;
|
||||
private ImageIcon img;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -34,15 +37,21 @@ public class Recipe {
|
||||
public void setTags(String tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public ImageIcon getImage() {
|
||||
return img;
|
||||
}
|
||||
public void setImage(ImageIcon img) {
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
public Recipe() {}
|
||||
public Recipe(int id, String name, String ingredients, String description, String tags) {
|
||||
public Recipe(int id, String name, String ingredients, String description, String tags, ImageIcon img) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.ingredients = ingredients;
|
||||
this.description = description;
|
||||
this.tags = tags;
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ksiazka.kucharska;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Toolkit;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
@@ -16,6 +18,12 @@ public class App
|
||||
try {
|
||||
System.out.println( "tworzę fMain..." );
|
||||
FrmMain fMain = new FrmMain();
|
||||
|
||||
//wyśrodkowanie okna na ekranie
|
||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
int x = (screenSize.width - fMain.getWidth()) / 2;
|
||||
int y = (screenSize.height - fMain.getHeight()) / 2;
|
||||
fMain.setLocation(x, y);
|
||||
fMain.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -10,6 +10,7 @@ import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JTextField;
|
||||
@@ -18,7 +19,6 @@ import java.awt.Graphics2D;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Image;
|
||||
import java.awt.Insets;
|
||||
import java.awt.RenderingHints;
|
||||
|
||||
@@ -37,6 +37,7 @@ import javax.swing.SwingConstants;
|
||||
import ksiazka.kucharska.RetrieveData;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
|
||||
public class FrmMain extends JFrame {
|
||||
|
||||
@@ -58,12 +59,13 @@ public class FrmMain extends JFrame {
|
||||
private JButton btnLoadImg;
|
||||
public static JLabel lblRecipeTitle;
|
||||
private static DefaultListModel recipesListModel;
|
||||
private JLabel imgRecipe;
|
||||
public static JLabel imgRecipe;
|
||||
private JFileChooser fcImg;
|
||||
private ImageIcon imgDefaultRecipe;
|
||||
public static ImageIcon imgDefaultRecipe;
|
||||
private JList recipesList;
|
||||
|
||||
void DeactivateAddMenu() {
|
||||
btnAddRecipe.setEnabled(true);
|
||||
btnCancel.setVisible(false);
|
||||
btnDelRecipe.setEnabled(true);
|
||||
btnLoadImg.setVisible(false);
|
||||
@@ -78,6 +80,7 @@ public class FrmMain extends JFrame {
|
||||
}
|
||||
|
||||
void ActivateAddMenu() {
|
||||
btnAddRecipe.setEnabled(false);
|
||||
btnCancel.setVisible(true);
|
||||
btnDelRecipe.setEnabled(false);
|
||||
btnLoadImg.setVisible(true);
|
||||
@@ -89,13 +92,14 @@ public class FrmMain extends JFrame {
|
||||
txtTags.setEditable(true);
|
||||
lblRecipeTitle.setVisible(false);
|
||||
recipesList.setEnabled(false);
|
||||
|
||||
imgRecipe.setIcon(imgDefaultRecipe);
|
||||
|
||||
//czyszczenie pól formularza
|
||||
txtDescription.setText("");
|
||||
txtEdTitle.setText("");
|
||||
txtIngredients.setText("");
|
||||
txtTags.setText("");
|
||||
|
||||
}
|
||||
|
||||
public static DefaultListModel getRecipesList()
|
||||
@@ -110,7 +114,7 @@ public class FrmMain extends JFrame {
|
||||
setResizable(false);
|
||||
setTitle("Książka Kucharska v1.0");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 800, 549);
|
||||
setBounds(100, 100, 723, 549);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
@@ -191,8 +195,23 @@ public class FrmMain extends JFrame {
|
||||
DefaultListModel model = (DefaultListModel) recipesList.getModel();
|
||||
if (recipesList.getSelectedIndex() != -1)
|
||||
{
|
||||
RetrieveData.removeRecipe(recipesList.getSelectedValue().toString());
|
||||
model.remove(recipesList.getSelectedIndex());
|
||||
int dialogResult = JOptionPane.showConfirmDialog (null, "Czy na pewno chcesz usunąć przepis?", "Pytanie", JOptionPane.YES_NO_OPTION);
|
||||
if(dialogResult == JOptionPane.YES_OPTION){
|
||||
RetrieveData.removeRecipe(recipesList.getSelectedValue().toString());
|
||||
model.remove(recipesList.getSelectedIndex());
|
||||
if (recipesList.getModel().getSize()>0)
|
||||
{
|
||||
recipesList.setSelectedIndex(0);
|
||||
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
txtDescription.setText("");
|
||||
lblRecipeTitle.setText("Nazwa przepisu");
|
||||
txtIngredients.setText("");
|
||||
txtTags.setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -206,9 +225,11 @@ public class FrmMain extends JFrame {
|
||||
btnSave.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
RetrieveData.addNewRecipe();
|
||||
DeactivateAddMenu();
|
||||
DefaultListModel model = (DefaultListModel) recipesList.getModel();
|
||||
model.addElement(txtEdTitle.getText());
|
||||
DeactivateAddMenu();
|
||||
recipesList.setSelectedIndex(recipesList.getModel().getSize()-1);
|
||||
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
|
||||
}
|
||||
});
|
||||
btnSave.setVisible(false);
|
||||
@@ -217,7 +238,7 @@ public class FrmMain extends JFrame {
|
||||
|
||||
txtEdTitle = new JTextField();
|
||||
txtEdTitle.setVisible(false);
|
||||
txtEdTitle.setBounds(12, 15, 295, 19);
|
||||
txtEdTitle.setBounds(26, 15, 281, 19);
|
||||
panel_2.add(txtEdTitle);
|
||||
txtEdTitle.setColumns(10);
|
||||
|
||||
@@ -253,38 +274,41 @@ public class FrmMain extends JFrame {
|
||||
btnLoadImg = new JButton("Wczytaj");
|
||||
|
||||
fcImg = new JFileChooser();
|
||||
FileNameExtensionFilter ffAll = new FileNameExtensionFilter("Wszystkie grafiki", "jpg", "jpeg", "gif", "png");
|
||||
fcImg.addChoosableFileFilter(ffAll);
|
||||
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki JPEG", "jpg", "jpeg"));
|
||||
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki GIF", "gif"));
|
||||
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki PNG", "png"));
|
||||
fcImg.setAcceptAllFileFilterUsed(false);
|
||||
fcImg.setFileFilter(ffAll);
|
||||
imgDefaultRecipe = new ImageIcon("no_img_recipe.jpg");
|
||||
imgRecipe.setIcon(imgDefaultRecipe);
|
||||
|
||||
|
||||
btnLoadImg.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
//otwórz okno dialogowe
|
||||
int returnVal = fcImg.showOpenDialog(FrmMain.this);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fcImg.getSelectedFile();
|
||||
try {
|
||||
BufferedImage srcImg = ImageIO.read(file);
|
||||
int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight());
|
||||
|
||||
System.out.println(w);
|
||||
|
||||
BufferedImage resizedImg = new BufferedImage(w, imgRecipe.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2 = resizedImg.createGraphics();
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
g2.drawImage(srcImg, 0, 0, w, imgRecipe.getHeight(), null);
|
||||
g2.dispose();
|
||||
|
||||
|
||||
ImageIcon iconLogo = new ImageIcon(resizedImg);
|
||||
imgRecipe.setIcon(iconLogo);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fcImg.getSelectedFile();
|
||||
try {
|
||||
BufferedImage srcImg = ImageIO.read(file); //czytaj plik
|
||||
|
||||
//wyznacz szerokość obrazka jako stosunek (przy maksymalizacji wysokości
|
||||
int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight());
|
||||
|
||||
BufferedImage resizedImg = new BufferedImage(w, imgRecipe.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2 = resizedImg.createGraphics();
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
g2.drawImage(srcImg, 0, 0, w, imgRecipe.getHeight(), null);
|
||||
g2.dispose();
|
||||
ImageIcon iconLogo = new ImageIcon(resizedImg);
|
||||
imgRecipe.setIcon(iconLogo);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
btnLoadImg.setVisible(false);
|
||||
@@ -305,6 +329,8 @@ public class FrmMain extends JFrame {
|
||||
btnCancel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DeactivateAddMenu();
|
||||
if(recipesList.getSelectedIndex() != -1)
|
||||
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
|
||||
}
|
||||
});
|
||||
btnCancel.setVisible(false);
|
||||
|
||||
@@ -3,6 +3,7 @@ package ksiazka.kucharska;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import db.Cookbook;
|
||||
import db.Recipe;
|
||||
@@ -18,11 +19,11 @@ public class RetrieveData extends FrmMain{
|
||||
myCb.insertRecipe("Kanapka",
|
||||
"Chleb, margaryna, szynka",
|
||||
"Posmarować kanapke margaryną i położyć na niej plaster szynki",
|
||||
"kanapka, szynka, chleb");
|
||||
"kanapka, szynka, chleb", imgDefaultRecipe);
|
||||
myCb.insertRecipe("Kanapka z sererm",
|
||||
"Chleb, margaryna, ser",
|
||||
"Posmarować kanapke margaryną i położyć na niej plaster sera",
|
||||
"kanapka, ser, chleb");
|
||||
"kanapka, ser, chleb", imgDefaultRecipe);
|
||||
myCb.closeConnection();
|
||||
}
|
||||
public static void fillList(){
|
||||
@@ -45,11 +46,13 @@ public class RetrieveData extends FrmMain{
|
||||
txtIngredients.setText(myRecipe.getIngredients());
|
||||
txtDescription.setText(myRecipe.getDescription());
|
||||
txtTags.setText(myRecipe.getTags());
|
||||
imgRecipe.setIcon(myRecipe.getImage());
|
||||
myCb.closeConnection();
|
||||
}
|
||||
public static void addNewRecipe(){
|
||||
Cookbook myCb = new Cookbook();
|
||||
myCb.insertRecipe(txtEdTitle.getText(), txtIngredients.getText(), txtDescription.getText(), txtTags.getText());
|
||||
myCb.insertRecipe(txtEdTitle.getText(), txtIngredients.getText(), txtDescription.getText(), txtTags.getText(), (ImageIcon)imgRecipe.getIcon());
|
||||
|
||||
myCb.closeConnection();
|
||||
}
|
||||
public static void removeRecipe(String name){
|
||||
|
||||
Reference in New Issue
Block a user