Created new abstract Controller class with load methods.

- loadScene method to load into current stage
- loadPopupScene method to load into a new window
- icon added to popup windows
- load methods return Controller of the new scee
- KeyBindingsController extends new Controller class and works correctly

#story[1261]
main
Jessica Syder 8 years ago
parent 1073113589
commit 4a0d04d7ab

@ -1,6 +1,7 @@
package visualiser.Controllers;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
@ -13,6 +14,7 @@ import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import visualiser.Controllers2.Controller;
import visualiser.gameController.Keys.ControlKey;
import visualiser.gameController.Keys.KeyFactory;
@ -25,7 +27,7 @@ import static visualiser.app.App.keyFactory;
/**
* Controller for the scene used to display and update current key bindings.
*/
public class KeyBindingsController {
public class KeyBindingsController extends Controller{
private @FXML Button btnSave;
private @FXML Button btnCancel;
private @FXML Button btnReset;
@ -43,6 +45,7 @@ public class KeyBindingsController {
initializeTable();
populateTable();
setKeyListener();
setClosedListener();
}
/**
@ -115,6 +118,29 @@ public class KeyBindingsController {
return newKeyFactory;
}
/**
* Creates a listener for when a user tries to close the current window.
*/
public void setClosedListener(){
anchor.sceneProperty().addListener((obsS, oldS, newS) -> {
if (newS != null) {
newS.windowProperty().addListener((obsW, oldW, newW) -> {
if (newW != null) {
Stage stage = (Stage)newW;
// WE is processed by onExit method
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
if (we.getEventType() == WindowEvent.WINDOW_CLOSE_REQUEST) {
onExit(we);
}
}
});
}
});
}
});
}
/**
* Creates a listener for the base anchorpane for key presses.
* It updates the current key bindings of the {@link KeyFactory} if

@ -3,7 +3,6 @@ package visualiser.Controllers;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import visualiser.app.App;
import visualiser.gameController.ControllerClient;
import visualiser.model.VisualiserBoat;
import visualiser.model.VisualiserRaceEvent;
@ -99,7 +98,7 @@ public class MainController extends Controller {
raceController.setParent(this);
connectionController.setParent(this);
finishController.setParent(this);
titleController.setParent(this);
// titleController.setParent(this);
hostController.setParent(this);
lobbyController.setParent(this);

@ -1,16 +1,11 @@
package visualiser.Controllers;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import visualiser.Controllers2.Controller;
import visualiser.app.App;
import java.io.IOException;
@ -42,7 +37,7 @@ public class TitleController extends Controller {
*/
public void hostAGame() throws IOException {
titleWrapper.setVisible(false);
parent.hostGame();
// parent.hostGame();
App.getStage().setResizable(true);
}
@ -59,7 +54,7 @@ public class TitleController extends Controller {
*/
public void joinAGame() {
titleWrapper.setVisible(false);
parent.enterLobby();
// parent.enterLobby();
App.getStage().setResizable(true);
}
@ -81,7 +76,7 @@ public class TitleController extends Controller {
dayModeRD.setSelected(false);
}
@Override
// @Override
public void initialize(URL location, ResourceBundle resources) {
}
@ -90,26 +85,9 @@ public class TitleController extends Controller {
*/
public void controlBtnPressed(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/visualiser/scenes/keyBindings.fxml"));
Parent layout = loader.load();
Scene scene = new Scene(layout);
Stage popupStage = new Stage();
popupStage.setResizable(false);
popupStage.setTitle("Game Controls");
popupStage.initModality(Modality.WINDOW_MODAL);
popupStage.centerOnScreen();
popupStage.setScene(scene);
popupStage.show();
KeyBindingsController controller = loader.getController();
popupStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
if (we.getEventType() == WindowEvent.WINDOW_CLOSE_REQUEST) {
controller.onExit(we);
}
}
});
} catch (Exception e){
loadPopupScene("/visualiser/scenes/keyBindings.fxml",
"Game Controls", Modality.WINDOW_MODAL);
} catch (IOException e) {
e.printStackTrace();
}
}

@ -0,0 +1,62 @@
package visualiser.Controllers2;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import visualiser.app.App;
import java.io.IOException;
/**
* Abstract controller class to give each subclass the functionality to load
* a new scene into the existing stage, or create a new popup window.
*/
public abstract class Controller {
/**
* Used to load a new scene in the currently open stage.
* @param fxmlUrl the URL of the FXML file to be loaded
* @return the controller of the new scene
* @throws IOException if there is an issue with the fxmlUrl
*/
protected Controller loadScene(String fxmlUrl) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlUrl));
Parent root = loader.load();
Stage stage = App.getStage();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
return loader.getController();
}
/**
* Used to load a scene in a new separate popup stage.
* @param fxmlUrl the URL of the FXML file to be loaded
* @param title title for the new window
* @param modality modality settings for popup window
* @return the controller of the new scene
* @throws IOException if there is an issue with the fxmlUrl
*/
protected Controller loadPopupScene(String fxmlUrl, String title, Modality
modality) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlUrl));
Parent root = loader.load();
Stage stage = new Stage();
stage.initModality(modality);
stage.setTitle(title);
stage.centerOnScreen();
stage.getIcons().add(new Image(getClass().getClassLoader().getResourceAsStream("images/SailIcon.png")));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
return loader.getController();
}
}
Loading…
Cancel
Save