Vytváření a mazání atributů.
1 package cz.frantovo.rozsireneAtributy.gui;
3 import cz.frantovo.rozsireneAtributy.Atribut;
4 import cz.frantovo.rozsireneAtributy.Model;
5 import java.io.IOException;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8 import javax.swing.JOptionPane;
9 import javax.swing.event.ListSelectionEvent;
10 import javax.swing.event.ListSelectionListener;
16 public class Panel extends javax.swing.JPanel {
18 private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
20 private Atribut vybranýAtribut;
22 public Panel(Model model) {
25 tabulka.setModel(model);
26 tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
28 public void valueChanged(ListSelectionEvent e) {
29 int řádek = tabulka.getSelectedRow();
31 vybranýAtribut = null;
32 tlačítkoSmazat.setEnabled(false);
34 vybranýAtribut = getModel().getAtribut(řádek);
35 tlačítkoSmazat.setEnabled(true);
41 private Model getModel() {
45 private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
46 JOptionPane.showMessageDialog(this, hláška, "Chyba", JOptionPane.ERROR_MESSAGE);
47 log.log(Level.WARNING, hláška, chyba);
50 @SuppressWarnings("unchecked")
51 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
52 private void initComponents() {
54 posuvnýPanel = new javax.swing.JScrollPane();
55 tabulka = new javax.swing.JTable();
56 tlačítkoPřidat = new javax.swing.JButton();
57 tlačítkoSmazat = new javax.swing.JButton();
58 tlačítkoZnovuNačíst = new javax.swing.JButton();
60 tabulka.setModel(new javax.swing.table.DefaultTableModel(
62 {null, null, null, null},
63 {null, null, null, null},
64 {null, null, null, null},
65 {null, null, null, null}
68 "Title 1", "Title 2", "Title 3", "Title 4"
71 tabulka.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
72 posuvnýPanel.setViewportView(tabulka);
74 tlačítkoPřidat.setText("Přidat atribut");
75 tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
76 public void actionPerformed(java.awt.event.ActionEvent evt) {
77 tlačítkoPřidatActionPerformed(evt);
81 tlačítkoSmazat.setText("Smazat atribut");
82 tlačítkoSmazat.setEnabled(false);
83 tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
84 public void actionPerformed(java.awt.event.ActionEvent evt) {
85 tlačítkoSmazatActionPerformed(evt);
89 tlačítkoZnovuNačíst.setText("Znovu načíst");
90 tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
91 public void actionPerformed(java.awt.event.ActionEvent evt) {
92 tlačítkoZnovuNačístActionPerformed(evt);
96 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
97 this.setLayout(layout);
98 layout.setHorizontalGroup(
99 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
100 .addGroup(layout.createSequentialGroup()
102 .addComponent(tlačítkoPřidat)
103 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
104 .addComponent(tlačítkoSmazat)
105 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
106 .addComponent(tlačítkoZnovuNačíst)
107 .addContainerGap(90, Short.MAX_VALUE))
108 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
110 layout.setVerticalGroup(
111 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
112 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
113 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
114 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
115 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
116 .addComponent(tlačítkoPřidat)
117 .addComponent(tlačítkoSmazat)
118 .addComponent(tlačítkoZnovuNačíst))
121 }// </editor-fold>//GEN-END:initComponents
123 private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
124 model.přidejAtribut(new Atribut());
125 }//GEN-LAST:event_tlačítkoPřidatActionPerformed
127 private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
129 model.odeberAtribut(vybranýAtribut);
130 } catch (IOException e) {
131 zobrazChybovouHlášku("Nepodařilo se smazat atribut.", e);
133 }//GEN-LAST:event_tlačítkoSmazatActionPerformed
135 private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
137 model.načtiAtributy();
138 } catch (IOException e) {
139 zobrazChybovouHlášku("Nepodařilo se načíst atributy", e);
141 }//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
142 // Variables declaration - do not modify//GEN-BEGIN:variables
143 private javax.swing.JScrollPane posuvnýPanel;
144 private javax.swing.JTable tabulka;
145 private javax.swing.JButton tlačítkoPřidat;
146 private javax.swing.JButton tlačítkoSmazat;
147 private javax.swing.JButton tlačítkoZnovuNačíst;
148 // End of variables declaration//GEN-END:variables