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