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