From 23f78083c6fe6ac9ae9f7b7ffde3c1d25af8c52a Mon Sep 17 00:00:00 2001 From: Sebastian Schneider Date: Tue, 10 Nov 2020 11:30:07 +0100 Subject: [PATCH] Neuer Commit 10.11.2020 --- nbproject/project.properties | 2 +- .../commands/CommandEintragHinzufuegen.java | 4 +- src/command/commands/CommandOpenFile.java | 81 +++++++++++++++++++ src/command/commands/CommandSaveFile.java | 68 ++++++++++++++++ src/controller/CommandController.java | 52 ++++++++++-- src/controller/OpenController.java | 43 ---------- src/model/AdressVerwaltungModel.java | 16 ++++ src/view/MainWindow.java | 10 +++ 8 files changed, 224 insertions(+), 52 deletions(-) create mode 100644 src/command/commands/CommandOpenFile.java create mode 100644 src/command/commands/CommandSaveFile.java delete mode 100644 src/controller/OpenController.java diff --git a/nbproject/project.properties b/nbproject/project.properties index 6739be2..66d924a 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -71,7 +71,7 @@ jlink.additionalmodules= jlink.additionalparam= jlink.launcher=true jlink.launcher.name=Adressverwaltung -main.class=adressverwaltung.main +main.class=adressverwaltung.Main manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/src/command/commands/CommandEintragHinzufuegen.java b/src/command/commands/CommandEintragHinzufuegen.java index d150256..b73c5af 100644 --- a/src/command/commands/CommandEintragHinzufuegen.java +++ b/src/command/commands/CommandEintragHinzufuegen.java @@ -33,9 +33,9 @@ public class CommandEintragHinzufuegen implements CommandInterface{ @Override public void undo() { - + this.model.eintragLoeschen(this.model.getRowCount()-1); } - + @Override public boolean isUndoable() { return true; diff --git a/src/command/commands/CommandOpenFile.java b/src/command/commands/CommandOpenFile.java new file mode 100644 index 0000000..838a7d8 --- /dev/null +++ b/src/command/commands/CommandOpenFile.java @@ -0,0 +1,81 @@ +/* + * 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; + } + +} diff --git a/src/command/commands/CommandSaveFile.java b/src/command/commands/CommandSaveFile.java new file mode 100644 index 0000000..031e792 --- /dev/null +++ b/src/command/commands/CommandSaveFile.java @@ -0,0 +1,68 @@ +/* + * 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.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.prefs.Preferences; +import javax.swing.JFileChooser; +import model.AdressVerwaltungModel; +import view.MainWindow; + +/** + * + * @author PC + */ +public class CommandSaveFile implements CommandInterface{ + + private MainWindow view; + private AdressVerwaltungModel model; + private Preferences prefs; + + public CommandSaveFile(MainWindow view, AdressVerwaltungModel model) { + + this.view = view; + this.model = model; + this.prefs = model.getPreferences(); + + } + + + @Override + public void execute() { + int wahl = view.getjFileChooser().showSaveDialog(view); + if(wahl == JFileChooser.APPROVE_OPTION) + { + File file = view.getjFileChooser().getSelectedFile(); + view.getStatusbar().setText("Datei wird gespeichert..."); + try + { + model.datenSpeichern(file); + } + catch (UnsupportedEncodingException ex) + { + view.getStatusbar().setText(ex.toString()); + } + catch (IOException ex ) + { + view.getStatusbar().setText(ex.toString()); + } + } + } + + @Override + public void undo() { + + } + + @Override + public boolean isUndoable() { + return false; + } + +} diff --git a/src/controller/CommandController.java b/src/controller/CommandController.java index d5adb09..0cdf6d7 100644 --- a/src/controller/CommandController.java +++ b/src/controller/CommandController.java @@ -9,6 +9,8 @@ import command.CommandInterface; import command.CommandInvoker; import command.commands.CommandEintragHinzufuegen; import command.commands.CommandEintragLoeschen; +import command.commands.CommandOpenFile; +import command.commands.CommandSaveFile; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; @@ -29,16 +31,15 @@ public class CommandController implements ActionListener, WindowListener{ private MainWindow view; private AdressVerwaltungModel model; private CommandInvoker invoker; - private Preferences prefs; + //private Preferences prefs; public CommandController(MainWindow view, AdressVerwaltungModel model) { this.view = view; this.model = model; this.invoker = new CommandInvoker(); - this.prefs = Preferences.userRoot().node(this.getClass().getName()); - - view.getjTableAdressen().setModel(model); //Übernehme Model + //this.prefs = Preferences.userRoot().node(this.getClass().getName()); + registerEvents(); registerCommands(); @@ -50,6 +51,15 @@ public class CommandController implements ActionListener, WindowListener{ { view.getJpopEintragHinzufuegen().addActionListener(this); view.getJpopEintragLoeschen().addActionListener(this); + + view.getJbOpen().addActionListener(this); + view.getJbSave().addActionListener(this); + + view.getFileOpen().addActionListener(this); + view.getFileSave().addActionListener(this); + + view.getMiUndo().addActionListener(this); + } @@ -57,17 +67,47 @@ public class CommandController implements ActionListener, WindowListener{ { CommandEintragHinzufuegen cmdHinzufuegen = new CommandEintragHinzufuegen(view, model); CommandEintragLoeschen cmdLoeschen = new CommandEintragLoeschen(view, model); + CommandSaveFile cmdSaveFile = new CommandSaveFile(view, model); + CommandOpenFile cmdOpenFile = new CommandOpenFile(view, model); + + invoker.addCommand(view.getJpopEintragHinzufuegen(), cmdHinzufuegen); invoker.addCommand(view.getJpopEintragLoeschen(), cmdLoeschen); + invoker.addCommand(view.getJbOpen(), cmdOpenFile); + invoker.addCommand(view.getJbSave(), cmdSaveFile); + invoker.addCommand(view.getFileOpen(), cmdOpenFile); + invoker.addCommand(view.getFileSave(), cmdSaveFile); + } - + + + + /** + * actionPerformed Funktion wird aufgerufen + * @param evt + *
+ * Hallo Guten Tag + */ + @Override public void actionPerformed(ActionEvent evt) { Object key = evt.getSource(); - invoker.executeCommand(key); + + if(key == view.getMiUndo()) + { + invoker.undoCommand(); + } + else + { + invoker.executeCommand(key); + } + + + + } @Override diff --git a/src/controller/OpenController.java b/src/controller/OpenController.java deleted file mode 100644 index 8f28c15..0000000 --- a/src/controller/OpenController.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 controller; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.File; -import java.util.Locale; -import javax.swing.JFileChooser; -import view.MainWindow; -/** - * - * @author PC - */ -public class OpenController implements ActionListener{ - private MainWindow mainwindow; - - public OpenController(MainWindow mainwindow) - { - this.mainwindow = mainwindow; - registerEvents(); - } - public void registerEvents() - { - mainwindow.getJbOpen().addActionListener(this); - mainwindow.getFileOpen().addActionListener(this); - } - - @Override - public void actionPerformed(ActionEvent ae) { - int wahl = mainwindow.getjFileChooser().showOpenDialog(mainwindow); - if(wahl == JFileChooser.APPROVE_OPTION) - { - File file = mainwindow.getjFileChooser().getSelectedFile(); - mainwindow.getStatusbar().setText(file.getAbsolutePath()); - - } - - } -} diff --git a/src/model/AdressVerwaltungModel.java b/src/model/AdressVerwaltungModel.java index 51bb5a0..59b8056 100644 --- a/src/model/AdressVerwaltungModel.java +++ b/src/model/AdressVerwaltungModel.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; +import java.util.prefs.Preferences; import javax.swing.table.AbstractTableModel; /** @@ -30,6 +31,7 @@ public class AdressVerwaltungModel extends AbstractTableModel private ArrayList> daten; private ArrayList adressEintraegeDaten; private ArrayList adressEintraegeNamen; + private Preferences prefs; /** @@ -37,6 +39,10 @@ public class AdressVerwaltungModel extends AbstractTableModel */ public AdressVerwaltungModel() { + + initializePreferences(); + + adressEintraegeDaten = new ArrayList<>(); adressEintraegeNamen = new ArrayList<>(); daten = new ArrayList<>(); @@ -92,6 +98,16 @@ public class AdressVerwaltungModel extends AbstractTableModel return adressEintraegeNamen.get(col); } + public Preferences getPreferences() + { + return this.prefs; + } + + public void initializePreferences() + { + this.prefs = Preferences.userRoot().node(this.getClass().getName()); + } + public ArrayList getLastRowData() { ArrayList lastRow = new ArrayList<>(); diff --git a/src/view/MainWindow.java b/src/view/MainWindow.java index 8a096a3..1e0e86a 100644 --- a/src/view/MainWindow.java +++ b/src/view/MainWindow.java @@ -25,6 +25,9 @@ public class MainWindow extends javax.swing.JFrame { return jpopEintragLoeschen; } + + + /** * Creates new form MainFrame */ @@ -287,4 +290,11 @@ public class MainWindow extends javax.swing.JFrame { public void setjTableAdressen(javax.swing.JTable jTableAdressen) { this.jTableAdressen = jTableAdressen; } + + /** + * @return the miUndo + */ + public javax.swing.JMenuItem getMiUndo() { + return miUndo; + } }