123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package grafikchat.controller;
-
- import grafikchat.model.ChatModel;
- import grafikchat.view.ChatView;
- import java.awt.Point;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.logging.Logger;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import grafikchat.model.Figures;
- import grafikchat.model.TransceiverData;
- import grafikchat.model.TransceiverDataEvent;
- import grafikchat.ohmlogger.OhmLogger;
-
- /**
- * Handle events from mouse and keyboard
- *
- * @author marian
- */
- public class GrafikController implements MouseMotionListener, MouseListener, KeyListener
- {
- private final ChatView view;
- private final ChatModel model;
- private boolean clicked;
- private final static Logger logger = OhmLogger.getLogger();
-
- /**
- * Constructor
- * @param view UI of zeichenprogramm
- * @param model Model of zeichenprogramm
- */
- public GrafikController(ChatView view, ChatModel model)
- {
- this.view = view;
- this.model = model;
-
- this.clicked = false;
- }
-
- /**
- * Register events for mouse and keyboard
- */
- public void registerEvents()
- {
- view.getGvDrawPane().addMouseMotionListener(this);
- view.getGvDrawPane().addMouseListener(this);
- view.getGvDrawPane().addKeyListener(this);
- view.getGvDrawPane().setFocusable(true);
-
- view.addKeyListener(this);
- view.setFocusable(true);
- }
-
- @Override
- public void mouseDragged(MouseEvent evt)
- {
- if (clicked) {
- Point p = evt.getPoint();
- model.getIntern().addPoint(p);
- model.sendMessage(new TransceiverData(TransceiverDataEvent.NEWPOINT, p));
- view.getGvDrawPane().drawPoint(true);
- }
- }
-
- @Override
- public void mouseMoved(MouseEvent e) {}
-
- @Override
- public void mouseClicked(MouseEvent e) {}
-
- @Override
- public void mousePressed(MouseEvent e)
- {
- if (e.getButton() == MouseEvent.BUTTON1) {
- logger.info("Mouse pressed");
- model.getIntern().newFigure();
- model.sendMessage(new TransceiverData(TransceiverDataEvent.NEWFIGURE, null));
- this.clicked = true;
- }
- }
-
- @Override
- public void mouseReleased(MouseEvent e)
- {
- if (e.getButton() == MouseEvent.BUTTON1) {
- logger.info("Mouse released");
- this.clicked = false;
- }
- }
-
- @Override
- public void mouseEntered(MouseEvent e) {}
-
- @Override
- public void mouseExited(MouseEvent e) {}
-
- /**
- * Save current graphics to file
- * @return True if successful, false otherwise
- */
- public boolean saveToFile()
- {
- JFileChooser fc = new JFileChooser();
- fc.setCurrentDirectory(new File(model.getFilename()));
- int returnVal = fc.showSaveDialog(view);
-
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File f = fc.getSelectedFile();
- if(!f.exists()) {
- try {
- f.createNewFile();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(view, "Datei konnte nicht erstellt werden.", "Warnung", JOptionPane.WARNING_MESSAGE);
- return false;
- }
- }
- } else {
- JOptionPane.showMessageDialog(view, "Es wurde keine Datei zum speichern ausgewählt.", "Warnung", JOptionPane.WARNING_MESSAGE);
- return false;
- }
- model.setFilename(fc.getSelectedFile().getPath());
-
-
- try {
- FileOutputStream fos = new FileOutputStream(model.getFilename());
- try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
- ArrayList<Figures> list = new ArrayList<>();
- list.add(model.getIntern());
- list.add(model.getExtern());
- oos.writeObject(list);
- }
- } catch (IOException e) {
- JOptionPane.showMessageDialog(view, "Grafik konnte nicht gespeichert werden.", "Warning", JOptionPane.WARNING_MESSAGE);
- }
-
- JOptionPane.showMessageDialog(view, "Grafik erfolgreich gespeichert.", "Information", JOptionPane.INFORMATION_MESSAGE);
-
- return true;
- }
-
- /**
- * Read graphics from file
- * @return True if successful, false otherwise
- */
- public boolean readFromFile()
- {
- JFileChooser fc = new JFileChooser();
- fc.setCurrentDirectory(new File(model.getFilename()));
- int returnVal = fc.showOpenDialog(view);
-
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File f = fc.getSelectedFile();
- if(!f.exists()) {
- try {
- f.createNewFile();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(view, "Datei konnte nicht erstellt werden.", "Warnung", JOptionPane.WARNING_MESSAGE);
- return false;
- }
- }
-
- String filepath = f.getPath();
- ArrayList<Figures> d;
- FileInputStream is;
-
- try {
- is = new FileInputStream(filepath);
- } catch (IOException e) {
- JOptionPane.showMessageDialog(view, "Datei wurde nicht gefunden.", "Warnung", JOptionPane.WARNING_MESSAGE);
- return false;
- }
-
- try {
- ObjectInputStream ois = new ObjectInputStream(is);
- d = (ArrayList<Figures>)ois.readObject();
- model.getIntern().setFigures(d.get(0).getFigures());
- model.getExtern().setFigures(d.get(1).getFigures());
- view.repaint();
- model.sendMessage(new TransceiverData(TransceiverDataEvent.REPAINT, d));
- } catch (ClassNotFoundException | IOException e) {
- JOptionPane.showMessageDialog(view, "Leeres Adressbuch geöffnet.", "Warnung", JOptionPane.INFORMATION_MESSAGE);
- }
-
- model.setFilename(filepath);
- }
-
- return true;
- }
-
- @Override
- public void keyTyped(KeyEvent ke)
- {
- }
-
- @Override
- public void keyPressed(KeyEvent ke)
- {
- }
-
- @Override
- public void keyReleased(KeyEvent ke)
- {
- char c = ke.getKeyChar();
- switch (c)
- {
- case 's':
- saveToFile();
- break;
- case 'o':
- readFromFile();
- break;
- case 'p':
- view.getGvDrawPane().doPrint();
- break;
- }
- }
-
-
- }
|