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.

GrafikController.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package grafikchat.controller;
  2. import grafikchat.model.ChatModel;
  3. import grafikchat.view.ChatView;
  4. import java.awt.Point;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.event.MouseMotionListener;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.ObjectInputStream;
  15. import java.io.ObjectOutputStream;
  16. import java.util.ArrayList;
  17. import java.util.logging.Logger;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JOptionPane;
  20. import grafikchat.model.Figures;
  21. import grafikchat.model.TransceiverData;
  22. import grafikchat.model.TransceiverDataEvent;
  23. import grafikchat.ohmlogger.OhmLogger;
  24. /**
  25. * Handle events from mouse and keyboard
  26. *
  27. * @author marian
  28. */
  29. public class GrafikController implements MouseMotionListener, MouseListener, KeyListener
  30. {
  31. private final ChatView view;
  32. private final ChatModel model;
  33. private boolean clicked;
  34. private final static Logger logger = OhmLogger.getLogger();
  35. /**
  36. * Constructor
  37. * @param view UI of zeichenprogramm
  38. * @param model Model of zeichenprogramm
  39. */
  40. public GrafikController(ChatView view, ChatModel model)
  41. {
  42. this.view = view;
  43. this.model = model;
  44. this.clicked = false;
  45. }
  46. /**
  47. * Register events for mouse and keyboard
  48. */
  49. public void registerEvents()
  50. {
  51. view.getGvDrawPane().addMouseMotionListener(this);
  52. view.getGvDrawPane().addMouseListener(this);
  53. view.getGvDrawPane().addKeyListener(this);
  54. view.getGvDrawPane().setFocusable(true);
  55. view.addKeyListener(this);
  56. view.setFocusable(true);
  57. }
  58. @Override
  59. public void mouseDragged(MouseEvent evt)
  60. {
  61. if (clicked) {
  62. Point p = evt.getPoint();
  63. model.getIntern().addPoint(p);
  64. model.sendMessage(new TransceiverData(TransceiverDataEvent.NEWPOINT, p));
  65. view.getGvDrawPane().drawPoint(true);
  66. }
  67. }
  68. @Override
  69. public void mouseMoved(MouseEvent e) {}
  70. @Override
  71. public void mouseClicked(MouseEvent e) {}
  72. @Override
  73. public void mousePressed(MouseEvent e)
  74. {
  75. if (e.getButton() == MouseEvent.BUTTON1) {
  76. logger.info("Mouse pressed");
  77. model.getIntern().newFigure();
  78. model.sendMessage(new TransceiverData(TransceiverDataEvent.NEWFIGURE, null));
  79. this.clicked = true;
  80. }
  81. }
  82. @Override
  83. public void mouseReleased(MouseEvent e)
  84. {
  85. if (e.getButton() == MouseEvent.BUTTON1) {
  86. logger.info("Mouse released");
  87. this.clicked = false;
  88. }
  89. }
  90. @Override
  91. public void mouseEntered(MouseEvent e) {}
  92. @Override
  93. public void mouseExited(MouseEvent e) {}
  94. /**
  95. * Save current graphics to file
  96. * @return True if successful, false otherwise
  97. */
  98. public boolean saveToFile()
  99. {
  100. JFileChooser fc = new JFileChooser();
  101. fc.setCurrentDirectory(new File(model.getFilename()));
  102. int returnVal = fc.showSaveDialog(view);
  103. if (returnVal == JFileChooser.APPROVE_OPTION) {
  104. File f = fc.getSelectedFile();
  105. if(!f.exists()) {
  106. try {
  107. f.createNewFile();
  108. } catch (IOException e) {
  109. JOptionPane.showMessageDialog(view, "Datei konnte nicht erstellt werden.", "Warnung", JOptionPane.WARNING_MESSAGE);
  110. return false;
  111. }
  112. }
  113. } else {
  114. JOptionPane.showMessageDialog(view, "Es wurde keine Datei zum speichern ausgewählt.", "Warnung", JOptionPane.WARNING_MESSAGE);
  115. return false;
  116. }
  117. model.setFilename(fc.getSelectedFile().getPath());
  118. try {
  119. FileOutputStream fos = new FileOutputStream(model.getFilename());
  120. try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
  121. ArrayList<Figures> list = new ArrayList<>();
  122. list.add(model.getIntern());
  123. list.add(model.getExtern());
  124. oos.writeObject(list);
  125. }
  126. } catch (IOException e) {
  127. JOptionPane.showMessageDialog(view, "Grafik konnte nicht gespeichert werden.", "Warning", JOptionPane.WARNING_MESSAGE);
  128. }
  129. JOptionPane.showMessageDialog(view, "Grafik erfolgreich gespeichert.", "Information", JOptionPane.INFORMATION_MESSAGE);
  130. return true;
  131. }
  132. /**
  133. * Read graphics from file
  134. * @return True if successful, false otherwise
  135. */
  136. public boolean readFromFile()
  137. {
  138. JFileChooser fc = new JFileChooser();
  139. fc.setCurrentDirectory(new File(model.getFilename()));
  140. int returnVal = fc.showOpenDialog(view);
  141. if (returnVal == JFileChooser.APPROVE_OPTION) {
  142. File f = fc.getSelectedFile();
  143. if(!f.exists()) {
  144. try {
  145. f.createNewFile();
  146. } catch (IOException e) {
  147. JOptionPane.showMessageDialog(view, "Datei konnte nicht erstellt werden.", "Warnung", JOptionPane.WARNING_MESSAGE);
  148. return false;
  149. }
  150. }
  151. String filepath = f.getPath();
  152. ArrayList<Figures> d;
  153. FileInputStream is;
  154. try {
  155. is = new FileInputStream(filepath);
  156. } catch (IOException e) {
  157. JOptionPane.showMessageDialog(view, "Datei wurde nicht gefunden.", "Warnung", JOptionPane.WARNING_MESSAGE);
  158. return false;
  159. }
  160. try {
  161. ObjectInputStream ois = new ObjectInputStream(is);
  162. d = (ArrayList<Figures>)ois.readObject();
  163. model.getIntern().setFigures(d.get(0).getFigures());
  164. model.getExtern().setFigures(d.get(1).getFigures());
  165. view.repaint();
  166. model.sendMessage(new TransceiverData(TransceiverDataEvent.REPAINT, d));
  167. } catch (ClassNotFoundException | IOException e) {
  168. JOptionPane.showMessageDialog(view, "Leeres Adressbuch geöffnet.", "Warnung", JOptionPane.INFORMATION_MESSAGE);
  169. }
  170. model.setFilename(filepath);
  171. }
  172. return true;
  173. }
  174. @Override
  175. public void keyTyped(KeyEvent ke)
  176. {
  177. }
  178. @Override
  179. public void keyPressed(KeyEvent ke)
  180. {
  181. }
  182. @Override
  183. public void keyReleased(KeyEvent ke)
  184. {
  185. char c = ke.getKeyChar();
  186. switch (c)
  187. {
  188. case 's':
  189. saveToFile();
  190. break;
  191. case 'o':
  192. readFromFile();
  193. break;
  194. case 'p':
  195. view.getGvDrawPane().doPrint();
  196. break;
  197. }
  198. }
  199. }