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