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 UserInterface.PDCPrimaryDoctorRole; import Business.Employee.Patient; import Business.Employee.PrimaryDoctor; import Business.PatientAttributes.VitalSigns; import Business.UserAccount.UserAccount; import Business.Utils.Methods; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.lang.reflect.Field; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javafx.scene.chart.NumberAxis; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.ValueMarker; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.RectangleAnchor; import org.jfree.ui.TextAnchor; /** * * @author Mohit */ public class PDCPrimaryDoctorReportsJPanel extends javax.swing.JPanel { private UserAccount userAccount; private JPanel userProcessContainer; /** * Creates new form PDCPrimaryDoctorReportsJPanel1 */ public PDCPrimaryDoctorReportsJPanel(JPanel userProcessContainer, UserAccount userAccount) { initComponents(); this.userAccount = userAccount; this.userProcessContainer = userProcessContainer; populateComboBox(); } /** * 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() { jLabel1 = new javax.swing.JLabel(); patientListComboBox = new javax.swing.JComboBox(); reportJPanel = new javax.swing.JPanel(); generateReportToggleBtn = new javax.swing.JToggleButton(); vitalSignComboBox = new javax.swing.JComboBox(); backBtn = new javax.swing.JButton(); setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Select Patient: "); patientListComboBox.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); patientListComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { patientListComboBoxActionPerformed(evt); } }); reportJPanel.setBackground(new java.awt.Color(255, 255, 255)); reportJPanel.setPreferredSize(new java.awt.Dimension(200, 200)); reportJPanel.setLayout(new java.awt.BorderLayout()); generateReportToggleBtn.setText("Generate Live Report"); generateReportToggleBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateReportToggleBtnActionPerformed(evt); } }); vitalSignComboBox.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); backBtn.setText("Back"); backBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(61, 61, 61).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(backBtn) .addComponent(reportJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(vitalSignComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(generateReportToggleBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(patientListComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(229, 229, 229))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(20, 20, 20).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(patientListComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(5, 5, 5) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(vitalSignComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(generateReportToggleBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(reportJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(55, 55, 55).addComponent(backBtn).addGap(46, 46, 46))); }// </editor-fold>//GEN-END:initComponents private void patientListComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patientListComboBoxActionPerformed // TODO add your handling code here: }//GEN-LAST:event_patientListComboBoxActionPerformed private void generateReportToggleBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateReportToggleBtnActionPerformed // TODO add your handling code here: if (!generateReportToggleBtn.isSelected()) { reportJPanel.removeAll(); //reportJPanel.revalidate(); reportJPanel.repaint(); generateReportToggleBtn.setText("Generate Live Graph"); } else { generateReportToggleBtn.setText("Stop Live Graph"); } ScheduledExecutorService scheduledExecutorService2 = Executors.newScheduledThreadPool(5); Runnable task = new Runnable() { @Override public void run() { if (generateReportToggleBtn.isSelected()) { Patient patient = (Patient) patientListComboBox.getSelectedItem(); String attribute = (String) vitalSignComboBox.getSelectedItem(); Methods.inputVitalSigns(patient); populateGraphs(patient, attribute); } else { scheduledExecutorService2.shutdown(); } } }; ScheduledFuture scheduledTask; scheduledTask = scheduledExecutorService2.scheduleAtFixedRate(task, 1, 10, TimeUnit.SECONDS); }//GEN-LAST:event_generateReportToggleBtnActionPerformed private void backBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBtnActionPerformed // TODO add your handling code here: userProcessContainer.remove(this); CardLayout layout = (CardLayout) userProcessContainer.getLayout(); layout.previous(userProcessContainer); }//GEN-LAST:event_backBtnActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton backBtn; private javax.swing.JToggleButton generateReportToggleBtn; private javax.swing.JLabel jLabel1; private javax.swing.JComboBox patientListComboBox; private javax.swing.JPanel reportJPanel; private javax.swing.JComboBox vitalSignComboBox; // End of variables declaration//GEN-END:variables private void populateComboBox() { patientListComboBox.removeAllItems(); PrimaryDoctor primaryDoctor = (PrimaryDoctor) userAccount.getEmployee(); for (Patient patient : primaryDoctor.getPrimaryDoctorsPatientList()) { patientListComboBox.addItem(patient); } vitalSignComboBox.removeAllItems(); String attribute[] = { "ECG", "HEART RATE", "RESPIRATORY RATE", "SYSTOLIC BLOOD PRESSURE" }; for (int i = 0; i < 4; i++) { vitalSignComboBox.addItem(attribute[i]); } } private void populateGraphs(Patient patient, String attribute) { int lowerNormal = 0; int higherNormal = 0; int yAxis = 0; DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset(); for (VitalSigns vitalSigns : patient.getVitalSignsHistory().getVitalSignsHistory()) { if (attribute.equalsIgnoreCase("ECG")) { yAxis = vitalSigns.getEcg(); lowerNormal = 46; higherNormal = 50; } else if (attribute.equalsIgnoreCase("HEART RATE")) { yAxis = vitalSigns.getHeartRate(); lowerNormal = 60; higherNormal = 90; } else if (attribute.equalsIgnoreCase("RESPIRATORY RATE")) { yAxis = vitalSigns.getHeartRate(); lowerNormal = 12; higherNormal = 25; } else if (attribute.equalsIgnoreCase("WEIGHT IN KG")) { yAxis = vitalSigns.getHeartRate(); lowerNormal = 80; higherNormal = 85; } else if (attribute.equalsIgnoreCase("SYSTOLIC BLOOD PRESSURE")) { yAxis = vitalSigns.getHeartRate(); lowerNormal = 90; higherNormal = 120; } Date currentTimeStamp = new Date(); int currentTime = (int) (currentTimeStamp.getTime()) / (1000 * 60); int vitalSignsTime = (int) vitalSigns.getTimeStamp().getTime() / (1000 * 60); if (currentTime - vitalSignsTime <= 1) { line_chart_dataset.addValue(yAxis, attribute, vitalSigns.getTimeStamp()); } } JFreeChart lineChart = ChartFactory.createLineChart(attribute + " Over Time", "Time", attribute, line_chart_dataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot pi = lineChart.getCategoryPlot(); pi.setRangeGridlinePaint(Color.WHITE); pi.getRenderer().setSeriesPaint(0, Color.GREEN); pi.setBackgroundPaint(Color.BLACK); ValueMarker marker = new ValueMarker(lowerNormal); marker.setLabel("Normal Range"); marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); marker.setLabelPaint(Color.WHITE); marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT); marker.setPaint(Color.CYAN); pi.addRangeMarker(marker); ValueMarker marker2 = new ValueMarker(higherNormal); marker2.setLabel("Normal Range"); marker2.setLabelPaint(Color.WHITE); marker2.setLabelAnchor(RectangleAnchor.TOP_RIGHT); marker2.setLabelTextAnchor(TextAnchor.TOP_RIGHT); marker2.setPaint(Color.CYAN); pi.addRangeMarker(marker2); reportJPanel.setLayout(new java.awt.BorderLayout()); ChartPanel panel = new ChartPanel(lineChart); reportJPanel.add(panel, BorderLayout.CENTER); reportJPanel.validate(); } }