iaws_desktop.VelibDispoDialogFrame.java Source code

Java tutorial

Introduction

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

import functionnality.VelibAvailableBikesST4;
import java.io.IOException;
import java.io.StringReader;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import model.VelibStation;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import webrequester.IAWSWebRequest;
import webrequester.VelibWebRequest;

/**
 *
 * @author loic
 */
public class VelibDispoDialogFrame extends javax.swing.JDialog {
    DefaultListModel<VelibStation> listModel;

    /**
     * Creates new form VelibDispoDialogFrame
     */
    public VelibDispoDialogFrame(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Document doc;
        JSONArray array;
        VelibWebRequest velibR = new VelibWebRequest("/vls/v1/stations");
        velibR.addParameterGet("contract", "Toulouse");
        try {
            array = (JSONArray) new JSONParser().parse(velibR.requestWithGet());
        } catch (ParseException ex) {
            return;
        }
        listModel = new DefaultListModel<>();
        JSONObject obj = null;
        for (int i = 0; i < array.size(); i++) {
            obj = (JSONObject) array.get(i);
            listModel.addElement(new VelibStation(obj.get("number").toString(), obj.get("name").toString()));
        }
        listVelib.setModel(listModel);
    }

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

        jScrollPane1 = new javax.swing.JScrollPane();
        listVelib = new javax.swing.JList();
        lblStatus = new javax.swing.JLabel();
        lblBikesA = new javax.swing.JLabel();
        lblBikesS = new javax.swing.JLabel();
        lblTxtStatus = new javax.swing.JLabel();
        lblTxtBikesAvailable = new javax.swing.JLabel();
        lblTxtBikesStand = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        textStation = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        listVelib.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        listVelib.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listVelibValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listVelib);

        lblStatus.setText("Status :");

        lblBikesA.setText("Bikes available :");

        lblBikesS.setText("Bikes stands :");

        lblTxtStatus.setText("Nothing");

        lblTxtBikesAvailable.setText("Nothing");

        lblTxtBikesStand.setText("Nothing");

        jLabel1.setText("Chercher une station :");

        textStation.setText("jTextField1");
        textStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textStationActionPerformed(evt);
            }
        });

        jButton1.setText("Rechercher");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Slectionner une station :");

        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)
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(textStation, javax.swing.GroupLayout.PREFERRED_SIZE, 99,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton1))
                                .addComponent(jLabel2))
                        .addGap(92, 92, 92)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(lblStatus).addGap(18, 18, 18)
                                        .addComponent(lblTxtStatus))
                                .addGroup(layout.createSequentialGroup().addComponent(lblBikesA)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblTxtBikesAvailable))
                                .addGroup(layout.createSequentialGroup().addComponent(lblBikesS)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblTxtBikesStand)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblStatus).addComponent(lblTxtStatus).addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblBikesA).addComponent(lblTxtBikesAvailable)
                                .addComponent(textStation, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblBikesS).addComponent(lblTxtBikesStand).addComponent(jLabel2))
                        .addGap(18, 18, 18).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(12, Short.MAX_VALUE)));

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

    private void listVelibValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listVelibValueChanged
        VelibStation v = null;
        if (listVelib.getSelectedIndex() > -1) {
            VelibAvailableBikesST4 velibST = new VelibAvailableBikesST4(
                    ((VelibStation) listVelib.getSelectedValue()));
            v = velibST.request();
            lblTxtStatus.setText(v.getStatus());
            lblTxtBikesStand.setText("" + v.getAvailableStands());
            lblTxtBikesAvailable.setText("" + v.getAvailableBikes());

        }
    }//GEN-LAST:event_listVelibValueChanged

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        DefaultListModel<VelibStation> listResultat = new DefaultListModel();
        if (textStation.getText().equals("")) {
            listVelib.setModel(listModel);
        } else {
            for (int i = 0; i < listModel.size(); i++) {
                if (listModel.getElementAt(i).getName().contains(textStation.getText())) {
                    listResultat.addElement(listModel.getElementAt(i));
                }
            }
            listVelib.setModel(listResultat);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBikesA;
    private javax.swing.JLabel lblBikesS;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTxtBikesAvailable;
    private javax.swing.JLabel lblTxtBikesStand;
    private javax.swing.JLabel lblTxtStatus;
    private javax.swing.JList listVelib;
    private javax.swing.JTextField textStation;
    // End of variables declaration//GEN-END:variables
}