Souborové dialogy si pamatují naposledy použitý adresář.
1 package cz.frantovo.superPostak;
3 import cz.frantovo.gui.tabulky.TableSorterModel;
4 import cz.frantovo.postak.InternetAddressKomu;
5 import cz.frantovo.postak.Nastaveni;
6 import cz.frantovo.postak.Postak;
7 import java.awt.Cursor;
8 import java.io.BufferedReader;
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;
28 public class PanelPrijemcu extends javax.swing.JPanel {
30 private static final Logger log = Logger.getLogger(PanelPrijemcu.class.getName());
31 private static final long serialVersionUID = -8981943048392428523L;
32 private ModelTabulky data = new ModelTabulky();
33 private Nastaveni nastaveni = new Nastaveni();
35 /** Creates new form PanelPrijemcu */
36 public PanelPrijemcu() {
38 tabulka.setModel(data);
39 tabulka.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
42 public ArrayList<InternetAddressKomu> getAdresy() {
43 /** TODO: vracet kopii? */
47 public void setNastaveni(Nastaveni nastaveni) {
48 this.nastaveni = nastaveni;
52 * @param prijemci pokud je null, nastaví prázdná kolekce.
54 public void setAdresy(Collection<InternetAddressKomu> adresy) {
55 data.data = new ArrayList<InternetAddressKomu>();
59 public void pridejAdresu(Collection<InternetAddressKomu> adresy) {
61 log.log(Level.FINER, "Prázdná množina adres - nic nepřidáme");
63 data.data.addAll(adresy);
64 data.upozorniPosluchace();
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.
72 public void setEditovatelny(boolean editovatelny) {
73 otevrit.setEnabled(editovatelny);
74 pridat.setEnabled(editovatelny);
75 odebrat.setEnabled(editovatelny);
76 tabulka.setEnabled(editovatelny);
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.
84 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
85 private void initComponents() {
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();
94 tabulka.setModel(new javax.swing.table.DefaultTableModel(
96 {null, null, null, null},
97 {null, null, null, null},
98 {null, null, null, null},
99 {null, null, null, null}
102 "Title 1", "Title 2", "Title 3", "Title 4"
105 jScrollPane1.setViewportView(tabulka);
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);
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);
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);
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);
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)
157 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
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))
171 }// </editor-fold>//GEN-END:initComponents
172 private void pridatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pridatActionPerformed
174 }//GEN-LAST:event_pridatActionPerformed
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);
182 }//GEN-LAST:event_odebratActionPerformed
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");
188 dialog.setCurrentDirectory(new File(nastaveni.getPracovniAdresar()));
189 int vysledek = dialog.showSaveDialog(this);
190 if (vysledek == JFileChooser.APPROVE_OPTION) {
191 File soubor = dialog.getSelectedFile();
192 nastaveni.setPracovniAdresar(soubor.getParent());
194 setCursor(new Cursor(Cursor.WAIT_CURSOR));
195 OutputStreamWriter zapisovac = new FileWriter(soubor);
196 int pocetDobrych = 0;
197 int pocetPreskocenych = 0;
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");
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.)";
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);
219 setCursor(Cursor.getDefaultCursor());
222 }//GEN-LAST:event_ulozitActionPerformed
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");
228 dialog.setCurrentDirectory(new File(nastaveni.getPracovniAdresar()));
229 int vysledek = dialog.showOpenDialog(this);
230 if (vysledek == JFileChooser.APPROVE_OPTION) {
231 File soubor = dialog.getSelectedFile();
232 nastaveni.setPracovniAdresar(soubor.getParent());
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>();
241 while (radka != null) {
242 if (Postak.zkontrolujAdresu(radka)) {
243 adresy.add(new InternetAddressKomu(radka, null));
250 radka = ctenar.readLine();
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.)";
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);
265 setCursor(Cursor.getDefaultCursor());
269 }//GEN-LAST:event_otevritActionPerformed
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
280 private class ModelTabulky implements TableModel {
282 private String[] sloupecky = {"Jméno", "e-mail"};
283 private ArrayList<InternetAddressKomu> data = new ArrayList<InternetAddressKomu>();
284 private HashSet<TableModelListener> posluchace = new HashSet<TableModelListener>();
286 public int getRowCount() {
290 public int getColumnCount() {
291 return sloupecky.length;
294 public String getColumnName(int n) {
298 public Class<?> getColumnClass(int n) {
302 public boolean isCellEditable(int m, int n) {
306 public Object getValueAt(int m, int n) {
308 return data.get(m).getPersonal();
310 return data.get(m).getAddress();
316 public void setValueAt(Object value, int m, int n) {
319 data.get(m).setPersonal(String.valueOf(value.toString()));
320 } catch (Exception ex) {
321 log.log(Level.SEVERE, "ModelTabulky.setValueAt(" + m + ", " + n + ")", ex);
324 data.get(m).setAddress(String.valueOf(value.toString()));
328 public void addTableModelListener(TableModelListener l) {
332 public void removeTableModelListener(TableModelListener l) {
333 posluchace.remove(l);
336 public void pridejRadek() {
337 data.add(new InternetAddressKomu());
339 upozorniPosluchace();
342 public void odeberRadek(int m) {
344 upozorniPosluchace();
347 private void upozorniPosluchace() {
348 for (TableModelListener p : posluchace) {
349 p.tableChanged(new TableModelEvent(this));