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