mergowanie pracy mojej i Grześka
This commit is contained in:
@@ -24,5 +24,6 @@
|
|||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="lib" path="miglayout15-swing.jar" sourcepath="miglayout-src.zip"/>
|
<classpathentry kind="lib" path="miglayout15-swing.jar" sourcepath="miglayout-src.zip"/>
|
||||||
<classpathentry kind="lib" path="forms-1.3.0.jar" sourcepath="forms-1.3.0-src.zip"/>
|
<classpathentry kind="lib" path="forms-1.3.0.jar" sourcepath="forms-1.3.0-src.zip"/>
|
||||||
|
<classpathentry kind="lib" path="D:/Documents and Settings/kozak/Pulpit/sqlite-jdbc-3.7.2.jar" sourcepath="D:/Documents and Settings/kozak/Pulpit/sqlite-jdbc-3.7.2-javadoc.jar"/>
|
||||||
<classpathentry kind="output" path="target/classes"/>
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|||||||
125
src/main/java/db/Cookbook.java
Normal file
125
src/main/java/db/Cookbook.java
Normal file
@@ -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<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() {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Problem z zamknieciem polaczenia");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/main/java/db/Recipe.java
Normal file
49
src/main/java/db/Recipe.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import javax.imageio.ImageIO;
|
|||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JFileChooser;
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JSplitPane;
|
import javax.swing.JSplitPane;
|
||||||
@@ -33,6 +34,9 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
|
import ksiazka.kucharska.RetrieveData;
|
||||||
|
import javax.swing.event.ListSelectionListener;
|
||||||
|
import javax.swing.event.ListSelectionEvent;
|
||||||
|
|
||||||
public class FrmMain extends JFrame {
|
public class FrmMain extends JFrame {
|
||||||
|
|
||||||
@@ -42,20 +46,19 @@ 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;
|
public static JTextField txtEdTitle;
|
||||||
private JTextField txtTags;
|
public static JTextField txtTags;
|
||||||
private JButton btnSave;
|
private JButton btnSave;
|
||||||
private JButton btnCancel;
|
private JButton btnCancel;
|
||||||
private JTextPane txtIngredients;
|
public static JTextPane txtIngredients;
|
||||||
private JTextPane txtDescription;
|
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 JLabel imgRecipe;
|
private static DefaultListModel recipesListModel;
|
||||||
private JFileChooser fcImg;
|
|
||||||
private ImageIcon imgDefaultRecipe;
|
|
||||||
|
|
||||||
void DeactivateAddMenu() {
|
void DeactivateAddMenu() {
|
||||||
btnCancel.setVisible(false);
|
btnCancel.setVisible(false);
|
||||||
@@ -89,6 +92,11 @@ public class FrmMain extends JFrame {
|
|||||||
txtTags.setText("");
|
txtTags.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DefaultListModel getRecipesList()
|
||||||
|
{
|
||||||
|
return recipesListModel;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
@@ -136,7 +144,14 @@ public class FrmMain extends JFrame {
|
|||||||
gbc_btnSearch.gridy = 0;
|
gbc_btnSearch.gridy = 0;
|
||||||
panel.add(btnSearch, gbc_btnSearch);
|
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();
|
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;
|
||||||
@@ -156,29 +171,38 @@ 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();
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
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) {
|
||||||
System.out.println("sprawdzam ikonę...");
|
RetrieveData.addNewRecipe();
|
||||||
if (imgRecipe.getIcon() == imgDefaultRecipe.getImage())
|
|
||||||
System.out.println("Ta sama ikona");
|
|
||||||
DeactivateAddMenu();
|
DeactivateAddMenu();
|
||||||
|
DefaultListModel model = (DefaultListModel) recipesList.getModel();
|
||||||
|
model.addElement(txtEdTitle.getText());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
btnSave.setVisible(false);
|
btnSave.setVisible(false);
|
||||||
@@ -281,5 +305,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);
|
||||||
|
|
||||||
|
//RetrieveData.insertSampleData();
|
||||||
|
RetrieveData.fillList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user