java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Sun Oct 07 15:11:42 2012 +0200 (2012-10-07)
changeset 22 bf06eb899671
parent 19 c20edbed09c3
child 23 69f49b79342e
permissions -rw-r--r--
nabídka standardizovaných atributů
http://www.freedesktop.org/wiki/CommonExtendedAttributes
     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.getColumnModel().getColumn(0).setCellEditor(new EditorNázvůAtributů());
    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 Model getModel() {
    70 		return model;
    71 	}
    72 
    73 	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
    74 		JOptionPane.showMessageDialog(this, hláška, překlady.getString("chyba"), JOptionPane.ERROR_MESSAGE);
    75 		log.log(Level.WARNING, hláška, chyba);
    76 	}
    77 
    78 	@SuppressWarnings("unchecked")
    79     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    80     private void initComponents() {
    81 
    82         posuvnýPanel = new javax.swing.JScrollPane();
    83         tlačítkoPřidat = new javax.swing.JButton();
    84         tlačítkoSmazat = new javax.swing.JButton();
    85         tlačítkoZnovuNačíst = new javax.swing.JButton();
    86 
    87         tlačítkoPřidat.setMnemonic('p');
    88         java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneAtributy/Překlady"); // NOI18N
    89         tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
    90         tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
    91             public void actionPerformed(java.awt.event.ActionEvent evt) {
    92                 tlačítkoPřidatActionPerformed(evt);
    93             }
    94         });
    95 
    96         tlačítkoSmazat.setMnemonic('s');
    97         tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
    98         tlačítkoSmazat.setEnabled(false);
    99         tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
   100             public void actionPerformed(java.awt.event.ActionEvent evt) {
   101                 tlačítkoSmazatActionPerformed(evt);
   102             }
   103         });
   104 
   105         tlačítkoZnovuNačíst.setMnemonic('z');
   106         tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
   107         tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
   108             public void actionPerformed(java.awt.event.ActionEvent evt) {
   109                 tlačítkoZnovuNačístActionPerformed(evt);
   110             }
   111         });
   112 
   113         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   114         this.setLayout(layout);
   115         layout.setHorizontalGroup(
   116             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   117             .addGroup(layout.createSequentialGroup()
   118                 .addContainerGap()
   119                 .addComponent(tlačítkoPřidat)
   120                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   121                 .addComponent(tlačítkoSmazat)
   122                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   123                 .addComponent(tlačítkoZnovuNačíst)
   124                 .addContainerGap(186, Short.MAX_VALUE))
   125             .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
   126         );
   127         layout.setVerticalGroup(
   128             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   129             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   130                 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
   131                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   132                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   133                     .addComponent(tlačítkoPřidat)
   134                     .addComponent(tlačítkoSmazat)
   135                     .addComponent(tlačítkoZnovuNačíst))
   136                 .addContainerGap())
   137         );
   138     }// </editor-fold>//GEN-END:initComponents
   139 
   140 	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
   141 		model.přidejAtribut(new Atribut());
   142 	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
   143 
   144 	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
   145 		try {
   146 			model.odeberAtribut(vybranýAtribut);
   147 		} catch (IOException e) {
   148 			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeSmazat"), e);
   149 		}
   150 	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
   151 
   152 	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
   153 		try {
   154 			model.načtiAtributy();
   155 		} catch (IOException e) {
   156 			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeNačíst"), e);
   157 		}
   158 	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
   159     // Variables declaration - do not modify//GEN-BEGIN:variables
   160     private javax.swing.JScrollPane posuvnýPanel;
   161     private javax.swing.JButton tlačítkoPřidat;
   162     private javax.swing.JButton tlačítkoSmazat;
   163     private javax.swing.JButton tlačítkoZnovuNačíst;
   164     // End of variables declaration//GEN-END:variables
   165 }