Re-engineered fxml controllers to use child controllers

- Removed a lot of unnecessary complexity in App class

#pair[jam339, esa46] #story[760]
main
Erika Savell 9 years ago
parent a689ffc689
commit 0772ca22c6

@ -6,10 +6,6 @@ import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import seng302.Controllers.Controller;
import seng302.Controllers.MainController;
import java.io.InputStream;
public class App extends Application { public class App extends Application {
Stage primaryStage; Stage primaryStage;
@ -25,54 +21,66 @@ public class App extends Application {
launch(args); launch(args);
} }
/** public void start(Stage stage) throws Exception {
* Loads and sets up the GUI elements
*
* @param primaryStage Base for all scenes
* @throws Exception Error in initialising programme
*/
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
primaryStage.minHeightProperty().setValue(600);
primaryStage.minWidthProperty().setValue(780);
//load the first container
try {
FXMLLoader loader = new FXMLLoader();
InputStream in = getClass().getClassLoader().getResourceAsStream("scenes/mainpane.fxml");
mainContainer = (BorderPane) loader.load(in);
mainScene = new Scene(mainContainer, 1200, 800);
primaryStage.setScene(mainScene);
primaryStage.sizeToScene();
MainController mainController = (MainController) loader.getController();
mainController.setParent(this);
in.close();
//add the center
loadPane("racepane.fxml");
} catch (Exception e) {
e.printStackTrace();
}
primaryStage.show();
}
/** FXMLLoader loader = new FXMLLoader(getClass().getResource("/scenes/main.fxml"));
* Loads panes for use in the GUI Parent root = loader.load();
* Scene scene = new Scene(root, 1050, 835);
* @param fxmlName name of resource fxml file stage.setScene(scene);
* @throws Exception critical error in loading file stage.setMinHeight(800);
*/ stage.setMinWidth(1200);
public void loadPane(String fxmlName) throws Exception { stage.show();
FXMLLoader loader = new FXMLLoader();
InputStream in = getClass().getClassLoader().getResourceAsStream("scenes/" + fxmlName);
Parent page;
try {
page = (Parent) loader.load(in);
} finally {
in.close();
}
mainContainer.getChildren().remove(mainContainer.getCenter());
mainContainer.setCenter(page);
Controller controller = (Controller) loader.getController();
controller.setParent(this);
} }
// /**
// * Loads and sets up the GUI elements
// *
// * @param primaryStage Base for all scenes
// * @throws Exception Error in initialising programme
// */
// @Override
// public void start(Stage primaryStage) throws Exception {
// this.primaryStage = primaryStage;
// primaryStage.minHeightProperty().setValue(600);
// primaryStage.minWidthProperty().setValue(780);
// //load the first container
// try {
// FXMLLoader loader = new FXMLLoader();
// InputStream in = getClass().getClassLoader().getResourceAsStream("scenes/main.fxml");
// mainContainer = (BorderPane) loader.load(in);
// mainScene = new Scene(mainContainer, 1200, 800);
// primaryStage.setScene(mainScene);
// primaryStage.sizeToScene();
// MainController mainController = (MainController) loader.getController();
// mainController.setParent(this);
// in.close();
// //add the center
// loadPane("race.fxml");
// } catch (Exception e) {
// e.printStackTrace();
// }
// primaryStage.show();
// }
//
// /**
// * Loads panes for use in the GUI
// *
// * @param fxmlName name of resource fxml file
// * @throws Exception critical error in loading file
// */
// public void loadPane(String fxmlName) throws Exception {
// FXMLLoader loader = new FXMLLoader();
// InputStream in = getClass().getClassLoader().getResourceAsStream("scenes/" + fxmlName);
// Parent page;
// try {
// page = (Parent) loader.load(in);
// } finally {
// in.close();
// }
// mainContainer.getChildren().remove(mainContainer.getCenter());
// mainContainer.setCenter(page);
// Controller controller = (Controller) loader.getController();
// controller.setParent(this);
// }
} }

@ -11,27 +11,17 @@ import java.util.ResourceBundle;
* Created by fwy13 on 15/03/2017. * Created by fwy13 on 15/03/2017.
*/ */
public abstract class Controller implements Initializable { public abstract class Controller implements Initializable {
protected App parent; protected Controller parent;
/** /**
* Sets the parent of the application * Sets the parent of the application
* *
* @param parent controller * @param parent controller
*/ */
public void setParent(App parent) { public void setParent(Controller parent) {
this.parent = parent; this.parent = parent;
} }
/**
* Sets the loads a pane into the parent.
*
* @param fxmlName fxml resource file to be loaded
* @throws Exception error in loading file
*/
public void loadPane(String fxmlName) throws Exception {
this.parent.loadPane(fxmlName);
}
/** /**
* Initialisation class that is run on start up. * Initialisation class that is run on start up.
* *

@ -1,5 +1,9 @@
package seng302.Controllers; package seng302.Controllers;
import javafx.fxml.FXML;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.GridPane;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -7,6 +11,11 @@ import java.util.ResourceBundle;
* Created by fwy13 on 15/03/2017. * Created by fwy13 on 15/03/2017.
*/ */
public class MainController extends Controller { public class MainController extends Controller {
@FXML StartController startController;
@FXML RaceController raceController;
/** /**
* Main Controller for the applications will house the menu and the displayed pane. * Main Controller for the applications will house the menu and the displayed pane.
@ -16,6 +25,7 @@ public class MainController extends Controller {
*/ */
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
startController.setParent(this);
raceController.setParent(this);
} }
} }

@ -1,14 +1,12 @@
package seng302.Controllers; package seng302.Controllers;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.util.Callback;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import seng302.Model.*; import seng302.Model.*;
import seng302.RaceXMLReader; import seng302.RaceXMLReader;
@ -31,7 +29,7 @@ public class RaceController extends Controller {
@FXML @FXML
GridPane startScreen; GridPane startScreen;
@FXML @FXML
SplitPane ongoingRacePane; SplitPane racepane;
@FXML @FXML
CheckBox showFPS; CheckBox showFPS;
@FXML @FXML
@ -79,26 +77,6 @@ public class RaceController extends Controller {
boatPlacingColumn.setCellValueFactory(cellData -> cellData.getValue().positionProperty()); boatPlacingColumn.setCellValueFactory(cellData -> cellData.getValue().positionProperty());
} }
/**
* Begins the race with a scale factor of 15
*/
public void startRace1Min() {
startRace(15);
}
/**
* Begins the race with a scale factor of 3
*/
public void startRace5Min() {
startRace(3);
}
/**
* Begins the race with a scale factor of 1
*/
public void startRaceNoScaling() {
startRace(1);
}
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@ -163,7 +141,7 @@ public class RaceController extends Controller {
canvasBase.getChildren().add(raceMap); canvasBase.getChildren().add(raceMap);
startScreen.setVisible(false); startScreen.setVisible(false);
ongoingRacePane.setVisible(true); racepane.setVisible(true);
initializeFPS(); initializeFPS();

@ -0,0 +1,43 @@
package seng302.Controllers;
import java.net.URL;
import java.util.ResourceBundle;
/**
* Created by esa46 on 6/04/17.
*/
public class StartController extends Controller {
/**
* Begins the race with a scale factor of 15
*/
public void startRace1Min() {
startRace(15);
}
/**
* Begins the race with a scale factor of 3
*/
public void startRace5Min() {
startRace(3);
}
/**
* Begins the race with a scale factor of 1
*/
public void startRaceNoScaling() {
startRace(1);
}
private void startRace(Integer raceScale){
}
@Override
public void initialize(URL location, ResourceBundle resources){
}
}

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane fx:id="main" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.MainController">
<children>
<fx:include source="race.fxml" fx:id="race"/>
<fx:include source="start.fxml" fx:id="start"/>
</children>
</AnchorPane>

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="seng302.Controllers.MainController"/>

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<SplitPane fx:id="race" dividerPositions="0.7" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController">
<items>
<GridPane fx:id="canvasBase">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Pane prefHeight="200.0" prefWidth="400.0" GridPane.halignment="LEFT" GridPane.valignment="TOP">
<children>
<TitledPane fx:id="userControl" animated="false" text="User Control" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="77.0" prefWidth="200.0">
<children>
<CheckBox fx:id="showFPS" mnemonicParsing="false" selected="true" text="Show FPS" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<CheckBox fx:id="showAnnotations" mnemonicParsing="false" selected="true" text="Show Annotation" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="30.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</Pane>
<Label fx:id="timer" layoutX="45.0" layoutY="146.0" text="0:0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" GridPane.halignment="RIGHT" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label fx:id="FPS" text="FPS: 0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" GridPane.halignment="LEFT" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
</children>
</GridPane>
<AnchorPane layoutX="450.0" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="200.0" GridPane.columnIndex="1">
<children>
<TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="600.0" prefWidth="264.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place" />
<TableColumn fx:id="boatTeamColumn" prefWidth="100.0" text="Team" />
<TableColumn fx:id="boatMarkColumn" prefWidth="130.0" text="Mark" />
<TableColumn fx:id="boatSpeedColumn" prefWidth="75.0" text="Speed" />
</columns>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>

@ -1,113 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController">
<children>
<GridPane fx:id="startScreen" prefHeight="600.0" prefWidth="780.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="189.0" minWidth="10.0" prefWidth="93.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="372.0" minWidth="10.0" prefWidth="184.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="394.0" minWidth="10.0" prefWidth="192.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="416.0" minWidth="10.0" prefWidth="273.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="416.0" minWidth="10.0" prefWidth="57.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="241.0" minHeight="10.0" prefHeight="102.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="383.0" minHeight="10.0" prefHeight="227.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="369.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="369.0" minHeight="10.0" prefHeight="38.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="178.0" minHeight="10.0" prefHeight="178.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Your Race Scaling:" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="23.0" />
</font>
</Text>
<Button mnemonicParsing="false" onAction="#startRace1Min" text="15x faster" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button mnemonicParsing="false" onAction="#startRaceNoScaling" text="No scaling" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Button mnemonicParsing="false" onAction="#startRace5Min" text="3x faster" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Label alignment="CENTER" text="Race will take ~1 minute" GridPane.columnIndex="1" GridPane.rowIndex="3">
<opaqueInsets>
<Insets />
</opaqueInsets>
<font>
<Font size="10.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="99.0" layoutY="407.0" text="Race will take ~5 minutes" GridPane.columnIndex="2" GridPane.rowIndex="3">
<font>
<Font size="10.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Label>
<Label alignment="CENTER" layoutX="279.0" layoutY="407.0" text="Race will take ~15 minutes" GridPane.columnIndex="3" GridPane.rowIndex="3">
<font>
<Font size="10.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Label>
</children>
</GridPane>
<SplitPane fx:id="ongoingRacePane" dividerPositions="0.7" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<GridPane fx:id="canvasBase">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Pane prefHeight="200.0" prefWidth="400.0" GridPane.halignment="LEFT" GridPane.valignment="TOP">
<children>
<TitledPane fx:id="userControl" animated="false" text="User Control" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="77.0" prefWidth="200.0">
<children>
<CheckBox fx:id="showFPS" mnemonicParsing="false" selected="true" text="Show FPS" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<CheckBox fx:id="showAnnotations" mnemonicParsing="false" selected="true" text="Show Annotation" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="30.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</Pane>
<Label fx:id="timer" layoutX="45.0" layoutY="146.0" text="0:0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" GridPane.halignment="RIGHT" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label fx:id="FPS" text="FPS: 0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" GridPane.halignment="LEFT" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
</children>
</GridPane>
<AnchorPane layoutX="450.0" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="200.0" GridPane.columnIndex="1">
<children>
<TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="600.0" prefWidth="264.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place" />
<TableColumn fx:id="boatTeamColumn" prefWidth="100.0" text="Team" />
<TableColumn fx:id="boatMarkColumn" prefWidth="130.0" text="Mark" />
<TableColumn fx:id="boatSpeedColumn" prefWidth="75.0" text="Speed" />
</columns>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:id="start" prefHeight="600.0" prefWidth="780.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.StartController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="189.0" minWidth="10.0" prefWidth="93.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="372.0" minWidth="10.0" prefWidth="184.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="394.0" minWidth="10.0" prefWidth="192.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="416.0" minWidth="10.0" prefWidth="273.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="416.0" minWidth="10.0" prefWidth="57.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="241.0" minHeight="10.0" prefHeight="102.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="383.0" minHeight="10.0" prefHeight="227.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="369.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="369.0" minHeight="10.0" prefHeight="38.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="178.0" minHeight="10.0" prefHeight="178.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Your Race Scaling:" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="23.0" />
</font>
</Text>
<Button mnemonicParsing="false" onAction="#startRace1Min" text="15x faster" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button mnemonicParsing="false" onAction="#startRaceNoScaling" text="No scaling" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Button mnemonicParsing="false" onAction="#startRace5Min" text="3x faster" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Label alignment="CENTER" text="Race will take ~1 minute" GridPane.columnIndex="1" GridPane.rowIndex="3">
<opaqueInsets>
<Insets />
</opaqueInsets>
<font>
<Font size="10.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="99.0" layoutY="407.0" text="Race will take ~5 minutes" GridPane.columnIndex="2" GridPane.rowIndex="3">
<font>
<Font size="10.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Label>
<Label alignment="CENTER" layoutX="279.0" layoutY="407.0" text="Race will take ~15 minutes" GridPane.columnIndex="3" GridPane.rowIndex="3">
<font>
<Font size="10.0" />
</font>
<opaqueInsets>
<Insets />
</opaqueInsets>
</Label>
</children>
</GridPane>
Loading…
Cancel
Save