franta-hg@19
|
1 |
/**
|
franta-hg@19
|
2 |
* Rozšířené atributy – program na správu rozšířených atributů souborů
|
franta-hg@19
|
3 |
* Copyright © 2012 František Kučera (frantovo.cz)
|
franta-hg@22
|
4 |
*
|
franta-hg@19
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@19
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@27
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
franta-hg@22
|
8 |
*
|
franta-hg@19
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@19
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@22
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@19
|
12 |
* GNU General Public License for more details.
|
franta-hg@22
|
13 |
*
|
franta-hg@19
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@22
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@19
|
16 |
*/
|
franta-hg@28
|
17 |
package cz.frantovo.rozsireneatributy.gui;
|
franta-hg@6
|
18 |
|
franta-hg@28
|
19 |
import cz.frantovo.rozsireneatributy.Atribut;
|
franta-hg@35
|
20 |
import cz.frantovo.rozsireneatributy.CSV;
|
franta-hg@35
|
21 |
import java.awt.Toolkit;
|
franta-hg@35
|
22 |
import java.awt.datatransfer.StringSelection;
|
franta-hg@41
|
23 |
import java.awt.event.KeyEvent;
|
franta-hg@10
|
24 |
import java.io.IOException;
|
franta-hg@35
|
25 |
import java.io.StringWriter;
|
franta-hg@15
|
26 |
import java.util.ResourceBundle;
|
franta-hg@10
|
27 |
import java.util.logging.Level;
|
franta-hg@10
|
28 |
import java.util.logging.Logger;
|
franta-hg@10
|
29 |
import javax.swing.JOptionPane;
|
franta-hg@16
|
30 |
import javax.swing.JTable;
|
franta-hg@41
|
31 |
import javax.swing.KeyStroke;
|
franta-hg@16
|
32 |
import javax.swing.ListSelectionModel;
|
franta-hg@10
|
33 |
import javax.swing.event.ListSelectionEvent;
|
franta-hg@10
|
34 |
import javax.swing.event.ListSelectionListener;
|
franta-hg@6
|
35 |
|
franta-hg@6
|
36 |
/**
|
franta-hg@30
|
37 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@6
|
38 |
*/
|
franta-hg@6
|
39 |
public class Panel extends javax.swing.JPanel {
|
franta-hg@6
|
40 |
|
franta-hg@32
|
41 |
public static final int SLOUPEC_NÁZVU = 0;
|
franta-hg@32
|
42 |
public static final int SLOUPEC_HODNOTY = 1;
|
franta-hg@29
|
43 |
private static final Logger log = Logger
|
franta-hg@29
|
44 |
.getLogger(Panel.class.getSimpleName());
|
franta-hg@29
|
45 |
private static final ResourceBundle překlady = ResourceBundle
|
franta-hg@29
|
46 |
.getBundle(Atribut.class.getPackageName() + ".Překlady");
|
franta-hg@10
|
47 |
private Model model;
|
franta-hg@10
|
48 |
private Atribut vybranýAtribut;
|
franta-hg@16
|
49 |
private JTable tabulka;
|
franta-hg@6
|
50 |
|
franta-hg@10
|
51 |
public Panel(Model model) {
|
franta-hg@6
|
52 |
this.model = model;
|
franta-hg@10
|
53 |
initComponents();
|
franta-hg@16
|
54 |
|
franta-hg@30
|
55 |
tlačítkoZamknout.setEnabled(model.isZámekPodporovaný());
|
franta-hg@30
|
56 |
tlačítkoZamknout.setToolTipText(model.isZámekPodporovaný()
|
franta-hg@30
|
57 |
? překlady.getString("zamknout.popis")
|
franta-hg@30
|
58 |
: překlady.getString("chyba.lzeZamknoutJenSoubor"));
|
franta-hg@30
|
59 |
|
franta-hg@16
|
60 |
tabulka = new JTable(model);
|
franta-hg@23
|
61 |
nastavEditor();
|
franta-hg@16
|
62 |
tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
franta-hg@16
|
63 |
posuvnýPanel.setViewportView(tabulka);
|
franta-hg@35
|
64 |
|
franta-hg@33
|
65 |
tabulka.setRowHeight((int) (tabulka.getRowHeight() * 1.3));
|
franta-hg@33
|
66 |
|
franta-hg@41
|
67 |
// Výběr aktuálního atributu v tabulce:
|
franta-hg@29
|
68 |
tabulka.getSelectionModel().addListSelectionListener(
|
franta-hg@29
|
69 |
new ListSelectionListener() {
|
franta-hg@6
|
70 |
|
franta-hg@22
|
71 |
@Override
|
franta-hg@10
|
72 |
public void valueChanged(ListSelectionEvent e) {
|
franta-hg@10
|
73 |
int řádek = tabulka.getSelectedRow();
|
franta-hg@10
|
74 |
if (řádek < 0) {
|
franta-hg@10
|
75 |
vybranýAtribut = null;
|
franta-hg@10
|
76 |
tlačítkoSmazat.setEnabled(false);
|
franta-hg@10
|
77 |
} else {
|
franta-hg@10
|
78 |
vybranýAtribut = getModel().getAtribut(řádek);
|
franta-hg@10
|
79 |
tlačítkoSmazat.setEnabled(true);
|
franta-hg@10
|
80 |
}
|
franta-hg@10
|
81 |
}
|
franta-hg@10
|
82 |
});
|
franta-hg@41
|
83 |
|
franta-hg@41
|
84 |
// Buňky tabulky editovatelné po stisku mezerníku:
|
franta-hg@41
|
85 |
tabulka.getInputMap()
|
franta-hg@41
|
86 |
.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
|
franta-hg@10
|
87 |
}
|
franta-hg@10
|
88 |
|
franta-hg@23
|
89 |
private void nastavEditor() {
|
franta-hg@29
|
90 |
tabulka.getColumnModel().getColumn(SLOUPEC_NÁZVU)
|
franta-hg@31
|
91 |
.setCellEditor(new EditorNázvůAtributů(model.getKonfigurace()));
|
franta-hg@32
|
92 |
tabulka.getColumnModel().getColumn(SLOUPEC_HODNOTY)
|
franta-hg@32
|
93 |
.setCellEditor(new EditorHodnotAtributů(model.getKonfigurace()));
|
franta-hg@23
|
94 |
}
|
franta-hg@23
|
95 |
|
franta-hg@10
|
96 |
private Model getModel() {
|
franta-hg@10
|
97 |
return model;
|
franta-hg@10
|
98 |
}
|
franta-hg@10
|
99 |
|
franta-hg@23
|
100 |
public void setModel(Model model) {
|
franta-hg@23
|
101 |
this.model = model;
|
franta-hg@23
|
102 |
tabulka.setModel(model);
|
franta-hg@23
|
103 |
nastavEditor();
|
franta-hg@23
|
104 |
}
|
franta-hg@23
|
105 |
|
franta-hg@10
|
106 |
private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
|
franta-hg@30
|
107 |
JOptionPane.showMessageDialog(this, hláška + "\n"
|
franta-hg@30
|
108 |
+ chyba.getLocalizedMessage(),
|
franta-hg@30
|
109 |
překlady.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE);
|
franta-hg@10
|
110 |
log.log(Level.WARNING, hláška, chyba);
|
franta-hg@10
|
111 |
}
|
franta-hg@35
|
112 |
|
franta-hg@35
|
113 |
private void kopírujDoSchránky() {
|
franta-hg@35
|
114 |
try {
|
franta-hg@35
|
115 |
StringWriter výstup = new StringWriter();
|
franta-hg@35
|
116 |
CSV csv = new CSV(výstup);
|
franta-hg@35
|
117 |
|
franta-hg@35
|
118 |
csv.hodnota(překlady.getString("tabulka.název").toLowerCase());
|
franta-hg@35
|
119 |
csv.hodnota(překlady.getString("tabulka.hodnota").toLowerCase());
|
franta-hg@35
|
120 |
csv.konecŘádku();
|
franta-hg@35
|
121 |
|
franta-hg@35
|
122 |
for (int i = 0; i < model.getRowCount(); i++) {
|
franta-hg@35
|
123 |
csv.hodnota(String.valueOf(model.getValueAt(i, 0)));
|
franta-hg@35
|
124 |
csv.hodnota(String.valueOf(model.getValueAt(i, 1)));
|
franta-hg@35
|
125 |
csv.konecŘádku();
|
franta-hg@35
|
126 |
}
|
franta-hg@35
|
127 |
|
franta-hg@35
|
128 |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
|
franta-hg@35
|
129 |
new StringSelection(výstup.toString()),
|
franta-hg@35
|
130 |
null
|
franta-hg@35
|
131 |
);
|
franta-hg@35
|
132 |
} catch (Exception e) {
|
franta-hg@35
|
133 |
zobrazChybovouHlášku(překlady
|
franta-hg@35
|
134 |
.getString("chyba.nepodařiloSeZkopírovat"), e);
|
franta-hg@35
|
135 |
}
|
franta-hg@35
|
136 |
}
|
franta-hg@10
|
137 |
|
franta-hg@10
|
138 |
@SuppressWarnings("unchecked")
|
franta-hg@6
|
139 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
franta-hg@6
|
140 |
private void initComponents() {
|
franta-hg@6
|
141 |
|
franta-hg@10
|
142 |
posuvnýPanel = new javax.swing.JScrollPane();
|
franta-hg@10
|
143 |
tlačítkoPřidat = new javax.swing.JButton();
|
franta-hg@10
|
144 |
tlačítkoSmazat = new javax.swing.JButton();
|
franta-hg@10
|
145 |
tlačítkoZnovuNačíst = new javax.swing.JButton();
|
franta-hg@30
|
146 |
tlačítkoZamknout = new javax.swing.JToggleButton();
|
franta-hg@35
|
147 |
tlačítkoKopírovat = new javax.swing.JButton();
|
franta-hg@6
|
148 |
|
franta-hg@11
|
149 |
tlačítkoPřidat.setMnemonic('p');
|
franta-hg@28
|
150 |
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneatributy/Překlady"); // NOI18N
|
franta-hg@15
|
151 |
tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
|
franta-hg@10
|
152 |
tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
|
franta-hg@10
|
153 |
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
franta-hg@10
|
154 |
tlačítkoPřidatActionPerformed(evt);
|
franta-hg@10
|
155 |
}
|
franta-hg@10
|
156 |
});
|
franta-hg@10
|
157 |
|
franta-hg@11
|
158 |
tlačítkoSmazat.setMnemonic('s');
|
franta-hg@15
|
159 |
tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
|
franta-hg@10
|
160 |
tlačítkoSmazat.setEnabled(false);
|
franta-hg@10
|
161 |
tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
|
franta-hg@10
|
162 |
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
franta-hg@10
|
163 |
tlačítkoSmazatActionPerformed(evt);
|
franta-hg@10
|
164 |
}
|
franta-hg@10
|
165 |
});
|
franta-hg@10
|
166 |
|
franta-hg@11
|
167 |
tlačítkoZnovuNačíst.setMnemonic('z');
|
franta-hg@15
|
168 |
tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
|
franta-hg@10
|
169 |
tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
|
franta-hg@10
|
170 |
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
franta-hg@10
|
171 |
tlačítkoZnovuNačístActionPerformed(evt);
|
franta-hg@10
|
172 |
}
|
franta-hg@10
|
173 |
});
|
franta-hg@6
|
174 |
|
franta-hg@30
|
175 |
tlačítkoZamknout.setText(bundle.getString("zamknout")); // NOI18N
|
franta-hg@30
|
176 |
tlačítkoZamknout.addActionListener(new java.awt.event.ActionListener() {
|
franta-hg@30
|
177 |
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
franta-hg@30
|
178 |
tlačítkoZamknoutActionPerformed(evt);
|
franta-hg@30
|
179 |
}
|
franta-hg@30
|
180 |
});
|
franta-hg@30
|
181 |
|
franta-hg@35
|
182 |
tlačítkoKopírovat.setText(bundle.getString("schránka.kopírovat")); // NOI18N
|
franta-hg@35
|
183 |
tlačítkoKopírovat.addActionListener(new java.awt.event.ActionListener() {
|
franta-hg@35
|
184 |
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
franta-hg@35
|
185 |
tlačítkoKopírovatActionPerformed(evt);
|
franta-hg@35
|
186 |
}
|
franta-hg@35
|
187 |
});
|
franta-hg@35
|
188 |
|
franta-hg@6
|
189 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
|
franta-hg@6
|
190 |
this.setLayout(layout);
|
franta-hg@6
|
191 |
layout.setHorizontalGroup(
|
franta-hg@6
|
192 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
franta-hg@10
|
193 |
.addGroup(layout.createSequentialGroup()
|
franta-hg@10
|
194 |
.addContainerGap()
|
franta-hg@10
|
195 |
.addComponent(tlačítkoPřidat)
|
franta-hg@10
|
196 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
franta-hg@10
|
197 |
.addComponent(tlačítkoSmazat)
|
franta-hg@10
|
198 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
franta-hg@10
|
199 |
.addComponent(tlačítkoZnovuNačíst)
|
franta-hg@30
|
200 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
franta-hg@30
|
201 |
.addComponent(tlačítkoZamknout)
|
franta-hg@35
|
202 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
franta-hg@35
|
203 |
.addComponent(tlačítkoKopírovat)
|
franta-hg@35
|
204 |
.addContainerGap(25, Short.MAX_VALUE))
|
franta-hg@11
|
205 |
.addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
|
franta-hg@6
|
206 |
);
|
franta-hg@6
|
207 |
layout.setVerticalGroup(
|
franta-hg@6
|
208 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
franta-hg@10
|
209 |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
franta-hg@10
|
210 |
.addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
|
franta-hg@10
|
211 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
franta-hg@10
|
212 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
franta-hg@10
|
213 |
.addComponent(tlačítkoPřidat)
|
franta-hg@10
|
214 |
.addComponent(tlačítkoSmazat)
|
franta-hg@30
|
215 |
.addComponent(tlačítkoZnovuNačíst)
|
franta-hg@35
|
216 |
.addComponent(tlačítkoZamknout)
|
franta-hg@35
|
217 |
.addComponent(tlačítkoKopírovat))
|
franta-hg@10
|
218 |
.addContainerGap())
|
franta-hg@6
|
219 |
);
|
franta-hg@6
|
220 |
}// </editor-fold>//GEN-END:initComponents
|
franta-hg@6
|
221 |
|
franta-hg@10
|
222 |
private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
|
franta-hg@10
|
223 |
model.přidejAtribut(new Atribut());
|
franta-hg@10
|
224 |
}//GEN-LAST:event_tlačítkoPřidatActionPerformed
|
franta-hg@6
|
225 |
|
franta-hg@10
|
226 |
private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
|
franta-hg@10
|
227 |
try {
|
franta-hg@10
|
228 |
model.odeberAtribut(vybranýAtribut);
|
franta-hg@10
|
229 |
} catch (IOException e) {
|
franta-hg@29
|
230 |
zobrazChybovouHlášku(překlady
|
franta-hg@29
|
231 |
.getString("chyba.nepodařiloSeSmazat"), e);
|
franta-hg@10
|
232 |
}
|
franta-hg@10
|
233 |
}//GEN-LAST:event_tlačítkoSmazatActionPerformed
|
franta-hg@10
|
234 |
|
franta-hg@10
|
235 |
private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
|
franta-hg@10
|
236 |
try {
|
franta-hg@10
|
237 |
model.načtiAtributy();
|
franta-hg@10
|
238 |
} catch (IOException e) {
|
franta-hg@29
|
239 |
zobrazChybovouHlášku(překlady
|
franta-hg@29
|
240 |
.getString("chyba.nepodařiloSeNačíst"), e);
|
franta-hg@10
|
241 |
}
|
franta-hg@10
|
242 |
}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
|
franta-hg@30
|
243 |
|
franta-hg@30
|
244 |
private void tlačítkoZamknoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZamknoutActionPerformed
|
franta-hg@35
|
245 |
try {
|
franta-hg@30
|
246 |
model.nastavZámek(tlačítkoZamknout.isSelected());
|
franta-hg@30
|
247 |
} catch (Exception e) {
|
franta-hg@30
|
248 |
zobrazChybovouHlášku(překlady
|
franta-hg@30
|
249 |
.getString("chyba.nepodařiloSeNastavitZámek"), e);
|
franta-hg@30
|
250 |
}
|
franta-hg@30
|
251 |
}//GEN-LAST:event_tlačítkoZamknoutActionPerformed
|
franta-hg@30
|
252 |
|
franta-hg@35
|
253 |
private void tlačítkoKopírovatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoKopírovatActionPerformed
|
franta-hg@35
|
254 |
kopírujDoSchránky();
|
franta-hg@35
|
255 |
}//GEN-LAST:event_tlačítkoKopírovatActionPerformed
|
franta-hg@35
|
256 |
|
franta-hg@6
|
257 |
// Variables declaration - do not modify//GEN-BEGIN:variables
|
franta-hg@10
|
258 |
private javax.swing.JScrollPane posuvnýPanel;
|
franta-hg@35
|
259 |
private javax.swing.JButton tlačítkoKopírovat;
|
franta-hg@10
|
260 |
private javax.swing.JButton tlačítkoPřidat;
|
franta-hg@10
|
261 |
private javax.swing.JButton tlačítkoSmazat;
|
franta-hg@30
|
262 |
private javax.swing.JToggleButton tlačítkoZamknout;
|
franta-hg@10
|
263 |
private javax.swing.JButton tlačítkoZnovuNačíst;
|
franta-hg@6
|
264 |
// End of variables declaration//GEN-END:variables
|
franta-hg@6
|
265 |
}
|