Skip to content

Commit

Permalink
Merge 69dd1d7 into d88513c
Browse files Browse the repository at this point in the history
  • Loading branch information
sreesubbash committed Nov 3, 2019
2 parents d88513c + 69dd1d7 commit 71af81f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 36 deletions.
60 changes: 44 additions & 16 deletions src/main/java/seedu/address/logic/parser/ParserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ public class ParserManager {
* and sets currentParser to HomeMode
*/
public ParserManager() {
this.mode = ModeEnum.HOME;

this.gameIsOver = true;

this.switchParser = new SpecificModeParser();

switchParser.add(SwitchToOpenCommand.class, null);
switchParser.add(SwitchToHomeCommand.class, null);
switchParser.add(SwitchToStartCommand.class, StartCommandParser.class);
switchParser.add(SwitchToSettingsCommand.class, null);

this.mode = ModeEnum.HOME;

this.currentParser = setCurrentParser(this.mode);
}

Expand All @@ -115,7 +120,9 @@ public ModeEnum getMode() {
private SpecificModeParser setCurrentParser(ModeEnum mode) {

SpecificModeParser temp = new SpecificModeParser();

switch (this.mode) {

case OPEN:
temp.add(AddCommand.class, AddCommandParser.class);
temp.add(EditCommand.class, EditCommandParser.class);
Expand All @@ -125,6 +132,7 @@ private SpecificModeParser setCurrentParser(ModeEnum mode) {
temp.add(ListCommand.class, ListCommandParser.class);
temp.add(SwitchToExitCommand.class, null);
return temp;

case HOME:
temp.add(SelectCommand.class, BankCommandParser.class);
temp.add(ImportCommand.class, ImportCommandParser.class);
Expand All @@ -134,22 +142,26 @@ private SpecificModeParser setCurrentParser(ModeEnum mode) {
temp.add(HelpCommand.class, null);
temp.add(SwitchToExitCommand.class, null);
return temp;

case SETTINGS:
temp.add(DifficultyCommand.class, DifficultyCommandParser.class);
temp.add(HintsCommand.class, HintsCommandParser.class);
temp.add(ThemeCommand.class, ThemeCommandParser.class);
temp.add(AvatarCommand.class, AvatarCommandParser.class);
temp.add(SwitchToExitCommand.class, null);
return temp;

case GAME:
temp.add(GuessCommand.class, GuessCommandParser.class);
temp.add(SkipCommand.class, null);
temp.add(StopCommand.class, null);
temp.add(SwitchToExitCommand.class, null);
return temp;

default:
return null;
}

}


Expand All @@ -171,16 +183,22 @@ public void updateState(boolean bankLoaded, boolean gameIsOver) {
* @return List of AutoFillActions
*/
public List<AutoFillAction> getAutoFill(String input) {
List<AutoFillAction> temp = new ArrayList<>();

List<AutoFillAction> result = new ArrayList<>();

if (gameIsOver && bankLoaded) {
for (AutoFillAction action : switchParser.getAutoFill(input)) {
temp.add(action);
result.add(action);
}
}
for (AutoFillAction action : currentParser.getAutoFill(input)) {
temp.add(action);

if (!(gameIsOver && mode == ModeEnum.GAME)) {
for (AutoFillAction action : currentParser.getAutoFill(input)) {
result.add(action);
}
}
return temp;

return result;
}


Expand All @@ -192,21 +210,28 @@ public List<AutoFillAction> getAutoFill(String input) {
* @throws ParseException if the user input does not conform the expected format
*/
public Command parseCommand(String userInput) throws ParseException, ModeSwitchException {
Command temp = null;
if (gameIsOver && bankLoaded) {
temp = switchParser.parseCommand(userInput);

SwitchCommand switchCommand = null;
Command currentModeCommand = null;

if (bankLoaded && gameIsOver) {
switchCommand = (SwitchCommand) switchParser.parseCommand(userInput);
}

if (!(mode == ModeEnum.GAME && gameIsOver)) {
currentModeCommand = currentParser.parseCommand(userInput);
}
if (temp != null) {
SwitchCommand switchCommand = (SwitchCommand) temp;

if (switchCommand != null) {
mode = switchCommand.getNewMode(mode);
currentParser = setCurrentParser(mode);
return switchCommand;
} else if (currentModeCommand != null) {
return currentModeCommand;
} else {
temp = currentParser.parseCommand(userInput);
}
if (temp != null) {
return temp;
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);

}

/**
Expand All @@ -215,13 +240,16 @@ public Command parseCommand(String userInput) throws ParseException, ModeSwitchE
* @return a list of ModeEnum
*/
public List<ModeEnum> getModes() {

List<ModeEnum> temp = new ArrayList<>();

if (gameIsOver && bankLoaded) {
temp.add(ModeEnum.OPEN);
temp.add(ModeEnum.HOME);
temp.add(ModeEnum.SETTINGS);
temp.add(ModeEnum.GAME);
}

return temp;
}

Expand Down
62 changes: 43 additions & 19 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class CommandBox extends UiPart<Region> {
private Label modesLabel;

@FXML
private MenuBar commandMenuField;
private MenuBar autoFillBar;

private Menu temp;
private List<Menu> currentMenus;
Expand All @@ -63,81 +63,104 @@ public CommandBox(CommandExecutor commandExecutor, UiLogicHelper uiLogicHelper)

initialiseText();
fillMenu();
fillCombo();
fillAvailableModes();

}

/**
* Sets change detection callback to textfield
* Sets change detection callback to textfield.
*/
private void initialiseText() {
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
commandTextField.textProperty().addListener((observable, oldCommand, newCommand) -> setStyleToDefault());

commandTextField.textProperty().addListener((observable, oldCommand, newCommand) -> {
//System.out.println("command changing from " + oldCommand + " to " + newCommand);
updateMenu(oldCommand, newCommand);
});
}

/**
* updates menu based on changes detected
* Updates menu based on changes detected.
* @param oldCommand in text field
* @param newCommand in text field
*/
private void updateMenu(String oldCommand, String newCommand) {
commandMenuField.getMenus().clear();

autoFillBar.getMenus().clear();

for (Action temp : uiLogicHelper.getMenuItems(newCommand)) {

Label label = new Label(temp.toString());

label.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
commandTextField.setText(temp.toString() + " ");
commandTextField.positionCaret(temp.toString().length() + 1);
}
});

Menu button = new Menu();
button.setGraphic(label);
commandMenuField.getMenus().add(button);

autoFillBar.getMenus().add(button);
}
}

/**
* Fills in Menu bar in UI
*/
private void fillMenu() {

for (Action temp : uiLogicHelper.getMenuItems("")) {

Label label = new Label(temp.toString());

label.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
commandTextField.setText(temp.toString() + " ");
commandTextField.positionCaret(temp.toString().length() + 1);
}
});

Menu button = new Menu();
button.setGraphic(label);
commandMenuField.getMenus().add(button);

autoFillBar.getMenus().add(button);
}
}

/**
* Fills in combo in ui
* Fills in available modes.
*/
private void fillCombo() {
modeLabel.setText(uiLogicHelper.getMode().toString());
private void fillAvailableModes() {

String currentModeString = uiLogicHelper.getMode().toString();

modeLabel.setText(currentModeString);

boolean otherModesExist = false;
String modes = " | ";
List<ModeEnum> temp = uiLogicHelper.getModes();
List<ModeEnum> otherModes = uiLogicHelper.getModes();

for (ModeEnum mode : otherModes) {

String otherModeString = mode.toString();

if (!currentModeString.equals(otherModeString)) {

modes += (otherModeString + " | ");
otherModesExist = true;

for (ModeEnum mode : temp) {
if (!uiLogicHelper.getMode().toString().equals(mode.toString())) {
modes += (mode.toString() + " | ");
}

}

if (modes.equals(" | ")) {
if (!otherModesExist) {
modes = "None";
}

modesLabel.setText(modes);
}

Expand All @@ -147,13 +170,14 @@ private void fillCombo() {
*/
@FXML
private void handleCommandEntered() {

try {
commandExecutor.execute(commandTextField.getText());
} catch (CommandException | ParseException e) {
setStyleToIndicateCommandFailure();
}
fillCombo();
//commandComboField.setValue(uiLogicHelper.getMode().toString());

fillAvailableModes();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/CommandBox.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

</children>
</HBox>
<MenuBar fx:id="commandMenuField" minHeight="-Infinity" prefHeight="30.0" prefWidth="1077.0">
<MenuBar fx:id="autoFillBar" minHeight="-Infinity" prefHeight="30.0" prefWidth="1077.0">
<menus>
</menus>
</MenuBar>
Expand Down

0 comments on commit 71af81f

Please sign in to comment.