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