1 package cz.frantovo.superPostak;
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;
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;
27 public class PanelPrijemcu extends javax.swing.JPanel {
29 private static Logger log = Logger.getLogger(PanelPrijemcu.class.getName());
30 private static final long serialVersionUID = -8981943048392428523L;
31 private ModelTabulky data = new ModelTabulky();
33 /** Creates new form PanelPrijemcu */
34 public PanelPrijemcu() {
36 tabulka.setModel(data);
37 tabulka.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
40 public ArrayList<InternetAddressKomu> getAdresy() {
41 /** TODO: vracet kopii? */
46 * @param prijemci pokud je null, nastaví prázdná kolekce.
48 public void setAdresy(Collection<InternetAddressKomu> adresy) {
49 data.data = new ArrayList<InternetAddressKomu>();
53 public void pridejAdresu(Collection<InternetAddressKomu> adresy) {
55 log.log(Level.FINER, "Prázdná množina adres - nic nepřidáme");
57 data.data.addAll(adresy);
58 data.upozorniPosluchace();
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.
66 public void setEditovatelny(boolean editovatelny) {
67 otevrit.setEnabled(editovatelny);
68 pridat.setEnabled(editovatelny);
69 odebrat.setEnabled(editovatelny);
70 tabulka.setEnabled(editovatelny);
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.
78 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
79 private void initComponents() {
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();
88 tabulka.setModel(new javax.swing.table.DefaultTableModel(
90 {null, null, null, null},
91 {null, null, null, null},
92 {null, null, null, null},
93 {null, null, null, null}
96 "Title 1", "Title 2", "Title 3", "Title 4"
99 jScrollPane1.setViewportView(tabulka);
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);
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);
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);
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);
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)
151 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
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))
165 }// </editor-fold>//GEN-END:initComponents
166 private void pridatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pridatActionPerformed
168 }//GEN-LAST:event_pridatActionPerformed
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);
176 }//GEN-LAST:event_odebratActionPerformed
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();
186 setCursor(new Cursor(Cursor.WAIT_CURSOR));
187 OutputStreamWriter zapisovac = new FileWriter(soubor);
188 int pocetDobrych = 0;
189 int pocetPreskocenych = 0;
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");
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.)";
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);
211 setCursor(Cursor.getDefaultCursor());
214 }//GEN-LAST:event_ulozitActionPerformed
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();
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>();
231 while (radka != null) {
232 if (Postak.zkontrolujAdresu(radka)) {
233 adresy.add(new InternetAddressKomu(radka, null));
240 radka = ctenar.readLine();
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.)";
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);
255 setCursor(Cursor.getDefaultCursor());
259 }//GEN-LAST:event_otevritActionPerformed
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
270 private class ModelTabulky implements TableModel {
272 private String[] sloupecky = {"Jméno", "e-mail"};
273 private ArrayList<InternetAddressKomu> data = new ArrayList<InternetAddressKomu>();
274 private HashSet<TableModelListener> posluchace = new HashSet<TableModelListener>();
276 public int getRowCount() {
280 public int getColumnCount() {
281 return sloupecky.length;
284 public String getColumnName(int n) {
288 public Class<?> getColumnClass(int n) {
292 public boolean isCellEditable(int m, int n) {
296 public Object getValueAt(int m, int n) {
298 return data.get(m).getPersonal();
300 return data.get(m).getAddress();
306 public void setValueAt(Object value, int m, int n) {
309 data.get(m).setPersonal(String.valueOf(value.toString()));
310 } catch (Exception ex) {
311 log.log(Level.SEVERE, "ModelTabulky.setValueAt(" + m + ", " + n + ")", ex);
314 data.get(m).setAddress(String.valueOf(value.toString()));
318 public void addTableModelListener(TableModelListener l) {
322 public void removeTableModelListener(TableModelListener l) {
323 posluchace.remove(l);
326 public void pridejRadek() {
327 data.add(new InternetAddressKomu());
329 upozorniPosluchace();
332 public void odeberRadek(int m) {
334 upozorniPosluchace();
337 private void upozorniPosluchace() {
338 for (TableModelListener p : posluchace) {
339 p.tableChanged(new TableModelEvent(this));