Browse Source

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
master
Piotr Dergun 10 years ago
parent
commit
239d26ff9a
8 changed files with 122 additions and 53 deletions
  1. +1
    -5
      .classpath
  2. +3
    -3
      .settings/org.eclipse.jdt.core.prefs
  3. BIN
      cookbook.db
  4. +33
    -6
      src/main/java/db/Cookbook.java
  5. +11
    -2
      src/main/java/db/Recipe.java
  6. +8
    -0
      src/main/java/ksiazka/kucharska/App.java
  7. +60
    -34
      src/main/java/ksiazka/kucharska/FrmMain.java
  8. +6
    -3
      src/main/java/ksiazka/kucharska/RetrieveData.java

+ 1
- 5
.classpath View File

@ -12,11 +12,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

+ 3
- 3
.settings/org.eclipse.jdt.core.prefs View File

@ -1,12 +1,12 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.source=1.7

BIN
cookbook.db View File


+ 33
- 6
src/main/java/db/Cookbook.java View File

@ -1,9 +1,15 @@
package db; package db;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import db.Recipe; import db.Recipe;
public class Cookbook { public class Cookbook {
@ -35,7 +41,7 @@ public class Cookbook {
} }
public boolean createTables() { 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 { try {
stat.execute(createRecipes); stat.execute(createRecipes);
@ -47,20 +53,40 @@ public class Cookbook {
return true; 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 { 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( PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?);");
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
prepStmt.setString(1, name); prepStmt.setString(1, name);
prepStmt.setString(2, ingredients); prepStmt.setString(2, ingredients);
prepStmt.setString(3, description); prepStmt.setString(3, description);
prepStmt.setString(4, tags); prepStmt.setString(4, tags);
prepStmt.setBytes(5, bStream.toByteArray());
prepStmt.execute(); 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"); System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
return true; return true;
} }
@ -72,11 +98,13 @@ public class Cookbook {
ResultSet result = query.executeQuery(); ResultSet result = query.executeQuery();
int id; int id;
String ingredients, description, tags; String ingredients, description, tags;
ImageIcon pict;
id = result.getInt("id"); id = result.getInt("id");
ingredients = result.getString("ingredients"); ingredients = result.getString("ingredients");
description = result.getString("description"); description = result.getString("description");
tags = result.getString("tags"); 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -89,7 +117,6 @@ public class Cookbook {
try { try {
PreparedStatement query = conn.prepareStatement("DELETE FROM recipes WHERE name=?"); PreparedStatement query = conn.prepareStatement("DELETE FROM recipes WHERE name=?");
query.setString(1, name); query.setString(1, name);
System.out.println(name);
query.execute(); query.execute();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();

+ 11
- 2
src/main/java/db/Recipe.java View File

@ -1,11 +1,14 @@
package db; package db;
import javax.swing.ImageIcon;
public class Recipe { public class Recipe {
private int id; private int id;
private String name; private String name;
private String ingredients; private String ingredients;
private String description; private String description;
private String tags; private String tags;
private ImageIcon img;
public int getId() { public int getId() {
return id; return id;
@ -34,15 +37,21 @@ public class Recipe {
public void setTags(String tags) { public void setTags(String tags) {
this.tags = tags; this.tags = tags;
} }
public ImageIcon getImage() {
return img;
}
public void setImage(ImageIcon img) {
this.img = img;
}
public Recipe() {} 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.id = id;
this.name = name; this.name = name;
this.ingredients = ingredients; this.ingredients = ingredients;
this.description = description; this.description = description;
this.tags = tags; this.tags = tags;
this.img = img;
} }

+ 8
- 0
src/main/java/ksiazka/kucharska/App.java View File

@ -1,6 +1,8 @@
package ksiazka.kucharska; package ksiazka.kucharska;
import java.awt.Dimension;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.Toolkit;
/** /**
* Hello world! * Hello world!
@ -16,6 +18,12 @@ public class App
try { try {
System.out.println( "tworzę fMain..." ); System.out.println( "tworzę fMain..." );
FrmMain fMain = new FrmMain(); 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); fMain.setVisible(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

+ 60
- 34
src/main/java/ksiazka/kucharska/FrmMain.java View File

@ -10,6 +10,7 @@ import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane; import javax.swing.JSplitPane;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JTextField; import javax.swing.JTextField;
@ -18,7 +19,6 @@ import java.awt.Graphics2D;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets; import java.awt.Insets;
import java.awt.RenderingHints; import java.awt.RenderingHints;
@ -37,6 +37,7 @@ import javax.swing.SwingConstants;
import ksiazka.kucharska.RetrieveData; import ksiazka.kucharska.RetrieveData;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FrmMain extends JFrame { public class FrmMain extends JFrame {
@ -58,12 +59,13 @@ public class FrmMain extends JFrame {
private JButton btnLoadImg; private JButton btnLoadImg;
public static JLabel lblRecipeTitle; public static JLabel lblRecipeTitle;
private static DefaultListModel recipesListModel; private static DefaultListModel recipesListModel;
private JLabel imgRecipe;
public static JLabel imgRecipe;
private JFileChooser fcImg; private JFileChooser fcImg;
private ImageIcon imgDefaultRecipe;
public static ImageIcon imgDefaultRecipe;
private JList recipesList; private JList recipesList;
void DeactivateAddMenu() { void DeactivateAddMenu() {
btnAddRecipe.setEnabled(true);
btnCancel.setVisible(false); btnCancel.setVisible(false);
btnDelRecipe.setEnabled(true); btnDelRecipe.setEnabled(true);
btnLoadImg.setVisible(false); btnLoadImg.setVisible(false);
@ -78,6 +80,7 @@ public class FrmMain extends JFrame {
} }
void ActivateAddMenu() { void ActivateAddMenu() {
btnAddRecipe.setEnabled(false);
btnCancel.setVisible(true); btnCancel.setVisible(true);
btnDelRecipe.setEnabled(false); btnDelRecipe.setEnabled(false);
btnLoadImg.setVisible(true); btnLoadImg.setVisible(true);
@ -89,13 +92,14 @@ public class FrmMain extends JFrame {
txtTags.setEditable(true); txtTags.setEditable(true);
lblRecipeTitle.setVisible(false); lblRecipeTitle.setVisible(false);
recipesList.setEnabled(false); recipesList.setEnabled(false);
imgRecipe.setIcon(imgDefaultRecipe);
//czyszczenie pól formularza //czyszczenie pól formularza
txtDescription.setText(""); txtDescription.setText("");
txtEdTitle.setText(""); txtEdTitle.setText("");
txtIngredients.setText(""); txtIngredients.setText("");
txtTags.setText(""); txtTags.setText("");
} }
public static DefaultListModel getRecipesList() public static DefaultListModel getRecipesList()
@ -110,7 +114,7 @@ public class FrmMain extends JFrame {
setResizable(false); setResizable(false);
setTitle("Książka Kucharska v1.0"); setTitle("Książka Kucharska v1.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 549);
setBounds(100, 100, 723, 549);
contentPane = new JPanel(); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0)); contentPane.setLayout(new BorderLayout(0, 0));
@ -191,8 +195,23 @@ public class FrmMain extends JFrame {
DefaultListModel model = (DefaultListModel) recipesList.getModel(); DefaultListModel model = (DefaultListModel) recipesList.getModel();
if (recipesList.getSelectedIndex() != -1) 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() { btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
RetrieveData.addNewRecipe(); RetrieveData.addNewRecipe();
DeactivateAddMenu();
DefaultListModel model = (DefaultListModel) recipesList.getModel(); DefaultListModel model = (DefaultListModel) recipesList.getModel();
model.addElement(txtEdTitle.getText()); model.addElement(txtEdTitle.getText());
DeactivateAddMenu();
recipesList.setSelectedIndex(recipesList.getModel().getSize()-1);
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
} }
}); });
btnSave.setVisible(false); btnSave.setVisible(false);
@ -217,7 +238,7 @@ public class FrmMain extends JFrame {
txtEdTitle = new JTextField(); txtEdTitle = new JTextField();
txtEdTitle.setVisible(false); txtEdTitle.setVisible(false);
txtEdTitle.setBounds(12, 15, 295, 19);
txtEdTitle.setBounds(26, 15, 281, 19);
panel_2.add(txtEdTitle); panel_2.add(txtEdTitle);
txtEdTitle.setColumns(10); txtEdTitle.setColumns(10);
@ -253,38 +274,41 @@ public class FrmMain extends JFrame {
btnLoadImg = new JButton("Wczytaj"); btnLoadImg = new JButton("Wczytaj");
fcImg = new JFileChooser(); 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"); imgDefaultRecipe = new ImageIcon("no_img_recipe.jpg");
imgRecipe.setIcon(imgDefaultRecipe); imgRecipe.setIcon(imgDefaultRecipe);
btnLoadImg.addActionListener(new ActionListener() { btnLoadImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
//otwórz okno dialogowe
int returnVal = fcImg.showOpenDialog(FrmMain.this); 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); btnLoadImg.setVisible(false);
@ -305,6 +329,8 @@ public class FrmMain extends JFrame {
btnCancel.addActionListener(new ActionListener() { btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
DeactivateAddMenu(); DeactivateAddMenu();
if(recipesList.getSelectedIndex() != -1)
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
} }
}); });
btnCancel.setVisible(false); btnCancel.setVisible(false);

+ 6
- 3
src/main/java/ksiazka/kucharska/RetrieveData.java View File

@ -3,6 +3,7 @@ package ksiazka.kucharska;
import java.util.List; import java.util.List;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import db.Cookbook; import db.Cookbook;
import db.Recipe; import db.Recipe;
@ -18,11 +19,11 @@ public class RetrieveData extends FrmMain{
myCb.insertRecipe("Kanapka", myCb.insertRecipe("Kanapka",
"Chleb, margaryna, szynka", "Chleb, margaryna, szynka",
"Posmarować kanapke margaryną i położyć na niej plaster szynki", "Posmarować kanapke margaryną i położyć na niej plaster szynki",
"kanapka, szynka, chleb");
"kanapka, szynka, chleb", imgDefaultRecipe);
myCb.insertRecipe("Kanapka z sererm", myCb.insertRecipe("Kanapka z sererm",
"Chleb, margaryna, ser", "Chleb, margaryna, ser",
"Posmarować kanapke margaryną i położyć na niej plaster sera", "Posmarować kanapke margaryną i położyć na niej plaster sera",
"kanapka, ser, chleb");
"kanapka, ser, chleb", imgDefaultRecipe);
myCb.closeConnection(); myCb.closeConnection();
} }
public static void fillList(){ public static void fillList(){
@ -45,11 +46,13 @@ public class RetrieveData extends FrmMain{
txtIngredients.setText(myRecipe.getIngredients()); txtIngredients.setText(myRecipe.getIngredients());
txtDescription.setText(myRecipe.getDescription()); txtDescription.setText(myRecipe.getDescription());
txtTags.setText(myRecipe.getTags()); txtTags.setText(myRecipe.getTags());
imgRecipe.setIcon(myRecipe.getImage());
myCb.closeConnection(); myCb.closeConnection();
} }
public static void addNewRecipe(){ public static void addNewRecipe(){
Cookbook myCb = new Cookbook(); 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(); myCb.closeConnection();
} }
public static void removeRecipe(String name){ public static void removeRecipe(String name){

Loading…
Cancel
Save