diff --git a/.classpath b/.classpath
index aab3aec..a868522 100644
--- a/.classpath
+++ b/.classpath
@@ -24,5 +24,6 @@
+
diff --git a/src/main/java/db/Cookbook.java b/src/main/java/db/Cookbook.java
new file mode 100644
index 0000000..7d67952
--- /dev/null
+++ b/src/main/java/db/Cookbook.java
@@ -0,0 +1,125 @@
+package db;
+
+import java.sql.*;
+import java.util.LinkedList;
+import java.util.List;
+
+import db.Recipe;
+
+public class Cookbook {
+
+ public static final String DRIVER = "org.sqlite.JDBC";
+ public static final String DB_URL = "jdbc:sqlite:cookbook";
+
+ private Connection conn;
+ private Statement stat;
+
+
+ public Cookbook() {
+ try {
+ Class.forName(Cookbook.DRIVER);
+ } catch (ClassNotFoundException e) {
+ System.err.println("Brak sterownika JDBC");
+ e.printStackTrace();
+ }
+
+ try {
+ conn = DriverManager.getConnection(DB_URL);
+ stat = conn.createStatement();
+ } catch (SQLException e) {
+ System.err.println("Problem z otwarciem polaczenia");
+ e.printStackTrace();
+ }
+
+ 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)";
+
+ try {
+ stat.execute(createRecipes);
+ } catch (SQLException e) {
+ System.err.println("Blad przy tworzeniu tabeli");
+ e.printStackTrace();
+ return false;
+ }
+ return true;
+ }
+
+ public boolean insertRecipe(String name, String ingredients, String description, String tags) {
+ try {
+ PreparedStatement prepStmt = conn.prepareStatement(
+ "INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?);");
+ prepStmt.setString(1, name);
+ prepStmt.setString(2, ingredients);
+ prepStmt.setString(3, description);
+ prepStmt.setString(4, tags);
+ prepStmt.execute();
+ } catch (SQLException e) {
+ System.err.println("Blad przy wstawianiu przepisu");
+ e.printStackTrace();
+ return false;
+ }
+ return true;
+ }
+
+ public Recipe selectRecipe(String name) {
+ Recipe selectedRecipe;
+ try {
+ PreparedStatement query = conn.prepareStatement("SELECT * FROM recipes WHERE name=?");
+ query.setString(1, name);
+ ResultSet result = query.executeQuery();
+ int id;
+ String ingredients, description, tags;
+ id = result.getInt("id");
+ ingredients = result.getString("ingredients");
+ description = result.getString("description");
+ tags = result.getString("tags");
+ selectedRecipe = new Recipe(id, name, ingredients, description, tags);
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ return selectedRecipe;
+ }
+
+ public boolean deleteRecipe(String name){
+ 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();
+ return false;
+ }
+ return true;
+ }
+
+ public List selectNames(){
+ List names = new LinkedList();
+ try {
+ ResultSet result = stat.executeQuery("SELECT name FROM recipes");
+ String name;
+ while(result.next()){
+ name = result.getString("name");
+ names.add(name);
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ return names;
+ }
+
+ public void closeConnection() {
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ System.err.println("Problem z zamknieciem polaczenia");
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/main/java/db/Recipe.java b/src/main/java/db/Recipe.java
new file mode 100644
index 0000000..3ed4472
--- /dev/null
+++ b/src/main/java/db/Recipe.java
@@ -0,0 +1,49 @@
+package db;
+
+public class Recipe {
+ private int id;
+ private String name;
+ private String ingredients;
+ private String description;
+ private String tags;
+
+ public int getId() {
+ return id;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getIngredients() {
+ return ingredients;
+ }
+ public void setIngredients(String ingredients) {
+ this.ingredients = ingredients;
+ }
+ public String getDescription() {
+ return description;
+ }
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ public String getTags() {
+ return tags;
+ }
+ public void setTags(String tags) {
+ this.tags = tags;
+ }
+
+
+ public Recipe() {}
+ public Recipe(int id, String name, String ingredients, String description, String tags) {
+ this.id = id;
+ this.name = name;
+ this.ingredients = ingredients;
+ this.description = description;
+ this.tags = tags;
+ }
+
+
+}
diff --git a/src/main/java/ksiazka/kucharska/FrmMain.java b/src/main/java/ksiazka/kucharska/FrmMain.java
index 5ce9538..da823a0 100644
--- a/src/main/java/ksiazka/kucharska/FrmMain.java
+++ b/src/main/java/ksiazka/kucharska/FrmMain.java
@@ -7,6 +7,7 @@ import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
+import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JSplitPane;
@@ -33,6 +34,9 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.SwingConstants;
+import ksiazka.kucharska.RetrieveData;
+import javax.swing.event.ListSelectionListener;
+import javax.swing.event.ListSelectionEvent;
public class FrmMain extends JFrame {
@@ -42,20 +46,19 @@ public class FrmMain extends JFrame {
private static final long serialVersionUID = 2634383304749703669L;
private JPanel contentPane;
private JTextField txtSearch;
- private JTextField txtEdTitle;
- private JTextField txtTags;
+ public static JTextField txtEdTitle;
+ public static JTextField txtTags;
private JButton btnSave;
private JButton btnCancel;
- private JTextPane txtIngredients;
- private JTextPane txtDescription;
+ public static JTextPane txtIngredients;
+ public static JTextPane txtDescription;
private JButton btnAddRecipe;
private JButton btnSearch;
private JButton btnDelRecipe;
private JButton btnLoadImg;
- private JLabel lblRecipeTitle;
- private JLabel imgRecipe;
- private JFileChooser fcImg;
- private ImageIcon imgDefaultRecipe;
+ public static JLabel lblRecipeTitle;
+ private static DefaultListModel recipesListModel;
+
void DeactivateAddMenu() {
btnCancel.setVisible(false);
@@ -89,6 +92,11 @@ public class FrmMain extends JFrame {
txtTags.setText("");
}
+ public static DefaultListModel getRecipesList()
+ {
+ return recipesListModel;
+ }
+
/**
* Create the frame.
*/
@@ -136,7 +144,14 @@ public class FrmMain extends JFrame {
gbc_btnSearch.gridy = 0;
panel.add(btnSearch, gbc_btnSearch);
- JList recipesList = new JList();
+ recipesListModel = new DefaultListModel();
+ final JList recipesList = new JList(recipesListModel);
+ recipesList.addListSelectionListener(new ListSelectionListener() {
+ public void valueChanged(ListSelectionEvent e){
+ if(recipesList.getSelectedIndex() != -1)
+ RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
+ }
+ });
GridBagConstraints gbc_recipesList = new GridBagConstraints();
gbc_recipesList.insets = new Insets(0, 0, 5, 0);
gbc_recipesList.gridwidth = 2;
@@ -156,29 +171,38 @@ public class FrmMain extends JFrame {
panel.add(panel_1, gbc_panel_1);
panel_1.setLayout(new GridLayout(0, 2, 0, 0));
- btnAddRecipe = new JButton("Dodaj");
+ btnAddRecipe = new JButton("Nowy");
btnAddRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ActivateAddMenu();
-
}
});
panel_1.add(btnAddRecipe);
btnDelRecipe = new JButton("Usuń");
+ btnDelRecipe.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ DefaultListModel model = (DefaultListModel) recipesList.getModel();
+ if (recipesList.getSelectedIndex() != -1)
+ {
+ RetrieveData.removeRecipe(recipesList.getSelectedValue().toString());
+ model.remove(recipesList.getSelectedIndex());
+ }
+ }
+ });
panel_1.add(btnDelRecipe);
JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
panel_2.setLayout(null);
- btnSave = new JButton("Zapisz");
+ btnSave = new JButton("Dodaj");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
- System.out.println("sprawdzam ikonę...");
- if (imgRecipe.getIcon() == imgDefaultRecipe.getImage())
- System.out.println("Ta sama ikona");
+ RetrieveData.addNewRecipe();
DeactivateAddMenu();
+ DefaultListModel model = (DefaultListModel) recipesList.getModel();
+ model.addElement(txtEdTitle.getText());
}
});
btnSave.setVisible(false);
@@ -281,5 +305,7 @@ public class FrmMain extends JFrame {
btnCancel.setBounds(319, 46, 117, 25);
panel_2.add(btnCancel);
+ //RetrieveData.insertSampleData();
+ RetrieveData.fillList();
}
}