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