UserInterface.AssetAdminRole.ManageMaintenaceJPanel.java Source code

Java tutorial

Introduction

Here is the source code for UserInterface.AssetAdminRole.ManageMaintenaceJPanel.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 UserInterface.AssetAdminRole;

import Business.Maintenance.MaintSchedule;
import Business.MedicalDevice.MedicalDevice;
import Business.Organization.AssetMgtOrganization;
import Business.Organization.Organization;
import java.awt.CardLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 *
 * @author Martin
 */
public class ManageMaintenaceJPanel extends javax.swing.JPanel {

    /**
     * Creates new form ManageMaintenaceJPanel
     */
    private JPanel userProcessContainer;
    private Organization organization;
    private MaintSchedule.MaintType maintType;

    public ManageMaintenaceJPanel(JPanel upc, Organization org) {
        initComponents();
        this.userProcessContainer = upc;
        this.organization = org;
        txtProblemDescrption.setVisible(false);
        problemJLabel.setVisible(false);
        buttonGroup.add(regularJRadioButton);
        buttonGroup.add(repairJRadioButton);
        regularJRadioButton.setSelected(true);
        maintType = MaintSchedule.MaintType.REG;

        refreshMaintTable();
    }

    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        maintenanceJTable = new javax.swing.JTable();
        backJButton = new javax.swing.JButton();
        maintJButton = new javax.swing.JButton();
        regularJRadioButton = new javax.swing.JRadioButton();
        repairJRadioButton = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtProblemDescrption = new javax.swing.JTextArea();
        problemJLabel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Manage Maintenance");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(13, 12, 0, 0);
        add(jLabel1, gridBagConstraints);

        maintenanceJTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null, null }, { null, null, null, null, null },
                        { null, null, null, null, null }, { null, null, null, null, null } },
                new String[] { "Device ID", "Last Maint Date", "Next Maint Date", "Days Left", "Status" }) {
            boolean[] canEdit = new boolean[] { false, false, true, false, false };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(maintenanceJTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 425;
        gridBagConstraints.ipady = 112;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(18, 12, 0, 33);
        add(jScrollPane1, gridBagConstraints);

        backJButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        backJButton.setText("<< Back");
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 12, 13, 0);
        add(backJButton, gridBagConstraints);

        maintJButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        maintJButton.setText("Request Maintenance");
        maintJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maintJButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 63, 13, 33);
        add(maintJButton, gridBagConstraints);

        regularJRadioButton.setText("Regular");
        regularJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regularJRadioButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 36, 0, 0);
        add(regularJRadioButton, gridBagConstraints);

        repairJRadioButton.setText("Repair");
        repairJRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repairJRadioButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 4, 0, 0);
        add(repairJRadioButton, gridBagConstraints);

        txtProblemDescrption.setColumns(20);
        txtProblemDescrption.setRows(5);
        txtProblemDescrption.setEnabled(false);
        jScrollPane2.setViewportView(txtProblemDescrption);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 425;
        gridBagConstraints.ipady = 47;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 12, 0, 33);
        add(jScrollPane2, gridBagConstraints);

        problemJLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        problemJLabel.setText("Problem Description");
        problemJLabel.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 12, 0, 0);
        add(problemJLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void maintJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maintJButtonActionPerformed
        // TODO add your handling code here:
        int selectedRow = maintenanceJTable.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "No device is selected");
            return;
        } else {
            MedicalDevice md = (MedicalDevice) maintenanceJTable.getValueAt(selectedRow, 0);
            if (md.getStatus().equals("In Maintenance")) {
                JOptionPane.showMessageDialog(null, "This equipment has not returned from maintenance!");
                return;
            }
            md.setStatus("In Maintenance");
            md.setLocation("Maintenance");
            md.getMaintScheduleHistory().newMaintenance();
            md.getMaintScheduleHistory().getLastMaintenace().setVender(md.getManufacturer());
            md.getMaintScheduleHistory().getLastMaintenace().setmType(maintType);
            md.getMaintScheduleHistory().getLastMaintenace().setProblemDesp(txtProblemDescrption.getText());
            ((AssetMgtOrganization) organization).getMaintRequestList().getMaintDevicesList().add(md);
            (md.getManufacturer()).getMaintRequestList().getMaintDevicesList().add(md);
        }
    }//GEN-LAST:event_maintJButtonActionPerformed

    private void repairJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repairJRadioButtonActionPerformed
        // TODO add your handling code here:
        problemJLabel.setEnabled(true);
        txtProblemDescrption.setEnabled(true);
        txtProblemDescrption.setVisible(true);
        problemJLabel.setVisible(true);
        maintType = MaintSchedule.MaintType.REP;
    }//GEN-LAST:event_repairJRadioButtonActionPerformed

    private void regularJRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regularJRadioButtonActionPerformed
        // TODO add your handling code here:
        problemJLabel.setEnabled(false);
        txtProblemDescrption.setEnabled(false);
        txtProblemDescrption.setVisible(false);
        problemJLabel.setVisible(false);
        maintType = MaintSchedule.MaintType.REG;
    }//GEN-LAST:event_regularJRadioButtonActionPerformed

    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed
        userProcessContainer.remove(this);
        CardLayout layout = (CardLayout) userProcessContainer.getLayout();
        layout.previous(userProcessContainer);
    }//GEN-LAST:event_backJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backJButton;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton maintJButton;
    private javax.swing.JTable maintenanceJTable;
    private javax.swing.JLabel problemJLabel;
    private javax.swing.JRadioButton regularJRadioButton;
    private javax.swing.JRadioButton repairJRadioButton;
    private javax.swing.JTextArea txtProblemDescrption;
    // End of variables declaration//GEN-END:variables

    private void refreshMaintTable() {
        DefaultTableModel dtm = (DefaultTableModel) maintenanceJTable.getModel();
        dtm.setRowCount(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date today = new Date();

        for (MedicalDevice md : ((AssetMgtOrganization) organization).getMedicalDeviceInventory()
                .getMedicalDeviceList()) {
            Object[] row = new Object[5];

            row[0] = md;
            row[1] = dateFormat.format(md.getMaintScheduleHistory().getLastMaintenace().getLastMaintDate());
            row[2] = dateFormat.format(md.getMaintScheduleHistory().getLastMaintenace().getNextMaintDate());
            if (today.after(md.getMaintScheduleHistory().getLastMaintenace().getNextMaintDate())) {
                row[3] = "Expired";
                md.setStatus("Out Of Service");
                row[4] = md.getStatus();
            } else {
                DateTime firstTime = new DateTime(
                        md.getMaintScheduleHistory().getLastMaintenace().getNextMaintDate());
                DateTime secondTime = new DateTime(today);
                int days = Days.daysBetween(new LocalDate(secondTime), new LocalDate(firstTime)).getDays();
                row[3] = days;
                row[4] = md.getStatus();
            }

            dtm.addRow(row);
        }
    }
}