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