1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
| package application;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Callback;
import netscape.javascript.JSObject;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class Main extends Application {
//////Mes déclarations de variables ///////
static int compteur=0;
static int ordrebridge = -1;
static int mavariable=0 ;
static String pointdelete ="";
static int taillefenetre=600;
int taillecolonne=200;
static String longibridge,latibridge;
static String coordoString = "";
String user="";
public final TableView<Coordonnees> tableCoor = new TableView<>();
Coordonnees depart = new Coordonnees ("0" ,"2,2","49,5");
private static final ObservableList<Coordonnees> data = FXCollections.observableArrayList();
static ArrayList <Coordonnees> mespoints = new ArrayList();
// Classe coordonees pour pouvoir les afficher dans le tableau TableView
public static class Coordonnees {
private final SimpleStringProperty longitude;
private final SimpleStringProperty latitude;
private final SimpleIntegerProperty ordre;
private Coordonnees (String ordre , String lati , String longi){
ordrebridge++;
this.ordre=new SimpleIntegerProperty(ordrebridge);
this.longitude = new SimpleStringProperty(longi);
this.latitude = new SimpleStringProperty(lati);
}
public String getLongitude() {
return longitude.get();
}
public void setLongitude(String longi) {
longitude.set(longi);
}
public String getLatitude() {
return latitude.get();
}
public void setLatitude(String lati) {
latitude.set(lati);
}
public int getOrdre() {
return ordre.get();
}
public void setOrdre(int position) {
ordre.set(position);
}
}
//Classe permettant d ajouter un bouton suppression a chaque ligne du tableau
public class ButtonCell extends TableCell<Coordonnees, Boolean> {
final Button cellButton = new Button("Suppression");
ButtonCell() {
cellButton.setMinWidth((taillecolonne/2)-4);
cellButton.getStyleClass().add("my-btn1");
WebView webview = new WebView();
cellButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
// do something when button clicked
System.out.println("action");
Coordonnees coordoasup = (Coordonnees) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
data.remove(coordoasup);
mespoints.remove(coordoasup);
user= Integer.toString(coordoasup.getOrdre());
webview.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
webview.getEngine().executeScript("bonjour();");
});
//webview.getEngine().executeScript("bonjour();");
// mettre une fonction permettant d incrementer la string gerant les points deletes
}
});
}
//Display button if the row is not empty
@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if (!empty) {
setGraphic(cellButton);
}
}
}
private final Bridge bridge = new Bridge();
@Override
public void start(Stage primaryStage) {
try {
MyBrowser browser = new MyBrowser();
browser.affichagemenu();
//BorderPane root = new BorderPane();
Scene scene = new Scene(browser,taillefenetre,taillefenetre);
scene.getStylesheets().add("application/application.css");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
//////Classe interface pour gerer les valeurs retournées par javascript ////////
public static class Bridge {
public void setmaposition(String value1,String value2) { try {
Coordonnees coordonnees= new Coordonnees(String.valueOf(compteur),value1,value2);
compteur++;
mespoints.add(coordonnees); // dans l'ArrayList pour retravailler ensuite ...
data.add(coordonnees);// dans l'observable list pour l'affichage
} catch (Exception e){System.out.println(e.toString());};
}
}
public class MyBrowser extends Region {
String [] latitrajet ;
String [] longitrajet;
final VBox vbox1 = new VBox();
final VBox vbox2 = new VBox();
final HBox hbox1 = new HBox();
ScrollPane sp = new ScrollPane();
Button btn0 = new Button();
Button btn1 = new Button();
Button btn2 = new Button();
Button btn3 = new Button();
WebView webview = new WebView();
WebEngine webengine = webview.getEngine();
Rectangle Monmenu = new Rectangle();
Group Totalite =new Group() ;
String Envoijavascript;
String UnPoint;
public MyBrowser() {
btn0.setOnAction(actionEvent -> {
webview.getEngine().executeScript("bonjour();");
});
btn1.setOnAction(actionEvent -> {
latitrajet = new String[mespoints.size()];
longitrajet = new String[mespoints.size()];
Envoijavascript ="";
for(int i = 0; i < mespoints.size(); i++)
{
latitrajet[i]= mespoints.get(i).getLatitude();
longitrajet[i]= mespoints.get(i).getLongitude();
}
String aenvoyer1=Concater_js(latitrajet,longitrajet);
webview.getEngine().executeScript("displayRoute("+aenvoyer1+");") ;
});
btn2.setOnAction(actionEvent -> {
webview.getEngine().executeScript("clearMarkers(1);");
});
btn3.setOnAction(actionEvent -> {
String envoiUDP=Concater_js(latitrajet,longitrajet);
ClientUDP envoie = new ClientUDP(envoiUDP);
});
webview.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
final boolean disabled = newValue != Worker.State.SUCCEEDED;
btn0.setDisable(disabled);
btn1.setDisable(disabled);
btn2.setDisable(disabled);
});
final TableColumn<Coordonnees, Boolean> visibleColumn = new TableColumn<>("A supprimer");
visibleColumn.setMinWidth(taillecolonne/2);
visibleColumn.setCellValueFactory(new PropertyValueFactory<>("visible"));
final TableColumn<Coordonnees, Integer> OrdreCol = new TableColumn<>("Ordre");
OrdreCol.setMinWidth(taillecolonne/2);
OrdreCol.setCellValueFactory( new PropertyValueFactory<>("Ordre"));
final TableColumn<Coordonnees, Double> LongeCol = new TableColumn<>("Longitude");
LongeCol.setMinWidth(taillecolonne);
LongeCol.setCellValueFactory( new PropertyValueFactory<>("Longitude"));
final TableColumn<Coordonnees, Double> LatCol = new TableColumn<>("Latitude");
LatCol.setMinWidth(taillecolonne);
LatCol.setCellValueFactory(new PropertyValueFactory<>("Latitude"));
tableCoor.getColumns().setAll(visibleColumn, OrdreCol, LongeCol, LatCol);
//////Gestion affichage bouton suppression Boolean sert � l'espace Bouton
visibleColumn.setCellFactory(
new Callback<TableColumn<Coordonnees, Boolean>, TableCell<Coordonnees, Boolean>>() {
@Override
public TableCell<Coordonnees, Boolean> call(TableColumn<Coordonnees, Boolean> p) {
return new ButtonCell();
} });
tableCoor.setItems(data);
sp.setContent(tableCoor); // Le tableau
sp.setPrefSize(taillefenetre,150);
sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
sp.setVmax(3);
//vbox1.getChildren().add(sp);
webview.getEngine().load(getClass().getResource("Position.html").toExternalForm());
// On passe maintenant l'objet java au formulaire javascript.
final JSObject jsobj = (JSObject) webview.getEngine().executeScript("window");
jsobj.setMember("java", bridge);
//vbox1.setMaxHeight(400);
getChildren().add(webview);
vbox2.getChildren().add(sp);
Totalite.getChildren().add(Monmenu);
Totalite.getChildren().add(btn0);
Totalite.getChildren().add(btn1);
Totalite.getChildren().add(btn2);
Totalite.getChildren().add(btn3);
getChildren().addAll(Totalite,vbox2);
}
public void affichagemenu () {
Monmenu.setX(10);
Monmenu.setY(10);
Monmenu.setWidth(160);
Monmenu.setHeight(580);
Monmenu.getStyleClass().add("my-rect");
//Monmenu.setFill(Color.PINK);
//Monmenu.setStroke(Color.DARKGREEN);
Monmenu.setStrokeWidth(5);
Monmenu.setArcHeight(30);
Monmenu.setArcWidth(30);
btn0.getStyleClass().add("my-btn");
btn0.setMinWidth(130);
btn0.setLayoutX(25);
btn0.setLayoutY(200);
btn0.setText(" Départ ");
btn1.getStyleClass().add("my-btn");
btn1.setMinWidth(130);
btn1.setLayoutX(25);
btn1.setLayoutY(250);
btn1.setText("Tracer ");
btn2.getStyleClass().add("my-btn");
btn2.setMinWidth(130);
btn2.setLayoutX(25);
btn2.setLayoutY(300);
btn2.setText("InfoDrone");
btn3.getStyleClass().add("my-btn");
btn3.setMinWidth(130);
btn3.setLayoutX(25);
btn3.setLayoutY(350);
btn3.setText("Valider ");
btn0.setDisable(true);
btn1.setDisable(true);
};
public String Concater_js (String[]latitudes , String[]longitudes) {
String mescoorjs="[";
int nb_points=0;
do {
mescoorjs=mescoorjs+"{lat: "+latitudes[nb_points]+", lng: "+longitudes[nb_points]+"}";
nb_points++;
if (nb_points<latitudes.length) {mescoorjs=mescoorjs+",";} else {mescoorjs=mescoorjs+"]";}
} while (nb_points<latitudes.length);
return mescoorjs;
}
public String Concater_UDP (String[]latitudes , String[]longitudes) {
String mescoorjs="[";
int nb_points=0;
do {
mescoorjs=mescoorjs+"{lat: "+latitudes[nb_points]+", lng: "+longitudes[nb_points]+"}";
nb_points++;
if (nb_points<latitudes.length) {mescoorjs=mescoorjs+",";} else {mescoorjs=mescoorjs+"]";}
} while (nb_points<latitudes.length);
return mescoorjs;
}
}
public class ClientUDP {
final static int port = 9632;
final static int taille = 1024;
byte buffer[] = new byte[taille];
String name ="127.0.0.1";
public ClientUDP(String monemission) {
try {
InetAddress serveur = InetAddress.getByName(name);
int length = monemission.length();
byte buffer[] = monemission.getBytes();
DatagramSocket socket = new DatagramSocket();
DatagramPacket donneesEmises = new DatagramPacket(buffer, length, serveur, port);
DatagramPacket donneesRecues = new DatagramPacket(new byte[taille], taille);
socket.setSoTimeout(30000);
socket.send(donneesEmises);
socket.receive(donneesRecues);
String Paquet= new String(donneesRecues.getData(),0, donneesRecues.getLength());
System.out.println("Envoyé:"+monemission);
System.out.println("Reçu:"+Paquet);
String monemission1=(String)monemission;
File ff=new File("debug1.txt"); // définir l'arborescence
ff.createNewFile();
FileWriter ffw=new FileWriter(ff);
ffw.write(Paquet); // écrire une ligne dans le fichier resultat.txt
ffw.write("\n"); // forcer le passage à la ligne
ffw.close();
if (!Paquet.equals(monemission1)) {System.out.println("Mauvaise transmission");} else {System.out.println("Tout c'est bien passé!!!");}
} catch (SocketTimeoutException ste) {
System.out.println("Le delai pour la reponse a expire");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
launch(args);
}
} |
Partager