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