java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 15 Dec 2023 02:30:46 +0100
branchv_0
changeset 33 4805f1eef561
parent 32 ca064ecf3ca3
child 35 72ea1c6d836c
permissions -rw-r--r--
vyšší řádky tabulky
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@33
    20
import java.awt.Dimension;
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@30
    32
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@6
    33
 */
franta-hg@6
    34
public class Panel extends javax.swing.JPanel {
franta-hg@6
    35
franta-hg@32
    36
	public static final int SLOUPEC_NÁZVU = 0;
franta-hg@32
    37
	public static final int SLOUPEC_HODNOTY = 1;
franta-hg@29
    38
	private static final Logger log = Logger
franta-hg@29
    39
		.getLogger(Panel.class.getSimpleName());
franta-hg@29
    40
	private static final ResourceBundle překlady = ResourceBundle
franta-hg@29
    41
		.getBundle(Atribut.class.getPackageName() + ".Překlady");
franta-hg@10
    42
	private Model model;
franta-hg@10
    43
	private Atribut vybranýAtribut;
franta-hg@16
    44
	private JTable tabulka;
franta-hg@6
    45
franta-hg@10
    46
	public Panel(Model model) {
franta-hg@6
    47
		this.model = model;
franta-hg@10
    48
		initComponents();
franta-hg@16
    49
franta-hg@30
    50
		tlačítkoZamknout.setEnabled(model.isZámekPodporovaný());
franta-hg@30
    51
		tlačítkoZamknout.setToolTipText(model.isZámekPodporovaný()
franta-hg@30
    52
			? překlady.getString("zamknout.popis")
franta-hg@30
    53
			: překlady.getString("chyba.lzeZamknoutJenSoubor"));
franta-hg@30
    54
franta-hg@16
    55
		tabulka = new JTable(model);
franta-hg@23
    56
		nastavEditor();
franta-hg@16
    57
		tabulka.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
franta-hg@16
    58
		posuvnýPanel.setViewportView(tabulka);
franta-hg@32
    59
		
franta-hg@33
    60
		tabulka.setRowHeight((int) (tabulka.getRowHeight() * 1.3));
franta-hg@33
    61
franta-hg@11
    62
		/** Výběr aktuálního atributu v tabulce */
franta-hg@29
    63
		tabulka.getSelectionModel().addListSelectionListener(
franta-hg@29
    64
			new ListSelectionListener() {
franta-hg@6
    65
franta-hg@22
    66
			@Override
franta-hg@10
    67
			public void valueChanged(ListSelectionEvent e) {
franta-hg@10
    68
				int řádek = tabulka.getSelectedRow();
franta-hg@10
    69
				if (řádek < 0) {
franta-hg@10
    70
					vybranýAtribut = null;
franta-hg@10
    71
					tlačítkoSmazat.setEnabled(false);
franta-hg@10
    72
				} else {
franta-hg@10
    73
					vybranýAtribut = getModel().getAtribut(řádek);
franta-hg@10
    74
					tlačítkoSmazat.setEnabled(true);
franta-hg@10
    75
				}
franta-hg@10
    76
			}
franta-hg@10
    77
		});
franta-hg@10
    78
	}
franta-hg@10
    79
franta-hg@23
    80
	private void nastavEditor() {
franta-hg@29
    81
		tabulka.getColumnModel().getColumn(SLOUPEC_NÁZVU)
franta-hg@31
    82
			.setCellEditor(new EditorNázvůAtributů(model.getKonfigurace()));
franta-hg@32
    83
		tabulka.getColumnModel().getColumn(SLOUPEC_HODNOTY)
franta-hg@32
    84
			.setCellEditor(new EditorHodnotAtributů(model.getKonfigurace()));
franta-hg@23
    85
	}
franta-hg@23
    86
franta-hg@10
    87
	private Model getModel() {
franta-hg@10
    88
		return model;
franta-hg@10
    89
	}
franta-hg@10
    90
franta-hg@23
    91
	public void setModel(Model model) {
franta-hg@23
    92
		this.model = model;
franta-hg@23
    93
		tabulka.setModel(model);
franta-hg@23
    94
		nastavEditor();
franta-hg@23
    95
	}
franta-hg@23
    96
franta-hg@10
    97
	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
franta-hg@30
    98
		JOptionPane.showMessageDialog(this, hláška + "\n"
franta-hg@30
    99
			+ chyba.getLocalizedMessage(),
franta-hg@30
   100
			překlady.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE);
franta-hg@10
   101
		log.log(Level.WARNING, hláška, chyba);
franta-hg@10
   102
	}
franta-hg@10
   103
franta-hg@10
   104
	@SuppressWarnings("unchecked")
franta-hg@6
   105
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
franta-hg@6
   106
    private void initComponents() {
franta-hg@6
   107
franta-hg@10
   108
        posuvnýPanel = new javax.swing.JScrollPane();
franta-hg@10
   109
        tlačítkoPřidat = new javax.swing.JButton();
franta-hg@10
   110
        tlačítkoSmazat = new javax.swing.JButton();
franta-hg@10
   111
        tlačítkoZnovuNačíst = new javax.swing.JButton();
franta-hg@30
   112
        tlačítkoZamknout = new javax.swing.JToggleButton();
franta-hg@6
   113
franta-hg@11
   114
        tlačítkoPřidat.setMnemonic('p');
franta-hg@28
   115
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/frantovo/rozsireneatributy/Překlady"); // NOI18N
franta-hg@15
   116
        tlačítkoPřidat.setText(bundle.getString("přidatAtribut")); // NOI18N
franta-hg@10
   117
        tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
   118
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
   119
                tlačítkoPřidatActionPerformed(evt);
franta-hg@10
   120
            }
franta-hg@10
   121
        });
franta-hg@10
   122
franta-hg@11
   123
        tlačítkoSmazat.setMnemonic('s');
franta-hg@15
   124
        tlačítkoSmazat.setText(bundle.getString("smazatAtribut")); // NOI18N
franta-hg@10
   125
        tlačítkoSmazat.setEnabled(false);
franta-hg@10
   126
        tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
   127
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
   128
                tlačítkoSmazatActionPerformed(evt);
franta-hg@10
   129
            }
franta-hg@10
   130
        });
franta-hg@10
   131
franta-hg@11
   132
        tlačítkoZnovuNačíst.setMnemonic('z');
franta-hg@15
   133
        tlačítkoZnovuNačíst.setText(bundle.getString("znovuNačíst")); // NOI18N
franta-hg@10
   134
        tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
   135
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
   136
                tlačítkoZnovuNačístActionPerformed(evt);
franta-hg@10
   137
            }
franta-hg@10
   138
        });
franta-hg@6
   139
franta-hg@30
   140
        tlačítkoZamknout.setText(bundle.getString("zamknout")); // NOI18N
franta-hg@30
   141
        tlačítkoZamknout.addActionListener(new java.awt.event.ActionListener() {
franta-hg@30
   142
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@30
   143
                tlačítkoZamknoutActionPerformed(evt);
franta-hg@30
   144
            }
franta-hg@30
   145
        });
franta-hg@30
   146
franta-hg@6
   147
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
franta-hg@6
   148
        this.setLayout(layout);
franta-hg@6
   149
        layout.setHorizontalGroup(
franta-hg@6
   150
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
   151
            .addGroup(layout.createSequentialGroup()
franta-hg@10
   152
                .addContainerGap()
franta-hg@10
   153
                .addComponent(tlačítkoPřidat)
franta-hg@10
   154
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   155
                .addComponent(tlačítkoSmazat)
franta-hg@10
   156
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   157
                .addComponent(tlačítkoZnovuNačíst)
franta-hg@30
   158
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@30
   159
                .addComponent(tlačítkoZamknout)
franta-hg@30
   160
                .addContainerGap(122, Short.MAX_VALUE))
franta-hg@11
   161
            .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
franta-hg@6
   162
        );
franta-hg@6
   163
        layout.setVerticalGroup(
franta-hg@6
   164
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
   165
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
franta-hg@10
   166
                .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
franta-hg@10
   167
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   168
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
franta-hg@10
   169
                    .addComponent(tlačítkoPřidat)
franta-hg@10
   170
                    .addComponent(tlačítkoSmazat)
franta-hg@30
   171
                    .addComponent(tlačítkoZnovuNačíst)
franta-hg@30
   172
                    .addComponent(tlačítkoZamknout))
franta-hg@10
   173
                .addContainerGap())
franta-hg@6
   174
        );
franta-hg@6
   175
    }// </editor-fold>//GEN-END:initComponents
franta-hg@6
   176
franta-hg@10
   177
	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
franta-hg@10
   178
		model.přidejAtribut(new Atribut());
franta-hg@10
   179
	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
franta-hg@6
   180
franta-hg@10
   181
	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   182
		try {
franta-hg@10
   183
			model.odeberAtribut(vybranýAtribut);
franta-hg@10
   184
		} catch (IOException e) {
franta-hg@29
   185
			zobrazChybovouHlášku(překlady
franta-hg@29
   186
				.getString("chyba.nepodařiloSeSmazat"), e);
franta-hg@10
   187
		}
franta-hg@10
   188
	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   189
franta-hg@10
   190
	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@10
   191
		try {
franta-hg@10
   192
			model.načtiAtributy();
franta-hg@10
   193
		} catch (IOException e) {
franta-hg@29
   194
			zobrazChybovouHlášku(překlady
franta-hg@29
   195
				.getString("chyba.nepodařiloSeNačíst"), e);
franta-hg@10
   196
		}
franta-hg@10
   197
	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@30
   198
franta-hg@30
   199
    private void tlačítkoZamknoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZamknoutActionPerformed
franta-hg@30
   200
        try {
franta-hg@30
   201
			model.nastavZámek(tlačítkoZamknout.isSelected());
franta-hg@30
   202
		} catch (Exception e) {
franta-hg@30
   203
			zobrazChybovouHlášku(překlady
franta-hg@30
   204
				.getString("chyba.nepodařiloSeNastavitZámek"), e);
franta-hg@30
   205
		}
franta-hg@30
   206
    }//GEN-LAST:event_tlačítkoZamknoutActionPerformed
franta-hg@30
   207
franta-hg@6
   208
    // Variables declaration - do not modify//GEN-BEGIN:variables
franta-hg@10
   209
    private javax.swing.JScrollPane posuvnýPanel;
franta-hg@10
   210
    private javax.swing.JButton tlačítkoPřidat;
franta-hg@10
   211
    private javax.swing.JButton tlačítkoSmazat;
franta-hg@30
   212
    private javax.swing.JToggleButton tlačítkoZamknout;
franta-hg@10
   213
    private javax.swing.JButton tlačítkoZnovuNačíst;
franta-hg@6
   214
    // End of variables declaration//GEN-END:variables
franta-hg@6
   215
}