java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Sat Aug 18 13:27:00 2012 +0200 (2012-08-18)
changeset 19 c20edbed09c3
parent 16 c39a2d3cb6d2
child 22 bf06eb899671
permissions -rw-r--r--
informace o licenci uvnitř .java souborů
     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, or
     8  * (at your option) any later version.
     9  * 
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  * 
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.rozsireneAtributy.gui;
    19 
    20 import cz.frantovo.rozsireneAtributy.Atribut;
    21 import java.io.IOException;
    22 import java.util.ResourceBundle;
    23 import java.util.logging.Level;
    24 import java.util.logging.Logger;
    25 import javax.swing.JOptionPane;
    26 import javax.swing.JTable;
    27 import javax.swing.ListSelectionModel;
    28 import javax.swing.event.ListSelectionEvent;
    29 import javax.swing.event.ListSelectionListener;
    30 
    31 /**
    32  *
    33  * @author fiki
    34  */
    35 public class Panel extends javax.swing.JPanel {
    36 
    37 	private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
    38 	private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.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 		tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    49 		posuvnýPanel.setViewportView(tabulka);
    50 
    51 		/** Výběr aktuálního atributu v tabulce */
    52 		tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    53 
    54 			public void valueChanged(ListSelectionEvent e) {
    55 				int řádek = tabulka.getSelectedRow();
    56 				if (řádek < 0) {
    57 					vybranýAtribut = null;
    58 					tlačítkoSmazat.setEnabled(false);
    59 				} else {
    60 					vybranýAtribut = getModel().getAtribut(řádek);
    61 					tlačítkoSmazat.setEnabled(true);
    62 				}
    63 			}
    64 		});
    65 	}
    66 
    67 	private Model getModel() {
    68 		return model;
    69 	}
    70 
    71 	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
    72 		JOptionPane.showMessageDialog(this, hláška, překlady.getString("chyba"), JOptionPane.ERROR_MESSAGE);
    73 		log.log(Level.WARNING, hláška, chyba);
    74 	}
    75 
    76 	@SuppressWarnings("unchecked")
    77     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    78     private void initComponents() {
    79 
    80         posuvnýPanel = new javax.swing.JScrollPane();
    81         tlačítkoPřidat = new javax.swing.JButton();
    82         tlačítkoSmazat = new javax.swing.JButton();
    83         tlačítkoZnovuNačíst = new javax.swing.JButton();
    84 
    85         tlačítkoPřidat.setMnemonic('p');
    86         java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneAtributy/Překlady"); // NOI18N
    87         tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
    88         tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
    89             public void actionPerformed(java.awt.event.ActionEvent evt) {
    90                 tlačítkoPřidatActionPerformed(evt);
    91             }
    92         });
    93 
    94         tlačítkoSmazat.setMnemonic('s');
    95         tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
    96         tlačítkoSmazat.setEnabled(false);
    97         tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
    98             public void actionPerformed(java.awt.event.ActionEvent evt) {
    99                 tlačítkoSmazatActionPerformed(evt);
   100             }
   101         });
   102 
   103         tlačítkoZnovuNačíst.setMnemonic('z');
   104         tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
   105         tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
   106             public void actionPerformed(java.awt.event.ActionEvent evt) {
   107                 tlačítkoZnovuNačístActionPerformed(evt);
   108             }
   109         });
   110 
   111         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   112         this.setLayout(layout);
   113         layout.setHorizontalGroup(
   114             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   115             .addGroup(layout.createSequentialGroup()
   116                 .addContainerGap()
   117                 .addComponent(tlačítkoPřidat)
   118                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   119                 .addComponent(tlačítkoSmazat)
   120                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   121                 .addComponent(tlačítkoZnovuNačíst)
   122                 .addContainerGap(186, Short.MAX_VALUE))
   123             .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
   124         );
   125         layout.setVerticalGroup(
   126             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   127             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   128                 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
   129                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   130                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   131                     .addComponent(tlačítkoPřidat)
   132                     .addComponent(tlačítkoSmazat)
   133                     .addComponent(tlačítkoZnovuNačíst))
   134                 .addContainerGap())
   135         );
   136     }// </editor-fold>//GEN-END:initComponents
   137 
   138 	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
   139 		model.přidejAtribut(new Atribut());
   140 	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
   141 
   142 	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
   143 		try {
   144 			model.odeberAtribut(vybranýAtribut);
   145 		} catch (IOException e) {
   146 			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeSmazat"), e);
   147 		}
   148 	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
   149 
   150 	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
   151 		try {
   152 			model.načtiAtributy();
   153 		} catch (IOException e) {
   154 			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeNačíst"), e);
   155 		}
   156 	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
   157     // Variables declaration - do not modify//GEN-BEGIN:variables
   158     private javax.swing.JScrollPane posuvnýPanel;
   159     private javax.swing.JButton tlačítkoPřidat;
   160     private javax.swing.JButton tlačítkoSmazat;
   161     private javax.swing.JButton tlačítkoZnovuNačíst;
   162     // End of variables declaration//GEN-END:variables
   163 }