parsegui.Window.java Source code

Java tutorial

Introduction

Here is the source code for parsegui.Window.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 parsegui;

import almonds.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.relation.RelationType;
import javax.management.relation.RoleInfo;
import javax.management.relation.RoleInfoNotFoundException;
import javax.swing.JOptionPane;
import org.json.JSONException;

/**
 *
 * @author Vishnu
 */
public class Window extends javax.swing.JFrame {

    /**
     * Creates new form Window
     */
    public Window() {
        initComponents();

        String applicationId = "VsP0MaAemUyNZn1bes7nHY7kAcSsSnD9tGyj0sHX";
        String restAPIKey = "OXdlgavrG63OMfHzy3HIV16IjREbVVQaVz4SA9sF";

        /* initializing parse to use the database */
        Parse.initialize(applicationId, restAPIKey);
        System.out.println("Parse initialised...");
        System.out.println("Refreshing the festivals...");

        ParseQuery query = new ParseQuery("Festival");
        query.findInBackground(new FindCallback() {
            @Override
            public void done(List<ParseObject> objects, almonds.ParseException e) {
                if (e == null) {
                    List<ParseObject> res = new ArrayList<>(objects);
                    //System.out.println(res.size());
                    for (ParseObject re : res) {
                        eventList.addItem(re.getString("Festival_name"));
                    }
                } else {
                    System.out.println("error");
                }
            }

        });

    }

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

        ticketTypeGrp = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        cancel = new javax.swing.JButton();
        buyTicket = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        phoneTextField = new javax.swing.JTextField();
        nameTextField = new javax.swing.JTextField();
        phoneN = new javax.swing.JLabel();
        mail = new javax.swing.JLabel();
        name = new javax.swing.JLabel();
        events = new javax.swing.JLabel();
        eventList = new javax.swing.JComboBox();
        mailTextField = new javax.swing.JTextField();
        ticketNo = new javax.swing.JLabel();
        ticketNoField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Buy Tickets");

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

        cancel.setText("Cancel");
        cancel.setToolTipText("Exit");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        buyTicket.setText("Buy Ticket");
        buyTicket.setToolTipText("Buy tickets and exit");
        buyTicket.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buyTicketActionPerformed(evt);
            }
        });

        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().addComponent(buyTicket)
                                .addGap(35, 35, 35).addComponent(cancel).addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(buyTicket).addComponent(cancel))
                        .addContainerGap()));

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

        phoneTextField.setToolTipText("Enter your 10 digit phone number");

        nameTextField.setToolTipText("Enter your name");

        phoneN.setText("Phone Number:");

        mail.setText("Email-ID:");

        name.setText("Name:");

        events.setText("Events:");

        eventList.setEditable(true);
        eventList.setMaximumRowCount(100);
        eventList.setToolTipText("Select the festival you want to attend");
        eventList.setAutoscrolls(true);
        eventList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventListActionPerformed(evt);
            }
        });

        mailTextField.setToolTipText("Enter your Email-ID");

        ticketNo.setText("Number of  Tickets:");

        ticketNoField.setToolTipText("Enter number of tickets you want to buy");

        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.LEADING)
                                .addComponent(name).addComponent(phoneN).addComponent(mail).addComponent(events)
                                .addComponent(ticketNo))
                        .addGap(49, 49, 49)
                        .addGroup(
                                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(nameTextField).addComponent(phoneTextField)
                                        .addComponent(mailTextField)
                                        .addComponent(ticketNoField, javax.swing.GroupLayout.DEFAULT_SIZE, 143,
                                                Short.MAX_VALUE)
                                        .addComponent(eventList, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(name)
                                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(50, 50, 50).addComponent(phoneN))
                        .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(mail).addComponent(mailTextField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ticketNo).addComponent(ticketNoField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(events).addComponent(eventList,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(34, Short.MAX_VALUE)));

        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setText("NOTE: All fields are compulsory.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().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().addContainerGap().addComponent(jPanel1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup().addGap(60, 60, 60).addComponent(jPanel2,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(12, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(15, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39).addComponent(jLabel1)));

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

    private void buyTicketActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buyTicketActionPerformed
        int ticketCount = 0;
        long phone = 0;

        //eventList.setSelectedItem(evt);
        String festSelection = (String) eventList.getSelectedItem();

        String personName = (String) nameTextField.getText();
        String email = (String) mailTextField.getText();

        //ParseObject rel = new ParseObject("Attendee");
        ParseObject aObj = new ParseObject("Attendee");
        try {
            int length = String.valueOf(phoneTextField.getText()).length();
            if (length == 0 || length < 10 || length > 10) {
                JOptionPane.showConfirmDialog(null, "Please enter a valid 10 digit phone number", "Error",
                        JOptionPane.CLOSED_OPTION);
                return;
            } else {
                phone = Long.parseLong(phoneTextField.getText());

            }
            ticketCount = Integer.parseInt(ticketNoField.getText());
            if (ticketCount <= 0) {
                JOptionPane.showConfirmDialog(null, "Number of tickets should be atleast 1", "Error",
                        JOptionPane.CLOSED_OPTION);
                return;
            }

        } catch (NumberFormatException e) {
            System.out.println("Exception" + e);
            JOptionPane.showConfirmDialog(null, "Please fill all the fields", "Error", JOptionPane.CLOSED_OPTION);
            return;

        }

        int b = JOptionPane.showConfirmDialog(null, "Confirm " + ticketCount + " tickets", "Confirm",
                JOptionPane.OK_CANCEL_OPTION);

        if (b == 0) {
            System.out.println("Phone number: " + phone);
            System.out.println("number of tickets: " + ticketCount);
            System.out.println("name: " + personName);
            System.out.println("Email-ID: " + email);
            System.out.println("festival selected: " + festSelection);

            aObj.put("Name", personName);
            aObj.put("PhoneNo", phone);
            aObj.put("EmailID", email);

            aObj.put("Tickets_purchased", ticketCount);

            /* fest selection*/
            ParseQuery festquery = new ParseQuery("Festival");
            ParseObject festObj = new ParseObject("Festival");
            // String id = festObj.getString("Rockathon");// id = null.. should be in loop nd use list/array
            System.out.println(eventList.getSelectedItem());

            //festquery.whereEqualTo(festSelection, festObj.getString("Rockathon"));
            /*
             festquery.findInBackground(new FindCallback() {
                
             @Override
             public void done(List<ParseObject> objects, ParseException e) {
                   
             if(e == null){
             System.out.println("QUERRIED ATLAST!!!!");
             }
             else{
             System.out.println("NOOOOOOOOOO");
             }
             }
             });*/
            festquery.findInBackground(new FindCallback() {

                @Override
                public void done(List<ParseObject> objects, almonds.ParseException e) {
                    if (e == null) {
                        List<ParseObject> res = new ArrayList<>(objects);
                        HashMap<String, String> hm = new HashMap<>();
                        System.out.println(res.size());
                        /*for (ParseObject re : res) {
                         System.out.println("IDS: " + re.getString("Festival_name"));
                         }*/
                        ParseObject re1 = new ParseObject("Festival");
                        for (ParseObject re : res) {
                            System.out.println("festival name: " + re.getString("Festival_name") + " festivalID : "
                                    + re.getObjectId());
                            ;
                            // n.add(re.getObjectId());
                            hm.put(re.getObjectId(), re.getString("Festival_name"));
                        }

                        if (hm.containsValue(festSelection)) {

                            for (Map.Entry entry : hm.entrySet()) {

                                if (festSelection.equals(entry.getValue())) {
                                    System.out.println("Selected festival key is " + entry.getKey());

                                }
                            }
                        }
                    } else {
                        System.out.println("error");
                    }
                }

            });

            try {
                aObj.save();
            } catch (ParseException ex) {
                Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showConfirmDialog(null, "ERROR: " + ex, "Error", JOptionPane.OK_CANCEL_OPTION);
            }
        } else {
            return;
        }

        System.exit(0);

    }//GEN-LAST:event_buyTicketActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        System.exit(0);

    }//GEN-LAST:event_cancelActionPerformed

    private void eventListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventListActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_eventListActionPerformed

    /**
     * @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 ("Windows".equals(info.getName())) {
         javax.swing.UIManager.setLookAndFeel(info.getClassName());
         break;
         }
         }
         } catch (ClassNotFoundException ex) {
         java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (InstantiationException ex) {
         java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (IllegalAccessException ex) {
         java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(Window.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 Window().setVisible(true);
            }

        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buyTicket;
    private javax.swing.JButton cancel;
    private javax.swing.JComboBox eventList;
    private javax.swing.JLabel events;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel mail;
    private javax.swing.JTextField mailTextField;
    private javax.swing.JLabel name;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel phoneN;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JLabel ticketNo;
    private javax.swing.JTextField ticketNoField;
    private javax.swing.ButtonGroup ticketTypeGrp;
    // End of variables declaration//GEN-END:variables

}