From 9c4546561d80c0415dec191534047f9c88bdd17d Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 25 Apr 2018 20:08:04 -0700 Subject: [PATCH] pie charts working again, added console tab functionality --- .../src/sarsoo/fmframework/fx/ConsoleTab.java | 29 +++++++++ .../fmframework/fx/TextAreaConsole.java | 40 +++++++++++++ .../fx/controller/ControllerMain.java | 60 +++++++++++-------- .../src/sarsoo/fmframework/fx/ui/main.fxml | 1 + .../src/sarsoo/fmframework/parser/Parser.java | 30 ++++++---- .../src/sarsoo/fmframework/util/Console.java | 6 ++ .../src/sarsoo/fmframework/util/Getter.java | 24 ++++---- .../sarsoo/fmframework/util/Reference.java | 31 ++++++---- 8 files changed, 161 insertions(+), 60 deletions(-) create mode 100644 fmframework/src/sarsoo/fmframework/fx/ConsoleTab.java create mode 100644 fmframework/src/sarsoo/fmframework/fx/TextAreaConsole.java create mode 100644 fmframework/src/sarsoo/fmframework/util/Console.java diff --git a/fmframework/src/sarsoo/fmframework/fx/ConsoleTab.java b/fmframework/src/sarsoo/fmframework/fx/ConsoleTab.java new file mode 100644 index 0000000..2110353 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/fx/ConsoleTab.java @@ -0,0 +1,29 @@ +package sarsoo.fmframework.fx; + +import javafx.scene.control.Tab; +import javafx.scene.layout.AnchorPane; + +public class ConsoleTab extends Tab{ + + public ConsoleTab(){ + + setText("console"); + +// AnchorPane pane = new AnchorPane(); + +// AnchorPane.setTopAnchor(pane, 0.0); +// AnchorPane.setLeftAnchor(pane, 0.0); +// AnchorPane.setRightAnchor(pane, 0.0); +// AnchorPane.setBottomAnchor(pane, 0.0); +// +// setContent(pane); + + + + setContent(TextAreaConsole.getInstance().getTextArea()); + + + + } + +} diff --git a/fmframework/src/sarsoo/fmframework/fx/TextAreaConsole.java b/fmframework/src/sarsoo/fmframework/fx/TextAreaConsole.java new file mode 100644 index 0000000..f4dbc3a --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/fx/TextAreaConsole.java @@ -0,0 +1,40 @@ +package sarsoo.fmframework.fx; + +import javafx.application.Platform; +import javafx.scene.control.TextArea; +import sarsoo.fmframework.util.Console; + +public class TextAreaConsole implements Console{ + + private static TextAreaConsole instance; + + private TextArea output; + + private TextAreaConsole() { + output = new TextArea(); + } + + public static TextAreaConsole getInstance(){ + if(instance == null){ + synchronized (TextAreaConsole.class) { + if(instance == null){ + instance = new TextAreaConsole(); + } + } + } + return instance; + } + + public void write(final String string){ + Platform.runLater(new Runnable() { + public void run() { + output.appendText(string + "\n"); + } + }); + } + + public TextArea getTextArea() { + return output; + } + +} diff --git a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java index b4c6f17..99e77dd 100644 --- a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java +++ b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java @@ -18,6 +18,8 @@ import javafx.fxml.FXMLLoader; import javafx.scene.text.Text; import sarsoo.fmframework.fx.AlbumTab; import sarsoo.fmframework.fx.ArtistTab; +import sarsoo.fmframework.fx.ConsoleTab; +import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.fx.FMObjListTab; import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.TrackTab; @@ -50,6 +52,9 @@ public class ControllerMain { @FXML public void initialize() { Reference.setUserName("sarsoo"); + + Reference.setVerbose(TextAreaConsole.getInstance()); + refresh(); } @@ -186,9 +191,9 @@ public class ControllerMain { - grime.getTotalUserScrobbles(); ObservableList rapData = FXCollections.observableArrayList( - new PieChart.Data("rap", rapTotal), - new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), - new PieChart.Data("grime", grime.getTotalUserScrobbles())); + new PieChart.Data(String.format("rap %d%%", (int)rapTotal*100/rap.getTotalUserScrobbles()), rapTotal), + new PieChart.Data(String.format("classic rap %d%%", (int)classicRap.getTotalUserScrobbles()*100/rap.getTotalUserScrobbles()), classicRap.getTotalUserScrobbles()), + new PieChart.Data(String.format("grime %d%%", (int)grime.getTotalUserScrobbles()*100/rap.getTotalUserScrobbles()), grime.getTotalUserScrobbles())); Collections.sort(rapData, new Comparator() { @@ -201,9 +206,9 @@ public class ControllerMain { int other = total - rap.getTotalUserScrobbles(); ObservableList rapTotalData = FXCollections.observableArrayList( - new PieChart.Data("rap", rapTotal), - new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), - new PieChart.Data("grime", grime.getTotalUserScrobbles())); + new PieChart.Data(String.format("rap %d%%", (int)rapTotal*100/total), rapTotal), + new PieChart.Data(String.format("classic rap %d%%", (int)classicRap.getTotalUserScrobbles()*100/total), classicRap.getTotalUserScrobbles()), + new PieChart.Data(String.format("grime %d%%", (int)grime.getTotalUserScrobbles()*100/total), grime.getTotalUserScrobbles())); Collections.sort(rapTotalData, new Comparator() { @@ -213,31 +218,33 @@ public class ControllerMain { } }); - rapTotalData.add(new PieChart.Data("other", other)); + rapTotalData.add(new PieChart.Data(String.format("other %d%%", (int)other*100/total), other)); int totalOther = total - rap.getTotalUserScrobbles() - classicRock.getTotalUserScrobbles() - popPunk.getTotalUserScrobbles() - electronic.getTotalUserScrobbles() - metal.getTotalUserScrobbles() - indie.getTotalUserScrobbles() - rock.getTotalUserScrobbles() - jazz.getTotalUserScrobbles() - blues.getTotalUserScrobbles() - core.getTotalUserScrobbles() - - rnb.getTotalUserScrobbles(); + - rnb.getTotalUserScrobbles() - soulFunk.getTotalUserScrobbles(); ObservableList genreData = FXCollections.observableArrayList( - new PieChart.Data(String.format("rap %.2f%%", rapTotal*100/total), rapTotal), - new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), - new PieChart.Data("grime", grime.getTotalUserScrobbles()), - - new PieChart.Data("classic rock", classicRock.getTotalUserScrobbles()), - new PieChart.Data("pop punk", popPunk.getTotalUserScrobbles()), - new PieChart.Data("electronic", electronic.getTotalUserScrobbles()), - new PieChart.Data("metal", metal.getTotalUserScrobbles()), - new PieChart.Data("indie", indie.getTotalUserScrobbles()), - new PieChart.Data("rock", rock.getTotalUserScrobbles()), - new PieChart.Data("jazz", jazz.getTotalUserScrobbles()), - new PieChart.Data("blues", blues.getTotalUserScrobbles()), - new PieChart.Data("core", core.getTotalUserScrobbles()), - new PieChart.Data("soulfunk", soulFunk.getTotalUserScrobbles()), - new PieChart.Data("rnb", rnb.getTotalUserScrobbles())); + new PieChart.Data(String.format("rap %d%%", (int)rapTotal*100/total), rapTotal), + new PieChart.Data(String.format("classic rap %d%%", (int)classicRap.getTotalUserScrobbles()*100/total), classicRap.getTotalUserScrobbles()), + new PieChart.Data(String.format("grime %d%%", (int)grime.getTotalUserScrobbles()*100/total), grime.getTotalUserScrobbles()), + + new PieChart.Data(String.format("classic rock %d%%", (int)classicRock.getTotalUserScrobbles()*100/total), classicRock.getTotalUserScrobbles()) + , + new PieChart.Data(String.format("pop punk %d%%", (int)popPunk.getTotalUserScrobbles()*100/total), popPunk.getTotalUserScrobbles()), + new PieChart.Data(String.format("electronic %d%%", (int)electronic.getTotalUserScrobbles()*100/total), electronic.getTotalUserScrobbles()), + new PieChart.Data(String.format("metal %d%%", (int)metal.getTotalUserScrobbles()*100/total), metal.getTotalUserScrobbles()), + new PieChart.Data(String.format("indie %d%%", (int)indie.getTotalUserScrobbles()*100/total), indie.getTotalUserScrobbles()), + new PieChart.Data(String.format("rock %d%%", (int)rock.getTotalUserScrobbles()*100/total), rock.getTotalUserScrobbles()), + new PieChart.Data(String.format("jazz %d%%", (int)jazz.getTotalUserScrobbles()*100/total), jazz.getTotalUserScrobbles()), + new PieChart.Data(String.format("blues %d%%", (int)blues.getTotalUserScrobbles()*100/total), blues.getTotalUserScrobbles()), + new PieChart.Data(String.format("core %d%%", (int)core.getTotalUserScrobbles()*100/total), core.getTotalUserScrobbles()), + new PieChart.Data(String.format("soul/funk %d%%", (int)soulFunk.getTotalUserScrobbles()*100/total), soulFunk.getTotalUserScrobbles()), + new PieChart.Data(String.format("rnb %d%%", (int)rnb.getTotalUserScrobbles()*100/total), rnb.getTotalUserScrobbles()) + ); Collections.sort(genreData, new Comparator() { @@ -248,7 +255,7 @@ public class ControllerMain { }); - genreData.add(new PieChart.Data("other", totalOther)); + genreData.add(new PieChart.Data(String.format("other %d%%", (int)totalOther*100/total), totalOther)); final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @@ -474,6 +481,11 @@ public class ControllerMain { } } + + @FXML + protected void handleOpenConsole(ActionEvent event){ + addTab(new ConsoleTab()); + } private ArrayList tags; diff --git a/fmframework/src/sarsoo/fmframework/fx/ui/main.fxml b/fmframework/src/sarsoo/fmframework/fx/ui/main.fxml index 7ac8dcc..8a29c19 100644 --- a/fmframework/src/sarsoo/fmframework/fx/ui/main.fxml +++ b/fmframework/src/sarsoo/fmframework/fx/ui/main.fxml @@ -23,6 +23,7 @@ + diff --git a/fmframework/src/sarsoo/fmframework/parser/Parser.java b/fmframework/src/sarsoo/fmframework/parser/Parser.java index 6f869e9..1e08381 100644 --- a/fmframework/src/sarsoo/fmframework/parser/Parser.java +++ b/fmframework/src/sarsoo/fmframework/parser/Parser.java @@ -17,6 +17,7 @@ import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Tag; @@ -30,7 +31,7 @@ public class Parser { public static Album parseAlbum(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseAlbum"); + Reference.getConsole().write("-parseAlbum"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { String name = doc.getElementsByTagName("name").item(0).getTextContent(); @@ -83,7 +84,7 @@ public class Parser { Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki); if(Reference.isVerbose()) - System.out.println(album); + Reference.getConsole().write(album.toString()); return album; } @@ -92,7 +93,7 @@ public class Parser { public static Artist parseArtist(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseArtist"); + TextAreaConsole.getInstance().write("-parseArtist"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { String name = doc.getElementsByTagName("name").item(0).getTextContent(); @@ -170,7 +171,7 @@ public class Parser { Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, wiki); if(Reference.isVerbose()) - System.out.println(artist); + Reference.getConsole().write(artist.toString()); return artist; } @@ -179,7 +180,8 @@ public class Parser { public static Track parseTrack(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseTrack"); +// System.out.println("-parseTrack"); + TextAreaConsole.getInstance().write("-parseTrack"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { String name = " "; @@ -256,7 +258,7 @@ public class Parser { Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki); if(Reference.isVerbose()) - System.out.println(track); + Reference.getConsole().write(track.toString()); return track; } else { @@ -267,7 +269,8 @@ public class Parser { public static Track parseLastTrack(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseLastTrack"); +// System.out.println("-parseLastTrack"); + TextAreaConsole.getInstance().write("-parseLastTrack"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { String name = doc.getElementsByTagName("name").item(0).getTextContent(); @@ -294,7 +297,7 @@ public class Parser { public static ArrayList parseUserTags(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseUserTags"); + Reference.getConsole().write("-parseUserTags"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { NodeList objList = doc.getElementsByTagName("tag"); @@ -319,7 +322,7 @@ public class Parser { Tag tag = new Tag(name, url); if(Reference.isVerbose()) - System.out.println(tag); + Reference.getConsole().write(tag.toString()); // System.out.println(name + " " + url); list.add(tag); @@ -332,7 +335,8 @@ public class Parser { public static FMObjList parseUserTagList(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseUserTagList"); + Reference.getConsole().write("-parseUserTagList"); +// Console.getInstance().write("-parseUserTagList"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { NodeList objList = doc.getElementsByTagName("artist"); @@ -356,7 +360,7 @@ public class Parser { Artist artist = Artist.getArtist(name, Reference.getUserName()); if(Reference.isVerbose()) - System.out.println(artist); + Reference.getConsole().write(artist.toString()); // System.out.println(artist); list.add(artist); @@ -369,7 +373,7 @@ public class Parser { public static FMObjList parseArtistTracks(Document doc) { if(Reference.isVerbose()) - System.out.println("-parseArtistTracks"); + Reference.getConsole().write("-parseArtistTracks"); if (doc.getDocumentElement().getAttribute("status").equals("ok")) { NodeList objList = doc.getElementsByTagName("track"); @@ -400,7 +404,7 @@ public class Parser { Track track = Track.getTrack(name, artist, Reference.getUserName()); if(Reference.isVerbose()) - System.out.println(track); + Reference.getConsole().write(track.toString()); list.add(track); } diff --git a/fmframework/src/sarsoo/fmframework/util/Console.java b/fmframework/src/sarsoo/fmframework/util/Console.java new file mode 100644 index 0000000..7c5b244 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/util/Console.java @@ -0,0 +1,6 @@ +package sarsoo.fmframework.util; + +public interface Console { + + public void write(String string); +} diff --git a/fmframework/src/sarsoo/fmframework/util/Getter.java b/fmframework/src/sarsoo/fmframework/util/Getter.java index 0f76f15..2850cae 100644 --- a/fmframework/src/sarsoo/fmframework/util/Getter.java +++ b/fmframework/src/sarsoo/fmframework/util/Getter.java @@ -16,7 +16,7 @@ import sarsoo.fmframework.parser.Parser; public class Getter { public static Track getLastTrack() { if(Reference.isVerbose()) - System.out.println("--getLastTrack"); + Reference.getConsole().write("--getLastTrack"); String url = URLBuilder.getLastTrackUrl(Reference.getUserName()); // TestCall.test(url); @@ -27,8 +27,8 @@ public class Getter { // System.out.println(doc.getDocumentElement().getAttribute("status")); Parser.stripSpace(doc.getDocumentElement()); Track track = Parser.parseLastTrack(doc); - if(Reference.isVerbose()) - System.out.println(track); +// if(Reference.isVerbose()) +// System.out.println(track); // return null; return track; } @@ -38,7 +38,7 @@ public class Getter { public static int getScrobbles(String username) { if(Reference.isVerbose()) - System.out.println("--getScrobbles"); + Reference.getConsole().write("--getScrobbles"); String url = URLBuilder.getUserInfoUrl(username); Document doc = Network.getResponse(url); @@ -46,7 +46,7 @@ public class Getter { String scrobbles = doc.getElementsByTagName("playcount").item(0).getTextContent(); if (scrobbles != null) if(Reference.isVerbose()) - System.out.println(scrobbles); + Reference.getConsole().write("total scrobbles: " + scrobbles); return Integer.parseInt(scrobbles); } return 0; @@ -54,7 +54,7 @@ public class Getter { public static int getScrobblesToday(String username) { if(Reference.isVerbose()) - System.out.println("--getScrobblesToday"); + Reference.getConsole().write("--getScrobblesToday"); String url = URLBuilder.getTodayScrobbles(username); Document doc = Network.getResponse(url); @@ -64,7 +64,7 @@ public class Getter { // System.out.println(var.getNamedItem("total").getNodeValue()); // if (scrobbles != null) if(Reference.isVerbose()) - System.out.println(var.getNamedItem("total").getNodeValue()); + Reference.getConsole().write("scrobbles today: " + var.getNamedItem("total").getNodeValue()); return Integer.parseInt(var.getNamedItem("total").getNodeValue()); } return 0; @@ -72,7 +72,7 @@ public class Getter { public static FMObjList getUserTag(String username, String tag) { if(Reference.isVerbose()) - System.out.println("--getUserTag"); + Reference.getConsole().write("--getUserTag"); String url = URLBuilder.getUserPersonalTags(username, tag); Document doc = Network.getResponse(url); @@ -81,13 +81,13 @@ public class Getter { int pages = Integer.valueOf( doc.getFirstChild().getFirstChild().getAttributes().getNamedItem("totalPages").getNodeValue()); if(Reference.isVerbose()) - System.out.println("pages: " + pages); + Reference.getConsole().write("tag pages: " + pages); FMObjList list = Parser.parseUserTagList(doc); list.setGroupName(tag); if(Reference.isVerbose()) - System.out.println("name: " + tag); + Reference.getConsole().write("tag: " + tag); if (pages > 1) { int counter; @@ -127,7 +127,7 @@ public class Getter { // } if(Reference.isVerbose()) - System.out.println("--getArtistTracks"); + Reference.getConsole().write("--getArtistTracks"); FMObjList totalList = new FMObjList(); FMObjList list = new FMObjList(); @@ -151,7 +151,7 @@ public class Getter { public static ArrayList getUserTags(String username) { if(Reference.isVerbose()) - System.out.println("--getUserTags"); + Reference.getConsole().write("--getUserTags"); String url = URLBuilder.getUserTopTags(username); Document doc = Network.getResponse(url); diff --git a/fmframework/src/sarsoo/fmframework/util/Reference.java b/fmframework/src/sarsoo/fmframework/util/Reference.java index 5c82468..6d7198d 100644 --- a/fmframework/src/sarsoo/fmframework/util/Reference.java +++ b/fmframework/src/sarsoo/fmframework/util/Reference.java @@ -7,8 +7,10 @@ import sarsoo.fmframework.music.Artist; public class Reference { private static String userName; - private static boolean isVerbose = true; - private static boolean isHeadless = true; + private static boolean isVerbose = false; + private static boolean isHeadless = false; + + private static Console console; public static String getUserName() { return userName; @@ -25,6 +27,22 @@ public class Reference { public static void setIsHeadless(boolean headlessIn) { isHeadless = headlessIn; } + + public static Console getConsole() { + return console; + } + + public static boolean isVerbose() { + return isVerbose; + } + + public static void setVerbose(Console consoleIn) { + if(consoleIn == null) + isVerbose = false; + else + isVerbose = true; + console = consoleIn; + } private static ArrayList groups = new ArrayList(); @@ -136,13 +154,4 @@ public class Reference { return emoTrio; } - - public static boolean isVerbose() { - return isVerbose; - } - - public static void setVerbose(boolean isVerbose) { - Reference.isVerbose = isVerbose; - } - }