diff --git a/src/Model/AdressverwaltungModel.java b/src/Model/AdressverwaltungModel.java new file mode 100644 index 0000000..fb7b2db --- /dev/null +++ b/src/Model/AdressverwaltungModel.java @@ -0,0 +1,120 @@ +/* + * 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.model; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import javax.swing.table.AbstractTableModel; + +/** + * + * @author le + */ +public class AdressverwaltungModel extends AbstractTableModel +{ + private ArrayList> daten; + private ArrayList adressEintraegeDaten; + private ArrayList adressEintraegeNamen; + + public AdressverwaltungModel() + { + adressEintraegeDaten = new ArrayList<>(); + adressEintraegeNamen = new ArrayList<>(); + daten = new ArrayList<>(); + adressEintraegeNamen.add("Name"); + adressEintraegeDaten.add("Lehner"); + adressEintraegeNamen.add("Telefon"); + adressEintraegeDaten.add("122345"); + daten.add(adressEintraegeDaten); + } + + @Override + public int getRowCount() + { + return daten.size(); + } + + @Override + public int getColumnCount() + { + return adressEintraegeDaten.size(); + } + + @Override + public Object getValueAt(int row, int col) + { + return daten.get(row).get(col); + } + @Override + public void setValueAt(Object value, int row, int col) + { + daten.get(row).set(col, (String)value); + } + + @Override + public boolean isCellEditable(int row, int col) + { + return true; + } + + @Override + public String getColumnName(int col) + { + return adressEintraegeNamen.get(col); + } + + public void eintragHinzufuegen() + { + adressEintraegeDaten = new ArrayList<>(); + adressEintraegeNamen.forEach(s -> adressEintraegeDaten.add(s)); + daten.add(adressEintraegeDaten); + this.fireTableDataChanged(); + } + + public void eintragLoeschen(int row) + { + daten.remove(row); + this.fireTableDataChanged(); + } + + public void spalteHinzufuegen(String name) + { + adressEintraegeNamen.add(name); + } + + public void datenSpeichern(File datei) throws FileNotFoundException, IOException + { + FileOutputStream fos = new FileOutputStream(datei); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(daten); + oos.writeObject(adressEintraegeNamen); + oos.flush(); + oos.close(); + } + + public void datenLesen(File datei) throws FileNotFoundException, IOException, ClassNotFoundException + { + FileInputStream fis = new FileInputStream(datei); + BufferedInputStream bis = new BufferedInputStream(fis); + ObjectInputStream ois = new ObjectInputStream(bis); + daten = (ArrayList>)ois.readObject(); + adressEintraegeNamen = (ArrayList)ois.readObject(); + adressEintraegeDaten = daten.get(daten.size()-1); + ois.close(); + this.fireTableDataChanged(); + } + +} diff --git a/src/controller/Controller.java b/src/controller/Controller.java new file mode 100644 index 0000000..091d5f5 --- /dev/null +++ b/src/controller/Controller.java @@ -0,0 +1,47 @@ +/* + * 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 adressverwaltung.model.AdressverwaltungModel; +import controller.commands.CommandOpen; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import prfourgui.View.AdressbuchView; + +/** + * + * @author jechowma68968 + */ +public class Controller implements ActionListener +{ + private AdressbuchView view; + private AdressverwaltungModel model; + private Invoker invoker; + public Controller(AdressbuchView view, AdressverwaltungModel model) + { + this.view = view; + this.model = model; + this.invoker = new Invoker(); + } + /** + * Hinterlegen aller Objekte in View die Interaktion ausüben + */ + public void registerEvents(){ + view.getMnuOpen().addActionListener(this); + } + + public void registerCommands(){ + invoker.addCommand(view.getMnuOpen(), new CommandOpen(view, model)); + } + + @Override + public void actionPerformed(ActionEvent ae) + { + Object key = ae.getSource(); + invoker.executeCommand(key); + } +} diff --git a/src/controller/Interface.java b/src/controller/Interface.java new file mode 100644 index 0000000..922b7a9 --- /dev/null +++ b/src/controller/Interface.java @@ -0,0 +1,16 @@ +/* + * 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; + +/** + * + * @author jechowma68968 + */ +public interface Interface +{ + public void execute(); + public void undo(); +} diff --git a/src/controller/Invoker.java b/src/controller/Invoker.java new file mode 100644 index 0000000..ab06cfe --- /dev/null +++ b/src/controller/Invoker.java @@ -0,0 +1,36 @@ +/* + * 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.util.HashMap; + +/** + * + * @author jechowma68968 + */ +public class Invoker +{ + private HashMap commands; + + public Invoker() + { + commands = new HashMap<>(); + } + + /** + * + * @param key ist das Object + * @param value ist auch etwas + */ + public void addCommand(Object key, Interface value){ + commands.put(key, value); + } + + public void executeCommand(Object key){ + commands.get(key).execute(); + } +} diff --git a/src/controller/commands/CommandOpen.java b/src/controller/commands/CommandOpen.java new file mode 100644 index 0000000..95c6d1d --- /dev/null +++ b/src/controller/commands/CommandOpen.java @@ -0,0 +1,62 @@ +/* + * 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.commands; + +import adressverwaltung.model.AdressverwaltungModel; +import controller.Interface; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import javax.swing.JFileChooser; +import prfourgui.View.AdressbuchView; + +/** + * + * @author jechowma68968 + */ +public class CommandOpen implements Interface +{ + private final AdressbuchView view; + private final AdressverwaltungModel model; + + public CommandOpen(AdressbuchView view, AdressverwaltungModel model) + { + this.view = view; + this.model = model; + } + + @Override + public void execute() + { + File datei; + JFileChooser fc = view.getjFileChooser1(); + int wahl = fc.showOpenDialog(view); + if(wahl == JFileChooser.APPROVE_OPTION){ + datei = fc.getSelectedFile(); + String dateiname = datei.getAbsolutePath(); + view.getTxtStatus().setText("Dateipfad:" + dateiname); + + try{ + model.datenLesen(datei); + } + catch(FileNotFoundException e){ + System.out.println("File not Found Error"); + } + catch(IOException e){ + System.out.println("Reading Error"); + } + catch(ClassNotFoundException e){ + System.out.println("Class not found"); + } + } + } + + @Override + public void undo() + { + } +} diff --git a/src/prfourgui/Icons/Undo24.gif b/src/prfourgui/Icons/Undo24.gif new file mode 100644 index 0000000..1d545a7 Binary files /dev/null and b/src/prfourgui/Icons/Undo24.gif differ diff --git a/src/prfourgui/Start.java b/src/prfourgui/Start.java index 6ba5860..7462850 100644 --- a/src/prfourgui/Start.java +++ b/src/prfourgui/Start.java @@ -6,7 +6,8 @@ package prfourgui; -import controller.ControllerOeffnen; +import adressverwaltung.model.AdressverwaltungModel; +import controller.Controller; import prfourgui.View.AdressbuchView; /** @@ -18,8 +19,13 @@ public class Start public Start() { AdressbuchView view = new AdressbuchView(); - ControllerOeffnen ctrlOeffnen = new ControllerOeffnen(view); - ctrlOeffnen.registerEvents(); + AdressverwaltungModel model = new AdressverwaltungModel(); + view.getAdressTable2().getTblAdressbuch().setModel(model); + + Controller con = new Controller(view, model); + con.registerEvents(); + con.registerCommands(); + view.setVisible(true); diff --git a/src/prfourgui/View/AdressTable.form b/src/prfourgui/View/AdressTable.form index fe67fd2..98f6be6 100644 --- a/src/prfourgui/View/AdressTable.form +++ b/src/prfourgui/View/AdressTable.form @@ -41,7 +41,7 @@ - + diff --git a/src/prfourgui/View/AdressTable.java b/src/prfourgui/View/AdressTable.java index 76a7f89..2f37140 100644 --- a/src/prfourgui/View/AdressTable.java +++ b/src/prfourgui/View/AdressTable.java @@ -11,6 +11,21 @@ package prfourgui.View; */ public class AdressTable extends javax.swing.JPanel { + /** + * @return the jTable1 + */ + public javax.swing.JTable getTblAdressbuch() + { + return tblAdressbuch; + } + + /** + * @param tblAdressbuch the jTable1 to set + */ + public void setTblAdressbuch(javax.swing.JTable tblAdressbuch) + { + this.tblAdressbuch = tblAdressbuch; + } /** * Creates new form AdressTable @@ -33,7 +48,7 @@ public class AdressTable extends javax.swing.JPanel jPopupMenu1 = new javax.swing.JPopupMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jScrollPane1 = new javax.swing.JScrollPane(); - jTable1 = new javax.swing.JTable(); + tblAdressbuch = new javax.swing.JTable(); jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/prfourgui/Icons/Delete24.gif"))); // NOI18N jMenuItem1.setText("Remove"); @@ -41,7 +56,7 @@ public class AdressTable extends javax.swing.JPanel setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS)); - jTable1.setModel(new javax.swing.table.DefaultTableModel( + tblAdressbuch.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, @@ -54,8 +69,8 @@ public class AdressTable extends javax.swing.JPanel "Name", "Telefon", "Mobil", "E-Mail" } )); - jTable1.setComponentPopupMenu(jPopupMenu1); - jScrollPane1.setViewportView(jTable1); + tblAdressbuch.setComponentPopupMenu(jPopupMenu1); + jScrollPane1.setViewportView(tblAdressbuch); add(jScrollPane1); }// //GEN-END:initComponents @@ -65,6 +80,6 @@ public class AdressTable extends javax.swing.JPanel private javax.swing.JMenuItem jMenuItem1; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JScrollPane jScrollPane1; - private javax.swing.JTable jTable1; + private javax.swing.JTable tblAdressbuch; // End of variables declaration//GEN-END:variables } diff --git a/src/prfourgui/View/AdressbuchView.form b/src/prfourgui/View/AdressbuchView.form index 7d7de59..9742c17 100644 --- a/src/prfourgui/View/AdressbuchView.form +++ b/src/prfourgui/View/AdressbuchView.form @@ -104,14 +104,19 @@ + + + + + + + + + + - - - - - diff --git a/src/prfourgui/View/AdressbuchView.java b/src/prfourgui/View/AdressbuchView.java index cf61bd5..102b3c0 100644 --- a/src/prfourgui/View/AdressbuchView.java +++ b/src/prfourgui/View/AdressbuchView.java @@ -34,6 +34,7 @@ public class AdressbuchView extends javax.swing.JFrame toolBar = new javax.swing.JToolBar(); toolAdd = new javax.swing.JButton(); toolRemove = new javax.swing.JButton(); + toolUndo = new javax.swing.JButton(); adressTable2 = new prfourgui.View.AdressTable(); pnStatus = new javax.swing.JScrollPane(); txtStatus = new javax.swing.JTextArea(); @@ -62,8 +63,13 @@ public class AdressbuchView extends javax.swing.JFrame toolRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolBar.add(toolRemove); - getContentPane().add(toolBar, java.awt.BorderLayout.NORTH); + toolUndo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/prfourgui/Icons/Undo24.gif"))); // NOI18N + toolUndo.setFocusable(false); + toolUndo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + toolUndo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + toolBar.add(toolUndo); + getContentPane().add(toolBar, java.awt.BorderLayout.NORTH); getContentPane().add(adressTable2, java.awt.BorderLayout.CENTER); txtStatus.setColumns(20); @@ -167,6 +173,7 @@ public class AdressbuchView extends javax.swing.JFrame private javax.swing.JButton toolAdd; private javax.swing.JToolBar toolBar; private javax.swing.JButton toolRemove; + private javax.swing.JButton toolUndo; private javax.swing.JTextArea txtStatus; // End of variables declaration//GEN-END:variables @@ -225,4 +232,20 @@ public class AdressbuchView extends javax.swing.JFrame { this.txtStatus = txtStatus; } + + /** + * @return the adressTable2 + */ + public prfourgui.View.AdressTable getAdressTable2() + { + return adressTable2; + } + + /** + * @param adressTable2 the adressTable2 to set + */ + public void setAdressTable2(prfourgui.View.AdressTable adressTable2) + { + this.adressTable2 = adressTable2; + } }