[0] | 1 | package cz.frantovo.superPostak; |
---|
| 2 | |
---|
| 3 | import cz.frantovo.gui.tabulky.TableSorterModel; |
---|
| 4 | import cz.frantovo.postak.InternetAddressKomu; |
---|
| 5 | import cz.frantovo.postak.Postak; |
---|
| 6 | import java.awt.Cursor; |
---|
| 7 | import java.io.BufferedReader; |
---|
| 8 | import java.io.File; |
---|
| 9 | import java.io.FileReader; |
---|
| 10 | import java.io.FileWriter; |
---|
| 11 | import java.io.OutputStreamWriter; |
---|
| 12 | import java.util.ArrayList; |
---|
| 13 | import java.util.Collection; |
---|
| 14 | import java.util.HashSet; |
---|
| 15 | import java.util.logging.Level; |
---|
| 16 | import java.util.logging.Logger; |
---|
| 17 | import javax.swing.DefaultListSelectionModel; |
---|
| 18 | import javax.swing.JFileChooser; |
---|
| 19 | import javax.swing.event.TableModelEvent; |
---|
| 20 | import javax.swing.event.TableModelListener; |
---|
| 21 | import javax.swing.table.TableModel; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * |
---|
| 25 | * @author fiki |
---|
| 26 | */ |
---|
| 27 | public class PanelPrijemcu extends javax.swing.JPanel { |
---|
| 28 | |
---|
| 29 | private static Logger log = Logger.getLogger(PanelPrijemcu.class.getName()); |
---|
[3] | 30 | private static final long serialVersionUID = -8981943048392428523L; |
---|
[0] | 31 | private ModelTabulky data = new ModelTabulky(); |
---|
| 32 | |
---|
| 33 | /** Creates new form PanelPrijemcu */ |
---|
| 34 | public PanelPrijemcu() { |
---|
| 35 | initComponents(); |
---|
| 36 | tabulka.setModel(data); |
---|
| 37 | tabulka.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public ArrayList<InternetAddressKomu> getAdresy() { |
---|
| 41 | /** TODO: vracet kopii? */ |
---|
| 42 | return data.data; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * @param prijemci pokud je null, nastaví prázdná kolekce. |
---|
| 47 | */ |
---|
| 48 | public void setAdresy(Collection<InternetAddressKomu> adresy) { |
---|
| 49 | data.data = new ArrayList<InternetAddressKomu>(); |
---|
| 50 | pridejAdresu(adresy); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public void pridejAdresu(Collection<InternetAddressKomu> adresy) { |
---|
| 54 | if (adresy == null) { |
---|
| 55 | log.log(Level.FINER, "Prázdná množina adres - nic nepřidáme"); |
---|
| 56 | } else { |
---|
| 57 | data.data.addAll(adresy); |
---|
| 58 | data.upozorniPosluchace(); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Pokud je needitovatelný, nelze měnit data v tabuce |
---|
| 64 | * a z tlačítek je povoleno pouze tlačítko pro uložení do souboru. |
---|
| 65 | */ |
---|
| 66 | public void setEditovatelny(boolean editovatelny) { |
---|
| 67 | otevrit.setEnabled(editovatelny); |
---|
| 68 | pridat.setEnabled(editovatelny); |
---|
| 69 | odebrat.setEnabled(editovatelny); |
---|
| 70 | tabulka.setEnabled(editovatelny); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** This method is called from within the constructor to |
---|
| 74 | * initialize the form. |
---|
| 75 | * WARNING: Do NOT modify this code. The content of this method is |
---|
| 76 | * always regenerated by the Form Editor. |
---|
| 77 | */ |
---|
| 78 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
---|
| 79 | private void initComponents() { |
---|
| 80 | |
---|
| 81 | jScrollPane1 = new javax.swing.JScrollPane(); |
---|
| 82 | tabulka = new cz.frantovo.gui.tabulky.JTable(); |
---|
| 83 | pridat = new javax.swing.JButton(); |
---|
| 84 | odebrat = new javax.swing.JButton(); |
---|
| 85 | ulozit = new javax.swing.JButton(); |
---|
| 86 | otevrit = new javax.swing.JButton(); |
---|
| 87 | |
---|
| 88 | tabulka.setModel(new javax.swing.table.DefaultTableModel( |
---|
| 89 | new Object [][] { |
---|
| 90 | {null, null, null, null}, |
---|
| 91 | {null, null, null, null}, |
---|
| 92 | {null, null, null, null}, |
---|
| 93 | {null, null, null, null} |
---|
| 94 | }, |
---|
| 95 | new String [] { |
---|
| 96 | "Title 1", "Title 2", "Title 3", "Title 4" |
---|
| 97 | } |
---|
| 98 | )); |
---|
| 99 | jScrollPane1.setViewportView(tabulka); |
---|
| 100 | |
---|
| 101 | pridat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/frantovo/superPostak/obrazky/vlozit.png"))); // NOI18N |
---|
| 102 | pridat.setMnemonic('i'); |
---|
| 103 | pridat.setText("Přidat"); |
---|
| 104 | pridat.setToolTipText("<html>\nPřidá prázdný řádek pro vložení nového příjemce<br/>\nAby se napsaná změna projevila, je potřeba kliknout jinam do tabulky<br/>\n(tzn. ukončit režim editace buňky)<br/>\n</html>"); |
---|
| 105 | pridat.addActionListener(new java.awt.event.ActionListener() { |
---|
| 106 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
---|
| 107 | pridatActionPerformed(evt); |
---|
| 108 | } |
---|
| 109 | }); |
---|
| 110 | |
---|
| 111 | odebrat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/frantovo/superPostak/obrazky/editdelete.png"))); // NOI18N |
---|
| 112 | odebrat.setMnemonic('r'); |
---|
| 113 | odebrat.setText("Odebrat"); |
---|
| 114 | odebrat.setToolTipText("Odebere vybraného příjemce"); |
---|
| 115 | odebrat.addActionListener(new java.awt.event.ActionListener() { |
---|
| 116 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
---|
| 117 | odebratActionPerformed(evt); |
---|
| 118 | } |
---|
| 119 | }); |
---|
| 120 | |
---|
| 121 | ulozit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/frantovo/superPostak/obrazky/ulozit.png"))); // NOI18N |
---|
| 122 | ulozit.setToolTipText("Uložit seznam příjemců do souboru."); |
---|
| 123 | ulozit.addActionListener(new java.awt.event.ActionListener() { |
---|
| 124 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
---|
| 125 | ulozitActionPerformed(evt); |
---|
| 126 | } |
---|
| 127 | }); |
---|
| 128 | |
---|
| 129 | otevrit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/frantovo/superPostak/obrazky/otevrit.png"))); // NOI18N |
---|
| 130 | otevrit.setToolTipText("<html>Přidat příjemce ze souboru.<br/>Na každém řádku se očekává jedna adresa.</html>"); |
---|
| 131 | otevrit.addActionListener(new java.awt.event.ActionListener() { |
---|
| 132 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
---|
| 133 | otevritActionPerformed(evt); |
---|
| 134 | } |
---|
| 135 | }); |
---|
| 136 | |
---|
| 137 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
---|
| 138 | this.setLayout(layout); |
---|
| 139 | layout.setHorizontalGroup( |
---|
| 140 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
---|
| 141 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
---|
| 142 | .addContainerGap(97, Short.MAX_VALUE) |
---|
| 143 | .addComponent(otevrit) |
---|
| 144 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
---|
| 145 | .addComponent(ulozit) |
---|
| 146 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
---|
| 147 | .addComponent(odebrat) |
---|
| 148 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
---|
| 149 | .addComponent(pridat) |
---|
| 150 | .addContainerGap()) |
---|
| 151 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE) |
---|
| 152 | ); |
---|
| 153 | layout.setVerticalGroup( |
---|
| 154 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
---|
| 155 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
---|
| 156 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE) |
---|
| 157 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
---|
| 158 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
---|
| 159 | .addComponent(pridat) |
---|
| 160 | .addComponent(odebrat) |
---|
| 161 | .addComponent(ulozit) |
---|
| 162 | .addComponent(otevrit)) |
---|
| 163 | .addContainerGap()) |
---|
| 164 | ); |
---|
| 165 | }// </editor-fold>//GEN-END:initComponents |
---|
| 166 | private void pridatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pridatActionPerformed |
---|
| 167 | data.pridejRadek(); |
---|
| 168 | }//GEN-LAST:event_pridatActionPerformed |
---|
| 169 | |
---|
| 170 | private void odebratActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odebratActionPerformed |
---|
| 171 | int radkaV = tabulka.getSelectedRow(); |
---|
| 172 | int radka = ((TableSorterModel) tabulka.getModel()).modelIndex(radkaV); |
---|
| 173 | if (radka > -1 && radka < data.getRowCount()) { |
---|
| 174 | data.odeberRadek(radka); |
---|
| 175 | } |
---|
| 176 | }//GEN-LAST:event_odebratActionPerformed |
---|
| 177 | |
---|
| 178 | private void ulozitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ulozitActionPerformed |
---|
| 179 | JFileChooser dialog = new JFileChooser(); |
---|
| 180 | dialog.setMultiSelectionEnabled(false); |
---|
| 181 | dialog.setDialogTitle("Uložit seznam do souboru"); |
---|
| 182 | int vysledek = dialog.showSaveDialog(this); |
---|
| 183 | if (vysledek == JFileChooser.APPROVE_OPTION) { |
---|
| 184 | File soubor = dialog.getSelectedFile(); |
---|
| 185 | try { |
---|
| 186 | setCursor(new Cursor(Cursor.WAIT_CURSOR)); |
---|
| 187 | OutputStreamWriter zapisovac = new FileWriter(soubor); |
---|
| 188 | int pocetDobrych = 0; |
---|
| 189 | int pocetPreskocenych = 0; |
---|
| 190 | |
---|
| 191 | for (InternetAddressKomu adresa : data.data) { |
---|
| 192 | /** TODO: různé formátování, případně jména */ |
---|
| 193 | if (Postak.zkontrolujAdresu(adresa.getAddress())) { |
---|
| 194 | zapisovac.write(adresa.getAddress() + "\n"); |
---|
| 195 | pocetDobrych++; |
---|
| 196 | } else { |
---|
| 197 | pocetPreskocenych++; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | zapisovac.close(); |
---|
| 202 | String preskocene = ""; |
---|
| 203 | if (pocetPreskocenych > 0) { |
---|
| 204 | preskocene = "\nPočet přeskočených: " + pocetPreskocenych + "\n(Přeskočené neodpovídaly regulárnímu výrazu.)"; |
---|
| 205 | |
---|
| 206 | } |
---|
| 207 | SuperPostak.zobrazInformacniDialog(this, "Uložení seznamu", "Adresy příjemců byly úspěšně uloženy do souboru:\n" + soubor.getAbsolutePath() + "\nPočet uložených adres: " + pocetDobrych + preskocene, true); |
---|
| 208 | } catch (Exception e) { |
---|
| 209 | SuperPostak.zobrazChybovyDialog(this, "Uložení seznamu selhalo", "Adresy se nepodařilo uložit do souboru.", null, e, Level.WARNING, false); |
---|
| 210 | } finally { |
---|
| 211 | setCursor(Cursor.getDefaultCursor()); |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | }//GEN-LAST:event_ulozitActionPerformed |
---|
| 215 | |
---|
| 216 | private void otevritActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_otevritActionPerformed |
---|
| 217 | JFileChooser dialog = new JFileChooser(); |
---|
| 218 | dialog.setMultiSelectionEnabled(false); |
---|
| 219 | dialog.setDialogTitle("Načíst adresy ze souboru"); |
---|
| 220 | int vysledek = dialog.showOpenDialog(this); |
---|
| 221 | if (vysledek == JFileChooser.APPROVE_OPTION) { |
---|
| 222 | File soubor = dialog.getSelectedFile(); |
---|
| 223 | try { |
---|
| 224 | setCursor(new Cursor(Cursor.WAIT_CURSOR)); |
---|
| 225 | BufferedReader ctenar = new BufferedReader(new FileReader(soubor)); |
---|
| 226 | int pocetDobrych = 0; |
---|
| 227 | int pocetPreskocenych = 0; |
---|
| 228 | String radka = ctenar.readLine(); |
---|
| 229 | ArrayList<InternetAddressKomu> adresy = new ArrayList<InternetAddressKomu>(); |
---|
| 230 | |
---|
| 231 | while (radka != null) { |
---|
| 232 | if (Postak.zkontrolujAdresu(radka)) { |
---|
| 233 | adresy.add(new InternetAddressKomu(radka, null)); |
---|
| 234 | pocetDobrych++; |
---|
| 235 | |
---|
| 236 | } else { |
---|
| 237 | pocetPreskocenych++; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | radka = ctenar.readLine(); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | setAdresy(adresy); |
---|
| 244 | ctenar.close(); |
---|
| 245 | String preskocene = ""; |
---|
| 246 | if (pocetPreskocenych > 0) { |
---|
| 247 | preskocene = "\nPočet přeskočených: " + pocetPreskocenych + "\n(Přeskočené neodpovídaly regulárnímu výrazu.)"; |
---|
| 248 | |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | SuperPostak.zobrazInformacniDialog(this, "Načtení adres", "Byl načten seznam adres ze souboru:\n" + soubor.getAbsolutePath() + "\nPočet načtených adres: " + pocetDobrych + preskocene, true); |
---|
| 252 | } catch (Exception e) { |
---|
| 253 | SuperPostak.zobrazChybovyDialog(this, "Načtení souboru selhalo", "Nepodařilo se načíst soubor s adresami.", null, e, Level.WARNING, false); |
---|
| 254 | } finally { |
---|
| 255 | setCursor(Cursor.getDefaultCursor()); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | }//GEN-LAST:event_otevritActionPerformed |
---|
| 260 | |
---|
| 261 | // Variables declaration - do not modify//GEN-BEGIN:variables |
---|
| 262 | private javax.swing.JScrollPane jScrollPane1; |
---|
| 263 | private javax.swing.JButton odebrat; |
---|
| 264 | private javax.swing.JButton otevrit; |
---|
| 265 | private javax.swing.JButton pridat; |
---|
| 266 | private cz.frantovo.gui.tabulky.JTable tabulka; |
---|
| 267 | private javax.swing.JButton ulozit; |
---|
| 268 | // End of variables declaration//GEN-END:variables |
---|
[3] | 269 | |
---|
[0] | 270 | private class ModelTabulky implements TableModel { |
---|
| 271 | |
---|
| 272 | private String[] sloupecky = {"Jméno", "e-mail"}; |
---|
| 273 | private ArrayList<InternetAddressKomu> data = new ArrayList<InternetAddressKomu>(); |
---|
| 274 | private HashSet<TableModelListener> posluchace = new HashSet<TableModelListener>(); |
---|
| 275 | |
---|
| 276 | public int getRowCount() { |
---|
| 277 | return data.size(); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | public int getColumnCount() { |
---|
| 281 | return sloupecky.length; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | public String getColumnName(int n) { |
---|
| 285 | return sloupecky[n]; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | public Class<?> getColumnClass(int n) { |
---|
| 289 | return String.class; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | public boolean isCellEditable(int m, int n) { |
---|
| 293 | return true; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | public Object getValueAt(int m, int n) { |
---|
| 297 | if (n == 0) { |
---|
| 298 | return data.get(m).getPersonal(); |
---|
| 299 | } else if (n == 1) { |
---|
| 300 | return data.get(m).getAddress(); |
---|
| 301 | } else { |
---|
| 302 | return null; |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | public void setValueAt(Object value, int m, int n) { |
---|
| 307 | if (n == 0) { |
---|
| 308 | try { |
---|
| 309 | data.get(m).setPersonal(String.valueOf(value.toString())); |
---|
| 310 | } catch (Exception ex) { |
---|
| 311 | log.log(Level.SEVERE, "ModelTabulky.setValueAt(" + m + ", " + n + ")", ex); |
---|
| 312 | } |
---|
| 313 | } else if (n == 1) { |
---|
| 314 | data.get(m).setAddress(String.valueOf(value.toString())); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | public void addTableModelListener(TableModelListener l) { |
---|
| 319 | posluchace.add(l); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | public void removeTableModelListener(TableModelListener l) { |
---|
| 323 | posluchace.remove(l); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | public void pridejRadek() { |
---|
| 327 | data.add(new InternetAddressKomu()); |
---|
| 328 | |
---|
| 329 | upozorniPosluchace(); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | public void odeberRadek(int m) { |
---|
| 333 | data.remove(m); |
---|
| 334 | upozorniPosluchace(); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | private void upozorniPosluchace() { |
---|
| 338 | for (TableModelListener p : posluchace) { |
---|
| 339 | p.tableChanged(new TableModelEvent(this)); |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | } |
---|