From 2a5de70181a3f56c8397e7789d4b89a622984276 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Mon, 12 Nov 2018 21:26:42 +0000 Subject: [PATCH 1/5] Refactored UI to automatically resize and to be clearer --- src/main/java/uk/co/harrypeach/core/Main.java | 5 +- src/main/resources/Core.fxml | 91 ++++++++++++++----- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/src/main/java/uk/co/harrypeach/core/Main.java b/src/main/java/uk/co/harrypeach/core/Main.java index 6e70511..31c317d 100644 --- a/src/main/java/uk/co/harrypeach/core/Main.java +++ b/src/main/java/uk/co/harrypeach/core/Main.java @@ -22,13 +22,10 @@ public class Main extends Application { public void start(Stage primaryStage) { try { Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Core.fxml")); - primaryStage.getIcons().add(new Image(getClass().getClassLoader().getResourceAsStream("icon.png"))); + primaryStage.getIcons().add(new Image(getClass().getClassLoader().getResource("icon.png").toExternalForm())); Scene scene = new Scene(root, 500, 300); primaryStage.setTitle("RedditMonitor"); primaryStage.setScene(scene); - - // Stop users from resizing the UI - primaryStage.setResizable(false); LOGGER.debug("Showing primary stage"); primaryStage.show(); diff --git a/src/main/resources/Core.fxml b/src/main/resources/Core.fxml index 27f838c..dddb330 100644 --- a/src/main/resources/Core.fxml +++ b/src/main/resources/Core.fxml @@ -2,49 +2,90 @@ + - - - - - - + + + + + + + + + + + \ No newline at end of file From 271679a3c61e3d4d0abf73335400e6040c69e251 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Mon, 12 Nov 2018 21:32:10 +0000 Subject: [PATCH 2/5] Added action events back to the UI --- .../co/harrypeach/ui/FXMLCoreController.java | 4 +-- src/main/resources/Core.fxml | 26 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java index f98d96a..3f38775 100644 --- a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java +++ b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java @@ -53,9 +53,9 @@ public class FXMLCoreController { @FXML private Button startButton; @FXML - private Label titleLabel; + private Hyperlink titleLabel; @FXML - private Label subredditLabel; + private Hyperlink subredditLabel; @FXML private Hyperlink urlLabel; @FXML diff --git a/src/main/resources/Core.fxml b/src/main/resources/Core.fxml index dddb330..966a313 100644 --- a/src/main/resources/Core.fxml +++ b/src/main/resources/Core.fxml @@ -9,7 +9,7 @@ + xmlns:fx="http://javafx.com/fxml/1" fx:controller="uk.co.harrypeach.ui.FXMLCoreController"> @@ -18,11 +18,11 @@ - + - + @@ -32,15 +32,15 @@ - - + + - - - + + + @@ -61,28 +61,28 @@ - + - + From c183a86023f773a59839180f557d1a9a68bdd0ad Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Mon, 12 Nov 2018 21:36:44 +0000 Subject: [PATCH 3/5] Re-added start button handler and added hyperlink url handlers --- .../co/harrypeach/ui/FXMLCoreController.java | 27 ++++++++++++++++++- src/main/resources/Core.fxml | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java index 3f38775..e1e9f20 100644 --- a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java +++ b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java @@ -25,7 +25,6 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Hyperlink; -import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; @@ -224,10 +223,36 @@ protected void initialize() { @Override public void changed(ObservableValue observable, Result oldValue, Result newValue) { String titleText = newValue.getTitle(); + String titleUrl = newValue.getUrl(); titleLabel.setText(titleText.substring(0, Math.min(titleText.length(), MAX_LABEL_CHARS))); + titleLabel.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent e) { + try { + LOGGER.debug("Attempting to open selected URL in the users browser"); + Desktop.getDesktop().browse(new URI(titleUrl)); + } catch (IOException | URISyntaxException e1) { + LOGGER.warn(e1.getMessage()); + e1.printStackTrace(); + } + } + }); String subredditText = newValue.getSubreddit(); + String subredditUrl = "https://reddit.com" + newValue.getSubreddit(); subredditLabel.setText(subredditText.substring(0, Math.min(subredditText.length(), MAX_LABEL_CHARS))); + subredditLabel.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent e) { + try { + LOGGER.debug("Attempting to open selected URL in the users browser"); + Desktop.getDesktop().browse(new URI(subredditUrl)); + } catch (IOException | URISyntaxException e1) { + LOGGER.warn(e1.getMessage()); + e1.printStackTrace(); + } + } + }); String urlText = newValue.getUrl(); urlLabel.setText(urlText.substring(0, Math.min(urlText.length(), MAX_LABEL_CHARS))); diff --git a/src/main/resources/Core.fxml b/src/main/resources/Core.fxml index 966a313..ce2809a 100644 --- a/src/main/resources/Core.fxml +++ b/src/main/resources/Core.fxml @@ -77,7 +77,7 @@ - + From cbb9b4ff48b1c26d2f53f558abef972fa6d6ea86 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Mon, 12 Nov 2018 21:55:42 +0000 Subject: [PATCH 4/5] Added a result field for postUrl and fixed hyperlinks --- .../java/uk/co/harrypeach/misc/Result.java | 130 ++++++++++-------- .../co/harrypeach/ui/FXMLCoreController.java | 8 +- 2 files changed, 74 insertions(+), 64 deletions(-) diff --git a/src/main/java/uk/co/harrypeach/misc/Result.java b/src/main/java/uk/co/harrypeach/misc/Result.java index ddbbe52..09c8e30 100644 --- a/src/main/java/uk/co/harrypeach/misc/Result.java +++ b/src/main/java/uk/co/harrypeach/misc/Result.java @@ -1,60 +1,70 @@ -package uk.co.harrypeach.misc; -/** - * Model for each result in the search, bound to the listview. - * @author harry - * - */ -public class Result { - - private String subreddit; - private String title; - private String url; - private String id; - - public Result(String subreddit, String title, String url, String id) { - this.subreddit = subreddit; - this.title = title; - this.url = url; - this.id = id; - } - - /** - * Default string return method which returns the title - */ - public String toString() { - return title; - } - - /** - * Gets the subreddit name - * @return subreddit string - */ - public String getSubreddit() { - return subreddit; - } - - /** - * Gets the title of the post - * @return title string - */ - public String getTitle() { - return title; - } - - /** - * Gets the URL of the post - * @return URL string - */ - public String getUrl() { - return url; - } - - /** - * Gets the ID of the post - * @return ID string - */ - public String getId() { - return id; - } - -} +package uk.co.harrypeach.misc; +/** + * Model for each result in the search, bound to the listview. + * @author harry + * + */ +public class Result { + + private String subreddit; + private String title; + private String url; + private String postUrl; + private String id; + + public Result(String subreddit, String title, String url, String postUrl, String id) { + this.subreddit = subreddit; + this.title = title; + this.url = url; + this.postUrl = postUrl; + this.id = id; + } + + /** + * Default string return method which returns the title + */ + public String toString() { + return title; + } + + /** + * Gets the subreddit name + * @return subreddit string + */ + public String getSubreddit() { + return subreddit; + } + + /** + * Gets the title of the post + * @return title string + */ + public String getTitle() { + return title; + } + + /** + * Gets the URL of the post's content + * @return URL string + */ + public String getUrl() { + return url; + } + + /** + * Gets the URL of the post itself + * @return URL string + */ + public String getPostUrl() { + return postUrl; + } + + /** + * Gets the ID of the post + * @return ID string + */ + public String getId() { + return id; + } + +} diff --git a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java index e1e9f20..b2e3fc0 100644 --- a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java +++ b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java @@ -181,7 +181,7 @@ protected void handleCsvExport(ActionEvent event) { @FXML protected void handleDebugAddItem(ActionEvent event) { LOGGER.debug("Adding dummy item to the post list"); - postList.getItems().add(new Result("/r/test", "This is a test post", "https://reddit.com/", "t35t")); + postList.getItems().add(new Result("/r/test", "This is a test post", "https://reddit.com/", "https://reddit.com/r/test/comments/t35t", "t35t")); playAlert(); } @@ -223,7 +223,7 @@ protected void initialize() { @Override public void changed(ObservableValue observable, Result oldValue, Result newValue) { String titleText = newValue.getTitle(); - String titleUrl = newValue.getUrl(); + String titleUrl = "https://reddit.com" + newValue.getPostUrl(); titleLabel.setText(titleText.substring(0, Math.min(titleText.length(), MAX_LABEL_CHARS))); titleLabel.setOnAction(new EventHandler() { @Override @@ -239,7 +239,7 @@ public void handle(ActionEvent e) { }); String subredditText = newValue.getSubreddit(); - String subredditUrl = "https://reddit.com" + newValue.getSubreddit(); + String subredditUrl = "https://reddit.com/r/" + newValue.getSubreddit(); subredditLabel.setText(subredditText.substring(0, Math.min(subredditText.length(), MAX_LABEL_CHARS))); subredditLabel.setOnAction(new EventHandler() { @Override @@ -385,7 +385,7 @@ public void run() { Listing submissions = paginator.next(); for (Submission s : submissions) { - Result r = new Result(s.getSubreddit(), s.getTitle(), s.getUrl(), s.getId()); + Result r = new Result(s.getSubreddit(), s.getTitle(), s.getUrl(), s.getPermalink(), s.getId()); // Checks whether the submission title contains a keyword, and whether it is // already in the result queue if (titleContainsWordList(r.getTitle(), stringList) && !containsResult(resultQueue, r)) { From b56a4c5b14ab7144725fe3a819bd9920cdc8c277 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Mon, 12 Nov 2018 22:02:45 +0000 Subject: [PATCH 5/5] Refactoring and renaming --- .../configuration/Configuration.java | 37 +++++++++++++++++++ .../co/harrypeach/ui/FXMLCoreController.java | 32 +++++++++++----- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/main/java/uk/co/harrypeach/configuration/Configuration.java b/src/main/java/uk/co/harrypeach/configuration/Configuration.java index 1e90864..feb9f9b 100644 --- a/src/main/java/uk/co/harrypeach/configuration/Configuration.java +++ b/src/main/java/uk/co/harrypeach/configuration/Configuration.java @@ -13,6 +13,10 @@ public class Configuration { private List keywordList; private List blacklistedSubreddits; + /** + * Whether NSFW filtering is enabled or not + * @return + */ public boolean isNsfwFilteringEnabled() { return nsfwFilteringEnabled; } @@ -20,6 +24,10 @@ public void setNsfwFilteringEnabled(boolean nsfwFilteringEnabled) { this.nsfwFilteringEnabled = nsfwFilteringEnabled; } + /** + * Whether the notification alert sound should be played + * @return + */ public boolean isAlertSoundEnabled() { return alertSoundEnabled; } @@ -27,36 +35,65 @@ public void setAlertSoundEnabled(boolean alertSoundEnabled) { this.alertSoundEnabled = alertSoundEnabled; } + /** + * What the current alert sound volume is + * @return + */ public double getAlertSoundVolume() { return alertSoundVolume; } public void setAlertSoundVolume(double alertSoundVolume) { this.alertSoundVolume = alertSoundVolume; } + + /** + * The OAuth string to identify the client + * @return + */ public String getOauthClient() { return oauthClient; } public void setOauthClient(String oauthClient) { this.oauthClient = oauthClient; } + + /** + * The keywords that posts are compared to in order to determine a match + * @return + */ public List getKeywordList() { return keywordList; } public void setKeywordList(List keywordList) { this.keywordList = keywordList; } + + /** + * Whether pop-up notifications are enabled + * @return + */ public boolean isNotificationsEnabled() { return notificationsEnabled; } public void setNotificationsEnabled(boolean notificationsEnabled) { this.notificationsEnabled = notificationsEnabled; } + + /** + * Return the list of blacklisted subreddits + * @return + */ public List getBlacklistedSubreddits() { return blacklistedSubreddits; } public void setBlacklistedSubreddits(List blacklistedSubreddits) { this.blacklistedSubreddits = blacklistedSubreddits; } + + /** + * Return the delay between each check for new posts + * @return + */ public int getUpdateDelay() { return updateDelay; } diff --git a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java index b2e3fc0..b279ae2 100644 --- a/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java +++ b/src/main/java/uk/co/harrypeach/ui/FXMLCoreController.java @@ -52,11 +52,11 @@ public class FXMLCoreController { @FXML private Button startButton; @FXML - private Hyperlink titleLabel; + private Hyperlink titleHyperlink; @FXML - private Hyperlink subredditLabel; + private Hyperlink subredditHyperlink; @FXML - private Hyperlink urlLabel; + private Hyperlink urlHyperlink; @FXML private AnchorPane anchorPane; @@ -141,6 +141,10 @@ protected void handleLogsButton(ActionEvent event) { logReaderDialog.show(); } + /** + * Exports the items within the list to a CSV file + * @param event + */ @FXML protected void handleCsvExport(ActionEvent event) { LOGGER.debug("Opening file dialog"); @@ -222,10 +226,12 @@ protected void initialize() { postList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, Result oldValue, Result newValue) { + + // Set the title hyperlink text and URL String titleText = newValue.getTitle(); String titleUrl = "https://reddit.com" + newValue.getPostUrl(); - titleLabel.setText(titleText.substring(0, Math.min(titleText.length(), MAX_LABEL_CHARS))); - titleLabel.setOnAction(new EventHandler() { + titleHyperlink.setText(titleText.substring(0, Math.min(titleText.length(), MAX_LABEL_CHARS))); + titleHyperlink.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { try { @@ -238,10 +244,11 @@ public void handle(ActionEvent e) { } }); + // Set the subreddit hyperlink text and URL String subredditText = newValue.getSubreddit(); String subredditUrl = "https://reddit.com/r/" + newValue.getSubreddit(); - subredditLabel.setText(subredditText.substring(0, Math.min(subredditText.length(), MAX_LABEL_CHARS))); - subredditLabel.setOnAction(new EventHandler() { + subredditHyperlink.setText(subredditText.substring(0, Math.min(subredditText.length(), MAX_LABEL_CHARS))); + subredditHyperlink.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { try { @@ -254,9 +261,10 @@ public void handle(ActionEvent e) { } }); + // Set the URL hyperlink text and URL String urlText = newValue.getUrl(); - urlLabel.setText(urlText.substring(0, Math.min(urlText.length(), MAX_LABEL_CHARS))); - urlLabel.setOnAction(new EventHandler() { + urlHyperlink.setText(urlText.substring(0, Math.min(urlText.length(), MAX_LABEL_CHARS))); + urlHyperlink.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { try { @@ -271,6 +279,7 @@ public void handle(ActionEvent e) { } }); + // Open the post's url in the browser when it is double clicked. postList.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent click) { @@ -355,6 +364,10 @@ public void shutdown() { } +/** + * The updater thread which continiously checks for new posts that match the keywords and that are not blacklisted or already included + * @author Harry Peach + */ class UpdateList implements Runnable { private FXMLCoreController controllerInstance; private static final int MAX_RESULT_QUEUE_SIZE = 100; @@ -406,6 +419,7 @@ public void run() { return; } + // Add item to the postlist and notify the user addToQueue(r); Runnable updater = new Runnable() { public void run() {