view.PnIncome.java Source code

Java tutorial

Introduction

Here is the source code for view.PnIncome.java

Source

/*
 * 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 view;

import Utils.PnIncomeFindMonth;
import Utils.PnIncomeFindQuarter;
import Utils.PnIncomeFindYear;
import common.Constant;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.sql.Date;
import java.util.Calendar;
import model.ModelCustomerService;
import model.ModelRoomStatus;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author GloomySunday
 */
public class PnIncome extends javax.swing.JPanel {

    private PnIncomeFindMonth mPnIncomeFindMonth;
    private PnIncomeFindQuarter mPnIncomeFindQuarter;
    private final int[][] mQuarter;
    private PnIncomeFindYear mPnIncomeFindYear;

    /**
     * Creates new form PnHotel
     */
    public PnIncome() {
        this.mQuarter = Constant.MONTH_OF_QUARTER;
        initComponents();
        pnFind.add(new PnIncomeFindMonth(), BorderLayout.CENTER);
        Calendar now = Calendar.getInstance();
        drawDateChartMonth(now.get(Calendar.MONTH) + 1, now.get(Calendar.YEAR));
        mPnIncomeFindMonth = new PnIncomeFindMonth();
        mPnIncomeFindMonth.getCbMonth().addActionListener((ActionEvent e) -> {
            pnCenter.removeAll();
            drawDateChartMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                    (int) mPnIncomeFindMonth.getCbYear().getSelectedItem());
        });
        mPnIncomeFindMonth.getCbYear().addActionListener((ActionEvent e) -> {
            pnCenter.removeAll();
            drawDateChartMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                    (int) mPnIncomeFindMonth.getCbYear().getSelectedItem());

        });

        mPnIncomeFindQuarter = new PnIncomeFindQuarter();
        mPnIncomeFindQuarter.getCbQuarter().addActionListener((ActionEvent e) -> {
            pnCenter.removeAll();
            drawDateChartQuarter(Integer.parseInt((String) mPnIncomeFindQuarter.getCbQuarter().getSelectedItem()),
                    (int) mPnIncomeFindQuarter.getCbYear().getSelectedItem());

        });
        mPnIncomeFindQuarter.getCbYear().addActionListener((ActionEvent e) -> {
            pnCenter.removeAll();
            drawDateChartQuarter((int) mPnIncomeFindQuarter.getCbQuarter().getSelectedItem(),
                    (int) mPnIncomeFindQuarter.getCbYear().getSelectedItem());

        });

        mPnIncomeFindYear = new PnIncomeFindYear();
        mPnIncomeFindYear.getCbYear().addActionListener((ActionEvent e) -> {
            pnCenter.removeAll();
            drawDateChartYear((int) mPnIncomeFindYear.getCbYear().getSelectedItem());

        });
    }

    private ChartPanel getChartPanel(String title, String type, DefaultCategoryDataset dataset) {
        JFreeChart lineChart = ChartFactory.createLineChart(title, "Thu nhp", type, dataset,
                PlotOrientation.VERTICAL, true, true, false);
        ChartPanel chartPanel = new ChartPanel(lineChart);
        return chartPanel;
    }

    private void drawDateChartMonth(int month, int year) {
        ModelCustomerService modelCustomerService = new ModelCustomerService();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.YEAR, year);
        int daysOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ModelRoomStatus modelRoomStatus = new ModelRoomStatus();
        for (int i = 1; i <= daysOfMonth; i++) {
            calendar.set(Calendar.DAY_OF_MONTH, i);
            dataset.addValue(
                    modelRoomStatus.getIncomeRoomByDay(new Date(calendar.getTimeInMillis()))
                            + modelCustomerService.getIncomeByDate(new Date(calendar.getTimeInMillis())),
                    "Ngy", String.valueOf(i));
        }
        pnCenter.add(getChartPanel("Thu nhp trong thng", "Thng", dataset), BorderLayout.CENTER);
    }

    private void drawDateChartQuarter(int quarter, int year) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ModelRoomStatus modelRoomStatus = new ModelRoomStatus();
        ModelCustomerService modelCustomerService = new ModelCustomerService();
        for (int i = 0; i < 3; i++) {
            dataset.addValue(
                    modelRoomStatus.getIncomeRoomByMonth(mQuarter[quarter - 1][i], year)
                            + modelCustomerService.getIncomeByMonth(mQuarter[quarter - 1][i], year),
                    "Qu " + quarter, "Thng " + mQuarter[quarter - 1][i]);
        }
        pnCenter.add(getChartPanel("Thu nhp qu", "Thng", dataset), BorderLayout.CENTER);
    }

    private void drawDateChartYear(int year) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ModelRoomStatus modelRoomStatus = new ModelRoomStatus();
        ModelCustomerService modelCustomerService = new ModelCustomerService();
        for (int i = 0; i < 4; i++) {
            dataset.addValue(
                    modelRoomStatus.getIncomeRoomByQuarter(i + 1, year)
                            + modelCustomerService.getIncomeByQuarter(i + 1, year),
                    "Nm " + year, "Qu " + (i + 1));
        }
        pnCenter.add(getChartPanel("Thu nhp nm", "Qu", dataset), BorderLayout.CENTER);
    }

    /**
     * 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() {

        pnFunction = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbTime = new javax.swing.JComboBox<>();
        pnFind = new javax.swing.JPanel();
        pnCenter = new javax.swing.JPanel();

        setBackground(new java.awt.Color(251, 250, 202));
        setLayout(new java.awt.BorderLayout());

        pnFunction.setBackground(new java.awt.Color(251, 250, 202));

        jPanel2.setBackground(new java.awt.Color(251, 250, 202));
        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5),
                javax.swing.BorderFactory.createTitledBorder(
                        new javax.swing.border.LineBorder(new java.awt.Color(0, 255, 204), 1, true),
                        "Khong th?i gian", javax.swing.border.TitledBorder.LEFT,
                        javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 11),
                        new java.awt.Color(0, 255, 204)))); // NOI18N

        jLabel1.setText("Th?i gian");

        cbTime.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Thng", "Qu", "Nm" }));
        cbTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTimeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(cbTime, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(cbTime, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        pnFind.setBackground(new java.awt.Color(251, 250, 202));
        pnFind.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout pnFunctionLayout = new javax.swing.GroupLayout(pnFunction);
        pnFunction.setLayout(pnFunctionLayout);
        pnFunctionLayout.setHorizontalGroup(pnFunctionLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnFind, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        pnFunctionLayout.setVerticalGroup(pnFunctionLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnFunctionLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0).addComponent(pnFind, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        add(pnFunction, java.awt.BorderLayout.LINE_END);

        pnCenter.setBackground(new java.awt.Color(251, 250, 202));
        pnCenter.setLayout(new java.awt.BorderLayout());
        add(pnCenter, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void cbTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTimeActionPerformed
        pnFind.removeAll();
        pnCenter.removeAll();
        Calendar now = Calendar.getInstance();
        switch (cbTime.getSelectedIndex()) {
        case 0:
            pnFind.add(mPnIncomeFindMonth, BorderLayout.CENTER);
            drawDateChartMonth(now.get(Calendar.MONTH) + 1, now.get(Calendar.YEAR));
            break;
        case 1:
            int quarter = (now.get(Calendar.MONTH) + 1) / 3 + 1;
            pnFind.add(mPnIncomeFindQuarter, BorderLayout.CENTER);
            drawDateChartQuarter(quarter, now.get(Calendar.YEAR));
            break;
        case 2:
            pnFind.add(mPnIncomeFindYear, BorderLayout.CENTER);
            drawDateChartYear(now.get(Calendar.YEAR));
            break;
        }
        pnFind.repaint();
        pnCenter.repaint();
    }//GEN-LAST:event_cbTimeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<String> cbTime;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel pnCenter;
    private javax.swing.JPanel pnFind;
    private javax.swing.JPanel pnFunction;
    // End of variables declaration//GEN-END:variables
}