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 java.awt.Dimension; import java.awt.HeadlessException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 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.JScrollPane; import javax.swing.JTable; import javax.swing.SwingWorker; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.DateTickUnit; import org.jfree.chart.axis.DateTickUnitType; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.time.Month; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.IntervalXYDataset; import org.metawidget.swing.SwingMetawidget; import sk.uniza.fri.pds.spotreba.energie.domain.CelkovaStatistika; import sk.uniza.fri.pds.spotreba.energie.domain.KrokSpotreby; import sk.uniza.fri.pds.spotreba.energie.domain.SeZamestnanecInfo; import sk.uniza.fri.pds.spotreba.energie.domain.SpotrebaDomacnosti; import sk.uniza.fri.pds.spotreba.energie.domain.StatistikaServisov; import sk.uniza.fri.pds.spotreba.energie.domain.StatistikaTypuKategorie; import sk.uniza.fri.pds.spotreba.energie.domain.ZvysenieSpotreby; import sk.uniza.fri.pds.spotreba.energie.gui.utils.DbClass; import sk.uniza.fri.pds.spotreba.energie.gui.utils.MetawidgetUtils; import sk.uniza.fri.pds.spotreba.energie.service.NajminajucejsiSpotrebitel; import sk.uniza.fri.pds.spotreba.energie.service.SeHistoriaService; import sk.uniza.fri.pds.spotreba.energie.service.SeServisService; import sk.uniza.fri.pds.spotreba.energie.service.SeZamestnanecService; import sk.uniza.fri.pds.spotreba.energie.service.ZamestnanecOdpisReport; import sk.uniza.fri.pds.spotreba.energie.service.util.IncreasedSpendingStatisticParams; import sk.uniza.fri.pds.spotreba.energie.service.util.NajminajucejsiSpotrebiteliaParams; import sk.uniza.fri.pds.spotreba.energie.service.util.ReportParams; import sk.uniza.fri.pds.spotreba.energie.service.util.SpendingStatisticsParameters; import sk.uniza.fri.pds.spotreba.energie.service.util.StatistikaSpotriebParams; import sk.uniza.fri.pds.spotreba.energie.service.util.StatistikaTypuKategorieParams; import sk.uniza.fri.pds.spotreba.energie.service.util.ZamestnanecRegionParams; /** * * @author Coder */ public class MainGui extends javax.swing.JFrame { /** * Creates new form MainGui */ public MainGui() { initComponents(); setLocationRelativeTo(null); for (DbClass cl : DbClass.values()) { tabs.add(new DbTab(cl.clazz, cl.service)); } } /** * 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() { tabs = new javax.swing.JTabbedPane(); menuBar = new javax.swing.JMenuBar(); spendingMenu = new javax.swing.JMenu(); showSpendingStatistics = new javax.swing.JMenuItem(); increasedSpending = new javax.swing.JMenuItem(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); typeAndCatMenuItem = new javax.swing.JMenuItem(); jMenuItem4 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem(); jMenuItem8 = new javax.swing.JMenuItem(); servisMenu = new javax.swing.JMenu(); servisStatsMenuItem = new javax.swing.JMenuItem(); jMenuItem7 = new javax.swing.JMenuItem(); jMenu1 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jMenuItem6 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tabs.setMinimumSize(new java.awt.Dimension(600, 900)); tabs.setPreferredSize(new java.awt.Dimension(600, 900)); getContentPane().add(tabs, java.awt.BorderLayout.CENTER); spendingMenu.setText("Spotreba"); showSpendingStatistics.setText("Vvoj spotreby"); showSpendingStatistics.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showSpendingStatisticsActionPerformed(evt); } }); spendingMenu.add(showSpendingStatistics); increasedSpending.setText("Zven spotreba o aspo 20%"); increasedSpending.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { increasedSpendingActionPerformed(evt); } }); spendingMenu.add(increasedSpending); jMenuItem1.setText("Zven spotreba o aspo 50% v poslednom roku"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); spendingMenu.add(jMenuItem1); jMenuItem2.setText("Spotreba v poslednom roku menia ako 10% priemeru"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); spendingMenu.add(jMenuItem2); typeAndCatMenuItem.setText("tatistika poda typu a kategrie"); typeAndCatMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { typeAndCatMenuItemActionPerformed(evt); } }); spendingMenu.add(typeAndCatMenuItem); jMenuItem4.setText("10% spotrebitelov s najv?ou spotrebou"); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem4ActionPerformed(evt); } }); spendingMenu.add(jMenuItem4); jMenuItem3.setText("Celkov tatistiky"); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem3ActionPerformed(evt); } }); spendingMenu.add(jMenuItem3); jMenuItem8.setText("Vytvori ro?n report pre spotrebitea "); jMenuItem8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem8ActionPerformed(evt); } }); spendingMenu.add(jMenuItem8); menuBar.add(spendingMenu); servisMenu.setText("Servisy"); servisStatsMenuItem.setText("tatistika"); servisStatsMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { servisStatsMenuItemActionPerformed(evt); } }); servisMenu.add(servisStatsMenuItem); jMenuItem7.setText( "Zobrazi informcie o spotrebe domcnost ktor mali za posledn rok vymen 2 zariadenia na meranie rovnakej veli?iny"); jMenuItem7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem7ActionPerformed(evt); } }); servisMenu.add(jMenuItem7); menuBar.add(servisMenu); jMenu1.setText("Zamestnanci"); jMenuItem5.setText("Zobrazi zamestnancov v regine"); jMenuItem5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem5ActionPerformed(evt); } }); jMenu1.add(jMenuItem5); jMenuItem6.setText("Zobrazi najvkonnejch 3 zamestnancov za posledn rok"); jMenuItem6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem6ActionPerformed(evt); } }); jMenu1.add(jMenuItem6); menuBar.add(jMenu1); setJMenuBar(menuBar); pack(); }// </editor-fold>//GEN-END:initComponents private void showSpendingStatisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showSpendingStatisticsActionPerformed final SpendingStatisticsParameters params = new SpendingStatisticsParameters(); int option = showUniversalInputDialog(params, "Vvoj spotreby"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getSpendingStatistics(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List<KrokSpotreby> spendingStatistics = get(); if (spendingStatistics != null) { final TimeSeries series = new TimeSeries(""); final String title = "Vvoj spotreby"; for (KrokSpotreby krok : spendingStatistics) { series.add(new Month(krok.getDatumOd()), krok.getSpotreba()); } final IntervalXYDataset dataset = (IntervalXYDataset) new TimeSeriesCollection(series); JFreeChart chart = ChartFactory.createXYBarChart(title, // title "", // x-axis label true, // date axis? "", // y-axis label dataset, // data PlotOrientation.VERTICAL, // orientation false, // create legend? true, // generate tooltips? false // generate URLs? ); // Set date axis style DateAxis axis = (DateAxis) ((XYPlot) chart.getPlot()).getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("yyyy")); DateFormat formatter = new SimpleDateFormat("yyyy"); DateTickUnit unit = new DateTickUnit(DateTickUnitType.YEAR, 1, formatter); axis.setTickUnit(unit); JOptionPane.showMessageDialog(null, new ChartPanel(chart)); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_showSpendingStatisticsActionPerformed private void increasedSpendingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_increasedSpendingActionPerformed final IncreasedSpendingStatisticParams params = new IncreasedSpendingStatisticParams(); int option = showUniversalInputDialog(params, "Zven spotreba"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getIncreasedSpendingStatistics(params, 1.2); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(ZvysenieSpotreby.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_increasedSpendingActionPerformed private void servisStatsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_servisStatsMenuItemActionPerformed new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeServisService.getInstance().getServiceStatistics(); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(StatistikaServisov.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); }//GEN-LAST:event_servisStatsMenuItemActionPerformed private void typeAndCatMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeAndCatMenuItemActionPerformed final StatistikaTypuKategorieParams params = new StatistikaTypuKategorieParams(); int option = showUniversalInputDialog(params, "tatistika poda typu a kategrie"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getTypeAndCategoryStatistics(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(StatistikaTypuKategorie.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_typeAndCatMenuItemActionPerformed private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed showLastYearChange(1.5); }//GEN-LAST:event_jMenuItem1ActionPerformed private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed showLastYearChange(0.1); }//GEN-LAST:event_jMenuItem2ActionPerformed private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed final StatistikaSpotriebParams params = new StatistikaSpotriebParams(); int option = showUniversalInputDialog(params, "tatistika poda typu a kategrie"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getOveralStatistics(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(CelkovaStatistika.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_jMenuItem3ActionPerformed private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed final NajminajucejsiSpotrebiteliaParams params = new NajminajucejsiSpotrebiteliaParams(); int option = showUniversalInputDialog(params, "10% spotrebiteov s najv?ou spotrebou"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getNajnminajucejsiSpotrebitelia(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(NajminajucejsiSpotrebitel.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_jMenuItem4ActionPerformed private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed final ZamestnanecRegionParams params = new ZamestnanecRegionParams(); int option = showUniversalInputDialog(params, "Zamestnanci v regine"); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeZamestnanecService.getInstance().findInRegion(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(SeZamestnanecInfo.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_jMenuItem5ActionPerformed private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeZamestnanecService.getInstance().findBest(3); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(ZamestnanecOdpisReport.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); }//GEN-LAST:event_jMenuItem6ActionPerformed private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getProblematickeDOmacnosti(); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(SpotrebaDomacnosti.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); }//GEN-LAST:event_jMenuItem7ActionPerformed private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed final ReportParams params = new ReportParams(); int option = showUniversalInputDialog(params, "Ro?n sprva pre zkaznka", new Dimension(400, 80)); if (option == JOptionPane.OK_OPTION) { new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().createLastYearReport(params); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List<String> data = get(); if (data != null) { JOptionPane.showMessageDialog(null, "Export prebehol spene"); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } }//GEN-LAST:event_jMenuItem8ActionPerformed private void showLastYearChange(double factor) throws HeadlessException { final IncreasedSpendingStatisticParams params = new IncreasedSpendingStatisticParams(); params.setDatumDo(new Date()); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -12); Date result = cal.getTime(); params.setDatumOd(result); new SwingWorker<List, RuntimeException>() { @Override protected List doInBackground() throws Exception { try { return SeHistoriaService.getInstance().getIncreasedSpendingStatistics(params, factor); } catch (RuntimeException e) { publish(e); return null; } } @Override protected void done() { try { List data = get(); showJTable(ZvysenieSpotreby.class, data); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void process(List<RuntimeException> chunks) { if (chunks.size() > 0) { showException("Chyba", chunks.get(0)); } } }.execute(); } private int showUniversalInputDialog(final Object params, String title) throws HeadlessException { return showUniversalInputDialog(params, title, null); } private int showUniversalInputDialog(final Object params, String title, Dimension d) throws HeadlessException { SwingMetawidget metawidget = new SwingMetawidget(); if (d != null) { metawidget.setSize(d); metawidget.setPreferredSize(d); } MetawidgetUtils.setCommonSettings(metawidget); metawidget.setToInspect(params); Object[] message = { metawidget }; int option = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION); return option; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainGui().setVisible(true); } }); } private void showException(String message, Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(), message, JOptionPane.ERROR_MESSAGE); } private void showJTable(Class clazz, List data) throws HeadlessException { JScrollPane jScrollPane = new JScrollPane(); Dimension dimension = new Dimension(1200, 400); jScrollPane.setSize(dimension); jScrollPane.setPreferredSize(dimension); final BeanTableModel beanTableModel = new BeanTableModel(clazz, data); beanTableModel.sortColumnNames(); JTable jTable = new JTable(beanTableModel); jScrollPane.getViewport().add(jTable); JOptionPane.showMessageDialog(null, jScrollPane); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem increasedSpending; private javax.swing.JMenu jMenu1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenuItem jMenuItem5; private javax.swing.JMenuItem jMenuItem6; private javax.swing.JMenuItem jMenuItem7; private javax.swing.JMenuItem jMenuItem8; private javax.swing.JMenuBar menuBar; private javax.swing.JMenu servisMenu; private javax.swing.JMenuItem servisStatsMenuItem; private javax.swing.JMenuItem showSpendingStatistics; private javax.swing.JMenu spendingMenu; private javax.swing.JTabbedPane tabs; private javax.swing.JMenuItem typeAndCatMenuItem; // End of variables declaration//GEN-END:variables }