Skip to content

Commit

Permalink
Merge branch 'master' into branch-Search1
Browse files Browse the repository at this point in the history
  • Loading branch information
yyihaoc committed Oct 16, 2024
2 parents d9643be + 158bfb9 commit 32bfe2b
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 59 deletions.
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Objects;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Person;

/**
* Represents the result of a command execution.
Expand All @@ -19,6 +20,24 @@ public class CommandResult {
/** The application should exit. */
private final boolean exit;

private boolean isView = false;

private Person person = null;

/**
* Constructs a {@code CommandResult} that's specifically a 'view' command
* @param feedbackToUser
* @param isView boolean value that's always initialised to 'true'
* @param person Person object initialised to 'person' field
*/
public CommandResult(String feedbackToUser, boolean isView, Person person) {
this.feedbackToUser = feedbackToUser;
this.isView = isView;
this.showHelp = false;
this.exit = false;
this.person = person;
}

/**
* Constructs a {@code CommandResult} with the specified fields.
*/
Expand Down Expand Up @@ -48,6 +67,14 @@ public boolean isExit() {
return exit;
}

public boolean isView() {
return this.isView;
}

public Person getViewPerson() {
return this.person;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -17,7 +16,7 @@ public class ViewCommand extends Command {

public static final String COMMAND_WORD = "view";

public static final String MESSAGE_SUCCESS = "Contact Info:\n";
public static final String MESSAGE_SUCCESS = "Viewing contact now...";

public static final String MESSAGE_NO_SUCH_TELEGRAM =
"There is no one in your address book with the telegram handle: @";
Expand All @@ -38,26 +37,15 @@ public ViewCommand(String tele) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
List<Person> lastShownList = model.getFilteredPersonList();
List<Person> p = lastShownList.stream().filter(person ->
person.getTelegram().value.toLowerCase().equals(this.tele)).toList();
if (p.isEmpty()) {
throw new CommandException(MESSAGE_NO_SUCH_TELEGRAM + this.tele);
}
Person person = p.get(0);
return new CommandResult(MESSAGE_SUCCESS + generateContactInformation(person));
return new CommandResult(MESSAGE_SUCCESS, true, person);
}

/**
* Formulates a message that displays all information of the specified contact
* @param p Person to which all their contact information is to be displayed
* @return String message which contains all the information of the specified contact
*/
public String generateContactInformation(Person p) {
Field[] fields = Person.class.getDeclaredFields();
StringBuilder contactInfo = new StringBuilder("");
Arrays.stream(fields).forEach(field -> contactInfo.append(field.getName().toUpperCase()
+ ": " + p.getString(field.getType()) + "\n"));
return contactInfo.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public ViewCommand parse(String args) throws ParseException {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE));
}

String tele = argMultiMap.getValue(PREFIX_TELEGRAM).get();
// ERROR! 'tele' here gives an empty string ""...
return new ViewCommand(tele);
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -106,6 +108,18 @@ public String getString(Class c) {
}
}

/**
* Formulates a message that displays all information of the specified contact
* @return String message which contains all the information of the specified contact
*/
public String generateContactInformation() {
Field[] fields = Person.class.getDeclaredFields();
StringBuilder contactInfo = new StringBuilder("");
Arrays.stream(fields).forEach(field -> contactInfo.append(field.getName().toUpperCase()
+ ": " + this.getString(field.getType()) + "\n"));
return contactInfo.toString();
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import javafx.scene.control.TextInputControl;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.Logic;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Person;

/**
* The Main Window. Provides the basic application layout containing
Expand All @@ -34,6 +37,16 @@ public class MainWindow extends UiPart<Stage> {
private PersonListPanel personListPanel;
private ResultDisplay resultDisplay;
private HelpWindow helpWindow;
private ViewPersonCard viewPersonCard;

@FXML
private VBox personList;

@FXML
private VBox viewPersonSection;

@FXML
private Pane viewPersonCardPlaceholder;

@FXML
private StackPane commandBoxPlaceholder;
Expand Down Expand Up @@ -123,6 +136,21 @@ void fillInnerParts() {
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());
}

/**
* Opens up ViewPersonCard of specified person
*/
void openViewPersonCard(Person person) {
viewPersonSection.setVisible(true);
viewPersonSection.setManaged(true);

personList.setVisible(false);
personList.setManaged(false);

viewPersonCardPlaceholder.getChildren().clear();
this.viewPersonCard = new ViewPersonCard(person);
viewPersonCardPlaceholder.getChildren().add(viewPersonCard.getRoot());
}

/**
* Sets the default size based on {@code guiSettings}.
*/
Expand Down Expand Up @@ -173,6 +201,12 @@ public PersonListPanel getPersonListPanel() {
* @see seedu.address.logic.Logic#execute(String)
*/
private CommandResult executeCommand(String commandText) throws CommandException, ParseException {
viewPersonSection.setVisible(false);
viewPersonSection.setManaged(false);

personList.setVisible(true);
personList.setManaged(true);

try {
CommandResult commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult.getFeedbackToUser());
Expand All @@ -186,6 +220,10 @@ private CommandResult executeCommand(String commandText) throws CommandException
handleExit();
}

if (commandResult.isView()) {
openViewPersonCard(commandResult.getViewPerson());
}

return commandResult;
} catch (CommandException | ParseException e) {
logger.info("An error occurred while executing command: " + commandText);
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/seedu/address/ui/ViewPersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.util.logging.Logger;

import javafx.collections.ObservableList;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.Person;
Expand All @@ -16,16 +17,18 @@ public class ViewPersonCard extends UiPart<Region> {
private static final String FXML = "ViewPersonCard.fxml";
private final Logger logger = LogsCenter.getLogger(ViewPersonCard.class);

private final StringProperty info;

@FXML
private ListView<Person> personListView;
private Label personInfo;

/**
* Creates a {@code ViewPersonCard} with the given {@code ObservableList}.
* Creates a {@code ViewPersonCard} with the given {@code Person}.
*/
public ViewPersonCard(ObservableList<Person> personList) {
public ViewPersonCard(Person person) {
super(FXML);
info = new SimpleStringProperty(person.generateContactInformation());
personInfo.textProperty().bind(info);
}


}

11 changes: 9 additions & 2 deletions src/main/resources/view/MainWindow.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.layout.Pane?>
<fx:root type="javafx.stage.Stage" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1"
title="Address App" minWidth="450" minHeight="600" onCloseRequest="#handleExit">
<icons>
Expand Down Expand Up @@ -50,7 +50,14 @@
<padding>
<Insets top="10" right="10" bottom="10" left="10" />
</padding>
<StackPane fx:id="personListPanelPlaceholder" VBox.vgrow="ALWAYS"/>
<StackPane fx:id="personListPanelPlaceholder" VBox.vgrow="ALWAYS" />
</VBox>

<VBox fx:id="viewPersonSection" styleClass="pane-with-border" minWidth="340" prefWidth="340" VBox.vgrow="ALWAYS" managed="false" visible="false">
<padding>
<Insets top="10" right="10" bottom="10" left="10" />
</padding>
<Pane fx:id="viewPersonCardPlaceholder" VBox.vgrow="ALWAYS"/>
</VBox>

<StackPane fx:id="statusbarPlaceholder" VBox.vgrow="NEVER" />
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/view/ViewPersonCard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#personInfo {
-fx-text-fill: white;
-fx-font-size: 20px;
}

#personTitle {
-fx-underline: true;
-fx-text-fill: white;
-fx-font-size: 30px;
-fx-font-weight: bolder;
}
40 changes: 8 additions & 32 deletions src/main/resources/view/ViewPersonCard.fxml
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<HBox id="cardPane" fx:id="cardPane" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<GridPane HBox.hgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10" prefWidth="150" />
</columnConstraints>
<VBox alignment="CENTER_LEFT" minHeight="105" GridPane.columnIndex="0">
<padding>
<Insets top="5" right="5" bottom="5" left="15" />
</padding>
<HBox spacing="0.5" alignment="CENTER_LEFT">
<Label fx:id="id" styleClass="cell_big_label">
<minWidth>
<!-- Ensures that the label text is never truncated -->
<Region fx:constant="USE_PREF_SIZE" />
</minWidth>
</Label>
<Label fx:id="name" text="\$first" styleClass="cell_big_label" />
</HBox>
<FlowPane fx:id="tags" />
<Label fx:id="phone" styleClass="cell_small_label" text="\$phone" />
<Label fx:id="telegram" styleClass="cell_small_label" text="\$telegram" />
<Label fx:id="email" styleClass="cell_small_label" text="\$email" />
</VBox>
</GridPane>
</HBox>

<?import java.net.URL?>
<VBox xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<Label id="personTitle" text="Contact Info" />
<Label fx:id="personInfo" text="{info}"/>
<stylesheets>
<URL value="@ViewPersonCard.css"/>
</stylesheets>
</VBox>

0 comments on commit 32bfe2b

Please sign in to comment.