java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java
changeset 10 ed2b6ebf138d
parent 6 734f104f2869
child 11 9b399cde6a3b
     1.1 --- a/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java	Wed Dec 15 20:07:14 2010 +0100
     1.2 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/gui/Panel.java	Wed Dec 15 23:29:14 2010 +0100
     1.3 @@ -1,6 +1,13 @@
     1.4  package cz.frantovo.rozsireneAtributy.gui;
     1.5  
     1.6 -import javax.swing.table.TableModel;
     1.7 +import cz.frantovo.rozsireneAtributy.Atribut;
     1.8 +import cz.frantovo.rozsireneAtributy.Model;
     1.9 +import java.io.IOException;
    1.10 +import java.util.logging.Level;
    1.11 +import java.util.logging.Logger;
    1.12 +import javax.swing.JOptionPane;
    1.13 +import javax.swing.event.ListSelectionEvent;
    1.14 +import javax.swing.event.ListSelectionListener;
    1.15  
    1.16  /**
    1.17   *
    1.18 @@ -8,20 +15,47 @@
    1.19   */
    1.20  public class Panel extends javax.swing.JPanel {
    1.21  
    1.22 -	private TableModel model;
    1.23 +	private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
    1.24 +	private Model model;
    1.25 +	private Atribut vybranýAtribut;
    1.26  
    1.27 -    public Panel(TableModel model) {
    1.28 +	public Panel(Model model) {
    1.29  		this.model = model;
    1.30 -        initComponents();
    1.31 +		initComponents();
    1.32  		tabulka.setModel(model);
    1.33 -    }
    1.34 +		tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    1.35  
    1.36 -    @SuppressWarnings("unchecked")
    1.37 +			public void valueChanged(ListSelectionEvent e) {
    1.38 +				int řádek = tabulka.getSelectedRow();
    1.39 +				if (řádek < 0) {
    1.40 +					vybranýAtribut = null;
    1.41 +					tlačítkoSmazat.setEnabled(false);
    1.42 +				} else {
    1.43 +					vybranýAtribut = getModel().getAtribut(řádek);
    1.44 +					tlačítkoSmazat.setEnabled(true);
    1.45 +				}
    1.46 +			}
    1.47 +		});
    1.48 +	}
    1.49 +
    1.50 +	private Model getModel() {
    1.51 +		return model;
    1.52 +	}
    1.53 +
    1.54 +	private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
    1.55 +		JOptionPane.showMessageDialog(this, hláška, "Chyba", JOptionPane.ERROR_MESSAGE);
    1.56 +		log.log(Level.WARNING, hláška, chyba);
    1.57 +	}
    1.58 +
    1.59 +	@SuppressWarnings("unchecked")
    1.60      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    1.61      private void initComponents() {
    1.62  
    1.63 -        scroll = new javax.swing.JScrollPane();
    1.64 +        posuvnýPanel = new javax.swing.JScrollPane();
    1.65          tabulka = new javax.swing.JTable();
    1.66 +        tlačítkoPřidat = new javax.swing.JButton();
    1.67 +        tlačítkoSmazat = new javax.swing.JButton();
    1.68 +        tlačítkoZnovuNačíst = new javax.swing.JButton();
    1.69  
    1.70          tabulka.setModel(new javax.swing.table.DefaultTableModel(
    1.71              new Object [][] {
    1.72 @@ -34,24 +68,82 @@
    1.73                  "Title 1", "Title 2", "Title 3", "Title 4"
    1.74              }
    1.75          ));
    1.76 -        scroll.setViewportView(tabulka);
    1.77 +        tabulka.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    1.78 +        posuvnýPanel.setViewportView(tabulka);
    1.79 +
    1.80 +        tlačítkoPřidat.setText("Přidat atribut");
    1.81 +        tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
    1.82 +            public void actionPerformed(java.awt.event.ActionEvent evt) {
    1.83 +                tlačítkoPřidatActionPerformed(evt);
    1.84 +            }
    1.85 +        });
    1.86 +
    1.87 +        tlačítkoSmazat.setText("Smazat atribut");
    1.88 +        tlačítkoSmazat.setEnabled(false);
    1.89 +        tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
    1.90 +            public void actionPerformed(java.awt.event.ActionEvent evt) {
    1.91 +                tlačítkoSmazatActionPerformed(evt);
    1.92 +            }
    1.93 +        });
    1.94 +
    1.95 +        tlačítkoZnovuNačíst.setText("Znovu načíst");
    1.96 +        tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
    1.97 +            public void actionPerformed(java.awt.event.ActionEvent evt) {
    1.98 +                tlačítkoZnovuNačístActionPerformed(evt);
    1.99 +            }
   1.100 +        });
   1.101  
   1.102          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   1.103          this.setLayout(layout);
   1.104          layout.setHorizontalGroup(
   1.105              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.106 -            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
   1.107 +            .addGroup(layout.createSequentialGroup()
   1.108 +                .addContainerGap()
   1.109 +                .addComponent(tlačítkoPřidat)
   1.110 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.111 +                .addComponent(tlačítkoSmazat)
   1.112 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.113 +                .addComponent(tlačítkoZnovuNačíst)
   1.114 +                .addContainerGap(90, Short.MAX_VALUE))
   1.115 +            .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
   1.116          );
   1.117          layout.setVerticalGroup(
   1.118              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.119 -            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
   1.120 +            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   1.121 +                .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
   1.122 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.123 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   1.124 +                    .addComponent(tlačítkoPřidat)
   1.125 +                    .addComponent(tlačítkoSmazat)
   1.126 +                    .addComponent(tlačítkoZnovuNačíst))
   1.127 +                .addContainerGap())
   1.128          );
   1.129      }// </editor-fold>//GEN-END:initComponents
   1.130  
   1.131 +	private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
   1.132 +		model.přidejAtribut(new Atribut());
   1.133 +	}//GEN-LAST:event_tlačítkoPřidatActionPerformed
   1.134  
   1.135 +	private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
   1.136 +		try {
   1.137 +			model.odeberAtribut(vybranýAtribut);
   1.138 +		} catch (IOException e) {
   1.139 +			zobrazChybovouHlášku("Nepodařilo se smazat atribut.", e);
   1.140 +		}
   1.141 +	}//GEN-LAST:event_tlačítkoSmazatActionPerformed
   1.142 +
   1.143 +	private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
   1.144 +		try {
   1.145 +			model.načtiAtributy();
   1.146 +		} catch (IOException e) {
   1.147 +			zobrazChybovouHlášku("Nepodařilo se načíst atributy", e);
   1.148 +		}
   1.149 +	}//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
   1.150      // Variables declaration - do not modify//GEN-BEGIN:variables
   1.151 -    private javax.swing.JScrollPane scroll;
   1.152 +    private javax.swing.JScrollPane posuvnýPanel;
   1.153      private javax.swing.JTable tabulka;
   1.154 +    private javax.swing.JButton tlačítkoPřidat;
   1.155 +    private javax.swing.JButton tlačítkoSmazat;
   1.156 +    private javax.swing.JButton tlačítkoZnovuNačíst;
   1.157      // End of variables declaration//GEN-END:variables
   1.158 -
   1.159  }