Skip to content

Commit

Permalink
Merge pull request #158 from Pluiexo/branch-RevampUI
Browse files Browse the repository at this point in the history
Branch revamp UI
  • Loading branch information
tsulim authored Apr 2, 2024
2 parents 7d7b618 + 562ce64 commit 02d7d64
Show file tree
Hide file tree
Showing 19 changed files with 726 additions and 290 deletions.
4 changes: 2 additions & 2 deletions src/main/java/staffconnect/commons/core/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
public class GuiSettings implements Serializable {

private static final double DEFAULT_HEIGHT = 600;
private static final double DEFAULT_WIDTH = 740;
private static final double DEFAULT_HEIGHT = 700;
private static final double DEFAULT_WIDTH = 840;

private final double windowWidth;
private final double windowHeight;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/staffconnect/logic/Logic.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staffconnect.logic;

import java.nio.file.Path;
import java.util.Optional;

import javafx.collections.ObservableList;
import staffconnect.commons.core.GuiSettings;
Expand Down Expand Up @@ -33,6 +34,9 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();

/** Returns the first person if it exists. */
Optional<Person> getFirstPersonIfExist();

/**
* Returns the user prefs' StaffConnect file path.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/staffconnect/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -71,6 +72,13 @@ public ObservableList<Person> getFilteredPersonList() {
return model.getSortedFilteredPersonList();
}

@Override
public Optional<Person> getFirstPersonIfExist() {

return model.getSortedFilteredPersonList().isEmpty()
? Optional.empty() : Optional.of(model.getSortedFilteredPersonList().get(0));
}

@Override
public Path getStaffConnectFilePath() {
return model.getStaffConnectFilePath();
Expand Down
158 changes: 73 additions & 85 deletions src/main/java/staffconnect/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import java.util.logging.Logger;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputControl;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import staffconnect.commons.core.GuiSettings;
Expand Down Expand Up @@ -35,11 +30,10 @@ public class MainWindow extends UiPart<Stage> {
private ResultDisplay resultDisplay;
private HelpWindow helpWindow;

@FXML
private StackPane commandBoxPlaceholder;
private PersonCard personOnDisplay;

@FXML
private MenuItem helpMenuItem;
private StackPane commandBoxPlaceholder;

@FXML
private StackPane personListPanelPlaceholder;
Expand All @@ -50,6 +44,9 @@ public class MainWindow extends UiPart<Stage> {
@FXML
private StackPane statusbarPlaceholder;

@FXML
private StackPane personCardPanelPlaceholder;

/**
* Creates a {@code MainWindow} with the given {@code Stage} and {@code Logic}.
*/
Expand All @@ -63,108 +60,59 @@ public MainWindow(Stage primaryStage, Logic logic) {
// Configure the UI
setWindowDefaultSize(logic.getGuiSettings());

setAccelerators();
personOnDisplay = logic.getFirstPersonIfExist()
.map(PersonCard::new).orElse(new PersonCard());

helpWindow = new HelpWindow();
}

public Stage getPrimaryStage() {
return primaryStage;
}

private void setAccelerators() {
setAccelerator(helpMenuItem, KeyCombination.valueOf("F1"));
}

/**
* Sets the accelerator of a MenuItem.
* @param keyCombination the KeyCombination value of the accelerator
* Sets the default size based on {@code guiSettings}.
*/
private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) {
menuItem.setAccelerator(keyCombination);

/*
* TODO: the code below can be removed once the bug reported here
* https://bugs.openjdk.java.net/browse/JDK-8131666
* is fixed in later version of SDK.
*
* According to the bug report, TextInputControl (TextField, TextArea) will
* consume function-key events. Because CommandBox contains a TextField, and
* ResultDisplay contains a TextArea, thus some accelerators (e.g F1) will
* not work when the focus is in them because the key event is consumed by
* the TextInputControl(s).
*
* For now, we add following event filter to capture such key events and open
* help window purposely so to support accelerators even when focus is
* in CommandBox or ResultDisplay.
*/
getRoot().addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getTarget() instanceof TextInputControl && keyCombination.match(event)) {
menuItem.getOnAction().handle(new ActionEvent());
event.consume();
}
});
private void setWindowDefaultSize(GuiSettings guiSettings) {
primaryStage.setHeight(guiSettings.getWindowHeight());
primaryStage.setWidth(guiSettings.getWindowWidth());
if (guiSettings.getWindowCoordinates() != null) {
primaryStage.setX(guiSettings.getWindowCoordinates().getX());
primaryStage.setY(guiSettings.getWindowCoordinates().getY());
}
}

public Stage getPrimaryStage() {
return primaryStage;
}

/**
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
personListPanel = new PersonListPanel(logic.getFilteredPersonList());

personListPanel = new PersonListPanel(logic.getFilteredPersonList(), this::changePersonCard);
personListPanel.setListSelectedIndex(0);
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());

StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getStaffConnectFilePath());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

CommandBox commandBox = new CommandBox(this::executeCommand);
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
}

/**
* Sets the default size based on {@code guiSettings}.
*/
private void setWindowDefaultSize(GuiSettings guiSettings) {
primaryStage.setHeight(guiSettings.getWindowHeight());
primaryStage.setWidth(guiSettings.getWindowWidth());
if (guiSettings.getWindowCoordinates() != null) {
primaryStage.setX(guiSettings.getWindowCoordinates().getX());
primaryStage.setY(guiSettings.getWindowCoordinates().getY());
}
}
StatusBarFooter statusBarFooter =
new StatusBarFooter(logic.getStaffConnectFilePath(), this::handleExit, this::handleHelp);
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());

/**
* Opens the help window or focuses on it if it's already opened.
*/
@FXML
public void handleHelp() {
if (!helpWindow.isShowing()) {
helpWindow.show();
} else {
helpWindow.focus();
}
}

void show() {
primaryStage.show();
}

/**
* Closes the application.
*/
@FXML
private void handleExit() {
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY());
logic.setGuiSettings(guiSettings);
helpWindow.hide();
primaryStage.hide();
}

public PersonListPanel getPersonListPanel() {
return personListPanel;
private void changePersonCard(PersonCard personToUpdate) {

personOnDisplay = personToUpdate;
personCardPanelPlaceholder.getChildren().clear();
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());

}

/**
Expand All @@ -178,6 +126,8 @@ private CommandResult executeCommand(String commandText) throws CommandException
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());

reloadPersonCardWithRoot();

if (commandResult.isShowHelp()) {
handleHelp();
}
Expand All @@ -193,4 +143,42 @@ private CommandResult executeCommand(String commandText) throws CommandException
throw e;
}
}

/**
* Closes the application.
*/
@FXML
public void handleExit() {
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY());
logic.setGuiSettings(guiSettings);
helpWindow.hide();
primaryStage.hide();
}

/**
* Opens the help window or focuses on it if it's already opened.
*/
public void handleHelp() {
if (!helpWindow.isShowing()) {
helpWindow.show();
} else {
helpWindow.focus();
}
}

private void reloadPersonCardWithRoot() {

personOnDisplay = logic.getFirstPersonIfExist().map(PersonCard::new).orElse(new PersonCard());

personCardPanelPlaceholder.getChildren().clear();
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());

personListPanel.setListSelectedIndex(0);

}

void show() {
primaryStage.show();
}
}
33 changes: 33 additions & 0 deletions src/main/java/staffconnect/ui/NameCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package staffconnect.ui;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import staffconnect.model.person.Person;

/**
* A UI component that displays the name only of a {@code Person}.
*/
public class NameCard extends UiPart<Region> {

private static final String FXML = "NameCard.fxml";
@javafx.fxml.FXML
private Label id;
@FXML
private Label name;

@FXML
private Label favourite;

/**
* Creates a {@code NameCard} with the given {@code Person} and index to display.
*/
public NameCard(Person person, int index) {
super(FXML);
id.setText(index + ". ");
name.setText(person.getName().fullName);
favourite.setStyle("-fx-text-fill: accent-orange");
favourite.setText(person.getFavourite().toDisplayString() + " ");
}

}
Loading

0 comments on commit 02d7d64

Please sign in to comment.