View.Main.java Source code

Java tutorial

Introduction

Here is the source code for View.Main.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 Access.Connector;
import Access.DataHolder;
import Access.DataHolder.Data;
import Access.DataSet;
import Access.Downloader;
import Domain.ImageAnalyzer;
import java.awt.Color;
import java.awt.Image;
import java.awt.Label;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javafx.application.Platform.exit;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.json.JSONException;
import org.neuroph.core.NeuralNetwork;

/**
 *
 * @author Minudika
 */
public class Main extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    String nnetPath;
    String imagePath;
    NeuralNetwork nnet;
    ImageAnalyzer imageAnalizer;
    Downloader downloader;
    Connector connector;

    public Main() throws IOException {
        initComponents();

        downloader = new Downloader();
        connector = new Connector();

        setup();
    }

    private void setup() {
        jButton_loadImage.setEnabled(true);
        jButton_next.setEnabled(false);
        jButton_prev.setEnabled(false);

        jLabel_txtDoctorName.setVisible(false);
        jLabel_txtHospitalContact.setVisible(false);
        jLabel_txtHospitalName.setVisible(false);
        jButton_loadImage.setEnabled(false);

        File file = new File(Data.filePath_downloadNnet);
        if (!file.exists()) {
            Object[] options = { "Yes", "No" };
            int choice = JOptionPane.showOptionDialog(null, Data.message_modelNotFoundatLocal, "Error",
                    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);

            if (choice == 0) {
                try {
                    System.out.println(Data.modelUpdating);
                    downloader.download_nnet();
                    jButton_loadImage.setEnabled(true);
                    jLabel_modelVersion.setText(downloader.getModelVersion());
                    System.out.println(Data.modelUpdated);
                } catch (JSONException ex) {
                    System.out.println(Data.error_modelDownloadFailed);
                    JOptionPane.showMessageDialog(null, Data.error_modelDownloadFailed, "Error",
                            JOptionPane.ERROR_MESSAGE);
                    //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    System.out.println(Data.error_modelDownloadFailed);
                    JOptionPane.showMessageDialog(null, Data.error_modelDownloadFailed, "Error",
                            JOptionPane.ERROR_MESSAGE);
                    //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } else {
            jButton_loadImage.setEnabled(true);
        }

        imageAnalizer = new ImageAnalyzer(Data.filePath_downloadNnet);
        try {
            downloader.createDirectories();
        } catch (IOException ex) {
            System.out.println(Data.error_createDirectories);

            // Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

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

        jPanel2 = new javax.swing.JPanel();
        jLabel_displayImage = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton_loadImage = new javax.swing.JButton();
        jLabel_displayRating = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel_modelVersion = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel_newsFeed_displayImageBefore = new javax.swing.JLabel();
        jLabel_newsFeed_displayImageAfter = new javax.swing.JLabel();
        jLabel_txtDoctorName = new javax.swing.JLabel();
        jLabel_txtHospitalName = new javax.swing.JLabel();
        jLabel_txtHospitalContact = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton_next = new javax.swing.JButton();
        jButton_prev = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel_doctorName = new javax.swing.JLabel();
        jLabel_hospitalName = new javax.swing.JLabel();
        jLabel_hospitalContact = new javax.swing.JLabel();
        jProgressBar_rating = new javax.swing.JProgressBar();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel_status = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Dental XRay Image Analizer");
        setPreferredSize(new java.awt.Dimension(1280, 720));
        setResizable(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel_displayImage.setAutoscrolls(true);
        jLabel_displayImage.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Image");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2).addComponent(jLabel_displayImage,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 303,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel2Layout.createSequentialGroup().addGap(17, 17, 17).addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel_displayImage, javax.swing.GroupLayout.PREFERRED_SIZE, 303,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap()));

        jButton_loadImage.setText("Load Image");
        jButton_loadImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_loadImageActionPerformed(evt);
            }
        });

        jLabel_displayRating.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel_displayRating.setToolTipText("");
        jLabel_displayRating.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Rating");

        jLabel_modelVersion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Model Version :");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel_newsFeed_displayImageBefore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_newsFeed_displayImageBefore.setText("No News Available");
        jLabel_newsFeed_displayImageBefore.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel_newsFeed_displayImageAfter.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_newsFeed_displayImageAfter.setText("No News Available");
        jLabel_newsFeed_displayImageAfter.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel_txtDoctorName.setText("Doctor's Name          :");

        jLabel_txtHospitalName.setText("Hospital Name          :");

        jLabel_txtHospitalContact.setText("Hospital Contact No :");

        jLabel7.setText("Before");

        jLabel8.setText("After");

        jButton_next.setText("Next");
        jButton_next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_nextActionPerformed(evt);
            }
        });

        jButton_prev.setText("Previous");
        jButton_prev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_prevActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel8))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel_txtHospitalContact, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel_txtDoctorName, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel_txtHospitalName, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(10, 10, 10)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel_doctorName,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(170, 170, 170))
                                        .addComponent(jLabel_hospitalName, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel_hospitalContact, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel_newsFeed_displayImageBefore,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97,
                                        Short.MAX_VALUE)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel_newsFeed_displayImageAfter,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 164,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                jPanel1Layout.createSequentialGroup().addGap(437, 437, 437)
                                        .addComponent(jButton_prev, javax.swing.GroupLayout.PREFERRED_SIZE, 81,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(jButton_next,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 67,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7).addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel_newsFeed_displayImageBefore,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel_newsFeed_displayImageAfter,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 256,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout
                                        .createSequentialGroup()
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel_doctorName,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel_txtDoctorName))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel_txtHospitalName))
                                .addComponent(jLabel_hospitalName, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel_txtHospitalContact).addComponent(jLabel_hospitalContact,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(67, 67, 67)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton_prev).addComponent(jButton_next))
                        .addGap(18, 18, 18).addComponent(jLabel9).addContainerGap(34, Short.MAX_VALUE)));

        jProgressBar_rating.setToolTipText("");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setText("Critical");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setText("Normal");

        jLabel_status.setToolTipText("status");
        jLabel_status.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel12.setText("Status");

        jLabel3.setText("News Feed");

        jMenu1.setText("File");

        jMenuItem5.setText("Exit");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Option");

        jMenuItem4.setText("Update Model");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuItem1.setText("SURF Match");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                                .createSequentialGroup().addGroup(layout.createParallelGroup(
                                        javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                layout.createParallelGroup(
                                                        javax.swing.GroupLayout.Alignment.LEADING, false).addGroup(
                                                                layout.createSequentialGroup().addComponent(jLabel1)
                                                                        .addGap(393, 393, 393))
                                                        .addGroup(layout
                                                                .createSequentialGroup().addComponent(jPanel2,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        Short.MAX_VALUE)
                                                                .addGap(80, 80, 80)))
                                        .addGroup(layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout
                                                        .createSequentialGroup()
                                                        .addComponent(jLabel_displayRating,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 153,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(jButton_loadImage))
                                                .addGroup(layout.createSequentialGroup().addComponent(jLabel10)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(jLabel11))
                                                .addComponent(jProgressBar_rating,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 344,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel_status, javax.swing.GroupLayout.PREFERRED_SIZE, 405,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel12))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3)))
                                .addGroup(layout.createSequentialGroup().addComponent(jLabel4).addGap(18, 18, 18)
                                        .addComponent(jLabel_modelVersion, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4).addComponent(jLabel_modelVersion,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(
                                                        jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        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(jButton_loadImage).addGap(24, 24, 24))
                                                .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jLabel_displayRating,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(11, 11, 11)))
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel10).addComponent(jLabel11))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jProgressBar_rating,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(29, 29, 29)
                                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(41, 41, 41).addComponent(jLabel12)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel_status, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(147, 147, 147)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_loadImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_loadImageActionPerformed
        // TODO add your handling code here: 
        jLabel_status.setText(" ");
        JFileChooser fc = new JFileChooser();
        Double rating;
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.addChoosableFileFilter(new FileNameExtensionFilter("JPEG files", "jpeg", "jpg"));
        fc.addChoosableFileFilter(new FileNameExtensionFilter("PNG files", "png", "PNG"));
        fc.addChoosableFileFilter(new FileNameExtensionFilter("BMP files", "bmp", "BMP"));
        fc.setAcceptAllFileFilterUsed(true);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            imagePath = fc.getSelectedFile().getAbsolutePath();
            rating = imageAnalizer.getRating(imagePath);
            setTextRating(rating);
            populateProgressBar(rating);
            try {
                setImage(imagePath);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Please select an image of valid format", "Error",
                        JOptionPane.WARNING_MESSAGE);
                //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

            getNews(rating);

        }
        //        else {
        //            JOptionPane.showMessageDialog(null, "Please select an image of valid format", "Error", JOptionPane.WARNING_MESSAGE);
        //        }    
    }//GEN-LAST:event_jButton_loadImageActionPerformed

    public void populateProgressBar(double rating) {
        int r = (int) (rating * 100);
        jProgressBar_rating.setValue(r);
    }

    private void getNews(double rating) {
        setStatus(Data.newsDownloading);
        System.out.println(Data.newsDownloading);
        boolean downloaded = false;
        try {
            nameList = downloader.downloadImageSet(rating);
            System.out.println(Data.newsDownloaded);

            downloaded = true;
            setStatus(Data.newsDownloaded);

        } catch (JSONException ex) {
            setStatus(Data.newsDownloadFailed);
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(Data.error_newsImagesDownloadFailed);

        } catch (IOException ex) {
            setStatus(Data.newsDownloadFailed);
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(Data.error_newsImagesDownloadFailed);

        }

        if (downloaded) {
            setData("start");
        } else {
            clearNewsFeed();
        }
    }

    int cnt = 0;
    int currentImageLocation = 0;

    private void jButton_nextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_nextActionPerformed
        // TODO add your handling code here:
        System.out.println(cnt);
        setData("next");

    }//GEN-LAST:event_jButton_nextActionPerformed

    private void jButton_prevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_prevActionPerformed
        // TODO add your handling code here:
        System.out.println(cnt);
        setData("prev");
    }//GEN-LAST:event_jButton_prevActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        boolean check = false;
        setStatus(Data.modelUpdating);
        System.out.println(Data.modelUpdating);
        try {
            check = downloader.download_nnet();

        } catch (JSONException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(Data.error_modelDownloadFailed);

        } catch (IOException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(Data.error_modelDownloadFailed);
        }
        if (check) {
            setStatus(Data.modelUpdated);
            imageAnalizer = new ImageAnalyzer(Data.filePath_downloadNnet);
            jLabel_modelVersion.setText(downloader.getModelVersion());
            setStatus(Data.modelUpdated);
            System.out.println(Data.modelUpdated);
        } else {
            setStatus(Data.modelUpdateFailed);
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        try {
            // TODO add your handling code here:
            Runtime.getRuntime().exec("./Release/SURF.exe", null);
        } catch (Exception e) {

        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void setStatus(String s) {
        jLabel_status.setText(s);
    }

    ArrayList<String> nameList = null;

    private void clearNewsFeed() {
        jButton_next.setEnabled(false);
        jButton_prev.setEnabled(false);

        jLabel_txtDoctorName.setVisible(false);
        jLabel_txtHospitalContact.setVisible(false);
        jLabel_txtHospitalName.setVisible(false);

        jLabel_hospitalName.setVisible(false);
        jLabel_hospitalContact.setVisible(false);
        jLabel_hospitalName.setVisible(false);
    }

    public void setData(String btnOption) {
        ArrayList<DataSet> dataset = downloader.getDataset();
        ArrayList<String> nameList = downloader.getNameList();

        if (dataset.size() == 0) {
            jLabel_txtDoctorName.setVisible(false);
            jLabel_txtHospitalContact.setVisible(false);
            jLabel_txtHospitalName.setVisible(false);

            jButton_next.setEnabled(false);
            jButton_prev.setEnabled(false);
        } else {
            jLabel_txtDoctorName.setVisible(true);
            jLabel_txtHospitalContact.setVisible(true);
            jLabel_txtHospitalName.setVisible(true);

            jButton_next.setEnabled(true);
            jButton_prev.setEnabled(true);
        }

        if (btnOption.toLowerCase().equals("start")) {
            cnt = 0;
        } else if (btnOption.toLowerCase().equals("next")) {
            cnt++;
        } else {
            cnt--;
        }

        if (cnt >= dataset.size()) {
            cnt = 0;
        } else if (cnt < 0) {
            cnt = dataset.size() - 1;
        }

        System.out.println("dataset size :" + dataset.size());

        if (dataset.size() > 1) {
            jLabel_doctorName.setText(dataset.get(cnt).getDoctorName());

            jLabel_hospitalName.setText(dataset.get(cnt).getHospitalName());
            jLabel_hospitalContact.setText(dataset.get(cnt).getHospitalContact());

            String imageA = Data.directoryPath_downloadNews + nameList.get(2 * cnt);
            String imageB = Data.directoryPath_downloadNews + nameList.get(2 * cnt + 1);
            System.out.println(imageA);

            File imageFileA = new File(imageA);
            File imageFileB = new File(imageB);

            BufferedImage bufImgA = null;
            BufferedImage bufImgB = null;

            try {
                bufImgA = ImageIO.read(imageFileA);
                bufImgB = ImageIO.read(imageFileB);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Image scaledImgA = bufImgA.getScaledInstance(jLabel_newsFeed_displayImageAfter.getWidth(),
                    jLabel_newsFeed_displayImageAfter.getHeight(), Image.SCALE_DEFAULT);
            Image scaledImgB = bufImgB.getScaledInstance(jLabel_newsFeed_displayImageBefore.getWidth(),
                    jLabel_newsFeed_displayImageBefore.getHeight(), Image.SCALE_DEFAULT);

            jLabel_newsFeed_displayImageAfter.setIcon(new ImageIcon(scaledImgA));
            jLabel_newsFeed_displayImageBefore.setIcon(new ImageIcon(scaledImgB));
        }
    }

    private void setImage(String path) throws IOException {
        BufferedImage bi = ImageIO.read(new File(path));
        Image scaledImg = bi.getScaledInstance(jLabel_displayImage.getWidth(), jLabel_displayImage.getHeight(),
                Image.SCALE_DEFAULT);
        jLabel_displayImage.setIcon(new ImageIcon(scaledImg));
        jLabel_displayImage.setHorizontalAlignment(JLabel.CENTER);
        jLabel_displayImage.setVerticalAlignment(JLabel.CENTER);
    }

    private void setTextRating(double r) {
        String rating = Double.toString(r);
        jLabel_displayRating.setText(rating);
        jLabel_displayRating.setHorizontalAlignment(JLabel.CENTER);
        jLabel_displayRating.setVerticalAlignment(JLabel.CENTER);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Main().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_loadImage;
    private javax.swing.JButton jButton_next;
    private javax.swing.JButton jButton_prev;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabel_displayImage;
    private javax.swing.JLabel jLabel_displayRating;
    private javax.swing.JLabel jLabel_doctorName;
    private javax.swing.JLabel jLabel_hospitalContact;
    private javax.swing.JLabel jLabel_hospitalName;
    private javax.swing.JLabel jLabel_modelVersion;
    private javax.swing.JLabel jLabel_newsFeed_displayImageAfter;
    private javax.swing.JLabel jLabel_newsFeed_displayImageBefore;
    private javax.swing.JLabel jLabel_status;
    private javax.swing.JLabel jLabel_txtDoctorName;
    private javax.swing.JLabel jLabel_txtHospitalContact;
    private javax.swing.JLabel jLabel_txtHospitalName;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar jProgressBar_rating;
    // End of variables declaration//GEN-END:variables
}