forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user guide to application (#199)
* Add UserGuideTabPane * Disable user guide tab when not selected This prevents the user from clicking on the invisible tab which holds the WebView. * Improve pseudo-tab behaviour of user guide button * Add URL whitelisting * Update 'No External Site' page link * Add logging whenever the state of the WebView updates * Add method to refresh page * Disable context menu of WebView * Add auto-refresh 10s after page load failure * Update 'No External Site' page link to be consistent * Add top anchor to the user guide button * Remove old help message
- Loading branch information
Showing
7 changed files
with
165 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/ay2021s1_cs2103_w16_3/finesse/ui/tabs/UserGuideTabPane.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package ay2021s1_cs2103_w16_3.finesse.ui.tabs; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import ay2021s1_cs2103_w16_3.finesse.commons.core.LogsCenter; | ||
import ay2021s1_cs2103_w16_3.finesse.ui.UiPart; | ||
import javafx.animation.KeyFrame; | ||
import javafx.animation.Timeline; | ||
import javafx.application.Platform; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.layout.StackPane; | ||
import javafx.scene.web.WebEngine; | ||
import javafx.scene.web.WebView; | ||
import javafx.util.Duration; | ||
|
||
/** | ||
* Tab pane that displays the user guide. | ||
*/ | ||
public class UserGuideTabPane extends UiPart<StackPane> { | ||
private static final String FXML = "UserGuideTabPane.fxml"; | ||
|
||
// Links | ||
private static final String GITHUB_PAGES_DOMAIN = "https://ay2021s1-cs2103t-w16-3.github.io"; | ||
private static final String USER_GUIDE_URL = "https://ay2021s1-cs2103t-w16-3.github.io/tp/UserGuide.html"; | ||
private static final String NO_EXTERNAL_SITE_PAGE_URL = | ||
"https://ay2021s1-cs2103t-w16-3.github.io/tp/NoExternalSite.html"; | ||
|
||
// Constants | ||
private static final double REFRESH_TIMEOUT_DELAY = 10000.0; | ||
|
||
// Logging messages | ||
private static final String WEB_ENGINE_EXTERNAL_SITE_REQUEST_BLOCKED = | ||
"Request to load page on external site blocked"; | ||
private static final String WEB_ENGINE_WORKER_STATE_CANCELLED = "Page load cancelled: "; | ||
private static final String WEB_ENGINE_WORKER_STATE_FAILED = "Unable to load page (no internet connection): "; | ||
private static final String WEB_ENGINE_WORKER_STATE_RUNNING = "Loading page: "; | ||
private static final String WEB_ENGINE_WORKER_STATE_SCHEDULED = "Page load scheduled: "; | ||
private static final String WEB_ENGINE_WORKER_STATE_SUCCEEDED = "Page successfully loaded: "; | ||
|
||
private final Logger logger = LogsCenter.getLogger(getClass()); | ||
|
||
@FXML | ||
private WebView webView; | ||
|
||
/** | ||
* Constructs a {@code UserGuideTabPane}. | ||
*/ | ||
public UserGuideTabPane() { | ||
super(FXML); | ||
initializeUserGuide(); | ||
} | ||
|
||
/** | ||
* Initializes the {@code WebEngine} that is backing the {@code WebView}. | ||
*/ | ||
private void initializeUserGuide() { | ||
// Disable right click. | ||
webView.setContextMenuEnabled(false); | ||
|
||
WebEngine webEngine = webView.getEngine(); | ||
|
||
webEngine.locationProperty().addListener((observableValue, oldUrl, newUrl) -> { | ||
if (!newUrl.startsWith(GITHUB_PAGES_DOMAIN)) { | ||
logger.info(WEB_ENGINE_EXTERNAL_SITE_REQUEST_BLOCKED); | ||
|
||
// Block requests to external sites. | ||
Platform.runLater(() -> { | ||
// Load the 'No External Site' page. | ||
webEngine.load(NO_EXTERNAL_SITE_PAGE_URL); | ||
}); | ||
} | ||
}); | ||
|
||
// Initialize refresh task. | ||
Timeline refreshTask = new Timeline(new KeyFrame(Duration.millis(REFRESH_TIMEOUT_DELAY), | ||
actionEvent -> refreshPage())); | ||
|
||
webEngine.getLoadWorker().stateProperty().addListener((observableValue, oldState, newState) -> { | ||
String location = webEngine.getLocation(); | ||
|
||
// Stop the refresh task to prevent multiple tasks running. | ||
refreshTask.stop(); | ||
|
||
switch (newState) { | ||
case CANCELLED: | ||
logger.info(WEB_ENGINE_WORKER_STATE_CANCELLED + location); | ||
break; | ||
case FAILED: | ||
logger.warning(WEB_ENGINE_WORKER_STATE_FAILED + location); | ||
refreshTask.playFromStart(); | ||
break; | ||
case RUNNING: | ||
logger.info(WEB_ENGINE_WORKER_STATE_RUNNING + location); | ||
break; | ||
case SCHEDULED: | ||
logger.info(WEB_ENGINE_WORKER_STATE_SCHEDULED + location); | ||
break; | ||
case SUCCEEDED: | ||
logger.info(WEB_ENGINE_WORKER_STATE_SUCCEEDED + location); | ||
break; | ||
default: | ||
} | ||
}); | ||
|
||
webEngine.load(USER_GUIDE_URL); | ||
} | ||
|
||
/** | ||
* Refreshes the WebView. | ||
*/ | ||
public void refreshPage() { | ||
// Cannot simply reload the page because if the page has not loaded a single time, | ||
// reloading does not work. | ||
String currentPage = webView.getEngine().getLocation(); | ||
webView.getEngine().load(currentPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?import javafx.scene.layout.StackPane?> | ||
<?import javafx.scene.web.WebView?> | ||
|
||
<StackPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> | ||
<WebView fx:id="webView" /> | ||
</StackPane> |