Skip to content

Commit

Permalink
Merge pull request #448 from HemanshuGandhi/feature-title-body-displays
Browse files Browse the repository at this point in the history
Add Title and Body Displays to Block Content View
  • Loading branch information
HemanshuGandhi authored Apr 12, 2020
2 parents 25df98a + 4609425 commit 5c68d20
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 180 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/notably/view/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.notably.logic.commands.exceptions.CommandException;
import com.notably.logic.parser.exceptions.ParseException;
import com.notably.view.suggestion.SuggestionsWindowView;

import javafx.application.Platform;
import javafx.beans.property.StringProperty;
Expand Down
28 changes: 5 additions & 23 deletions src/main/java/com/notably/view/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import com.notably.logic.parser.exceptions.ParseException;
import com.notably.model.Model;
import com.notably.view.blockcontent.BlockContent;
import com.notably.view.sidebar.SideBarTreeView;
import com.notably.view.suggestion.SuggestionsWindowView;

import javafx.beans.Observable;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class MainWindow extends ViewPart<Stage> {
private StackPane sideBarPlaceholder;

@FXML
private StackPane blockContentPlaceholder;
private VBox blockContentPlaceholder;

@FXML
private VBox suggestionsWindow;
Expand All @@ -67,33 +68,13 @@ public MainWindow(Stage primaryStage, Logic logic, Model model) {
this.model = model;

setWindowDefaultSize(logic.getGuiSettings());
//setWindowSettings(primaryStage);

initializeHelpWindow(model);
}

public Stage getPrimaryStage() {
return primaryStage;
}

/**
* Provides an alternative way to set the settings of the main window, as an alternative
* to doing so via fxml tags.
*
* @param primaryStage the stage corresponding to the main app window.
*/
private void setWindowSettings(Stage primaryStage) {
primaryStage.focusedProperty().addListener(((observable, unused, isFocused) -> {
if (isFocused) {
mainWindow.setEffect(null);
} else {
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(-0.4);
mainWindow.setEffect(colorAdjust);
}
}));
}

/**
* Fills up all the placeholders of this window.
*/
Expand All @@ -106,7 +87,8 @@ void fillInnerParts() {

blockContent = new BlockContent(blockContentPlaceholder, logic, model);

suggestionsWindowView = new SuggestionsWindowView(model.getSuggestions(), model.responseTextProperty());
suggestionsWindowView = new SuggestionsWindowView(model.getSuggestions(),
model.responseTextProperty());
suggestionsWindow.getChildren().add(suggestionsWindowView.getRoot());
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/notably/view/blockcontent/BlockContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.notably.logic.Logic;
import com.notably.model.Model;

import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

/**
* View of the currently open block's content.
Expand All @@ -15,14 +15,13 @@ public class BlockContent {
private final BlockContentDisplayView blockContentDisplayView;
private final BlockContentEditView blockContentEditView;

public BlockContent(StackPane blockContentPlaceholder, Logic logic, Model model) {
public BlockContent(VBox blockContentPlaceholder, Logic logic, Model model) {
requireAllNonNull(blockContentPlaceholder, model, logic);

blockContentDisplayView = new BlockContentDisplayView(model);
blockContentEditView = new BlockContentEditView(logic, model);

// TODO: Integrate BlockContentEditView
blockContentPlaceholder.getChildren().addAll(blockContentDisplayView.getRoot());
blockContentPlaceholder.getChildren().add(blockContentDisplayView.getRoot());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import com.notably.view.ViewPart;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;

/**
Expand All @@ -21,47 +25,114 @@ public class BlockContentDisplayView extends ViewPart<WebView> {
private final Model model;

@FXML
private WebView blockContentDisplay;
private VBox blockContentDisplay;

@FXML
private HBox blockTitlePathContainer;

@FXML
private Label blockTitleDisplay;

@FXML
private Pane separator;

@FXML
private Label blockPathDisplay;

@FXML
private WebView blockBodyDisplay;

public BlockContentDisplayView(Model model) {
super(FXML);

Objects.requireNonNull(model);

this.model = model;

setup();
}

/**
* Sets up the view's initial data and wire up all required change listeners.
* Sets up the view's initial data and wires up all required change listeners.
*/
private void setup() {
// Initialize content display
reload(model);

// Setup change listeners
model.currentlyOpenPathProperty().addListener(observable -> reload(model));
model.getBlockTree().getRootBlock().getTreeItem()
.addEventHandler(TreeItem.treeNotificationEvent(), event -> reload(model));
updateBlockContentDisplay(model);
setChangeListeners();
}

/**
* Reloads the {@link WebView}'s content with the currently open block's rendered HTML body.
* Updates the content of the view-components to that of the currently open block.
*
* @param model App's model
* @param model App's model.
*/
private void reload(Model model) {
private void updateBlockContentDisplay(Model model) {
Objects.requireNonNull(model);

AbsolutePath currentlyOpenPath = model.getCurrentlyOpenPath();
BlockTreeItem currentlyOpenBlock = model.getBlockTree().get(currentlyOpenPath);
String markdownBody = currentlyOpenBlock.getBody().getText();

String htmlBody = Compiler.compile(markdownBody);
blockContentDisplay.getEngine().setUserStyleSheetLocation(getClass()
updateBlockTitleDisplay(currentlyOpenBlock);
updateBlockPathDisplay(currentlyOpenPath);
updateBlockBodyDisplay(currentlyOpenBlock);
}

/**
* Sets the blockTitleDisplay's text to the currently open block's {@link com.notably.model.block.Title}.
*
* @param currentlyOpenBlock A {@link BlockTreeItem} representing the currently open note block.
*/
private void updateBlockTitleDisplay(BlockTreeItem currentlyOpenBlock) {
Objects.requireNonNull(currentlyOpenBlock);

String titleString = currentlyOpenBlock.getTitle().getText();
blockTitleDisplay.setText(titleString);
}

/**
* Sets the blockPathDisplay's text to the currently open block's {@link AbsolutePath}.
*
* @param currentlyOpenPath A {@link BlockTreeItem} representing the currently open note block.
*/
private void updateBlockPathDisplay(AbsolutePath currentlyOpenPath) {
Objects.requireNonNull(currentlyOpenPath);

String pathString = currentlyOpenPath.getStringRepresentation();
blockPathDisplay.setText(pathString);
}

/**
* Sets the blockBodyDisplay's text to the currently open block's {@link com.notably.model.block.Body}.
*
* @param currentlyOpenBlock A {@link BlockTreeItem} representing the currently open note block.
*/
private void updateBlockBodyDisplay(BlockTreeItem currentlyOpenBlock) {
Objects.requireNonNull(currentlyOpenBlock);

String htmlBody = getHtmlBody(currentlyOpenBlock);
blockBodyDisplay.getEngine().loadContent(htmlBody);
blockBodyDisplay.getEngine().setUserStyleSheetLocation(getClass()
.getResource("/view/blockcontent/BlockContentDisplay.css").toExternalForm());
blockContentDisplay.getEngine().loadContent(htmlBody);
}

/**
* Helper function to retrieve the currently open block's {@link com.notably.model.block.Body}
* rendered as HTML.
*
* @param currentlyOpenBlock A {@link BlockTreeItem} representing the currently open note block.
* @return A String representing the note's body.
*/
private String getHtmlBody(BlockTreeItem currentlyOpenBlock) {
Objects.requireNonNull(currentlyOpenBlock);

String markdownBody = currentlyOpenBlock.getBody().getText();
String htmlBody = Compiler.compile(markdownBody);
return htmlBody;
}

/**
* Sets up listeners for View state management.
*/
private void setChangeListeners() {
model.currentlyOpenPathProperty().addListener(observable -> updateBlockContentDisplay(model));
model.getBlockTree().getRootBlock().getTreeItem()
.addEventHandler(TreeItem.treeNotificationEvent(), event -> updateBlockContentDisplay(model));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void setInitialData() {
}

/**
* Sets the {@link TextArea}'s content to the currently open block's Markdown body.
* Sets the {@link TextArea}'s content to the currently open block's content in markdown form.
*
* @param model App's model
*/
Expand All @@ -168,6 +168,7 @@ private void setText(Model model) {

AbsolutePath currentlyOpenPath = model.getCurrentlyOpenPath();
BlockTreeItem currentlyOpenBlock = model.getBlockTree().get(currentlyOpenPath);

String markdownBody = currentlyOpenBlock.getBody().getText();

blockContentTextArea.setText(markdownBody);
Expand Down Expand Up @@ -201,7 +202,6 @@ private void handleEdit() {
* ie. the Block Edit modal is to be shown, false otherwise.
*/
private void updateMainWindowStyle(Boolean isBlockEditable) {
//logger.severe(isBlockEditable.toString());
Node mainWindow = parentStage.getScene().lookup("#mainWindow");
ChangeListener<Boolean> focusedListener = (observable, unused, isFocused) -> {
if (!isFocused && stage.isShowing()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.notably.view;
package com.notably.view.sidebar;

import com.notably.commons.path.AbsolutePath;
import com.notably.model.block.Block;
import com.notably.model.block.BlockTree;
import com.notably.model.block.BlockTreeItem;
import com.notably.view.ViewPart;

import javafx.beans.property.Property;
import javafx.event.Event;
import javafx.event.EventDispatchChain;
import javafx.event.EventDispatcher;
import javafx.fxml.FXML;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;

/**
Expand Down Expand Up @@ -69,7 +66,7 @@ private void setTreeSettings() {
* Calls helper functions that set the strategies for populating the SideBarTreeView with data.
*/
private void setTreeStructure() {
sideBarTreeView.setCellFactory(treeView -> new SideBarTreeViewCell());
sideBarTreeView.setCellFactory(treeView -> new SideBarTreeViewCell(currentlyOpenedNote));
useLevelDisplayStrategy();
useExpansionStrategy();
}
Expand Down Expand Up @@ -135,56 +132,4 @@ private void useExpansionStrategy() {
});
});
}

/**
* Custom {@code TreeCell} that displays the text of a {@code Block}.
*/
class SideBarTreeViewCell extends TreeCell<Block> {
@Override
protected void updateItem(Block block, boolean empty) {
super.updateItem(block, empty);

setGraphic(null);
if (empty || block == null) {
setText(null);
} else {
setText(getNoteTitle());
setSelectedProperty(block);
}
}

private String getNoteTitle() {
return getItem().getTitle().getText();
}

private void setSelectedProperty(Block block) {
if ((block == currentlyOpenedNote.getBlock())) {
updateSelected(true);
} else {
updateSelected(false);
}
}
}

/**
* Custom {@code EventDispatcher} to allow for finer control over mouse click events.
* A {@link SideBarTreeViewCell} should not respond to any mouse click events.
*
*/
class TreeCellEventDispatcher implements EventDispatcher {
private final EventDispatcher original;

public TreeCellEventDispatcher(EventDispatcher original) {
this.original = original;
}

@Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {

if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
event.consume();
}
return original.dispatchEvent(event, tail);
}
}
}
Loading

0 comments on commit 5c68d20

Please sign in to comment.