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.

CommandSave.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package kontakte.controller;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.ObjectOutputStream;
  10. import javax.swing.JOptionPane;
  11. import kontakte.model.Model;
  12. import kontakte.view.View;
  13. /**
  14. *
  15. * @author nobody
  16. */
  17. public class CommandSave implements CommandInterface
  18. {
  19. private View view;
  20. private Model model;
  21. public CommandSave(View view, Model model)
  22. {
  23. this.view = view;
  24. this.model = model;
  25. }
  26. @Override
  27. public void execute()
  28. {
  29. if (view.getFileChooser().getSelectedFile() == null)
  30. {
  31. view.getFileChooser().showSaveDialog(view);
  32. String fileAdd = view.getFileChooser().getSelectedFile().getAbsolutePath();
  33. view.getTextFileAdress().setText(fileAdd);
  34. }
  35. try
  36. {
  37. FileOutputStream fileOut = new FileOutputStream(view.getFileChooser().getSelectedFile());
  38. ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
  39. objOut.writeObject(model.kontaktliste);
  40. objOut.close();
  41. }
  42. catch(Exception e)
  43. {
  44. JOptionPane.showMessageDialog(view, "Konnte nicht schreiben.", "Fehler", JOptionPane.ERROR_MESSAGE);
  45. }
  46. }
  47. @Override
  48. public void undo()
  49. {
  50. }
  51. }