Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S1#86 from jiholim/master
Browse files Browse the repository at this point in the history
[V1.3] UI Update & Refactor
  • Loading branch information
jiholim authored Oct 27, 2018
2 parents 6fb424a + e7e3461 commit a4489b6
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.address.commons.events.ui;

import seedu.address.commons.events.BaseEvent;

/**
* Represents a selection change in the Wish List Panel
*/
public class WishUpdatedEvent extends BaseEvent {

public WishUpdatedEvent() {

}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.core.Messages;
import seedu.address.commons.events.ui.WishUpdatedEvent;
import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.wish.NameContainsKeywordsPredicate;
Expand Down Expand Up @@ -31,6 +33,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredWishList(predicate);
EventsCenter.getInstance().post(new WishUpdatedEvent());
return new CommandResult(
String.format(Messages.MESSAGE_WISHES_LISTED_OVERVIEW, model.getFilteredSortedWishList().size()));
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/seedu/address/ui/BrowserPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.address.ui;

import java.net.URL;
import java.util.logging.Logger;

import com.google.common.eventbus.Subscribe;
Expand All @@ -10,7 +9,6 @@
import javafx.fxml.FXML;
import javafx.scene.layout.Region;
import javafx.scene.web.WebView;
import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.ui.WishPanelSelectionChangedEvent;
import seedu.address.model.wish.Wish;
Expand All @@ -21,28 +19,32 @@
public class BrowserPanel extends UiPart<Region> {

public static final String DEFAULT_PAGE = "default.html";
public static final String SEARCH_PAGE_URL =
"https://se-edu.github.io/addressbook-level4/DummySearchPage.html?name=";
public static final String SEARCH_PAGE_URL = "https://www.google.com";

private static final String FXML = "BrowserPanel.fxml";

private final Logger logger = LogsCenter.getLogger(getClass());

private String agent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) Version/3.0 Mobile/1A543a Safari/419.3";

@FXML
private WebView browser;

public BrowserPanel() {
super(FXML);

// To prevent triggering events for typing inside the loaded Web page.

getRoot().setOnKeyPressed(Event::consume);

browser.getEngine().setJavaScriptEnabled(true);
browser.getEngine().setUserAgent(agent);

loadDefaultPage();
registerAsAnEventHandler(this);
}

private void loadWishPage(Wish wish) {
loadPage(SEARCH_PAGE_URL + wish.getName().fullName);
loadPage(wish.getUrl().toString());
}

public void loadPage(String url) {
Expand All @@ -53,8 +55,7 @@ public void loadPage(String url) {
* Loads a default HTML file with a background that matches the general theme.
*/
private void loadDefaultPage() {
URL defaultPage = MainApp.class.getResource(FXML_FILE_FOLDER + DEFAULT_PAGE);
loadPage(defaultPage.toExternalForm());
loadPage(SEARCH_PAGE_URL);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,10 @@ private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) {
void fillInnerParts() {
WishTransaction transaction = logic.getWishTransaction(); // Access to WishTransaction

wishDetailPanel = new WishDetailPanel();
wishDetailPanel = new WishDetailPanel(transaction);
wishDetailPlaceholder.getChildren().add(wishDetailPanel.getRoot());

wishListPanel = new WishListPanel(logic.getFilteredWishList());
// New constructor below to be used to access WishTransaction in model
// wishListPanel = new WishListPanel(logic.getFilteredWishList(), logic.getWishTransaction());
wishListPanelPlaceholder.getChildren().add(wishListPanel.getRoot());

ResultDisplay resultDisplay = new ResultDisplay();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/ui/SavingHistoryCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.ui;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;

/**
* An UI component that displays information of a {@code Wish}.
*/
public class SavingHistoryCell extends UiPart<Region> {

private static final String FXML = "SavingHistoryCell.fxml";

/**
* Note: Certain keywords such as "location" and "resources" are reserved keywords in JavaFX.
* As a consequence, UI elements' variable names cannot be set to such keywords
* or an exception will be thrown by JavaFX during runtime.
*
* @see <a href="https://github.com/se-edu/addressbook-level4/issues/336">The issue on WishBook level 4</a>
*/

@FXML
private Label history;

public SavingHistoryCell(String savingHistory) {
super(FXML);
history.setText(savingHistory);
}
}
32 changes: 17 additions & 15 deletions src/main/java/seedu/address/ui/WishDetailPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,42 @@ public class WishDetailPanel extends UiPart<Region> {

private WishDetailSavingAmount wishDetailSavingAmount;
private WishDetailSavingHistory wishDetailSavingHistory;
private BrowserPanel wishBrowserPanel;

@FXML
private StackPane wishSavingAmountPlaceholder;

@FXML
private StackPane wishSavingHistoryPlaceholder;

@FXML
private StackPane wishBrowserPlaceHolder;

@FXML
private Label name;

@FXML
private Label remarks;

@FXML
private FlowPane tags;

public WishDetailPanel() {
public WishDetailPanel(WishTransaction wishTransaction) {
super(FXML);

// To prevent triggering events for typing inside the loaded Web page.
getRoot().setOnKeyPressed(Event::consume);

wishDetailSavingAmount = new WishDetailSavingAmount();
wishSavingAmountPlaceholder.getChildren().add(wishDetailSavingAmount.getRoot());

wishDetailSavingHistory = new WishDetailSavingHistory();
wishDetailSavingHistory = new WishDetailSavingHistory(wishTransaction);
wishSavingHistoryPlaceholder.getChildren().add(wishDetailSavingHistory.getRoot());

loadDefaultPage();
registerAsAnEventHandler(this);

// TODO: [Jiho] Remove this constructor once the one immediately below this is used.
}

public WishDetailPanel(WishTransaction wishTransaction) {
super(FXML);

// To prevent triggering events for typing inside the loaded Web page.
getRoot().setOnKeyPressed(Event::consume);
wishBrowserPanel = new BrowserPanel();
wishBrowserPlaceHolder.getChildren().add(wishBrowserPanel.getRoot());

loadDefaultPage();
registerAsAnEventHandler(this);
// TODO: [Jiho] Utilize wishTransaction's data in the WishDetailPanel (if wanted).
}

/**
Expand All @@ -82,6 +78,12 @@ public void loadDefaultPage() {
private void loadWishPage(Wish wish) {
name.setText(wish.getName().fullName);
initTags(wish);

if (wish.getRemark().toString().equals("")) {
remarks.setText("Save by: " + wish.getDate().toString());
} else {
remarks.setText("Save by: " + wish.getDate().toString() + " - " + wish.getRemark());
}
}

/**
Expand Down
69 changes: 66 additions & 3 deletions src/main/java/seedu/address/ui/WishDetailSavingHistory.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,102 @@
package seedu.address.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.logging.Logger;

import com.google.common.eventbus.Subscribe;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.Region;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.ui.WishDataUpdatedEvent;
import seedu.address.commons.events.ui.WishPanelSelectionChangedEvent;
import seedu.address.model.WishTransaction;
import seedu.address.model.wish.Wish;

/**
* Panel containing the detail of wish.
*/
public class WishDetailSavingHistory extends UiPart<Region> {

private static final String FXML = "WishDetailSavingHistory.fxml";

private final Logger logger = LogsCenter.getLogger(getClass());

public WishDetailSavingHistory() {
private WishTransaction wishTransaction;
private ArrayList savingHistoryList = new ArrayList();
private String id;

@FXML
private ListView<String> savingHistoryListView;

public WishDetailSavingHistory(WishTransaction wishTransaction) {
super(FXML);

this.wishTransaction = wishTransaction;

registerAsAnEventHandler(this);
}

private void setConnections(ObservableList<String> savingHistoryList) {
savingHistoryListView.setItems(savingHistoryList);
savingHistoryListView.setCellFactory(listView -> new WishDetailSavingHistory.SavingHistoryListViewCell());
}

/**
* Load the page that shows the detail of wish.
*/
private void loadWishDetails(Wish wish) {
this.savingHistoryList.clear();
this.id = wish.getId().toString();

ListIterator<Wish> entry = wishTransaction.getWishMap().get(wish.getId()).listIterator(1);

while (entry.hasNext()) {
Wish prevWish = wishTransaction.getWishMap().get(wish.getId()).get(entry.previousIndex());
double prevAmount = prevWish.getSavedAmount().value;
double nextAmount = entry.next().getSavedAmount().value;
double diff = nextAmount - prevAmount;
savingHistoryList.add("Saved $" + String.format("%.2f", diff));
}

Collections.reverse(savingHistoryList);

ObservableList<String> oSavingHistoryList = FXCollections.observableArrayList(savingHistoryList);
setConnections(oSavingHistoryList);
}

@Subscribe
private void handleWishPanelSelectionChangedEvent(WishPanelSelectionChangedEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event));
loadWishDetails(event.getNewSelection());
}

@Subscribe
private void handleWishDataUpdatedEvent(WishDataUpdatedEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event));
if (this.id.equals(event.getNewData().getId().toString())) {
loadWishDetails(event.getNewData());
}
}

/**
* Custom {@code ListCell} that displays the graphics of a {@code String} using a {@code WishSavingHistoryCell}.
*/
class SavingHistoryListViewCell extends ListCell<String> {
@Override
protected void updateItem(String savingHistory, boolean empty) {
super.updateItem(savingHistory, empty);

if (empty || savingHistory == null) {
setGraphic(null);
setText(null);
} else {
setGraphic(new SavingHistoryCell(savingHistory).getRoot());
}
}
}
}
18 changes: 6 additions & 12 deletions src/main/java/seedu/address/ui/WishListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.ui.JumpToListRequestEvent;
import seedu.address.commons.events.ui.WishPanelSelectionChangedEvent;
import seedu.address.model.WishTransaction;
import seedu.address.commons.events.ui.WishUpdatedEvent;
import seedu.address.model.wish.Wish;

/**
Expand All @@ -31,16 +31,6 @@ public WishListPanel(ObservableList<Wish> wishList) {
setConnections(wishList);
registerAsAnEventHandler(this);
scrollTo(0);
// TODO: [Jiho] Remove this constructor once the one immediately below this is used.
}


public WishListPanel(ObservableList<Wish> wishList, WishTransaction wishTransaction) {
super(FXML);
setConnections(wishList);
registerAsAnEventHandler(this);

// TODO: [Jiho] Utilize wishTransaction's data in the WishListPanel (if wanted).
}

private void setConnections(ObservableList<Wish> wishList) {
Expand Down Expand Up @@ -75,6 +65,11 @@ private void handleJumpToListRequestEvent(JumpToListRequestEvent event) {
scrollTo(event.targetIndex);
}

@Subscribe
private void handleWishUpdatedEvent(WishUpdatedEvent event) {
wishListView.getSelectionModel().clearAndSelect(0);
}

/**
* Custom {@code ListCell} that displays the graphics of a {@code Wish} using a {@code WishCard}.
*/
Expand All @@ -91,5 +86,4 @@ protected void updateItem(Wish wish, boolean empty) {
}
}
}

}
14 changes: 13 additions & 1 deletion src/main/resources/view/BrowserPanel.fxml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.web.WebView?>

<StackPane xmlns:fx="http://javafx.com/fxml/1">
<WebView fx:id="browser"/>
<VBox minHeight="400" GridPane.columnIndex="0">
<padding>
<Insets top="5" bottom="5" />
</padding>
<Label styleClass="detail_sub_title_label">
Product
</Label>
<WebView fx:id="browser"/>
</VBox>
</StackPane>
Loading

0 comments on commit a4489b6

Please sign in to comment.