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