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