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"/>
</attributes>
</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">
<attributes>
<attribute name="maven.pomderived" value="true"/>

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

@ -1,12 +1,12 @@
eclipse.preferences.version=1
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.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.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
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;
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();

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

@ -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;
}

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

@ -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();

+ 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.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);

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

@ -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){

Loading…
Cancel
Save