Compare commits

...

2 Commits

Author SHA1 Message Date
Sebastian Schneider
c111929961 Neuer Commit 10.11.2020 2020-11-10 11:30:22 +01:00
Sebastian Schneider
23f78083c6 Neuer Commit 10.11.2020 2020-11-10 11:30:07 +01:00
9 changed files with 224 additions and 92 deletions

View File

@ -71,7 +71,7 @@ jlink.additionalmodules=
jlink.additionalparam= jlink.additionalparam=
jlink.launcher=true jlink.launcher=true
jlink.launcher.name=Adressverwaltung jlink.launcher.name=Adressverwaltung
main.class=adressverwaltung.main main.class=adressverwaltung.Main
manifest.file=manifest.mf manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false mkdist.disabled=false

View File

@ -1,40 +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 adressverwaltung;
import controller.CommandController;
import view.MainWindow;
import controller.OpenController;
import model.AdressVerwaltungModel;
/**
*
* @author PC
*/
public class main {
public main() {
MainWindow mainwindow = new MainWindow();
OpenController opencontroller = new OpenController(mainwindow);
AdressVerwaltungModel model = new AdressVerwaltungModel();
CommandController cmdController = new CommandController(mainwindow, model);
mainwindow.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new main();
}
}

View File

@ -33,9 +33,9 @@ public class CommandEintragHinzufuegen implements CommandInterface{
@Override @Override
public void undo() { public void undo() {
this.model.eintragLoeschen(this.model.getRowCount()-1);
} }
@Override @Override
public boolean isUndoable() { public boolean isUndoable() {
return true; return true;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -9,6 +9,8 @@ import command.CommandInterface;
import command.CommandInvoker; import command.CommandInvoker;
import command.commands.CommandEintragHinzufuegen; import command.commands.CommandEintragHinzufuegen;
import command.commands.CommandEintragLoeschen; import command.commands.CommandEintragLoeschen;
import command.commands.CommandOpenFile;
import command.commands.CommandSaveFile;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
@ -29,16 +31,15 @@ public class CommandController implements ActionListener, WindowListener{
private MainWindow view; private MainWindow view;
private AdressVerwaltungModel model; private AdressVerwaltungModel model;
private CommandInvoker invoker; private CommandInvoker invoker;
private Preferences prefs; //private Preferences prefs;
public CommandController(MainWindow view, AdressVerwaltungModel model) public CommandController(MainWindow view, AdressVerwaltungModel model)
{ {
this.view = view; this.view = view;
this.model = model; this.model = model;
this.invoker = new CommandInvoker(); this.invoker = new CommandInvoker();
this.prefs = Preferences.userRoot().node(this.getClass().getName()); //this.prefs = Preferences.userRoot().node(this.getClass().getName());
view.getjTableAdressen().setModel(model); //Übernehme Model
registerEvents(); registerEvents();
registerCommands(); registerCommands();
@ -50,6 +51,15 @@ public class CommandController implements ActionListener, WindowListener{
{ {
view.getJpopEintragHinzufuegen().addActionListener(this); view.getJpopEintragHinzufuegen().addActionListener(this);
view.getJpopEintragLoeschen().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); CommandEintragHinzufuegen cmdHinzufuegen = new CommandEintragHinzufuegen(view, model);
CommandEintragLoeschen cmdLoeschen = new CommandEintragLoeschen(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.getJpopEintragHinzufuegen(), cmdHinzufuegen);
invoker.addCommand(view.getJpopEintragLoeschen(), cmdLoeschen); 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
* <br>
* Hallo Guten Tag
*/
@Override @Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
Object key = evt.getSource(); Object key = evt.getSource();
invoker.executeCommand(key);
if(key == view.getMiUndo())
{
invoker.undoCommand();
}
else
{
invoker.executeCommand(key);
}
} }
@Override @Override

View File

@ -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());
}
}
}

View File

@ -15,6 +15,7 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.prefs.Preferences;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
/** /**
@ -30,6 +31,7 @@ public class AdressVerwaltungModel extends AbstractTableModel
private ArrayList<ArrayList<String>> daten; private ArrayList<ArrayList<String>> daten;
private ArrayList<String> adressEintraegeDaten; private ArrayList<String> adressEintraegeDaten;
private ArrayList<String> adressEintraegeNamen; private ArrayList<String> adressEintraegeNamen;
private Preferences prefs;
/** /**
@ -37,6 +39,10 @@ public class AdressVerwaltungModel extends AbstractTableModel
*/ */
public AdressVerwaltungModel() public AdressVerwaltungModel()
{ {
initializePreferences();
adressEintraegeDaten = new ArrayList<>(); adressEintraegeDaten = new ArrayList<>();
adressEintraegeNamen = new ArrayList<>(); adressEintraegeNamen = new ArrayList<>();
daten = new ArrayList<>(); daten = new ArrayList<>();
@ -92,6 +98,16 @@ public class AdressVerwaltungModel extends AbstractTableModel
return adressEintraegeNamen.get(col); return adressEintraegeNamen.get(col);
} }
public Preferences getPreferences()
{
return this.prefs;
}
public void initializePreferences()
{
this.prefs = Preferences.userRoot().node(this.getClass().getName());
}
public ArrayList<String> getLastRowData() public ArrayList<String> getLastRowData()
{ {
ArrayList<String> lastRow = new ArrayList<>(); ArrayList<String> lastRow = new ArrayList<>();

View File

@ -25,6 +25,9 @@ public class MainWindow extends javax.swing.JFrame {
return jpopEintragLoeschen; return jpopEintragLoeschen;
} }
/** /**
* Creates new form MainFrame * Creates new form MainFrame
*/ */
@ -287,4 +290,11 @@ public class MainWindow extends javax.swing.JFrame {
public void setjTableAdressen(javax.swing.JTable jTableAdressen) { public void setjTableAdressen(javax.swing.JTable jTableAdressen) {
this.jTableAdressen = jTableAdressen; this.jTableAdressen = jTableAdressen;
} }
/**
* @return the miUndo
*/
public javax.swing.JMenuItem getMiUndo() {
return miUndo;
}
} }