Cook book written in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.4 KiB

10 years ago
10 years ago
  1. package ksiazka.kucharska;
  2. import java.awt.Graphics2D;
  3. import java.awt.RenderingHints;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.List;
  10. import javax.imageio.ImageIO;
  11. import javax.swing.DefaultListModel;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JFileChooser;
  14. import javax.swing.JLabel;
  15. import javax.swing.JList;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.event.DocumentEvent;
  18. import javax.swing.event.DocumentListener;
  19. import javax.swing.event.ListSelectionEvent;
  20. import javax.swing.event.ListSelectionListener;
  21. import db.SearchRecipeAll;
  22. import db.Cookbook;
  23. import db.SearchRecipe;
  24. import db.SearchRecipeNameTag;
  25. public class CB_Controller {
  26. private static CB_View theView;
  27. private static CB_Model theModel;
  28. public CB_Controller(CB_View theView, CB_Model theModel) {
  29. CB_Controller.theView = theView;
  30. CB_Controller.theModel = theModel;
  31. CB_Controller.theView.addListener(new AddListener());
  32. CB_Controller.theView.delListener(new DelListener());
  33. CB_Controller.theView.loadListener(new LoadListener());
  34. CB_Controller.theView.cancelListener(new CancelListener());
  35. CB_Controller.theView.saveListener(new SaveListener());
  36. CB_Controller.theView.listSelectListener(new SelectionListener());
  37. CB_Controller.theView.searchTypeListener(new SearchListener());
  38. }
  39. public void fillList(){
  40. Cookbook myCb = new Cookbook();
  41. SearchRecipe sr;
  42. if (theView.getSearchValue() != "") {
  43. sr = new SearchRecipeNameTag(theView);
  44. }
  45. else
  46. {
  47. sr = new SearchRecipeAll(theView);
  48. }
  49. List<String> names = sr.obtainRecipeNames();
  50. theView.setRecipesList(names);
  51. myCb.closeConnection();
  52. }
  53. public static void fillComponents(String name){
  54. Cookbook myCb = new Cookbook();
  55. theModel = myCb.selectRecipe(name);
  56. theView.setRecipeTitle(theModel.getName());
  57. theView.setRecipeIngredients(theModel.getIngredients());
  58. theView.setRecipeDescription(theModel.getDescription());
  59. theView.setRecipeTags(theModel.getTags());
  60. theView.setRecipeIcon(theModel.getImage());
  61. theView.resetCaretPositions();
  62. myCb.closeConnection();
  63. }
  64. public static void addNewRecipe(){
  65. Cookbook myCb = new Cookbook();
  66. RecipeManager rm = new RecipeManager();
  67. rm.setRecipeBuilder(new NewRecipeBuilder(theView));
  68. rm.constructRecipe();
  69. CB_Model obj = rm.getRecipe();
  70. myCb.insertRecipe(obj);
  71. myCb.closeConnection();
  72. }
  73. public static void removeRecipe(String name){
  74. Cookbook myCb = new Cookbook();
  75. myCb.deleteRecipe(name);
  76. myCb.closeConnection();
  77. }
  78. public ImageIcon resizeImage(File f) {
  79. //File file = f;
  80. ImageIcon iconLogo=null;
  81. try {
  82. BufferedImage srcImg = ImageIO.read(f);
  83. int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*theView.getImgRecipe().getHeight());
  84. BufferedImage resizedImg = new BufferedImage(w, theView.getImgRecipe().getHeight(), BufferedImage.TYPE_INT_ARGB);
  85. Graphics2D g2 = resizedImg.createGraphics();
  86. g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  87. g2.drawImage(srcImg, 0, 0, w, theView.getImgRecipe().getHeight(), null);
  88. g2.dispose();
  89. iconLogo = new ImageIcon(resizedImg);
  90. } catch (IOException e1) {
  91. e1.printStackTrace();
  92. }
  93. return iconLogo;
  94. }
  95. class AddListener implements ActionListener{
  96. public void actionPerformed(ActionEvent e) {
  97. try{
  98. theView.ActivateAddMenu();
  99. }
  100. catch(NumberFormatException ex){
  101. ex.printStackTrace();
  102. }
  103. }
  104. }
  105. class DelListener implements ActionListener{
  106. public void actionPerformed(ActionEvent e) {
  107. try{
  108. JList recipesList = theView.getRecipesList();
  109. DefaultListModel model = (DefaultListModel) recipesList.getModel();
  110. if (recipesList.getSelectedIndex() != -1)
  111. {
  112. int dialogResult = JOptionPane.showConfirmDialog (null, "Czy na pewno chcesz usunąć przepis?", "Pytanie", JOptionPane.YES_NO_OPTION);
  113. if(dialogResult == JOptionPane.YES_OPTION){
  114. removeRecipe(recipesList.getSelectedValue().toString());
  115. model.remove(recipesList.getSelectedIndex());
  116. if (recipesList.getModel().getSize()>0)
  117. {
  118. recipesList.setSelectedIndex(0);
  119. fillComponents(recipesList.getSelectedValue().toString());
  120. }
  121. else
  122. {
  123. theView.setRecipeTitle("Nazwa przepisu");
  124. theView.setRecipeIngredients("");
  125. theView.setRecipeDescription("");
  126. theView.setRecipeTags("");
  127. }
  128. }
  129. }
  130. }
  131. catch(NumberFormatException ex){
  132. ex.printStackTrace();
  133. }
  134. }
  135. }
  136. class LoadListener implements ActionListener{
  137. public void actionPerformed(ActionEvent e) {
  138. try{
  139. JFileChooser fcImg = theView.getFileChooser();
  140. int returnVal = fcImg.showOpenDialog(theView);
  141. if (returnVal == JFileChooser.APPROVE_OPTION) {
  142. theView.getImgRecipe().setIcon(resizeImage(fcImg.getSelectedFile()));
  143. }
  144. }
  145. catch(NumberFormatException ex){
  146. ex.printStackTrace();
  147. }
  148. }
  149. }
  150. class CancelListener implements ActionListener{
  151. public void actionPerformed(ActionEvent e) {
  152. try{
  153. theView.DeactivateAddMenu();
  154. JList recipesList = theView.getRecipesList();
  155. if(recipesList.getSelectedIndex() != -1)
  156. fillComponents(recipesList.getSelectedValue().toString());
  157. }
  158. catch(NumberFormatException ex){
  159. ex.printStackTrace();
  160. }
  161. }
  162. }
  163. class SaveListener implements ActionListener{
  164. public void actionPerformed(ActionEvent e) {
  165. try{
  166. addNewRecipe();
  167. theView.setRecipesListItem(theView.getRecipeEdTitle());
  168. theView.DeactivateAddMenu();
  169. theView.getRecipesList().setSelectedIndex(theView.getRecipesList().getModel().getSize()-1);
  170. fillComponents(theView.getRecipesList().getSelectedValue().toString());
  171. }
  172. catch(NumberFormatException ex){
  173. ex.printStackTrace();
  174. }
  175. }
  176. }
  177. class SelectionListener implements ListSelectionListener{
  178. public void valueChanged(ListSelectionEvent e) {
  179. try{
  180. JList recipesList = theView.getRecipesList();
  181. if(recipesList.getSelectedIndex() != -1)
  182. fillComponents(recipesList.getSelectedValue().toString());
  183. }
  184. catch(NumberFormatException ex){
  185. ex.printStackTrace();
  186. }
  187. }
  188. }
  189. class SearchListener implements DocumentListener{
  190. public void changedUpdate(DocumentEvent e) {
  191. fillList();
  192. }
  193. public void insertUpdate(DocumentEvent e) {
  194. fillList();
  195. }
  196. public void removeUpdate(DocumentEvent e) {
  197. fillList();
  198. }
  199. }
  200. }