java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Tue Dec 28 17:24:05 2010 +0100 (2010-12-28)
changeset 16 c39a2d3cb6d2
parent 15 8854e172a18f
child 19 c20edbed09c3
permissions -rw-r--r--
JTable: zjednodušení.
franta-hg@6
     1
package cz.frantovo.rozsireneAtributy.gui;
franta-hg@6
     2
franta-hg@10
     3
import cz.frantovo.rozsireneAtributy.Atribut;
franta-hg@10
     4
import java.io.IOException;
franta-hg@15
     5
import java.util.ResourceBundle;
franta-hg@10
     6
import java.util.logging.Level;
franta-hg@10
     7
import java.util.logging.Logger;
franta-hg@10
     8
import javax.swing.JOptionPane;
franta-hg@16
     9
import javax.swing.JTable;
franta-hg@16
    10
import javax.swing.ListSelectionModel;
franta-hg@10
    11
import javax.swing.event.ListSelectionEvent;
franta-hg@10
    12
import javax.swing.event.ListSelectionListener;
franta-hg@6
    13
franta-hg@6
    14
/**
franta-hg@6
    15
 *
franta-hg@6
    16
 * @author fiki
franta-hg@6
    17
 */
franta-hg@6
    18
public class Panel extends javax.swing.JPanel {
franta-hg@6
    19
franta-hg@10
    20
	private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
franta-hg@15
    21
	private static final ResourceBundle překlady = ResourceBundle.getBundle("cz.frantovo.rozsireneAtributy.Překlady");
franta-hg@10
    22
	private Model model;
franta-hg@10
    23
	private Atribut vybranýAtribut;
franta-hg@16
    24
	private JTable tabulka;
franta-hg@6
    25
franta-hg@10
    26
	public Panel(Model model) {
franta-hg@6
    27
		this.model = model;
franta-hg@10
    28
		initComponents();
franta-hg@16
    29
franta-hg@16
    30
		tabulka = new JTable(model);
franta-hg@16
    31
		tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
franta-hg@16
    32
		posuvnýPanel.setViewportView(tabulka);
franta-hg@16
    33
franta-hg@11
    34
		/** Výběr aktuálního atributu v tabulce */
franta-hg@10
    35
		tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
franta-hg@6
    36
franta-hg@10
    37
			public void valueChanged(ListSelectionEvent e) {
franta-hg@10
    38
				int řádek = tabulka.getSelectedRow();
franta-hg@10
    39
				if (řádek < 0) {
franta-hg@10
    40
					vybranýAtribut = null;
franta-hg@10
    41
					tlačítkoSmazat.setEnabled(false);
franta-hg@10
    42
				} else {
franta-hg@10
    43
					vybranýAtribut = getModel().getAtribut(řádek);
franta-hg@10
    44
					tlačítkoSmazat.setEnabled(true);
franta-hg@10
    45
				}
franta-hg@10
    46
			}
franta-hg@10
    47
		});
franta-hg@10
    48
	}
franta-hg@10
    49
franta-hg@10
    50
	private Model getModel() {
franta-hg@10
    51
		return model;
franta-hg@10
    52
	}
franta-hg@10
    53
franta-hg@10
    54
	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
franta-hg@15
    55
		JOptionPane.showMessageDialog(this, hláška, překlady.getString("chyba"), JOptionPane.ERROR_MESSAGE);
franta-hg@10
    56
		log.log(Level.WARNING, hláška, chyba);
franta-hg@10
    57
	}
franta-hg@10
    58
franta-hg@10
    59
	@SuppressWarnings("unchecked")
franta-hg@6
    60
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
franta-hg@6
    61
    private void initComponents() {
franta-hg@6
    62
franta-hg@10
    63
        posuvnýPanel = new javax.swing.JScrollPane();
franta-hg@10
    64
        tlačítkoPřidat = new javax.swing.JButton();
franta-hg@10
    65
        tlačítkoSmazat = new javax.swing.JButton();
franta-hg@10
    66
        tlačítkoZnovuNačíst = new javax.swing.JButton();
franta-hg@6
    67
franta-hg@11
    68
        tlačítkoPřidat.setMnemonic('p');
franta-hg@15
    69
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneAtributy/Překlady"); // NOI18N
franta-hg@15
    70
        tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
franta-hg@10
    71
        tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    72
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    73
                tlačítkoPřidatActionPerformed(evt);
franta-hg@10
    74
            }
franta-hg@10
    75
        });
franta-hg@10
    76
franta-hg@11
    77
        tlačítkoSmazat.setMnemonic('s');
franta-hg@15
    78
        tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
franta-hg@10
    79
        tlačítkoSmazat.setEnabled(false);
franta-hg@10
    80
        tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    81
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    82
                tlačítkoSmazatActionPerformed(evt);
franta-hg@10
    83
            }
franta-hg@10
    84
        });
franta-hg@10
    85
franta-hg@11
    86
        tlačítkoZnovuNačíst.setMnemonic('z');
franta-hg@15
    87
        tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
franta-hg@10
    88
        tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    89
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    90
                tlačítkoZnovuNačístActionPerformed(evt);
franta-hg@10
    91
            }
franta-hg@10
    92
        });
franta-hg@6
    93
franta-hg@6
    94
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
franta-hg@6
    95
        this.setLayout(layout);
franta-hg@6
    96
        layout.setHorizontalGroup(
franta-hg@6
    97
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
    98
            .addGroup(layout.createSequentialGroup()
franta-hg@10
    99
                .addContainerGap()
franta-hg@10
   100
                .addComponent(tlačítkoPřidat)
franta-hg@10
   101
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   102
                .addComponent(tlačítkoSmazat)
franta-hg@10
   103
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   104
                .addComponent(tlačítkoZnovuNačíst)
franta-hg@11
   105
                .addContainerGap(186, Short.MAX_VALUE))
franta-hg@11
   106
            .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
franta-hg@6
   107
        );
franta-hg@6
   108
        layout.setVerticalGroup(
franta-hg@6
   109
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
   110
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
franta-hg@10
   111
                .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
franta-hg@10
   112
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   113
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
franta-hg@10
   114
                    .addComponent(tlačítkoPřidat)
franta-hg@10
   115
                    .addComponent(tlačítkoSmazat)
franta-hg@10
   116
                    .addComponent(tlačítkoZnovuNačíst))
franta-hg@10
   117
                .addContainerGap())
franta-hg@6
   118
        );
franta-hg@6
   119
    }// </editor-fold>//GEN-END:initComponents
franta-hg@6
   120
franta-hg@10
   121
	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
franta-hg@10
   122
		model.přidejAtribut(new Atribut());
franta-hg@10
   123
	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
franta-hg@6
   124
franta-hg@10
   125
	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   126
		try {
franta-hg@10
   127
			model.odeberAtribut(vybranýAtribut);
franta-hg@10
   128
		} catch (IOException e) {
franta-hg@15
   129
			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeSmazat"), e);
franta-hg@10
   130
		}
franta-hg@10
   131
	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   132
franta-hg@10
   133
	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@10
   134
		try {
franta-hg@10
   135
			model.načtiAtributy();
franta-hg@10
   136
		} catch (IOException e) {
franta-hg@15
   137
			zobrazChybovouHlášku(překlady.getString("chyba.nepodařiloSeNačíst"), e);
franta-hg@10
   138
		}
franta-hg@10
   139
	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@6
   140
    // Variables declaration - do not modify//GEN-BEGIN:variables
franta-hg@10
   141
    private javax.swing.JScrollPane posuvnýPanel;
franta-hg@10
   142
    private javax.swing.JButton tlačítkoPřidat;
franta-hg@10
   143
    private javax.swing.JButton tlačítkoSmazat;
franta-hg@10
   144
    private javax.swing.JButton tlačítkoZnovuNačíst;
franta-hg@6
   145
    // End of variables declaration//GEN-END:variables
franta-hg@6
   146
}