/* | |||||
* 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(); | |||||
} | |||||
} |
/* | |||||
* 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); | |||||
} | |||||
} |
/* | |||||
* 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(); | |||||
} |
/* | |||||
* 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(); | |||||
} | |||||
} |
/* | |||||
* 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() | |||||
{ | |||||
} | |||||
} |
package prfourgui; | package prfourgui; | ||||
import controller.ControllerOeffnen; | |||||
import adressverwaltung.model.AdressverwaltungModel; | |||||
import controller.Controller; | |||||
import prfourgui.View.AdressbuchView; | import prfourgui.View.AdressbuchView; | ||||
/** | /** | ||||
public Start() | public Start() | ||||
{ | { | ||||
AdressbuchView view = new AdressbuchView(); | 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); | view.setVisible(true); | ||||
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | ||||
<SubComponents> | <SubComponents> | ||||
<Component class="javax.swing.JTable" name="jTable1"> | |||||
<Component class="javax.swing.JTable" name="tblAdressbuch"> | |||||
<Properties> | <Properties> | ||||
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> | <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> | ||||
<Table columnCount="4" rowCount="4"> | <Table columnCount="4" rowCount="4"> |
*/ | */ | ||||
public class AdressTable extends javax.swing.JPanel | 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 | * Creates new form AdressTable | ||||
jPopupMenu1 = new javax.swing.JPopupMenu(); | jPopupMenu1 = new javax.swing.JPopupMenu(); | ||||
jMenuItem1 = new javax.swing.JMenuItem(); | jMenuItem1 = new javax.swing.JMenuItem(); | ||||
jScrollPane1 = new javax.swing.JScrollPane(); | 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.setIcon(new javax.swing.ImageIcon(getClass().getResource("/prfourgui/Icons/Delete24.gif"))); // NOI18N | ||||
jMenuItem1.setText("Remove"); | jMenuItem1.setText("Remove"); | ||||
setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS)); | 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 [][] | new Object [][] | ||||
{ | { | ||||
{null, null, null, null}, | {null, null, null, null}, | ||||
"Name", "Telefon", "Mobil", "E-Mail" | "Name", "Telefon", "Mobil", "E-Mail" | ||||
} | } | ||||
)); | )); | ||||
jTable1.setComponentPopupMenu(jPopupMenu1); | |||||
jScrollPane1.setViewportView(jTable1); | |||||
tblAdressbuch.setComponentPopupMenu(jPopupMenu1); | |||||
jScrollPane1.setViewportView(tblAdressbuch); | |||||
add(jScrollPane1); | add(jScrollPane1); | ||||
}// </editor-fold>//GEN-END:initComponents | }// </editor-fold>//GEN-END:initComponents | ||||
private javax.swing.JMenuItem jMenuItem1; | private javax.swing.JMenuItem jMenuItem1; | ||||
private javax.swing.JPopupMenu jPopupMenu1; | private javax.swing.JPopupMenu jPopupMenu1; | ||||
private javax.swing.JScrollPane jScrollPane1; | private javax.swing.JScrollPane jScrollPane1; | ||||
private javax.swing.JTable jTable1; | |||||
private javax.swing.JTable tblAdressbuch; | |||||
// End of variables declaration//GEN-END:variables | // End of variables declaration//GEN-END:variables | ||||
} | } |
<Property name="verticalTextPosition" type="int" value="3"/> | <Property name="verticalTextPosition" type="int" value="3"/> | ||||
</Properties> | </Properties> | ||||
</Component> | </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> | </SubComponents> | ||||
</Container> | </Container> | ||||
<Component class="prfourgui.View.AdressTable" name="adressTable2"> | <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> | <Constraints> | ||||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | ||||
<BorderConstraints direction="Center"/> | <BorderConstraints direction="Center"/> |
toolBar = new javax.swing.JToolBar(); | toolBar = new javax.swing.JToolBar(); | ||||
toolAdd = new javax.swing.JButton(); | toolAdd = new javax.swing.JButton(); | ||||
toolRemove = new javax.swing.JButton(); | toolRemove = new javax.swing.JButton(); | ||||
toolUndo = new javax.swing.JButton(); | |||||
adressTable2 = new prfourgui.View.AdressTable(); | adressTable2 = new prfourgui.View.AdressTable(); | ||||
pnStatus = new javax.swing.JScrollPane(); | pnStatus = new javax.swing.JScrollPane(); | ||||
txtStatus = new javax.swing.JTextArea(); | txtStatus = new javax.swing.JTextArea(); | ||||
toolRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); | toolRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); | ||||
toolBar.add(toolRemove); | 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); | getContentPane().add(adressTable2, java.awt.BorderLayout.CENTER); | ||||
txtStatus.setColumns(20); | txtStatus.setColumns(20); | ||||
private javax.swing.JButton toolAdd; | private javax.swing.JButton toolAdd; | ||||
private javax.swing.JToolBar toolBar; | private javax.swing.JToolBar toolBar; | ||||
private javax.swing.JButton toolRemove; | private javax.swing.JButton toolRemove; | ||||
private javax.swing.JButton toolUndo; | |||||
private javax.swing.JTextArea txtStatus; | private javax.swing.JTextArea txtStatus; | ||||
// End of variables declaration//GEN-END:variables | // End of variables declaration//GEN-END:variables | ||||
{ | { | ||||
this.txtStatus = txtStatus; | 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; | |||||
} | |||||
} | } |