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