java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 15 Dec 2023 02:29:57 +0100
branchv_0
changeset 32 ca064ecf3ca3
parent 31 1ab5ce94a146
child 33 4805f1eef561
permissions -rw-r--r--
nabízení hodnot atributů dle konfigurace
     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 java.io.IOException;
    21 import java.util.ResourceBundle;
    22 import java.util.logging.Level;
    23 import java.util.logging.Logger;
    24 import javax.swing.JOptionPane;
    25 import javax.swing.JTable;
    26 import javax.swing.ListSelectionModel;
    27 import javax.swing.event.ListSelectionEvent;
    28 import javax.swing.event.ListSelectionListener;
    29 
    30 /**
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class Panel extends javax.swing.JPanel {
    34 
    35 	public static final int SLOUPEC_NÁZVU = 0;
    36 	public static final int SLOUPEC_HODNOTY = 1;
    37 	private static final Logger log = Logger
    38 		.getLogger(Panel.class.getSimpleName());
    39 	private static final ResourceBundle překlady = ResourceBundle
    40 		.getBundle(Atribut.class.getPackageName() + ".Překlady");
    41 	private Model model;
    42 	private Atribut vybranýAtribut;
    43 	private JTable tabulka;
    44 
    45 	public Panel(Model model) {
    46 		this.model = model;
    47 		initComponents();
    48 
    49 		tlačítkoZamknout.setEnabled(model.isZámekPodporovaný());
    50 		tlačítkoZamknout.setToolTipText(model.isZámekPodporovaný()
    51 			? překlady.getString("zamknout.popis")
    52 			: překlady.getString("chyba.lzeZamknoutJenSoubor"));
    53 
    54 		tabulka = new JTable(model);
    55 		nastavEditor();
    56 		tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    57 		posuvnýPanel.setViewportView(tabulka);
    58 		
    59 		/** Výběr aktuálního atributu v tabulce */
    60 		tabulka.getSelectionModel().addListSelectionListener(
    61 			new ListSelectionListener() {
    62 
    63 			@Override
    64 			public void valueChanged(ListSelectionEvent e) {
    65 				int řádek = tabulka.getSelectedRow();
    66 				if (řádek < 0) {
    67 					vybranýAtribut = null;
    68 					tlačítkoSmazat.setEnabled(false);
    69 				} else {
    70 					vybranýAtribut = getModel().getAtribut(řádek);
    71 					tlačítkoSmazat.setEnabled(true);
    72 				}
    73 			}
    74 		});
    75 	}
    76 
    77 	private void nastavEditor() {
    78 		tabulka.getColumnModel().getColumn(SLOUPEC_NÁZVU)
    79 			.setCellEditor(new EditorNázvůAtributů(model.getKonfigurace()));
    80 		tabulka.getColumnModel().getColumn(SLOUPEC_HODNOTY)
    81 			.setCellEditor(new EditorHodnotAtributů(model.getKonfigurace()));
    82 	}
    83 
    84 	private Model getModel() {
    85 		return model;
    86 	}
    87 
    88 	public void setModel(Model model) {
    89 		this.model = model;
    90 		tabulka.setModel(model);
    91 		nastavEditor();
    92 	}
    93 
    94 	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
    95 		JOptionPane.showMessageDialog(this, hláška + "\n"
    96 			+ chyba.getLocalizedMessage(),
    97 			překlady.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE);
    98 		log.log(Level.WARNING, hláška, chyba);
    99 	}
   100 
   101 	@SuppressWarnings("unchecked")
   102     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   103     private void initComponents() {
   104 
   105         posuvnýPanel = new javax.swing.JScrollPane();
   106         tlačítkoPřidat = new javax.swing.JButton();
   107         tlačítkoSmazat = new javax.swing.JButton();
   108         tlačítkoZnovuNačíst = new javax.swing.JButton();
   109         tlačítkoZamknout = new javax.swing.JToggleButton();
   110 
   111         tlačítkoPřidat.setMnemonic('p');
   112         java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneatributy/Překlady"); // NOI18N
   113         tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
   114         tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
   115             public void actionPerformed(java.awt.event.ActionEvent evt) {
   116                 tlačítkoPřidatActionPerformed(evt);
   117             }
   118         });
   119 
   120         tlačítkoSmazat.setMnemonic('s');
   121         tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
   122         tlačítkoSmazat.setEnabled(false);
   123         tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
   124             public void actionPerformed(java.awt.event.ActionEvent evt) {
   125                 tlačítkoSmazatActionPerformed(evt);
   126             }
   127         });
   128 
   129         tlačítkoZnovuNačíst.setMnemonic('z');
   130         tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
   131         tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
   132             public void actionPerformed(java.awt.event.ActionEvent evt) {
   133                 tlačítkoZnovuNačístActionPerformed(evt);
   134             }
   135         });
   136 
   137         tlačítkoZamknout.setText(bundle.getString("zamknout")); // NOI18N
   138         tlačítkoZamknout.addActionListener(new java.awt.event.ActionListener() {
   139             public void actionPerformed(java.awt.event.ActionEvent evt) {
   140                 tlačítkoZamknoutActionPerformed(evt);
   141             }
   142         });
   143 
   144         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   145         this.setLayout(layout);
   146         layout.setHorizontalGroup(
   147             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   148             .addGroup(layout.createSequentialGroup()
   149                 .addContainerGap()
   150                 .addComponent(tlačítkoPřidat)
   151                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   152                 .addComponent(tlačítkoSmazat)
   153                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   154                 .addComponent(tlačítkoZnovuNačíst)
   155                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   156                 .addComponent(tlačítkoZamknout)
   157                 .addContainerGap(122, Short.MAX_VALUE))
   158             .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
   159         );
   160         layout.setVerticalGroup(
   161             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   162             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   163                 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
   164                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   165                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   166                     .addComponent(tlačítkoPřidat)
   167                     .addComponent(tlačítkoSmazat)
   168                     .addComponent(tlačítkoZnovuNačíst)
   169                     .addComponent(tlačítkoZamknout))
   170                 .addContainerGap())
   171         );
   172     }// </editor-fold>//GEN-END:initComponents
   173 
   174 	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
   175 		model.přidejAtribut(new Atribut());
   176 	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
   177 
   178 	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
   179 		try {
   180 			model.odeberAtribut(vybranýAtribut);
   181 		} catch (IOException e) {
   182 			zobrazChybovouHlášku(překlady
   183 				.getString("chyba.nepodařiloSeSmazat"), e);
   184 		}
   185 	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
   186 
   187 	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
   188 		try {
   189 			model.načtiAtributy();
   190 		} catch (IOException e) {
   191 			zobrazChybovouHlášku(překlady
   192 				.getString("chyba.nepodařiloSeNačíst"), e);
   193 		}
   194 	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
   195 
   196     private void tlačítkoZamknoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZamknoutActionPerformed
   197         try {
   198 			model.nastavZámek(tlačítkoZamknout.isSelected());
   199 		} catch (Exception e) {
   200 			zobrazChybovouHlášku(překlady
   201 				.getString("chyba.nepodařiloSeNastavitZámek"), e);
   202 		}
   203     }//GEN-LAST:event_tlačítkoZamknoutActionPerformed
   204 
   205     // Variables declaration - do not modify//GEN-BEGIN:variables
   206     private javax.swing.JScrollPane posuvnýPanel;
   207     private javax.swing.JButton tlačítkoPřidat;
   208     private javax.swing.JButton tlačítkoSmazat;
   209     private javax.swing.JToggleButton tlačítkoZamknout;
   210     private javax.swing.JButton tlačítkoZnovuNačíst;
   211     // End of variables declaration//GEN-END:variables
   212 }