Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package sk.uniza.fri.pds.spotreba.energie.gui; import sk.uniza.fri.pds.spotreba.energie.gui.utils.MetawidgetUtils; import java.io.Serializable; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.SwingWorker; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import org.apache.commons.lang3.SerializationUtils; import sk.uniza.fri.pds.spotreba.energie.service.SeService; /** * * @author Coder */ public class DbTab extends javax.swing.JPanel { private BeanTableModel tableModel; private Class clazz; private Object oldObj; private SeService service; /** * Creates new form DbTab */ public DbTab() { initComponents(); } public DbTab(Class clazz, SeService service) { this.clazz = clazz; this.service = service; tableModel = new BeanTableModel(clazz); this.tableModel.sortColumnNames(); setName(clazz.getSimpleName()); initComponents(); try { MetawidgetUtils.setCommonSettings(metawidget); metawidget.setToInspect(clazz.newInstance()); jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { if (jTable.getSelectedRow() > -1) { Object row = ((BeanTableModel) jTable.getModel()).getRow(jTable.getSelectedRow()); oldObj = SerializationUtils.clone((Serializable) row); metawidget.setToInspect(row); } } }); } catch (InstantiationException | IllegalAccessException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { table = new javax.swing.JScrollPane(); jTable = new javax.swing.JTable(); bot = new javax.swing.JPanel(); metawidget = new org.metawidget.swing.SwingMetawidget(); buttons = new javax.swing.JPanel(); deleteButton = new javax.swing.JButton(); newButton = new javax.swing.JButton(); loadButton = new javax.swing.JButton(); saveButton = new javax.swing.JButton(); setLayout(new java.awt.BorderLayout()); jTable.setModel(tableModel); table.setViewportView(jTable); add(table, java.awt.BorderLayout.CENTER); bot.setLayout(new java.awt.BorderLayout()); bot.add(metawidget, java.awt.BorderLayout.PAGE_START); deleteButton.setText("Delete"); deleteButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteButtonActionPerformed(evt); } }); buttons.add(deleteButton); newButton.setText("New"); newButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { newButtonActionPerformed(evt); } }); buttons.add(newButton); loadButton.setText("Load table"); loadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadButtonActionPerformed(evt); } }); buttons.add(loadButton); saveButton.setText("Save"); saveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveButtonActionPerformed(evt); } }); buttons.add(saveButton); bot.add(buttons, java.awt.BorderLayout.LINE_END); add(bot, java.awt.BorderLayout.SOUTH); }// </editor-fold>//GEN-END:initComponents private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed if (service != null) { if (jTable.getSelectedRow() > -1) { update(); } else { create(); } } }//GEN-LAST:event_saveButtonActionPerformed private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed if (service != null) { load(); } }//GEN-LAST:event_loadButtonActionPerformed private void load() { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return service.findAll(); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { if (get() != null) { tableModel = new BeanTableModel(clazz, get()); tableModel.sortColumnNames(); jTable.setModel(tableModel); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { showException("Chyba", chunks.get(0)); } }.execute(); } private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed try { metawidget.setToInspect(clazz.newInstance()); jTable.clearSelection(); oldObj = null; } catch (InstantiationException | IllegalAccessException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_newButtonActionPerformed private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed if (service != null) { if (jTable.getSelectedRow() > -1) { delete(); } } }//GEN-LAST:event_deleteButtonActionPerformed private void create() { new SwingWorker<Boolean, RuntimeException>() { @Override protected Boolean doInBackground() throws Exception { try { service.create(metawidget.getToInspect()); return true; } catch (RuntimeException e) { publish(e); } return false; } @Override protected void done() { try { if (get()) { load(); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { showException("Chyba", chunks.get(0)); } }.execute(); } private void update() { new SwingWorker<Boolean, RuntimeException>() { @Override protected Boolean doInBackground() throws Exception { try { service.update(oldObj, metawidget.getToInspect()); return true; } catch (RuntimeException e) { publish(e); } return false; } @Override protected void done() { try { if (get()) { tableModel.fireTableRowsUpdated(jTable.getSelectedRow(), jTable.getSelectedRow()); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { showException("Chyba", chunks.get(0)); } }.execute(); } private void delete() { new SwingWorker<Boolean, RuntimeException>() { @Override protected Boolean doInBackground() throws Exception { try { service.delete(metawidget.getToInspect()); return true; } catch (RuntimeException e) { publish(e); } return false; } @Override protected void done() { try { if (get()) { load(); newButtonActionPerformed(null); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { showException("Chyba", chunks.get(0)); } }.execute(); } private void showException(String message, Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(), message, JOptionPane.ERROR_MESSAGE); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel bot; private javax.swing.JPanel buttons; private javax.swing.JButton deleteButton; private javax.swing.JTable jTable; private javax.swing.JButton loadButton; private org.metawidget.swing.SwingMetawidget metawidget; private javax.swing.JButton newButton; private javax.swing.JButton saveButton; private javax.swing.JScrollPane table; // End of variables declaration//GEN-END:variables }