You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdressmanagementModel.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ///*
  2. // * To change this license header, choose License Headers in Project Properties.
  3. // * To change this template file, choose Tools | Templates
  4. // * and open the template in the editor.
  5. // */
  6. //
  7. //package adressmanagement.model;
  8. //
  9. //import java.io.BufferedInputStream;
  10. //import java.io.BufferedReader;
  11. //import java.io.File;
  12. //import java.io.FileInputStream;
  13. //import java.io.FileNotFoundException;
  14. //import java.io.IOException;
  15. //import java.io.InputStreamReader;
  16. //import java.io.UnsupportedEncodingException;
  17. //
  18. ///**
  19. // *
  20. // * @author chris
  21. // */
  22. //public class AdressmanagementModel
  23. //{
  24. // private String text;
  25. // public AdressmanagementModel()
  26. // {
  27. // text = "?";
  28. // }
  29. // public String getText()
  30. // {
  31. // return text;
  32. // }
  33. // public void readFromFile(File f) throws FileNotFoundException, UnsupportedEncodingException, IOException
  34. // {
  35. // //Streams
  36. // FileInputStream fis = new FileInputStream(f);
  37. // InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
  38. // BufferedReader in = new BufferedReader(isr);
  39. // StringBuilder data = new StringBuilder();
  40. // String line = "";
  41. // while((line = in.readLine()) != null)
  42. // {
  43. // data.append(line);
  44. // data.append("\n");
  45. // }
  46. // text = data.toString();
  47. // in.close();
  48. // //BufferedInputStream bin = new BufferedInputStream(fis);
  49. // //WICHTIG: ObjectInputStream, ObjectOutputStream --> readObject oder writeObject = Serialisierung und damit Persistenz
  50. //
  51. // }
  52. //}
  53. ///*
  54. // * To change this license header, choose License Headers in Project Properties.
  55. // * To change this template file, choose Tools | Templates
  56. // * and open the template in the editor.
  57. // */
  58. package adressmanagement.model;
  59. import java.io.BufferedInputStream;
  60. import java.io.BufferedOutputStream;
  61. import java.io.File;
  62. import java.io.FileInputStream;
  63. import java.io.FileNotFoundException;
  64. import java.io.FileOutputStream;
  65. import java.io.IOException;
  66. import java.io.ObjectInputStream;
  67. import java.io.ObjectOutputStream;
  68. import java.util.ArrayList;
  69. import javax.swing.table.AbstractTableModel;
  70. /**
  71. *
  72. * @author le
  73. */
  74. public class AdressmanagementModel extends AbstractTableModel
  75. {
  76. private ArrayList<ArrayList<String>> daten;
  77. private ArrayList<String> adressEintraegeDaten;
  78. private ArrayList<String> adressEintraegeNamen;
  79. public AdressmanagementModel()
  80. {
  81. adressEintraegeDaten = new ArrayList<>();
  82. adressEintraegeNamen = new ArrayList<>();
  83. daten = new ArrayList<>();
  84. adressEintraegeNamen.add("Name");
  85. adressEintraegeDaten.add("Lehner");
  86. adressEintraegeNamen.add("Telefon");
  87. adressEintraegeDaten.add("122345");
  88. daten.add(adressEintraegeDaten);
  89. }
  90. @Override
  91. public int getRowCount()
  92. {
  93. return daten.size();
  94. }
  95. @Override
  96. public int getColumnCount()
  97. {
  98. return adressEintraegeDaten.size();
  99. }
  100. @Override
  101. public Object getValueAt(int row, int col)
  102. {
  103. return daten.get(row).get(col);
  104. }
  105. @Override
  106. public void setValueAt(Object value, int row, int col)
  107. {
  108. daten.get(row).set(col, (String)value);
  109. }
  110. @Override
  111. public boolean isCellEditable(int row, int col)
  112. {
  113. return true;
  114. }
  115. @Override
  116. public String getColumnName(int col)
  117. {
  118. return adressEintraegeNamen.get(col);
  119. }
  120. public void eintragHinzufuegen()
  121. {
  122. adressEintraegeDaten = new ArrayList<>();
  123. adressEintraegeNamen.forEach(s -> adressEintraegeDaten.add(s));
  124. daten.add(adressEintraegeDaten);
  125. this.fireTableDataChanged();
  126. }
  127. public void eintragLoeschen(int row)
  128. {
  129. daten.remove(row);
  130. this.fireTableDataChanged();
  131. }
  132. public void spalteHinzufuegen(String name)
  133. {
  134. adressEintraegeNamen.add(name);
  135. }
  136. public void datenSpeichern(File datei) throws FileNotFoundException, IOException
  137. {
  138. FileOutputStream fos = new FileOutputStream(datei);
  139. BufferedOutputStream bos = new BufferedOutputStream(fos);
  140. ObjectOutputStream oos = new ObjectOutputStream(bos);
  141. oos.writeObject(daten);
  142. oos.writeObject(adressEintraegeNamen);
  143. oos.flush();
  144. oos.close();
  145. }
  146. public void datenLesen(File datei) throws FileNotFoundException, IOException, ClassNotFoundException
  147. {
  148. FileInputStream fis = new FileInputStream(datei);
  149. BufferedInputStream bis = new BufferedInputStream(fis);
  150. ObjectInputStream ois = new ObjectInputStream(bis);
  151. daten = (ArrayList<ArrayList<String>>)ois.readObject();
  152. adressEintraegeNamen = (ArrayList<String>)ois.readObject();
  153. adressEintraegeDaten = daten.get(daten.size()-1);
  154. ois.close();
  155. this.fireTableDataChanged();
  156. }
  157. }