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ů.
     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 		tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    27 
    28 			public void valueChanged(ListSelectionEvent e) {
    29 				int řádek = tabulka.getSelectedRow();
    30 				if (řádek < 0) {
    31 					vybranýAtribut = null;
    32 					tlačítkoSmazat.setEnabled(false);
    33 				} else {
    34 					vybranýAtribut = getModel().getAtribut(řádek);
    35 					tlačítkoSmazat.setEnabled(true);
    36 				}
    37 			}
    38 		});
    39 	}
    40 
    41 	private Model getModel() {
    42 		return model;
    43 	}
    44 
    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);
    48 	}
    49 
    50 	@SuppressWarnings("unchecked")
    51     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    52     private void initComponents() {
    53 
    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();
    59 
    60         tabulka.setModel(new javax.swing.table.DefaultTableModel(
    61             new Object [][] {
    62                 {null, null, null, null},
    63                 {null, null, null, null},
    64                 {null, null, null, null},
    65                 {null, null, null, null}
    66             },
    67             new String [] {
    68                 "Title 1", "Title 2", "Title 3", "Title 4"
    69             }
    70         ));
    71         tabulka.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    72         posuvnýPanel.setViewportView(tabulka);
    73 
    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);
    78             }
    79         });
    80 
    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);
    86             }
    87         });
    88 
    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);
    93             }
    94         });
    95 
    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()
   101                 .addContainerGap()
   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)
   109         );
   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))
   119                 .addContainerGap())
   120         );
   121     }// </editor-fold>//GEN-END:initComponents
   122 
   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
   126 
   127 	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
   128 		try {
   129 			model.odeberAtribut(vybranýAtribut);
   130 		} catch (IOException e) {
   131 			zobrazChybovouHlášku("Nepodařilo se smazat atribut.", e);
   132 		}
   133 	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
   134 
   135 	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
   136 		try {
   137 			model.načtiAtributy();
   138 		} catch (IOException e) {
   139 			zobrazChybovouHlášku("Nepodařilo se načíst atributy", e);
   140 		}
   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
   149 }