package seng202.group9.GUI; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.MouseEvent; import seng202.group9.Controller.DataException; import seng202.group9.Controller.Dataset; import seng202.group9.Controller.SceneCode; import seng202.group9.Controller.Session; import seng202.group9.Core.FlightPath; import seng202.group9.Core.FlightPoint; import javax.swing.*; import java.util.ArrayList; import java.util.LinkedHashMap; /** * Controller for the Flights Raw Data Scene. * Created by Liam Beckett on 13/09/2016. */ public class FlightRDController extends Controller { private Dataset theDataSet = null; private int currentPathId = 0; private int currentPathIndex = 0; @FXML private TableView flightTableView; @FXML private TableColumn flightIdCol; @FXML private TableColumn flightNameCol; @FXML private TableColumn flightTypeCol; @FXML private TableColumn flightViaCol; @FXML private TableColumn flightAltitudeCol; @FXML private TableColumn flightLatCol; @FXML private TableColumn flightLongCol; @FXML private TableColumn flightHeadCol; @FXML private TableColumn flightLegDisCol; @FXML private TableColumn flightTotDisCol; @FXML ListView flightPathListView; private ObservableList flightList = FXCollections.observableArrayList(); @FXML private TextField flightNameBox; @FXML private TextField flightTypeBox; @FXML private TextField flightViaBox; @FXML private TextField flightAltitudeBox; @FXML private TextField flightLatitudeBox; @FXML private TextField flightLongitudeBox; @FXML private TextField flightHeadingBox; @FXML private TextField flightLegDistBox; @FXML private TextField flightTotDistBox; /** * Loads the Flight paths into the List View and waits for a mouse clicked event for which it will update the table * to display the selected Flight paths points. Called from the MenuController. */ private void flightPathListView() { try { ArrayList flightPaths; flightPaths = theDataSet.getFlightPaths(); for(int i = 0; i() { public void handle(MouseEvent event) { String flightPathDisplayNameClicked = flightPathListView.getSelectionModel().getSelectedItem(); String[] segments = flightPathDisplayNameClicked.split("_"); String pathIdClicked = segments[0]; currentPathIndex = theDataSet.getFlightPaths().indexOf(theDataSet.getFlightPathDictionary() .get(Integer.parseInt(pathIdClicked))); currentPathId = Integer.parseInt(pathIdClicked); ArrayList flightPaths; flightPaths = theDataSet.getFlightPaths(); ArrayList flightPoints = flightPaths.get(currentPathIndex).getFlight(); flightTableView.setItems(FXCollections.observableArrayList(flightPoints)); } }); flightPathListView.setItems(flightList); } catch (Exception e) { e.printStackTrace(); } } /** * Used to load the table for the Flight points initially from the MenuController */ public void load() { theDataSet = getParent().getCurrentDataset(); try { currentPathId = theDataSet.getFlightPaths().get(0).getID(); //Sets the default to the 1st Path } catch (DataException e) { e.printStackTrace(); } flightIdCol.setCellValueFactory(new PropertyValueFactory("ID")); flightNameCol.setCellValueFactory(new PropertyValueFactory("Name")); flightTypeCol.setCellValueFactory(new PropertyValueFactory("Type")); flightViaCol.setCellValueFactory(new PropertyValueFactory("Via")); flightAltitudeCol.setCellValueFactory(new PropertyValueFactory("Altitude")); flightLatCol.setCellValueFactory(new PropertyValueFactory("Latitude")); flightLongCol.setCellValueFactory(new PropertyValueFactory("Longitude")); flightHeadCol.setCellValueFactory(new PropertyValueFactory("Heading")); flightLegDisCol.setCellValueFactory(new PropertyValueFactory("LegDistance")); flightTotDisCol.setCellValueFactory(new PropertyValueFactory("totalDistance")); ArrayList flightPaths; flightPaths = theDataSet.getFlightPaths(); ArrayList flightPoints = flightPaths.get(0).getFlight(); flightTableView.setItems(FXCollections.observableArrayList(flightPoints)); } /** * Will take the inputs from the text fields and adds the point to the current flight path. */ public void addFlightPoint() { try { theDataSet.addFlightPointToPath(currentPathId, flightNameBox.getText(), flightTypeBox.getText(), flightViaBox.getText(), flightAltitudeBox.getText(), flightLatitudeBox.getText(), flightLongitudeBox.getText(), flightHeadingBox.getText(), flightLegDistBox.getText(), flightTotDistBox.getText()); flightNameBox.clear(); flightTypeBox.clear(); flightViaBox.clear(); flightAltitudeBox.clear(); flightLatitudeBox.clear(); flightLongitudeBox.clear(); flightHeadingBox.clear(); flightLegDistBox.clear(); flightTotDistBox.clear(); updateTable(currentPathIndex); } catch ( Exception e ) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Flight Point Data Error"); alert.setHeaderText("Error adding a custom flight point entry."); alert.setContentText(e.getMessage()); } } /** * Creates a pop up dialog which prompts the user for two ICAO airport codes which will use when creating a new path. */ public void newPath() { NewPathPopUp dialogBox = new NewPathPopUp(); dialogBox.display(); String destAirport = dialogBox.getDestinationAirport(); String sourceAirport = dialogBox.getSourceAirport(); if (destAirport != null && sourceAirport != null){ theDataSet.addFlightPath(sourceAirport, destAirport); flightPathListView.getItems().clear(); flightPathListView(); } } /** * Removes the selected point from the table and database. */ public void deletePoint() { FlightPoint toDelete = flightTableView.getSelectionModel().getSelectedItem(); int pathID; try { pathID = toDelete.getIndex(); } catch (DataException e) { e.printStackTrace(); System.err.println("Point is Undeletable as the Index ID is not set."); return; } LinkedHashMap flightPathDict = theDataSet.getFlightPathDictionary(); FlightPath toDeletesPath = flightPathDict.get(pathID); theDataSet.deleteFlightPoint(toDelete, toDeletesPath); currentPathIndex = theDataSet.getFlightPaths().indexOf(theDataSet.getFlightPathDictionary().get(pathID)); updateTable(currentPathIndex); } /** * Loads the pop up for the edit data scene and updates the table when the window is closed. */ public void editPoint() { FlightPoint toEdit = flightTableView.getSelectionModel().getSelectedItem(); try { Session session = getParent().getSession(); session.setCurrentFlightPointID(toEdit.getID()); session.setCurrentFlightPathtID(currentPathId); } catch (DataException e) { e.printStackTrace(); System.err.println("Point is Uneditable as the Index ID is not set."); return; } createPopUpStage(SceneCode.FLIGHT_EDITOR, 600, 289); updateTable(currentPathIndex); } /** * Removes the selected path from the list view of paths and from the database. */ public void deletePath() { String toDeleteStr = flightPathListView.getSelectionModel().getSelectedItem(); String[] segments = toDeleteStr.split("_"); String pathIdClicked = segments[0]; int toDeleteIndex = theDataSet.getFlightPaths().indexOf(theDataSet.getFlightPathDictionary() .get(Integer.parseInt(pathIdClicked))); theDataSet.deleteFlightPath(toDeleteIndex); flightPathListView.getItems().clear(); flightPathListView(); } /** * Updates the table so that when the database is changed (deleted or edited) it still shows the correct data values. * @param currentPathIndex The index of the current path in the Path array list. */ private void updateTable(int currentPathIndex) { ArrayList flightPaths; flightPaths = theDataSet.getFlightPaths(); ArrayList flightPoints = flightPaths.get(currentPathIndex).getFlight(); flightTableView.setItems(FXCollections.observableArrayList(flightPoints)); flightTableView.refresh(); } /** * Will link to the flight analyser when implemented. */ public void flightAnalyser(){ JOptionPane.showMessageDialog(null, "This is not Implemented yet"); } @Override public void loadOnce(){ flightPathListView(); } }