Browse Source

Dodawanie/usuwanie przepisów

master
Grzegorz 10 years ago
parent
commit
3bb95fb890
3 changed files with 75 additions and 27 deletions
  1. +42
    -13
      src/main/java/db/Cookbook.java
  2. +1
    -4
      src/main/java/db/Recipe.java
  3. +32
    -10
      src/main/java/ksiazka/kucharska/FrmMain.java

+ 42
- 13
src/main/java/db/Cookbook.java View File

@ -64,25 +64,54 @@ public class Cookbook {
return true; return true;
} }
public List<Recipe> selectRecipes() {
List<Recipe> recipes = new LinkedList<Recipe>();
public Recipe selectRecipe(String name) {
Recipe selectedRecipe;
try { try {
ResultSet result = stat.executeQuery("SELECT * FROM recipes");
PreparedStatement query = conn.prepareStatement("SELECT * FROM recipes WHERE name=?");
query.setString(1, name);
ResultSet result = query.executeQuery();
int id; int id;
String name, ingredients, description, tags;
while(result.next()) {
id = result.getInt("id");
name = result.getString("name");
ingredients = result.getString("ingredients");
description = result.getString("description");
tags = result.getString("tags");
recipes.add(new Recipe(id, name, ingredients, description, tags));
}
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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
return recipes;
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<String> selectNames(){
List<String> names = new LinkedList<String>();
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() { public void closeConnection() {

+ 1
- 4
src/main/java/db/Recipe.java View File

@ -10,13 +10,10 @@ public class Recipe {
public int getId() { public int getId() {
return id; return id;
} }
public void setId(int id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }
public void setImie(String name) {
public void setName(String name) {
this.name = name; this.name = name;
} }
public String getIngredients() { public String getIngredients() {

+ 32
- 10
src/main/java/ksiazka/kucharska/FrmMain.java View File

@ -22,7 +22,9 @@ import javax.swing.border.LineBorder;
import java.awt.SystemColor; import java.awt.SystemColor;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import ksiazka.kucharska.retrieveTest;
import ksiazka.kucharska.RetrieveData;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class FrmMain extends JFrame { public class FrmMain extends JFrame {
@ -32,17 +34,17 @@ public class FrmMain extends JFrame {
private static final long serialVersionUID = 2634383304749703669L; private static final long serialVersionUID = 2634383304749703669L;
private JPanel contentPane; private JPanel contentPane;
private JTextField txtSearch; private JTextField txtSearch;
private JTextField txtEdTitle;
private JTextField txtTags;
public static JTextField txtEdTitle;
public static JTextField txtTags;
private JButton btnSave; private JButton btnSave;
private JButton btnCancel; private JButton btnCancel;
private JTextPane txtIngredients;
private JTextPane txtDescription;
public static JTextPane txtIngredients;
public static JTextPane txtDescription;
private JButton btnAddRecipe; private JButton btnAddRecipe;
private JButton btnSearch; private JButton btnSearch;
private JButton btnDelRecipe; private JButton btnDelRecipe;
private JButton btnLoadImg; private JButton btnLoadImg;
private JLabel lblRecipeTitle;
public static JLabel lblRecipeTitle;
private static DefaultListModel recipesListModel; private static DefaultListModel recipesListModel;
@ -126,7 +128,13 @@ public class FrmMain extends JFrame {
panel.add(btnSearch, gbc_btnSearch); panel.add(btnSearch, gbc_btnSearch);
recipesListModel = new DefaultListModel(); recipesListModel = new DefaultListModel();
JList recipesList = new JList(recipesListModel);
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(); GridBagConstraints gbc_recipesList = new GridBagConstraints();
gbc_recipesList.insets = new Insets(0, 0, 5, 0); gbc_recipesList.insets = new Insets(0, 0, 5, 0);
gbc_recipesList.gridwidth = 2; gbc_recipesList.gridwidth = 2;
@ -146,7 +154,7 @@ public class FrmMain extends JFrame {
panel.add(panel_1, gbc_panel_1); panel.add(panel_1, gbc_panel_1);
panel_1.setLayout(new GridLayout(0, 2, 0, 0)); panel_1.setLayout(new GridLayout(0, 2, 0, 0));
btnAddRecipe = new JButton("Dodaj");
btnAddRecipe = new JButton("Nowy");
btnAddRecipe.addActionListener(new ActionListener() { btnAddRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ActivateAddMenu(); ActivateAddMenu();
@ -155,16 +163,29 @@ public class FrmMain extends JFrame {
panel_1.add(btnAddRecipe); panel_1.add(btnAddRecipe);
btnDelRecipe = new JButton("Usuń"); 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); panel_1.add(btnDelRecipe);
JPanel panel_2 = new JPanel(); JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2); splitPane.setRightComponent(panel_2);
panel_2.setLayout(null); panel_2.setLayout(null);
btnSave = new JButton("Zapisz");
btnSave = new JButton("Dodaj");
btnSave.addActionListener(new ActionListener() { btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
RetrieveData.addNewRecipe();
DeactivateAddMenu(); DeactivateAddMenu();
DefaultListModel model = (DefaultListModel) recipesList.getModel();
model.addElement(txtEdTitle.getText());
} }
}); });
btnSave.setVisible(false); btnSave.setVisible(false);
@ -230,6 +251,7 @@ public class FrmMain extends JFrame {
btnCancel.setBounds(319, 46, 117, 25); btnCancel.setBounds(319, 46, 117, 25);
panel_2.add(btnCancel); panel_2.add(btnCancel);
retrieveTest.retrieveData();
//RetrieveData.insertSampleData();
RetrieveData.fillList();
} }
} }

Loading…
Cancel
Save