franta-hg@19: /** franta-hg@19: * Rozšířené atributy – program na správu rozšířených atributů souborů franta-hg@19: * Copyright © 2012 František Kučera (frantovo.cz) franta-hg@22: * franta-hg@19: * This program is free software: you can redistribute it and/or modify franta-hg@19: * it under the terms of the GNU General Public License as published by franta-hg@27: * the Free Software Foundation, either version 3 of the License. franta-hg@22: * franta-hg@19: * This program is distributed in the hope that it will be useful, franta-hg@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@22: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@19: * GNU General Public License for more details. franta-hg@22: * franta-hg@19: * You should have received a copy of the GNU General Public License franta-hg@22: * along with this program. If not, see . franta-hg@19: */ franta-hg@28: package cz.frantovo.rozsireneatributy.gui; franta-hg@6: franta-hg@28: import cz.frantovo.rozsireneatributy.Atribut; franta-hg@35: import cz.frantovo.rozsireneatributy.CSV; franta-hg@42: import cz.frantovo.rozsireneatributy.Konfigurace; franta-hg@35: import java.awt.Toolkit; franta-hg@35: import java.awt.datatransfer.StringSelection; franta-hg@41: import java.awt.event.KeyEvent; franta-hg@10: import java.io.IOException; franta-hg@35: import java.io.StringWriter; franta-hg@15: import java.util.ResourceBundle; franta-hg@10: import java.util.logging.Level; franta-hg@10: import java.util.logging.Logger; franta-hg@10: import javax.swing.JOptionPane; franta-hg@16: import javax.swing.JTable; franta-hg@41: import javax.swing.KeyStroke; franta-hg@16: import javax.swing.ListSelectionModel; franta-hg@10: import javax.swing.event.ListSelectionEvent; franta-hg@10: import javax.swing.event.ListSelectionListener; franta-hg@42: import javax.swing.table.TableCellEditor; franta-hg@6: franta-hg@6: /** franta-hg@30: * @author Ing. František Kučera (frantovo.cz) franta-hg@6: */ franta-hg@6: public class Panel extends javax.swing.JPanel { franta-hg@6: franta-hg@32: public static final int SLOUPEC_NÁZVU = 0; franta-hg@32: public static final int SLOUPEC_HODNOTY = 1; franta-hg@29: private static final Logger log = Logger franta-hg@29: .getLogger(Panel.class.getSimpleName()); franta-hg@29: private static final ResourceBundle překlady = ResourceBundle franta-hg@29: .getBundle(Atribut.class.getPackageName() + ".Překlady"); franta-hg@10: private Model model; franta-hg@10: private Atribut vybranýAtribut; franta-hg@16: private JTable tabulka; franta-hg@6: franta-hg@10: public Panel(Model model) { franta-hg@6: this.model = model; franta-hg@10: initComponents(); franta-hg@16: franta-hg@30: tlačítkoZamknout.setEnabled(model.isZámekPodporovaný()); franta-hg@30: tlačítkoZamknout.setToolTipText(model.isZámekPodporovaný() franta-hg@30: ? překlady.getString("zamknout.popis") franta-hg@30: : překlady.getString("chyba.lzeZamknoutJenSoubor")); franta-hg@30: franta-hg@16: tabulka = new JTable(model); franta-hg@23: nastavEditor(); franta-hg@16: tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); franta-hg@16: posuvnýPanel.setViewportView(tabulka); franta-hg@35: franta-hg@33: tabulka.setRowHeight((int) (tabulka.getRowHeight() * 1.3)); franta-hg@33: franta-hg@41: // Výběr aktuálního atributu v tabulce: franta-hg@29: tabulka.getSelectionModel().addListSelectionListener( franta-hg@29: new ListSelectionListener() { franta-hg@6: franta-hg@22: @Override franta-hg@10: public void valueChanged(ListSelectionEvent e) { franta-hg@10: int řádek = tabulka.getSelectedRow(); franta-hg@42: if (řádek < 0) vybranýAtribut = null; franta-hg@42: else vybranýAtribut = getModel().getAtribut(řádek); franta-hg@42: aktualizujPovolenéÚpravy(); franta-hg@10: } franta-hg@10: }); franta-hg@41: franta-hg@41: // Buňky tabulky editovatelné po stisku mezerníku: franta-hg@41: tabulka.getInputMap() franta-hg@41: .put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing"); franta-hg@42: franta-hg@42: aktualizujPovolenéÚpravy(); franta-hg@42: franta-hg@42: // TODO: Provádět IO operace v jiném vlákně franta-hg@42: // SwingWorker.doInBackground() + SwingUtilities.invokeLater() franta-hg@42: } franta-hg@42: franta-hg@42: private void aktualizujPovolenéÚpravy() { franta-hg@42: Konfigurace k = model.getKonfigurace(); franta-hg@42: switch (k.getRežimZamykání()) { franta-hg@42: case VYPNUTÉ: franta-hg@42: tlačítkoZamknout.setVisible(false); franta-hg@42: setPovolenéÚpravy(true); franta-hg@42: break; franta-hg@42: case VOLITELNÉ: franta-hg@42: setPovolenéÚpravy(true); franta-hg@42: break; franta-hg@42: case POVINNÉ: franta-hg@42: setPovolenéÚpravy(tlačítkoZamknout.isSelected()); franta-hg@42: break; franta-hg@42: default: franta-hg@42: break; franta-hg@42: } franta-hg@42: } franta-hg@42: franta-hg@42: private void setPovolenéÚpravy(boolean povolené) { franta-hg@42: if (!povolené) { franta-hg@42: TableCellEditor ce = tabulka.getCellEditor(); franta-hg@42: if (ce != null) ce.cancelCellEditing(); franta-hg@42: tabulka.clearSelection(); franta-hg@42: } franta-hg@42: franta-hg@42: tabulka.setEnabled(povolené); franta-hg@42: tlačítkoPřidat.setEnabled(povolené); franta-hg@42: tlačítkoSmazat.setEnabled(povolené && tabulka.getSelectedRow() > -1); franta-hg@10: } franta-hg@10: franta-hg@23: private void nastavEditor() { franta-hg@29: tabulka.getColumnModel().getColumn(SLOUPEC_NÁZVU) franta-hg@31: .setCellEditor(new EditorNázvůAtributů(model.getKonfigurace())); franta-hg@32: tabulka.getColumnModel().getColumn(SLOUPEC_HODNOTY) franta-hg@32: .setCellEditor(new EditorHodnotAtributů(model.getKonfigurace())); franta-hg@23: } franta-hg@23: franta-hg@10: private Model getModel() { franta-hg@10: return model; franta-hg@10: } franta-hg@10: franta-hg@23: public void setModel(Model model) { franta-hg@23: this.model = model; franta-hg@23: tabulka.setModel(model); franta-hg@23: nastavEditor(); franta-hg@23: } franta-hg@23: franta-hg@10: private void zobrazChybovouHlášku(String hláška, Throwable chyba) { franta-hg@30: JOptionPane.showMessageDialog(this, hláška + "\n" franta-hg@30: + chyba.getLocalizedMessage(), franta-hg@30: překlady.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE); franta-hg@10: log.log(Level.WARNING, hláška, chyba); franta-hg@10: } franta-hg@42: franta-hg@35: private void kopírujDoSchránky() { franta-hg@35: try { franta-hg@35: StringWriter výstup = new StringWriter(); franta-hg@35: CSV csv = new CSV(výstup); franta-hg@35: franta-hg@35: csv.hodnota(překlady.getString("tabulka.název").toLowerCase()); franta-hg@35: csv.hodnota(překlady.getString("tabulka.hodnota").toLowerCase()); franta-hg@35: csv.konecŘádku(); franta-hg@35: franta-hg@35: for (int i = 0; i < model.getRowCount(); i++) { franta-hg@35: csv.hodnota(String.valueOf(model.getValueAt(i, 0))); franta-hg@35: csv.hodnota(String.valueOf(model.getValueAt(i, 1))); franta-hg@35: csv.konecŘádku(); franta-hg@35: } franta-hg@35: franta-hg@35: Toolkit.getDefaultToolkit().getSystemClipboard().setContents( franta-hg@35: new StringSelection(výstup.toString()), franta-hg@35: null franta-hg@35: ); franta-hg@35: } catch (Exception e) { franta-hg@35: zobrazChybovouHlášku(překlady franta-hg@35: .getString("chyba.nepodařiloSeZkopírovat"), e); franta-hg@35: } franta-hg@35: } franta-hg@10: franta-hg@10: @SuppressWarnings("unchecked") franta-hg@6: // //GEN-BEGIN:initComponents franta-hg@6: private void initComponents() { franta-hg@6: franta-hg@10: posuvnýPanel = new javax.swing.JScrollPane(); franta-hg@10: tlačítkoPřidat = new javax.swing.JButton(); franta-hg@10: tlačítkoSmazat = new javax.swing.JButton(); franta-hg@10: tlačítkoZnovuNačíst = new javax.swing.JButton(); franta-hg@30: tlačítkoZamknout = new javax.swing.JToggleButton(); franta-hg@35: tlačítkoKopírovat = new javax.swing.JButton(); franta-hg@6: franta-hg@11: tlačítkoPřidat.setMnemonic('p'); franta-hg@28: java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneatributy/Překlady"); // NOI18N franta-hg@15: tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N franta-hg@10: tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() { franta-hg@10: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@10: tlačítkoPřidatActionPerformed(evt); franta-hg@10: } franta-hg@10: }); franta-hg@10: franta-hg@11: tlačítkoSmazat.setMnemonic('s'); franta-hg@15: tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N franta-hg@10: tlačítkoSmazat.setEnabled(false); franta-hg@10: tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() { franta-hg@10: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@10: tlačítkoSmazatActionPerformed(evt); franta-hg@10: } franta-hg@10: }); franta-hg@10: franta-hg@11: tlačítkoZnovuNačíst.setMnemonic('z'); franta-hg@15: tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N franta-hg@10: tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() { franta-hg@10: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@10: tlačítkoZnovuNačístActionPerformed(evt); franta-hg@10: } franta-hg@10: }); franta-hg@6: franta-hg@30: tlačítkoZamknout.setText(bundle.getString("zamknout")); // NOI18N franta-hg@30: tlačítkoZamknout.addActionListener(new java.awt.event.ActionListener() { franta-hg@30: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@30: tlačítkoZamknoutActionPerformed(evt); franta-hg@30: } franta-hg@30: }); franta-hg@30: franta-hg@35: tlačítkoKopírovat.setText(bundle.getString("schránka.kopírovat")); // NOI18N franta-hg@35: tlačítkoKopírovat.addActionListener(new java.awt.event.ActionListener() { franta-hg@35: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@35: tlačítkoKopírovatActionPerformed(evt); franta-hg@35: } franta-hg@35: }); franta-hg@35: franta-hg@6: javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); franta-hg@6: this.setLayout(layout); franta-hg@6: layout.setHorizontalGroup( franta-hg@6: layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@10: .addGroup(layout.createSequentialGroup() franta-hg@10: .addContainerGap() franta-hg@10: .addComponent(tlačítkoPřidat) franta-hg@10: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@10: .addComponent(tlačítkoSmazat) franta-hg@10: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@10: .addComponent(tlačítkoZnovuNačíst) franta-hg@30: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@30: .addComponent(tlačítkoZamknout) franta-hg@35: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@35: .addComponent(tlačítkoKopírovat) franta-hg@35: .addContainerGap(25, Short.MAX_VALUE)) franta-hg@11: .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE) franta-hg@6: ); franta-hg@6: layout.setVerticalGroup( franta-hg@6: layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@10: .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() franta-hg@10: .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE) franta-hg@10: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@10: .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) franta-hg@10: .addComponent(tlačítkoPřidat) franta-hg@10: .addComponent(tlačítkoSmazat) franta-hg@30: .addComponent(tlačítkoZnovuNačíst) franta-hg@35: .addComponent(tlačítkoZamknout) franta-hg@35: .addComponent(tlačítkoKopírovat)) franta-hg@10: .addContainerGap()) franta-hg@6: ); franta-hg@6: }// //GEN-END:initComponents franta-hg@6: franta-hg@10: private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed franta-hg@10: model.přidejAtribut(new Atribut()); franta-hg@10: }//GEN-LAST:event_tlačítkoPřidatActionPerformed franta-hg@6: franta-hg@10: private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed franta-hg@10: try { franta-hg@10: model.odeberAtribut(vybranýAtribut); franta-hg@10: } catch (IOException e) { franta-hg@29: zobrazChybovouHlášku(překlady franta-hg@29: .getString("chyba.nepodařiloSeSmazat"), e); franta-hg@10: } franta-hg@10: }//GEN-LAST:event_tlačítkoSmazatActionPerformed franta-hg@10: franta-hg@10: private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed franta-hg@10: try { franta-hg@10: model.načtiAtributy(); franta-hg@10: } catch (IOException e) { franta-hg@29: zobrazChybovouHlášku(překlady franta-hg@29: .getString("chyba.nepodařiloSeNačíst"), e); franta-hg@10: } franta-hg@10: }//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed franta-hg@30: franta-hg@30: private void tlačítkoZamknoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZamknoutActionPerformed franta-hg@35: try { franta-hg@42: aktualizujPovolenéÚpravy(); franta-hg@30: model.nastavZámek(tlačítkoZamknout.isSelected()); franta-hg@30: } catch (Exception e) { franta-hg@30: zobrazChybovouHlášku(překlady franta-hg@30: .getString("chyba.nepodařiloSeNastavitZámek"), e); franta-hg@30: } franta-hg@30: }//GEN-LAST:event_tlačítkoZamknoutActionPerformed franta-hg@30: franta-hg@35: private void tlačítkoKopírovatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoKopírovatActionPerformed franta-hg@35: kopírujDoSchránky(); franta-hg@35: }//GEN-LAST:event_tlačítkoKopírovatActionPerformed franta-hg@35: franta-hg@6: // Variables declaration - do not modify//GEN-BEGIN:variables franta-hg@10: private javax.swing.JScrollPane posuvnýPanel; franta-hg@35: private javax.swing.JButton tlačítkoKopírovat; franta-hg@10: private javax.swing.JButton tlačítkoPřidat; franta-hg@10: private javax.swing.JButton tlačítkoSmazat; franta-hg@30: private javax.swing.JToggleButton tlačítkoZamknout; franta-hg@10: private javax.swing.JButton tlačítkoZnovuNačíst; franta-hg@6: // End of variables declaration//GEN-END:variables franta-hg@6: }