Browse Source

MVC Implementation

master
Grzegorz 10 years ago
parent
commit
17d917cc3f
8 changed files with 521 additions and 28 deletions
  1. +6
    -5
      src/main/java/db/Cookbook.java
  2. +8
    -7
      src/main/java/ksiazka/kucharska/App.java
  3. +211
    -0
      src/main/java/ksiazka/kucharska/CB_Controller.java
  4. +4
    -5
      src/main/java/ksiazka/kucharska/CB_Model.java
  5. +285
    -0
      src/main/java/ksiazka/kucharska/CB_View.java
  6. +0
    -1
      src/main/java/ksiazka/kucharska/FrmMain.java
  7. +4
    -5
      src/main/java/ksiazka/kucharska/RecipeBuilder.java
  8. +3
    -5
      src/main/java/ksiazka/kucharska/RetrieveData.java

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

@ -10,7 +10,8 @@ import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import db.Recipe;
import ksiazka.kucharska.CB_Model;
public class Cookbook {
@ -82,7 +83,7 @@ public class Cookbook {
return true;
}
public boolean insertRecipe(Recipe ob) {
public boolean insertRecipe(CB_Model ob) {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
@ -103,8 +104,8 @@ public class Cookbook {
return true;
}
public Recipe selectRecipe(String name) {
Recipe selectedRecipe;
public CB_Model selectRecipe(String name) {
CB_Model selectedRecipe;
try {
PreparedStatement query = conn.prepareStatement("SELECT * FROM recipes WHERE name=?");
query.setString(1, name);
@ -117,7 +118,7 @@ public class Cookbook {
description = result.getString("description");
tags = result.getString("tags");
pict = new ImageIcon(result.getBytes("img"));
selectedRecipe = new Recipe(id, name, ingredients, description, tags, pict);
selectedRecipe = new CB_Model(id, name, ingredients, description, tags, pict);
} catch (SQLException e) {
e.printStackTrace();

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

@ -16,15 +16,16 @@ public class App
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
System.out.println( "tworzę fMain..." );
FrmMain fMain = new FrmMain();
CB_View theView = new CB_View();
CB_Model theModel = new CB_Model();
CB_Controller theController = new CB_Controller(theView, theModel);
//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);
int x = (screenSize.width - theView.getWidth()) / 2;
int y = (screenSize.height - theView.getHeight()) / 2;
theView.setLocation(x, y);
theView.setVisible(true);
theController.fillList();
} catch (Exception e) {
e.printStackTrace();
}

+ 211
- 0
src/main/java/ksiazka/kucharska/CB_Controller.java View File

@ -0,0 +1,211 @@
package ksiazka.kucharska;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import db.Cookbook;
public class CB_Controller {
private static CB_View theView;
private static CB_Model theModel;
public CB_Controller(CB_View theView, CB_Model theModel) {
CB_Controller.theView = theView;
CB_Controller.theModel = theModel;
CB_Controller.theView.addListener(new AddListener());
CB_Controller.theView.delListener(new DelListener());
CB_Controller.theView.loadListener(new LoadListener());
CB_Controller.theView.cancelListener(new CancelListener());
CB_Controller.theView.saveListener(new SaveListener());
CB_Controller.theView.listSelectListener(new SelectionListener());
}
public void fillList(){
Cookbook myCb = new Cookbook();
List<String> names = myCb.selectNames();
theView.setRecipesList(names);
myCb.closeConnection();
}
public static void fillComponents(String name){
Cookbook myCb = new Cookbook();
theModel = myCb.selectRecipe(name);
theView.setRecipeTitle(theModel.getName());
theView.setRecipeIngredients(theModel.getIngredients());
theView.setRecipeDescription(theModel.getDescription());
theView.setRecipeTags(theModel.getTags());
theView.setRecipeIcon(theModel.getImage());
theView.resetCaretPositions();
myCb.closeConnection();
}
public static void addNewRecipe(){
Cookbook myCb = new Cookbook();
RecipeManager rm = new RecipeManager();
rm.setRecipeBuilder(new NewRecipeBuilder());
rm.constructRecipe();
CB_Model obj = rm.getRecipe();
myCb.insertRecipe(obj);
myCb.closeConnection();
}
public static void removeRecipe(String name){
Cookbook myCb = new Cookbook();
myCb.deleteRecipe(name);
myCb.closeConnection();
}
class AddListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
theView.ActivateAddMenu();
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class DelListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JList recipesList = theView.getRecipesList();
DefaultListModel model = (DefaultListModel) recipesList.getModel();
if (recipesList.getSelectedIndex() != -1)
{
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
{
theView.setRecipeTitle("Nazwa przepisu");
theView.setRecipeIngredients("");
theView.setRecipeDescription("");
theView.setRecipeTags("");
}
}
}
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class LoadListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JFileChooser fcImg = theView.getFileChooser();
int returnVal = fcImg.showOpenDialog(theView);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fcImg.getSelectedFile();
try {
BufferedImage srcImg = ImageIO.read(file);
JLabel imgRecipe = theView.getImgRecipe();
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 e1) {
e1.printStackTrace();
}
}
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class CancelListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
theView.DeactivateAddMenu();
JList recipesList = theView.getRecipesList();
if(recipesList.getSelectedIndex() != -1)
fillComponents(recipesList.getSelectedValue().toString());
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
addNewRecipe();
/*JList recipesList = theView.getRecipesList();
DefaultListModel model = (DefaultListModel) recipesList.getModel();
model.addElement(theView.getRecipeTitle());*/
theView.DeactivateAddMenu();
/*recipesList.setSelectedIndex(recipesList.getModel().getSize()-1);
fillComponents(recipesList.getSelectedValue().toString());*/
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
class SelectionListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
try{
JList recipesList = theView.getRecipesList();
if(recipesList.getSelectedIndex() != -1)
fillComponents(recipesList.getSelectedValue().toString());
}
catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
}

src/main/java/db/Recipe.java → src/main/java/ksiazka/kucharska/CB_Model.java View File

@ -1,4 +1,4 @@
package db;
package ksiazka.kucharska;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
@ -7,7 +7,7 @@ import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Recipe {
public class CB_Model {
private int id;
private String name;
private String ingredients;
@ -50,7 +50,6 @@ public class Recipe {
}
public byte[] getImageArr() {
try {
//przekształcam ImageIcon -> BufferedImage
BufferedImage bImg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
bImg.getGraphics().drawImage(img.getImage(), 0,0, null);
@ -71,8 +70,8 @@ public class Recipe {
}
}
public Recipe() {}
public Recipe(int id, String name, String ingredients, String description, String tags, ImageIcon img) {
public CB_Model() {}
public CB_Model(int id, String name, String ingredients, String description, String tags, ImageIcon img) {
this.id = id;
this.name = name;
this.ingredients = ingredients;

+ 285
- 0
src/main/java/ksiazka/kucharska/CB_View.java View File

@ -0,0 +1,285 @@
package ksiazka.kucharska;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
public class CB_View extends JFrame{
private static final long serialVersionUID = 2634383304749703669L;
private JTextField txtSearch = new JTextField();
private JTextField txtEdTitle = new JTextField();
private JTextField txtTags = new JTextField();
private JTextArea txtDescription = new JTextArea();
private JTextArea txtIngredients = new JTextArea();
private JButton btnAddRecipe = new JButton("Nowy");
private JButton btnDelRecipe = new JButton("Usuń");
private JButton btnLoadImg = new JButton("Wczytaj");
private JButton btnCancel = new JButton("Anuluj");
private JButton btnSave = new JButton("Dodaj");
private ImageIcon imgDefaultRecipe = new ImageIcon();
private JScrollPane scrollPane_1 = new JScrollPane();
private JScrollPane scrollPane_2 = new JScrollPane();
private JSplitPane splitPane = new JSplitPane();
private DefaultListModel recipesListModel = new DefaultListModel();
private JList recipesList = new JList(recipesListModel);
private JLabel lblIngredients = new JLabel("Składniki:");
private JLabel lblDescription = new JLabel("Opis wykonania:");
private JLabel imgRecipe = new JLabel("");
private JLabel lblRecipeTitle = new JLabel("Nazwa przepisu");
private JLabel lblTags = new JLabel("Tagi:");
private JFileChooser fcImg = new JFileChooser();
private JScrollPane scrollPane = new JScrollPane();
CB_View(){
this.setResizable(false);
this.setTitle("Książka Kucharska v1.0");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 723, 549);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
this.setContentPane(contentPane);
splitPane.setResizeWeight(0.1);
splitPane.setRequestFocusEnabled(false);
splitPane.setPreferredSize(new Dimension(200, 27));
splitPane.setDividerSize(5);
splitPane.setEnabled(false);
contentPane.add(splitPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setMaximumSize(new Dimension(100, 32767));
splitPane.setLeftComponent(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] {0, 0};
gbl_panel.rowHeights = new int[] {0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0};
panel.setLayout(gbl_panel);
GridBagConstraints gbc_txtSearch = new GridBagConstraints();
gbc_txtSearch.gridwidth = 2;
gbc_txtSearch.insets = new Insets(5, 5, 5, 0);
gbc_txtSearch.fill = GridBagConstraints.HORIZONTAL;
gbc_txtSearch.gridx = 0;
gbc_txtSearch.gridy = 0;
txtSearch.setColumns(10);
scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setMaximumSize(new Dimension(100, 100));
scrollPane_2.setViewportView(recipesList);
GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
gbc_scrollPane_2.gridwidth = 2;
gbc_scrollPane_2.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 0;
gbc_scrollPane_2.gridy = 1;
GridBagConstraints gbc_btnAddRecipe = new GridBagConstraints();
gbc_btnAddRecipe.fill = GridBagConstraints.HORIZONTAL;
gbc_btnAddRecipe.insets = new Insets(0, 5, 0, 5);
gbc_btnAddRecipe.gridx = 0;
gbc_btnAddRecipe.gridy = 2;
GridBagConstraints gbc_btnDelRecipe = new GridBagConstraints();
gbc_btnDelRecipe.fill = GridBagConstraints.HORIZONTAL;
gbc_btnDelRecipe.gridx = 1;
gbc_btnDelRecipe.gridy = 2;
JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
panel_2.setLayout(null);
btnSave.setVisible(false);
btnSave.setBounds(319, 12, 117, 25);
txtEdTitle.setVisible(false);
txtEdTitle.setBounds(26, 15, 281, 19);
txtEdTitle.setColumns(10);
lblRecipeTitle.setFont(new Font("Dialog", Font.BOLD, 18));
lblRecipeTitle.setBounds(12, 12, 442, 22);
lblIngredients.setBounds(26, 61, 70, 15);
imgRecipe.setHorizontalAlignment(SwingConstants.CENTER);
imgRecipe.setBorder(new LineBorder(SystemColor.textHighlightText));
imgRecipe.setBounds(272, 88, 164, 126);
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.setVisible(false);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(26, 254, 410, 196);
txtDescription = new JTextArea();
txtDescription.setEditable(false);
txtDescription.setLineWrap(true);
scrollPane.setViewportView(txtDescription);
btnLoadImg.setBounds(304, 217, 102, 25);
txtTags.setEditable(false);
txtTags.setBounds(26, 480, 410, 19);
txtTags.setColumns(10);
lblTags.setBounds(26, 462, 70, 15);
btnCancel.setVisible(false);
btnCancel.setBounds(319, 46, 117, 25);
lblDescription.setBounds(26, 227, 152, 15);
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(26, 88, 228, 126);
txtIngredients.setEditable(false);
txtIngredients.setLineWrap(true);
scrollPane_1.setViewportView(txtIngredients);
scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setMaximumSize(new Dimension(100, 100));
scrollPane_2.setViewportView(recipesList);
gbc_scrollPane_2.gridwidth = 2;
gbc_scrollPane_2.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 0;
gbc_scrollPane_2.gridy = 1;
panel.add(txtSearch, gbc_txtSearch);
panel.add(scrollPane_2, gbc_scrollPane_2);
panel.add(btnAddRecipe, gbc_btnAddRecipe);
panel.add(btnDelRecipe, gbc_btnDelRecipe);
panel.add(txtSearch, gbc_txtSearch);
panel_2.add(btnSave);
panel_2.add(txtEdTitle);
panel_2.add(lblRecipeTitle);
panel_2.add(lblIngredients);
panel_2.add(lblTags);
panel_2.add(imgRecipe);
panel_2.add(scrollPane);
panel_2.add(btnLoadImg);
panel_2.add(txtTags);
panel_2.add(btnCancel);
panel_2.add(lblDescription);
panel_2.add(scrollPane_1);
}
public String getRecipeTitle(){
return lblRecipeTitle.getText().toString();
}
public String getRecipeIngredients(){
return txtIngredients.getText().toString();
}
public String getRecipeDescription(){
return txtDescription.getText().toString();
}
public String getRecipeTags(){
return txtTags.getText().toString();
}
public JFileChooser getFileChooser(){
return fcImg;
}
public JList getRecipesList(){
return recipesList;
}
public JLabel getImgRecipe(){
return imgRecipe;
}
public void setRecipeTitle(String name){
lblRecipeTitle.setText(name);
}
public void setRecipeIngredients(String ingredients){
txtIngredients.setText(ingredients);
}
public void setRecipeDescription(String description){
txtDescription.setText(description);
}
public void setRecipeTags(String tags){
txtTags.setText(tags);
}
public void setRecipesList(List<String> names){
for (String r : names)
{
recipesListModel.addElement(r);
}
}
public void setRecipeIcon(Icon icon){
imgRecipe.setIcon(icon);
}
public void resetCaretPositions(){
txtDescription.setCaretPosition(0);
txtIngredients.setCaretPosition(0);
}
void addListener(ActionListener listenForAddButton){
btnAddRecipe.addActionListener(listenForAddButton);
}
void delListener(ActionListener listenForDelButton){
btnDelRecipe.addActionListener(listenForDelButton);
}
void loadListener(ActionListener listenForLoadButton){
btnLoadImg.addActionListener(listenForLoadButton);
}
void cancelListener(ActionListener listenForAddButton){
btnCancel.addActionListener(listenForAddButton);
}
void saveListener(ActionListener listenForAddButton){
btnSave.addActionListener(listenForAddButton);
}
void listSelectListener(ListSelectionListener listenForSelectionChagnge){
recipesList.addListSelectionListener(listenForSelectionChagnge);
}
void DeactivateAddMenu() {
btnAddRecipe.setEnabled(true);
btnCancel.setVisible(false);
btnDelRecipe.setEnabled(true);
btnLoadImg.setVisible(false);
txtSearch.setEnabled(true);
btnSave.setVisible(false);
txtDescription.setEditable(false);
txtEdTitle.setVisible(false);
txtIngredients.setEditable(false);
txtTags.setEditable(false);
lblRecipeTitle.setVisible(true);
recipesList.setEnabled(true);
}
void ActivateAddMenu() {
btnAddRecipe.setEnabled(false);
btnCancel.setVisible(true);
btnDelRecipe.setEnabled(false);
btnLoadImg.setVisible(true);
txtSearch.setEnabled(false);
btnSave.setVisible(true);
txtDescription.setEditable(true);
txtEdTitle.setVisible(true);
txtIngredients.setEditable(true);
txtTags.setEditable(true);
lblRecipeTitle.setVisible(false);
recipesList.setEnabled(false);
imgRecipe.setIcon(imgDefaultRecipe);
txtDescription.setText("");
txtEdTitle.setText("");
txtIngredients.setText("");
txtTags.setText("");
}
}

+ 0
- 1
src/main/java/ksiazka/kucharska/FrmMain.java View File

@ -140,7 +140,6 @@ public class FrmMain extends JFrame {
panel.setLayout(gbl_panel);
txtSearch = new JTextField();
txtSearch.setText("wpisz tytuł/tagi...");
GridBagConstraints gbc_txtSearch = new GridBagConstraints();
gbc_txtSearch.gridwidth = 2;
gbc_txtSearch.insets = new Insets(5, 5, 5, 0);

+ 4
- 5
src/main/java/ksiazka/kucharska/RecipeBuilder.java View File

@ -1,18 +1,17 @@
package ksiazka.kucharska;
import javax.swing.ImageIcon;
import db.Recipe;
abstract class RecipeBuilder {
protected Recipe rOb;
protected CB_Model rOb;
public Recipe build() {
public CB_Model build() {
return rOb;
}
public void newRecipe() {
rOb = new Recipe();
rOb = new CB_Model();
}
public abstract void buildTitle();
@ -68,7 +67,7 @@ class RecipeManager {
public void setRecipeBuilder(RecipeBuilder rb) {
recipeBuilder = rb;
}
public Recipe getRecipe() {
public CB_Model getRecipe() {
return recipeBuilder.build();
}
public void constructRecipe() {

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

@ -3,10 +3,8 @@ package ksiazka.kucharska;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import db.Cookbook;
import db.Recipe;
import ksiazka.kucharska.FrmMain;
public class RetrieveData extends FrmMain{
@ -41,7 +39,7 @@ public class RetrieveData extends FrmMain{
}
public static void fillComponents(String name){
Cookbook myCb = new Cookbook();
Recipe myRecipe = myCb.selectRecipe(name);
CB_Model myRecipe = myCb.selectRecipe(name);
lblRecipeTitle.setText(name);
txtIngredients.setText(myRecipe.getIngredients());
txtDescription.setText(myRecipe.getDescription());
@ -58,9 +56,9 @@ public class RetrieveData extends FrmMain{
rm.setRecipeBuilder(new NewRecipeBuilder());
rm.constructRecipe();
Recipe obj = rm.getRecipe();
CB_Model obj = rm.getRecipe();
myCb.insertRecipe(obj);
//myCb.insertRecipe(txtEdTitle.getText(), txtIngredients.getText(), txtDescription.getText(), txtTags.getText(), (ImageIcon)imgRecipe.getIcon());
// myCb.insertRecipe(txtEdTitle.getText(), txtIngredients.getText(), txtDescription.getText(), txtTags.getText(), (ImageIcon)imgRecipe.getIcon());
myCb.closeConnection();
}

Loading…
Cancel
Save