Browse Source

Numer 2 - Half way there

master
jechowma68968 2 years ago
parent
commit
fa08e56964

+ 120
- 0
src/Model/AdressverwaltungModel.java View File

@@ -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<ArrayList<String>> daten;
private ArrayList<String> adressEintraegeDaten;
private ArrayList<String> 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<ArrayList<String>>)ois.readObject();
adressEintraegeNamen = (ArrayList<String>)ois.readObject();
adressEintraegeDaten = daten.get(daten.size()-1);
ois.close();
this.fireTableDataChanged();
}
}

+ 47
- 0
src/controller/Controller.java View File

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

+ 16
- 0
src/controller/Interface.java View File

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

+ 36
- 0
src/controller/Invoker.java View File

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

+ 62
- 0
src/controller/commands/CommandOpen.java View File

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

BIN
src/prfourgui/Icons/Undo24.gif View File


+ 9
- 3
src/prfourgui/Start.java View File

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


+ 1
- 1
src/prfourgui/View/AdressTable.form View File

@@ -41,7 +41,7 @@

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTable" name="jTable1">
<Component class="javax.swing.JTable" name="tblAdressbuch">
<Properties>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
<Table columnCount="4" rowCount="4">

+ 20
- 5
src/prfourgui/View/AdressTable.java View File

@@ -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);
}// </editor-fold>//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
}

+ 10
- 5
src/prfourgui/View/AdressbuchView.form View File

@@ -104,14 +104,19 @@
<Property name="verticalTextPosition" type="int" value="3"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="toolUndo">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/prfourgui/Icons/Undo24.gif"/>
</Property>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
<Property name="verticalTextPosition" type="int" value="3"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="prfourgui.View.AdressTable" name="adressTable2">
<Properties>
<Property name="componentPopupMenu" type="javax.swing.JPopupMenu" editor="org.netbeans.modules.form.ComponentChooserEditor">
<ComponentRef name="default"/>
</Property>
</Properties>
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
<BorderConstraints direction="Center"/>

+ 24
- 1
src/prfourgui/View/AdressbuchView.java View File

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

Loading…
Cancel
Save