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.cdcRole; import business.Vaccine; import business.enterprise.CdcEnterprise; import business.enterprise.Enterprise; import business.enterprise.PhdEnterprise; import business.inventory.Inventory; import business.network.Network; import business.organization.DistributorOrganization; import business.organization.HospitalOrganization; import business.organization.ManufactureOrganization; import business.organization.Organization; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; import javax.swing.JPanel; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Dev */ public class OverviewJPanel extends javax.swing.JPanel { //CdcEnterprise enterprise; /** * Creates new form OverviewJPanel */ JPanel userProcessContainer; CdcEnterprise cdcEnterprise; Network n; public OverviewJPanel(JPanel userProcessContainer, CdcEnterprise cdcEnterprise, Network n) { initComponents(); this.userProcessContainer = userProcessContainer; this.cdcEnterprise = cdcEnterprise; this.n = n; jComboBox1.removeAllItems(); for (Network n1 : n.getStateList()) { for (Enterprise e : n1.getEnterpriseDirectory().getEnterpriseList()) { if (e instanceof PhdEnterprise) { jComboBox1.addItem(e); } } } //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private static JFreeChart createChart1(CategoryDataset categorydataset) { JFreeChart jfreechart = ChartFactory.createBarChart("Distribution of Vaccines", "Vaccine", "Quantity", categorydataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setDomainGridlinesVisible(true); categoryplot.setRangeCrosshairVisible(true); categoryplot.setRangeCrosshairPaint(Color.green); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); barrenderer.setDrawBarOutline(false); GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 0, 64)); GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0)); GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0)); barrenderer.setSeriesPaint(0, gradientpaint); barrenderer.setSeriesPaint(1, gradientpaint1); barrenderer.setSeriesPaint(2, gradientpaint2); barrenderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator("Tooltip: {0}")); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); categoryaxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(0.52359877559829882D)); return jfreechart; } private CategoryDataset createDataset1(PhdEnterprise p) { DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); String s = "Quantity Available"; for (Organization o : cdcEnterprise.getOrganizationDirectory().getOrganizationList()) { if (o instanceof DistributorOrganization) { for (Organization o1 : o.getOrganizationDirectory().getOrganizationList()) { if (o1 instanceof ManufactureOrganization) { ManufactureOrganization mo = (ManufactureOrganization) o1; for (Vaccine v : mo.getProductCatalog()) { for (Network n1 : n.getStateList()) { for (Enterprise e : n1.getEnterpriseDirectory().getEnterpriseList()) { if (e instanceof PhdEnterprise) { if (e.equals(p)) { PhdEnterprise pd = (PhdEnterprise) e; int a = pd.getTotalInState(v); defaultcategorydataset.addValue(a, s, v.getName()); } } } } } } } } } return defaultcategorydataset; } /** * 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. */ public void populate(PhdEnterprise p) { DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel(); dtm.setRowCount(0); for (Organization o : p.getOrganizationDirectory().getOrganizationList()) { if (o instanceof HospitalOrganization) { Object row[] = new Object[1]; row[0] = o; dtm.addRow(row); } } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jTable2 = new javax.swing.JTable(); jButton1 = new javax.swing.JButton(); setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setLayout(new java.awt.CardLayout()); jComboBox1.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jTable1.setModel( new javax.swing.table.DefaultTableModel(new Object[][] { { null } }, new String[] { "Hospials" })); jScrollPane1.setViewportView(jTable1); jButton2.setText(">>"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jTable2.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { null, null } }, new String[] { "Vaccine", "Inventory" })); jScrollPane2.setViewportView(jTable2); jButton1.setText(">>"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(618, 618, 618).addComponent( jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 695, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButton2).addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButton1).addGap(18, 18, 18) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(1304, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(11, 11, 11) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(48, 48, 48) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 468, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(46, 46, 46)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(194, 194, 194)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(193, 193, 193)))))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed PhdEnterprise p = (PhdEnterprise) jComboBox1.getSelectedItem(); JFreeChart jfreechart1 = createChart1(createDataset1(p)); ChartPanel chartpanel1 = new ChartPanel(jfreechart1); chartpanel1.setPreferredSize(new Dimension(1000, 540)); jPanel1.add(chartpanel1); CardLayout layout1 = (CardLayout) jPanel1.getLayout(); layout1.next(jPanel1); //populate(p); }//GEN-LAST:event_jComboBox1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed PhdEnterprise p = (PhdEnterprise) jComboBox1.getSelectedItem(); populate(p); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed int s = jTable1.getSelectedRow(); DefaultTableModel dtm = (DefaultTableModel) jTable2.getModel(); dtm.setRowCount(0); if (s < 0) { return; } else { HospitalOrganization o = (HospitalOrganization) jTable1.getValueAt(s, 0); for (Inventory i : o.getInventoryCatalog().getInventoryList()) { Object row[] = new Object[2]; row[0] = i.getVaccine().getName(); row[1] = i.getTotalQuantity(); dtm.addRow(row); } } }//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.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JTable jTable2; // End of variables declaration//GEN-END:variables }