Skip to content
Snippets Groups Projects
Verified Commit 3e5c01dd authored by Théo Pirkl's avatar Théo Pirkl :nail_care:
Browse files

Implements events and controls reactivity.

parent 8cd9b31a
No related branches found
No related tags found
3 merge requests!21*poof* final version,!16Gui transport,!11Gui transport
package ch.hepia.ui;
import ch.hepia.api.transport.Connection;
import ch.hepia.api.transport.LinkAPI;
import ch.hepia.api.transport.Section;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import org.json.JSONArray;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class MainWindowController implements Initializable {
......@@ -25,35 +35,79 @@ public class MainWindowController implements Initializable {
@FXML
private ComboBox<String> destinationComboBox;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
currentJourneyLabel.setText("Vous n'avez prévu aucun voyage pour le moment.");
startStopLabel.setText(""); // No text should be visible when no journey has been selected.
LinkAPI transportApi = new LinkAPI();
@FXML
private Canvas connectionCanvas;
originComboBox.valueProperty().addListener(
(observable, oldValue, newValue) -> {
if (!newValue.isEmpty() && newValue.length() > 3){
originComboBox.getItems().clear();
/**
* Shows a sad message when the API crashes.
*/
private void showSadMessage(){
AlertUtils.dialog(Alert.AlertType.ERROR, "Erreur",
"Une erreur est survenue.",
"Impossible de contacter les services de transport Suisses.");
}
/**
* Searches stops that matches the query.
* @param newValue The stop name query.
* @param transportApi The API to connect to
* @param target The combo box where to put the results.
*/
private void searchStops(String newValue, LinkAPI transportApi, ComboBox<String> target){
if (!newValue.isEmpty() && newValue.length() > 3 && !target.getItems().contains(newValue)){
try {
JSONArray a = transportApi.getStations(newValue);
ArrayList<String> results = new ArrayList<>();
for (int i = 0; i < a.length(); i++){
originComboBox.getItems().add(a.getJSONObject(i).getString("name"));
results.add(a.getJSONObject(i).getString("name"));
}
target.getItems().clear();
target.getItems().addAll(results);
target.show();
} catch (IOException e) {
showSadMessage();
}
} catch (Exception e) {
AlertUtils.dialog(
Alert.AlertType.ERROR, "Erreur",
"Une erreur est survenue.",
"Impossible de contacter les services de transport Suisses.");
}
}
});
private void drawConnection(Connection connection, int x, int y){
List<Section> sections = connection.getSections();
for (int i = 0; i < sections.size(); i++){
connectionCanvas.getGraphicsContext2D().strokeLine(
x + (800 / sections.size()) * i, y, 800 / sections.size(), y);
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
currentJourneyLabel.setText("Vous n'avez prévu aucun voyage pour le moment.");
startStopLabel.setText(""); // No text should be visible when no journey has been selected.
LinkAPI transportApi = new LinkAPI();
originComboBox.valueProperty().addListener(
(observable, oldValue, newValue) -> searchStops(newValue, transportApi, originComboBox)
);
destinationComboBox.valueProperty().addListener(
(observable, oldValue, newValue) -> {
if (!newValue.isEmpty() && !originComboBox.getValue().isEmpty()){
searchStops(newValue, transportApi, destinationComboBox);
if (destinationComboBox.getItems().contains(newValue) &&
originComboBox.getItems().contains(originComboBox.getValue())){
// Both are autofilled, plotting route
try {
JSONArray connections = transportApi.getConnections(
originComboBox.getValue(), destinationComboBox.getValue());
startStopLabel.setText(originComboBox.getValue() + " - " + destinationComboBox.getValue());
for (int i = 0; i < connections.length(); i++){
// Now iterating over connections
Connection c = new Connection.ConnectionBuilder(connections.getJSONObject(i)).build();
drawConnection(c, 30, 100 + 30 * i);
}
} catch (IOException | ParseException e) {
showSadMessage();
}
}
}
);
}
......
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
......@@ -50,6 +51,7 @@
</font>
</Label>
<Line endX="100.0" layoutX="115.0" layoutY="49.0" startX="-100.0" stroke="#4c7ba8" />
<Canvas id="connectionCanvas" fx:id="connectionCanvas" height="451.0" layoutX="15.0" layoutY="59.0" width="652.0" />
</children>
</Pane>
</center>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment