java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 23:29:14 2010 +0100 (2010-12-15)
changeset 10 ed2b6ebf138d
parent 6 734f104f2869
child 11 9b399cde6a3b
permissions -rw-r--r--
Vytváření a mazání atributů.
franta-hg@6
     1
package cz.frantovo.rozsireneAtributy.gui;
franta-hg@6
     2
franta-hg@10
     3
import cz.frantovo.rozsireneAtributy.Atribut;
franta-hg@10
     4
import cz.frantovo.rozsireneAtributy.Model;
franta-hg@10
     5
import java.io.IOException;
franta-hg@10
     6
import java.util.logging.Level;
franta-hg@10
     7
import java.util.logging.Logger;
franta-hg@10
     8
import javax.swing.JOptionPane;
franta-hg@10
     9
import javax.swing.event.ListSelectionEvent;
franta-hg@10
    10
import javax.swing.event.ListSelectionListener;
franta-hg@6
    11
franta-hg@6
    12
/**
franta-hg@6
    13
 *
franta-hg@6
    14
 * @author fiki
franta-hg@6
    15
 */
franta-hg@6
    16
public class Panel extends javax.swing.JPanel {
franta-hg@6
    17
franta-hg@10
    18
	private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
franta-hg@10
    19
	private Model model;
franta-hg@10
    20
	private Atribut vybranýAtribut;
franta-hg@6
    21
franta-hg@10
    22
	public Panel(Model model) {
franta-hg@6
    23
		this.model = model;
franta-hg@10
    24
		initComponents();
franta-hg@6
    25
		tabulka.setModel(model);
franta-hg@10
    26
		tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
franta-hg@6
    27
franta-hg@10
    28
			public void valueChanged(ListSelectionEvent e) {
franta-hg@10
    29
				int řádek = tabulka.getSelectedRow();
franta-hg@10
    30
				if (řádek < 0) {
franta-hg@10
    31
					vybranýAtribut = null;
franta-hg@10
    32
					tlačítkoSmazat.setEnabled(false);
franta-hg@10
    33
				} else {
franta-hg@10
    34
					vybranýAtribut = getModel().getAtribut(řádek);
franta-hg@10
    35
					tlačítkoSmazat.setEnabled(true);
franta-hg@10
    36
				}
franta-hg@10
    37
			}
franta-hg@10
    38
		});
franta-hg@10
    39
	}
franta-hg@10
    40
franta-hg@10
    41
	private Model getModel() {
franta-hg@10
    42
		return model;
franta-hg@10
    43
	}
franta-hg@10
    44
franta-hg@10
    45
	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
franta-hg@10
    46
		JOptionPane.showMessageDialog(this, hláška, "Chyba", JOptionPane.ERROR_MESSAGE);
franta-hg@10
    47
		log.log(Level.WARNING, hláška, chyba);
franta-hg@10
    48
	}
franta-hg@10
    49
franta-hg@10
    50
	@SuppressWarnings("unchecked")
franta-hg@6
    51
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
franta-hg@6
    52
    private void initComponents() {
franta-hg@6
    53
franta-hg@10
    54
        posuvnýPanel = new javax.swing.JScrollPane();
franta-hg@6
    55
        tabulka = new javax.swing.JTable();
franta-hg@10
    56
        tlačítkoPřidat = new javax.swing.JButton();
franta-hg@10
    57
        tlačítkoSmazat = new javax.swing.JButton();
franta-hg@10
    58
        tlačítkoZnovuNačíst = new javax.swing.JButton();
franta-hg@6
    59
franta-hg@6
    60
        tabulka.setModel(new javax.swing.table.DefaultTableModel(
franta-hg@6
    61
            new Object [][] {
franta-hg@6
    62
                {null, null, null, null},
franta-hg@6
    63
                {null, null, null, null},
franta-hg@6
    64
                {null, null, null, null},
franta-hg@6
    65
                {null, null, null, null}
franta-hg@6
    66
            },
franta-hg@6
    67
            new String [] {
franta-hg@6
    68
                "Title 1", "Title 2", "Title 3", "Title 4"
franta-hg@6
    69
            }
franta-hg@6
    70
        ));
franta-hg@10
    71
        tabulka.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
franta-hg@10
    72
        posuvnýPanel.setViewportView(tabulka);
franta-hg@10
    73
franta-hg@10
    74
        tlačítkoPřidat.setText("Přidat atribut");
franta-hg@10
    75
        tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    76
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    77
                tlačítkoPřidatActionPerformed(evt);
franta-hg@10
    78
            }
franta-hg@10
    79
        });
franta-hg@10
    80
franta-hg@10
    81
        tlačítkoSmazat.setText("Smazat atribut");
franta-hg@10
    82
        tlačítkoSmazat.setEnabled(false);
franta-hg@10
    83
        tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    84
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    85
                tlačítkoSmazatActionPerformed(evt);
franta-hg@10
    86
            }
franta-hg@10
    87
        });
franta-hg@10
    88
franta-hg@10
    89
        tlačítkoZnovuNačíst.setText("Znovu načíst");
franta-hg@10
    90
        tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
franta-hg@10
    91
            public void actionPerformed(java.awt.event.ActionEvent evt) {
franta-hg@10
    92
                tlačítkoZnovuNačístActionPerformed(evt);
franta-hg@10
    93
            }
franta-hg@10
    94
        });
franta-hg@6
    95
franta-hg@6
    96
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
franta-hg@6
    97
        this.setLayout(layout);
franta-hg@6
    98
        layout.setHorizontalGroup(
franta-hg@6
    99
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
   100
            .addGroup(layout.createSequentialGroup()
franta-hg@10
   101
                .addContainerGap()
franta-hg@10
   102
                .addComponent(tlačítkoPřidat)
franta-hg@10
   103
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   104
                .addComponent(tlačítkoSmazat)
franta-hg@10
   105
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   106
                .addComponent(tlačítkoZnovuNačíst)
franta-hg@10
   107
                .addContainerGap(90, Short.MAX_VALUE))
franta-hg@10
   108
            .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
franta-hg@6
   109
        );
franta-hg@6
   110
        layout.setVerticalGroup(
franta-hg@6
   111
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
franta-hg@10
   112
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
franta-hg@10
   113
                .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
franta-hg@10
   114
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
franta-hg@10
   115
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
franta-hg@10
   116
                    .addComponent(tlačítkoPřidat)
franta-hg@10
   117
                    .addComponent(tlačítkoSmazat)
franta-hg@10
   118
                    .addComponent(tlačítkoZnovuNačíst))
franta-hg@10
   119
                .addContainerGap())
franta-hg@6
   120
        );
franta-hg@6
   121
    }// </editor-fold>//GEN-END:initComponents
franta-hg@6
   122
franta-hg@10
   123
	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
franta-hg@10
   124
		model.přidejAtribut(new Atribut());
franta-hg@10
   125
	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
franta-hg@6
   126
franta-hg@10
   127
	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   128
		try {
franta-hg@10
   129
			model.odeberAtribut(vybranýAtribut);
franta-hg@10
   130
		} catch (IOException e) {
franta-hg@10
   131
			zobrazChybovouHlášku("Nepodařilo se smazat atribut.", e);
franta-hg@10
   132
		}
franta-hg@10
   133
	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
franta-hg@10
   134
franta-hg@10
   135
	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@10
   136
		try {
franta-hg@10
   137
			model.načtiAtributy();
franta-hg@10
   138
		} catch (IOException e) {
franta-hg@10
   139
			zobrazChybovouHlášku("Nepodařilo se načíst atributy", e);
franta-hg@10
   140
		}
franta-hg@10
   141
	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
franta-hg@6
   142
    // Variables declaration - do not modify//GEN-BEGIN:variables
franta-hg@10
   143
    private javax.swing.JScrollPane posuvnýPanel;
franta-hg@6
   144
    private javax.swing.JTable tabulka;
franta-hg@10
   145
    private javax.swing.JButton tlačítkoPřidat;
franta-hg@10
   146
    private javax.swing.JButton tlačítkoSmazat;
franta-hg@10
   147
    private javax.swing.JButton tlačítkoZnovuNačíst;
franta-hg@6
   148
    // End of variables declaration//GEN-END:variables
franta-hg@6
   149
}