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