/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package command.commands; import command.CommandInterface; import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.prefs.Preferences; import javax.swing.JFileChooser; import model.AdressVerwaltungModel; import view.MainWindow; /** * * @author PC */ public class CommandOpenFile implements CommandInterface{ private MainWindow view; private AdressVerwaltungModel model; private Preferences prefs; public CommandOpenFile(MainWindow view, AdressVerwaltungModel model) { this.view = view; this.model = model; this.prefs = model.getPreferences(); } @Override public void execute() { String lastDirectoryPath = prefs.get("lastFileOpened", ""); File file = new File(lastDirectoryPath); view.getjFileChooser().setCurrentDirectory(file); int wahl = view.getjFileChooser().showOpenDialog(view); if(wahl == JFileChooser.APPROVE_OPTION) { file = view.getjFileChooser().getSelectedFile(); view.getStatusbar().setText(file.getAbsolutePath()); prefs.put("lastFileOpened", file.getAbsolutePath()); try { model.datenLesen(file); } catch (UnsupportedEncodingException ex) { view.getStatusbar().setText(ex.toString()); } catch (IOException | ClassNotFoundException ex ) { view.getStatusbar().setText(ex.toString()); } } } @Override public void undo() { } @Override public boolean isUndoable() { return false; } }