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.FarmerRole; import Business.Inventory.InventoryItem; import Business.Sensors.HumiditySensor; import Business.Sensors.TemperatureSensor; import Business.UserAccount.UserAccount; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.DialShape; import org.jfree.chart.plot.JThermometer; import org.jfree.chart.plot.MeterInterval; import org.jfree.chart.plot.MeterPlot; import org.jfree.data.Range; import org.jfree.data.general.DefaultValueDataset; import org.jfree.data.general.ValueDataset; /** * * @author user */ public class MyDashboardFarmer extends javax.swing.JPanel { /** * Creates new form MyDashboardFarmer */ private JPanel userProcessContainer; private UserAccount userAccount; public MyDashboardFarmer(JPanel userProcessContainer, UserAccount userAccount) { initComponents(); this.userProcessContainer = userProcessContainer; this.userAccount = userAccount; populateLowQuantityTable(); } public void populateLowQuantityTable() { DefaultTableModel dtm = (DefaultTableModel) lowQuantityTable.getModel(); dtm.setRowCount(0); for (InventoryItem item : userAccount.getInventory().getInventoryList()) { if (item.getQuantity() < item.getThreshold()) { Object row[] = new Object[3]; row[0] = item; row[1] = item.getThreshold(); row[2] = item.getQuantity(); dtm.addRow(row); } } } /** * 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() { jLabel3 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); tempBtn = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); pathTxtField = new javax.swing.JTextField(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); lowQuantityTable = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); setBackground(new java.awt.Color(255, 255, 255)); jLabel3.setFont(new java.awt.Font("Trebuchet MS", 3, 26)); // NOI18N jLabel3.setForeground(new java.awt.Color(255, 0, 0)); jLabel3.setText("Inventory DashBoard"); jButton2.setBackground(new java.awt.Color(51, 51, 51)); jButton2.setFont(new java.awt.Font("Trebuchet MS", 3, 14)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 255, 255)); jButton2.setText("<< Back"); jButton2.setBorder(new javax.swing.border.MatteBorder(null)); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Temperature Sensor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14), new java.awt.Color(255, 0, 0))); // NOI18N tempBtn.setBackground(new java.awt.Color(51, 51, 51)); tempBtn.setFont(new java.awt.Font("Trebuchet MS", 3, 14)); // NOI18N tempBtn.setForeground(new java.awt.Color(255, 255, 255)); tempBtn.setText("Inventory Temperature"); tempBtn.setBorder(new javax.swing.border.MatteBorder(null)); tempBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tempBtnActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N jLabel2.setText("Path:"); pathTxtField.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N pathTxtField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pathTxtFieldActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap().addComponent(jLabel2).addGap(18, 18, 18) .addComponent(pathTxtField, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(tempBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(15, 15, 15).addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(pathTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2).addComponent(tempBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(28, 28, 28))); jPanel2.setBackground(new java.awt.Color(255, 255, 255)); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Humidity Sensor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14), new java.awt.Color(255, 0, 0))); // NOI18N jButton1.setBackground(new java.awt.Color(51, 51, 51)); jButton1.setFont(new java.awt.Font("Trebuchet MS", 3, 14)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 255, 255)); jButton1.setText("Inventory Humidity"); jButton1.setBorder(new javax.swing.border.MatteBorder(null)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N jLabel4.setText("Path:"); jTextField1.setFont(new java.awt.Font("Trebuchet MS", 0, 14)); // NOI18N javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap().addComponent(jLabel4).addGap(18, 18, 18) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(16, 16, 16).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27))); lowQuantityTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Product Name", "Threshold", "Availability" }) { boolean[] canEdit = new boolean[] { false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(lowQuantityTable); jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N jLabel1.setText("Below Threshold Products:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(89, 89, 89) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(232, 232, 232) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(282, 282, 282).addComponent(jLabel3))) .addGap(526, 526, 526))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(jLabel3).addGap(18, 18, 18) .addComponent(jLabel1).addGap(4, 4, 4) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20))); }// </editor-fold>//GEN-END:initComponents private void tempBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tempBtnActionPerformed String csvFile = "C:/Users/user/Desktop/FinalProject/src/UserInterface/FarmerRole/TemperatureFarmer.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = ","; try { br = new BufferedReader(new FileReader(csvFile)); while ((line = br.readLine()) != null) { // use comma as separator String[] country = line.split(cvsSplitBy); TemperatureSensor ts = userAccount.getInventory().getTemperatureSensorDirectory() .addTemp(Double.parseDouble(country[0]), country[1]); } //loop map JFrame f = new JFrame("Inventory Temperature"); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(1, 0)); for (TemperatureSensor t : userAccount.getInventory().getTemperatureSensorDirectory() .getTempSensorList()) { f.add(new ThermometerDemo(t.getCurrentTemperature(), t.getPerishability())); } f.pack(); f.setVisible(true); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }//GEN-LAST:event_tempBtnActionPerformed private void pathTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathTxtFieldActionPerformed // TODO add your handling code here: }//GEN-LAST:event_pathTxtFieldActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String csvFile = "C:/Users/user/Desktop/FinalProject/src/UserInterface/FarmerRole/HumidityFarmer.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = ","; try { br = new BufferedReader(new FileReader(csvFile)); while ((line = br.readLine()) != null) { // use comma as separator String[] country = line.split(cvsSplitBy); HumiditySensor hs = userAccount.getInventory().getHumiditySensorDirectory() .addHumidity(Double.parseDouble(country[0]), country[1]); } JFrame f = new JFrame("Inventory Humidity"); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(1, 0)); for (HumiditySensor t : userAccount.getInventory().getHumiditySensorDirectory() .getHumiditySensorList()) { f.add(new HumidityGraph(t.getCurrentHumidity(), t.getType())); } f.pack(); f.setVisible(true); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } //HumidityGraph hg = new HumidityGraph(20); }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField jTextField1; private javax.swing.JTable lowQuantityTable; private javax.swing.JTextField pathTxtField; private javax.swing.JButton tempBtn; // End of variables declaration//GEN-END:variables }