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.

CommandOpenFile.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 command.commands;
  7. import command.CommandInterface;
  8. import java.awt.event.ActionEvent;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.UnsupportedEncodingException;
  12. import java.util.ArrayList;
  13. import java.util.prefs.Preferences;
  14. import javax.swing.JFileChooser;
  15. import model.AdressVerwaltungModel;
  16. import view.MainWindow;
  17. /**
  18. *
  19. * @author PC
  20. */
  21. public class CommandOpenFile implements CommandInterface{
  22. private MainWindow view;
  23. private AdressVerwaltungModel model;
  24. private Preferences prefs;
  25. public CommandOpenFile(MainWindow view, AdressVerwaltungModel model) {
  26. this.view = view;
  27. this.model = model;
  28. this.prefs = model.getPreferences();
  29. }
  30. @Override
  31. public void execute() {
  32. String lastDirectoryPath = prefs.get("lastFileOpened", "");
  33. File file = new File(lastDirectoryPath);
  34. view.getjFileChooser().setCurrentDirectory(file);
  35. int wahl = view.getjFileChooser().showOpenDialog(view);
  36. if(wahl == JFileChooser.APPROVE_OPTION)
  37. {
  38. file = view.getjFileChooser().getSelectedFile();
  39. view.getStatusbar().setText(file.getAbsolutePath());
  40. prefs.put("lastFileOpened", file.getAbsolutePath());
  41. try
  42. {
  43. model.datenLesen(file);
  44. }
  45. catch (UnsupportedEncodingException ex)
  46. {
  47. view.getStatusbar().setText(ex.toString());
  48. }
  49. catch (IOException | ClassNotFoundException ex )
  50. {
  51. view.getStatusbar().setText(ex.toString());
  52. }
  53. }
  54. }
  55. @Override
  56. public void undo() {
  57. }
  58. @Override
  59. public boolean isUndoable() {
  60. return false;
  61. }
  62. }