/*
 * 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 techquiz.gui;

import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import techquiz.DAO.QuestionDAO;
import techquiz.POJO.ExamPOJO;
import techquiz.POJO.Question;
import techquiz.POJO.QuestionStore;
import techquiz.POJO.UserProfilePOJO;

/**
 *
 * @author Lenovo
 */
public class EditQuestionsFrame extends javax.swing.JFrame {

    Color oldColor = new Color(255,51,51);
    JFrame fr;                              //Showing Polymorphism .
    
    private ExamPOJO editExam;
    private QuestionStore qstore;
    private HashMap <String,String> options;
    private int qno,pos=0;
    private String question,option1,option2,option3,option4,correctOption;

    
    public EditQuestionsFrame() 
    {
        initComponents();
        this.setLocationRelativeTo(null);
        lblEditQuestionsUser.setText("Hello "+UserProfilePOJO.getUsername());
        qstore=new QuestionStore();
        options=new HashMap<>();
        options.put("Option 1","Answer1");
        options.put("Option 2","Answer2");
        options.put("Option 3","Answer3");
        options.put("Option 4","Answer4");
        qno=1;
        lblEditQuestionsQno.setText(lblEditQuestionsQno.getText()+qno);


    }
    
    public EditQuestionsFrame(ExamPOJO editExam) 
    {
        this();
        this.editExam=editExam;
        lblEditQuestions.setText("EDITING "+editExam.getLanguage().toUpperCase()+ " PAPER");        
        loadQuestions();
        showQuestion();
    }

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

        jPanel1 = new javax.swing.JPanel();
        lblEditQuestionsUser = new javax.swing.JLabel();
        lblEditQuestions = new javax.swing.JLabel();
        lblEditQuestionsLogOut = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblEditQuestionsQno = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtEditQuestions = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        txtEditQuestionsOption1 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtEditQuestionsOption2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtEditQuestionsOption3 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtEditQuestionsOption4 = new javax.swing.JTextField();
        jcEditQuestionsCorrectOption = new javax.swing.JComboBox<>();
        btnEditQuestionsDone = new javax.swing.JButton();
        btnEditQuestionsCancel = new javax.swing.JButton();
        btnEditQuestionsNext = new javax.swing.JButton();
        btnEditQuestionsPrevious = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        lblEditQuestionsUser.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        lblEditQuestionsUser.setForeground(new java.awt.Color(255, 51, 0));

        lblEditQuestions.setFont(new java.awt.Font("sansserif", 1, 18)); // NOI18N
        lblEditQuestions.setForeground(new java.awt.Color(255, 51, 0));
        lblEditQuestions.setText(" 1 QUESTIONS REMAINING");

        lblEditQuestionsLogOut.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        lblEditQuestionsLogOut.setForeground(new java.awt.Color(255, 51, 0));
        lblEditQuestionsLogOut.setText("Log Out");
        lblEditQuestionsLogOut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblEditQuestionsLogOutMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblEditQuestionsLogOutMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblEditQuestionsLogOutMouseExited(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/techquiz/img/questionpaper4.jpg"))); // NOI18N

        lblEditQuestionsQno.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        lblEditQuestionsQno.setForeground(new java.awt.Color(255, 51, 0));
        lblEditQuestionsQno.setText("Question No : ");

        txtEditQuestions.setColumns(20);
        txtEditQuestions.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        txtEditQuestions.setForeground(new java.awt.Color(255, 51, 51));
        txtEditQuestions.setRows(5);
        jScrollPane1.setViewportView(txtEditQuestions);

        jLabel7.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 51, 0));
        jLabel7.setText("Option 1");

        txtEditQuestionsOption1.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        txtEditQuestionsOption1.setForeground(new java.awt.Color(255, 51, 51));

        jLabel6.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 51, 0));
        jLabel6.setText("Option 2");

        txtEditQuestionsOption2.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        txtEditQuestionsOption2.setForeground(new java.awt.Color(255, 51, 51));

        jLabel8.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 51, 0));
        jLabel8.setText("Option 3");

        txtEditQuestionsOption3.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        txtEditQuestionsOption3.setForeground(new java.awt.Color(255, 51, 51));

        jLabel10.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(255, 51, 0));
        jLabel10.setText("Correct Answer");

        jLabel9.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(255, 51, 0));
        jLabel9.setText("Option 4");

        txtEditQuestionsOption4.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        txtEditQuestionsOption4.setForeground(new java.awt.Color(255, 51, 51));

        jcEditQuestionsCorrectOption.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jcEditQuestionsCorrectOption.setForeground(new java.awt.Color(255, 51, 51));
        jcEditQuestionsCorrectOption.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Option 1", "Option 2", "Option 3", "Option 4" }));

        btnEditQuestionsDone.setBackground(new java.awt.Color(0, 0, 0));
        btnEditQuestionsDone.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        btnEditQuestionsDone.setForeground(new java.awt.Color(255, 51, 0));
        btnEditQuestionsDone.setText("Done");
        btnEditQuestionsDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditQuestionsDoneActionPerformed(evt);
            }
        });

        btnEditQuestionsCancel.setBackground(new java.awt.Color(0, 0, 0));
        btnEditQuestionsCancel.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        btnEditQuestionsCancel.setForeground(new java.awt.Color(255, 51, 0));
        btnEditQuestionsCancel.setText("Cancel");
        btnEditQuestionsCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditQuestionsCancelActionPerformed(evt);
            }
        });

        btnEditQuestionsNext.setBackground(new java.awt.Color(0, 0, 0));
        btnEditQuestionsNext.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        btnEditQuestionsNext.setForeground(new java.awt.Color(255, 51, 0));
        btnEditQuestionsNext.setText("Next");
        btnEditQuestionsNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditQuestionsNextActionPerformed(evt);
            }
        });

        btnEditQuestionsPrevious.setBackground(new java.awt.Color(0, 0, 0));
        btnEditQuestionsPrevious.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        btnEditQuestionsPrevious.setForeground(new java.awt.Color(255, 51, 0));
        btnEditQuestionsPrevious.setText("Previous");
        btnEditQuestionsPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditQuestionsPreviousActionPerformed(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()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(lblEditQuestionsQno)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(189, 189, 189)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtEditQuestionsOption1)
                                .addComponent(jLabel8)
                                .addComponent(txtEditQuestionsOption3, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                            .addComponent(jLabel7)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(76, 76, 76)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEditQuestionsOption4)
                            .addComponent(jcEditQuestionsCorrectOption, 0, 143, Short.MAX_VALUE)
                            .addComponent(txtEditQuestionsOption2)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel9))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addGap(35, 35, 35))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblEditQuestionsUser, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblEditQuestions)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblEditQuestionsLogOut, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(209, 209, 209)
                .addComponent(btnEditQuestionsNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEditQuestionsPrevious, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnEditQuestionsCancel)
                .addGap(18, 18, 18)
                .addComponent(btnEditQuestionsDone)
                .addGap(58, 58, 58))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(216, 216, 216))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(lblEditQuestionsUser, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblEditQuestionsLogOut, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(183, 183, 183))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lblEditQuestions, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEditQuestionsQno, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEditQuestionsOption1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel8))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEditQuestionsOption2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtEditQuestionsOption3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtEditQuestionsOption4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jcEditQuestionsCorrectOption, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEditQuestionsNext)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnEditQuestionsDone)
                        .addComponent(btnEditQuestionsCancel)
                        .addComponent(btnEditQuestionsPrevious)))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

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

    // Loading Questions .
    public void loadQuestions()
    {
        try
        {
            ArrayList <Question> questionList = QuestionDAO.getQuestionsByExamId(editExam.getExamId());
            for(Question obj : questionList)
            {
                qstore.addQuestion(obj);
            }
        }
        
        catch(SQLException ex)
        {
            JOptionPane.showMessageDialog(null, "Error while connecting to DB!","Edit Questions!",JOptionPane.ERROR_MESSAGE);
        }

    }
    
    public void showQuestion()                                  //NullPointerException
    {
        Question question=qstore.getQuestionByQno(qno);
        if(question==null)
        {
            
        }
        txtEditQuestions.setText(question.getQuestion());
        txtEditQuestionsOption1.setText(question.getAnswer1());
        txtEditQuestionsOption2.setText(question.getAnswer2());
        txtEditQuestionsOption3.setText(question.getAnswer3());
        txtEditQuestionsOption4.setText(question.getAnswer4());
        String correctAnswer=question.getCorrectAnswer();
        String correctOption=getKeyFromValue(correctAnswer);
        jcEditQuestionsCorrectOption.setSelectedItem(correctOption);
    }
    
    //Using Hashmap to get key from corresponding values .
    
    public String getKeyFromValue(String value)
    {
        Set<String>keys=options.keySet(); //Creating set to keep all the 4 options as key-value pairs.
        for (String key : keys)           //keySet() method is used to make pairs of key & value .
        {
            String currValue=options.get(key); //currValue is the respective option of ArrayList .
            if (currValue.equals(value)) 
            {
             return key;                  //key is the corresponding value respectively .
            }
        }

        return null;        //will never return null as in any case anyone of the 4 options will be matched with respective keys .
    }                       //but to satisfy java, we returned null .

    
    // validateInput() to validate input is right or not .
    public boolean validateInput()
    {    
        question=txtEditQuestions.getText().trim();
        option1=txtEditQuestionsOption1.getText().trim();
        option2=txtEditQuestionsOption2.getText().trim();
        option3=txtEditQuestionsOption3.getText().trim();
        option4=txtEditQuestionsOption4.getText().trim();
        correctOption=jcEditQuestionsCorrectOption.getSelectedItem().toString();
        if(question.isEmpty()||option1.isEmpty()||option2.isEmpty()||option3.isEmpty()||option4.isEmpty()||correctOption.isEmpty())
            return false;
        else
            return true;
    }   

    private void lblEditQuestionsLogOutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblEditQuestionsLogOutMouseClicked
        LoginFrame login = new LoginFrame();
        login.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_lblEditQuestionsLogOutMouseClicked

    private void lblEditQuestionsLogOutMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblEditQuestionsLogOutMouseEntered
        lblEditQuestionsLogOut.setForeground(Color.white);
    }//GEN-LAST:event_lblEditQuestionsLogOutMouseEntered

    private void lblEditQuestionsLogOutMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblEditQuestionsLogOutMouseExited
        lblEditQuestionsLogOut.setForeground(oldColor);
    }//GEN-LAST:event_lblEditQuestionsLogOutMouseExited

    private void btnEditQuestionsDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQuestionsDoneActionPerformed
        try
        {
            QuestionDAO.updateQuestions(qstore);
            JOptionPane.showMessageDialog(null, "Your question set has been successfully updated in the Database","Exam Added!",JOptionPane.INFORMATION_MESSAGE);
            fr = new EditPaperFrame();
            fr.setVisible(true);
            this.dispose();
        }
        
        catch(SQLException ex)
        {
            JOptionPane.showMessageDialog(null, "Error while connecting to DB!","Edit Question Frame!",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();

        }
    }//GEN-LAST:event_btnEditQuestionsDoneActionPerformed

    private void btnEditQuestionsCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQuestionsCancelActionPerformed
        fr = new EditPaperFrame();
        fr.setVisible(true);
        this.dispose();
        
    }//GEN-LAST:event_btnEditQuestionsCancelActionPerformed

    private void btnEditQuestionsNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQuestionsNextActionPerformed
        if(!validateInput())
        {
            JOptionPane.showMessageDialog(null, "Please fill all the input fields","Cannot Add Question!",JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        String optionName=options.get(correctOption);
        Question q = new Question(editExam.getExamId(),editExam.getLanguage(),option1,option2,option3,option4,optionName,question,qno);
        qstore.removeQuestion(pos);
        qstore.setQuestionAt(pos,q);
        pos++;
        if(pos>=qstore.getCount())
        {
            JOptionPane.showMessageDialog(null, "Your question set has been successfully updated.\nPress the DONE button to send it to the database ","Exam Added!",JOptionPane.INFORMATION_MESSAGE);
            pos=0;
        }
        showQuestion();
        qno++;
        
        
        if(qno>qstore.getCount())
            qno=1;
        lblEditQuestionsQno.setText("Question no:"+qno);

    }//GEN-LAST:event_btnEditQuestionsNextActionPerformed

    private void btnEditQuestionsPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQuestionsPreviousActionPerformed
        if(!validateInput())
        {
            JOptionPane.showMessageDialog(null, "Please fill all the input fields","Cannot Add Question!",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String optionName = options.get(correctOption);
        Question q = new Question(editExam.getExamId(), editExam.getLanguage(), option1, option2, option3, option4, optionName, question, qno);
        qstore.removeQuestion(pos);
        qstore.setQuestionAt(pos,q);
        pos--;
        if(pos<0)
        {
            JOptionPane.showMessageDialog(null, "Your question set has been successfully updated.\nPress the DONE button to send it to the database ","Exam Added!",JOptionPane.INFORMATION_MESSAGE);
            pos = qstore.getCount()-1;
        }
        --qno;
        showQuestion();
        
        
        if(qno<=0)
        {
            qno = qstore.getCount();
        }
        lblEditQuestionsQno.setText("Question No: " +qno);
    }//GEN-LAST:event_btnEditQuestionsPreviousActionPerformed

    /**
     * @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(EditQuestionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditQuestionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditQuestionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditQuestionsFrame.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() {
                new EditQuestionsFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditQuestionsCancel;
    private javax.swing.JButton btnEditQuestionsDone;
    private javax.swing.JButton btnEditQuestionsNext;
    private javax.swing.JButton btnEditQuestionsPrevious;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox<String> jcEditQuestionsCorrectOption;
    private javax.swing.JLabel lblEditQuestions;
    private javax.swing.JLabel lblEditQuestionsLogOut;
    private javax.swing.JLabel lblEditQuestionsQno;
    private javax.swing.JLabel lblEditQuestionsUser;
    private javax.swing.JTextArea txtEditQuestions;
    private javax.swing.JTextField txtEditQuestionsOption1;
    private javax.swing.JTextField txtEditQuestionsOption2;
    private javax.swing.JTextField txtEditQuestionsOption3;
    private javax.swing.JTextField txtEditQuestionsOption4;
    // End of variables declaration//GEN-END:variables

    
}
