Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package interfaceTisseoWS; import java.io.IOException; import java.net.URISyntaxException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultListModel; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import tisseows.RequestJCDecaux; import tisseows.RequestTisseo; /** * * @author Hugo */ public class ST3 extends javax.swing.JFrame { private JSONObject array; /** * Creates new form ST3 */ public ST3() { initComponents(); } /** * Indique si l'une des lignes qui passe par cet arrt (format JSon) est une ligne au dpart de l'universit * @param dest * @return */ public boolean estDeservieDepuisPS(JSONArray dest) { String line; for (int i = 0; i < dest.size(); i++) { for (int j = 0; j < ((JSONArray) ((JSONObject) dest.get(i)).get("line")).size(); j++) { line = (String) ((JSONObject) ((JSONArray) ((JSONObject) dest.get(i)).get("line")).get(j)) .get("shortName"); if (estLignePS(line)) { lblResultat.setText("Votre zone est dsservie directement depuis \nPaul Sabatier par la ligne " + line + ", utilisez le rseau Tisso"); return true; } } } return false; } /** * Indique si un nom de ligne donne fait parti des lignes au dpart de l'universit * @param str * @return */ public static boolean estLignePS(String str) { String[] words = { "B", "2", "2s", "34", "54", "56", "78", "78s", "79s", "81", "81s", "82", "88", "88s" }; return (Arrays.asList(words).contains(str)); } /** * 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() { txtAdresse = new javax.swing.JTextField(); btnGo = new javax.swing.JButton(); listResultats = new javax.swing.JScrollPane(); listRes = new javax.swing.JList(); jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); lblResultat = new javax.swing.JTextArea(); setTitle("ST3"); btnGo.setText("Recherche"); btnGo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnGoActionPerformed(evt); } }); listRes.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { listResMouseClicked(evt); } }); listRes.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { listResMouseDragged(evt); } }); listRes.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { listResValueChanged(evt); } }); listResultats.setViewportView(listRes); jButton1.setText("Retour"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setText("Rsultat sur le type de transport prendre :"); jLabel2.setText("Veuillez entrer votre point de dpart :"); lblResultat.setEditable(false); lblResultat.setColumns(20); lblResultat.setRows(5); jScrollPane1.setViewportView(lblResultat); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtAdresse, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addComponent(btnGo, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup() .addComponent(listResultats, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 139, Short.MAX_VALUE)) .addComponent(jScrollPane1)))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2) .addComponent(txtAdresse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnGo)).addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(listResultats, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoActionPerformed lblResultat.setText(""); try { RequestTisseo r = new RequestTisseo(); JSONParser parser = new JSONParser(); r.setPathURIB("/placesList"); r.addParamURIB("term", txtAdresse.getText()); //r.addParamURIB("displayOnlyRoads", "1"); Object obj = parser.parse(r.request()); array = (JSONObject) obj; int nbResultats = (((JSONArray) ((JSONObject) array.get("placesList")).get("place")).size()); DefaultListModel lm = new DefaultListModel(); for (int i = 0; i < nbResultats; i++) { lm.addElement( ((JSONObject) ((JSONArray) ((JSONObject) array.get("placesList")).get("place")).get(i)) .get("label")); } listRes.setModel(lm); } catch (ParseException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnGoActionPerformed private void listResMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listResMouseClicked String x = (String) ((JSONObject) ((JSONArray) ((JSONObject) array.get("placesList")).get("place")) .get(listRes.getSelectedIndex())).get("x"); String y = (String) ((JSONObject) ((JSONArray) ((JSONObject) array.get("placesList")).get("place")) .get(listRes.getSelectedIndex())).get("y"); double distanceAParcourir = getDistance(1.46944, 43.561255, Double.valueOf(x), Double.valueOf(y)); double ax = Double.valueOf(x) - 0.005; double ay = Double.valueOf(y) - 0.003; double bx = Double.valueOf(x) + 0.005; double by = Double.valueOf(y) + 0.003; // x+-0.005 y+-0.003 boolean contientTisseo, contientVelib = false; try { //recherche lignes tisseo dans la zone RequestTisseo r = new RequestTisseo(); JSONParser parser = new JSONParser(); r.setPathURIB("/stopPointsList"); r.addParamURIB("srid", "4326"); r.addParamURIB("displayLines", "1"); r.addParamURIB("sortByDistance", "1"); String bbox = Double.toString(ax) + "," + Double.toString(ay) + "," + Double.toString(bx) + "," + Double.toString(by); r.addParamURIB("bbox", bbox); Object obj = parser.parse(r.request()); JSONObject array2 = (JSONObject) obj; contientTisseo = (((JSONArray) ((JSONObject) array2.get("physicalStops")).get("physicalStop")) .size()) > 0; //recherche stations velib dans la zone si point moins de 3 kilomtre if (distanceAParcourir < 3.0) { RequestJCDecaux r3 = new RequestJCDecaux(); JSONParser parser3 = new JSONParser(); r3.setPathURIB("/vls/v1/stations"); Object obj3 = parser3.parse(r3.request()); JSONArray array3 = (JSONArray) obj3; int nbStations = array3.size(); double X, Y; int i = 0; while (i < nbStations && !contientVelib) { X = (double) ((JSONObject) ((JSONObject) (JSONObject) array3.get(i)).get("position")) .get("lng"); Y = (double) ((JSONObject) ((JSONObject) (JSONObject) array3.get(i)).get("position")) .get("lat"); if (X > ax && X < bx && Y > ay && Y < by) { contientVelib = true; } i++; } } if (!contientTisseo && !contientVelib) { lblResultat.setText("Aucun rsultat"); } else { if (contientTisseo && !contientVelib) { lblResultat.setText("Utilisez le rseau Tisso"); } else { if (!contientTisseo && contientVelib) { lblResultat.setText("Utilisez VelToulouse"); } else { int nbArrivees = (((JSONArray) ((JSONObject) array2.get("physicalStops")) .get("physicalStop")).size()); int j = 0; while (j < nbArrivees) { if (estDeservieDepuisPS(((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) array2 .get("physicalStops")).get("physicalStop")).get(j)).get("destinations")))) { break; } j++; if (j == nbArrivees) { lblResultat.setText( "Aucun ligne Tisso ne dssert directement votre zone depuis \nPaul Sabatier, la distance est infrieure 3 km donc nous vous \nconseillons d'utiliser le service VelToulouse"); } } } } } } catch (ParseException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(ST3.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_listResMouseClicked private void listResMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listResMouseDragged }//GEN-LAST:event_listResMouseDragged private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed setVisible(false); }//GEN-LAST:event_jButton1ActionPerformed private void listResValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listResValueChanged lblResultat.setText("Calcul en cours..."); }//GEN-LAST:event_listResValueChanged public double getDistance(double x1, double y1, double x2, double y2) { double d2r = Math.PI / 180; try { double dlong = (x2 - x1) * d2r; double dlat = (y2 - y1) * d2r; double a = Math.pow(Math.sin(dlat / 2.0), 2) + Math.cos(y1 * d2r) * Math.cos(y2 * d2r) * Math.pow(Math.sin(dlong / 2.0), 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = 6367 * c; return d; } catch (Exception e) { } return -1.0; } /** * @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(ST3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ST3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ST3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ST3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new ST3().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnGo; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea lblResultat; private javax.swing.JList listRes; private javax.swing.JScrollPane listResultats; private javax.swing.JTextField txtAdresse; // End of variables declaration//GEN-END:variables }