From 0ea8f91e140f18306154611e4e061023c04fd1dc Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 18 Apr 2018 22:46:48 -0700 Subject: [PATCH] added track tab --- .../src/sarsoo/fmframework/fx/TrackTab.java | 35 ++++- .../fx/controller/ControllerMain.java | 18 ++- .../controller/FMObjListPaneController.java | 50 ++++-- .../fx/controller/TrackPaneController.java | 146 ++++++++++++++++++ .../sarsoo/fmframework/fx/ui/TrackPane.fxml | 91 +++++++++++ .../src/sarsoo/fmframework/fx/ui/main.fxml | 2 +- .../src/sarsoo/fmframework/util/Getter.java | 23 ++- 7 files changed, 341 insertions(+), 24 deletions(-) create mode 100644 fmframework/src/sarsoo/fmframework/fx/controller/TrackPaneController.java create mode 100644 fmframework/src/sarsoo/fmframework/fx/ui/TrackPane.fxml diff --git a/fmframework/src/sarsoo/fmframework/fx/TrackTab.java b/fmframework/src/sarsoo/fmframework/fx/TrackTab.java index e18ecfd..60e7391 100644 --- a/fmframework/src/sarsoo/fmframework/fx/TrackTab.java +++ b/fmframework/src/sarsoo/fmframework/fx/TrackTab.java @@ -1,5 +1,38 @@ package sarsoo.fmframework.fx; -public class TrackTab { +import java.io.IOException; + +import javafx.scene.control.*; +import javafx.scene.layout.*; +import sarsoo.fmframework.fx.controller.AlbumPaneController; +import sarsoo.fmframework.fx.controller.TrackPaneController; +import sarsoo.fmframework.music.Album; +import sarsoo.fmframework.music.Track; +import javafx.fxml.FXMLLoader; + +public class TrackTab extends Tab { + + public TrackTab(Track track) throws IOException { + + setText(track.getName()); + + FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/TrackPane.fxml")); + + AnchorPane pane = (AnchorPane) loader.load(); + + AnchorPane.setTopAnchor(pane, 0.0); + AnchorPane.setLeftAnchor(pane, 0.0); + AnchorPane.setRightAnchor(pane, 0.0); + AnchorPane.setBottomAnchor(pane, 0.0); + + setContent(pane); + + TrackPaneController control = (TrackPaneController) loader.getController(); + + control.populate(track); + + + + } } diff --git a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java index 984782e..824004e 100644 --- a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java +++ b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java @@ -15,6 +15,7 @@ import javafx.scene.text.Text; import sarsoo.fmframework.fx.AlbumTab; import sarsoo.fmframework.fx.ArtistTab; import sarsoo.fmframework.fx.FMObjListTab; +import sarsoo.fmframework.fx.TrackTab; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.util.FMObjList; @@ -67,7 +68,7 @@ public class ControllerMain { int counter; for (counter = 0; counter < tags.size(); counter++) { - String name = tags.get(counter).getName(); + String name = tags.get(counter).getName().toLowerCase(); // System.out.println(name); @@ -80,7 +81,7 @@ public class ControllerMain { FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name)); tabPane.getTabs().add(tab); - System.out.println("tab added"); +// System.out.println("tab added"); } catch (IOException e1) { e1.printStackTrace(); } @@ -150,6 +151,12 @@ public class ControllerMain { tabPane.getTabs().add(new ArtistTab(Getter.getArtist())); } + + @FXML + protected void handleLookupTrack(ActionEvent event) throws IOException { + + tabPane.getTabs().add(new TrackTab(Getter.getTrack())); + } @FXML private Menu menuTag; @@ -168,7 +175,7 @@ public class ControllerMain { int counter; for (counter = 0; counter < tags.size(); counter++) { - String name = tags.get(counter).getName(); + String name = tags.get(counter).getName().toLowerCase(); // System.out.println(name); @@ -198,7 +205,7 @@ public class ControllerMain { if (event.getCode() == KeyCode.F5) { refresh(); - System.out.println("refreshed"); +// System.out.println("refreshed"); } } @@ -242,5 +249,8 @@ public class ControllerMain { public void addTab(Tab tab) { tabPane.getTabs().add(tab); + SingleSelectionModel selectionModel = tabPane.getSelectionModel(); + + selectionModel.select(tab); } } diff --git a/fmframework/src/sarsoo/fmframework/fx/controller/FMObjListPaneController.java b/fmframework/src/sarsoo/fmframework/fx/controller/FMObjListPaneController.java index 33eb772..23392be 100644 --- a/fmframework/src/sarsoo/fmframework/fx/controller/FMObjListPaneController.java +++ b/fmframework/src/sarsoo/fmframework/fx/controller/FMObjListPaneController.java @@ -1,11 +1,19 @@ package sarsoo.fmframework.fx.controller; +import java.io.IOException; import java.text.NumberFormat; import java.util.Collections; import java.util.Locale; +import javafx.event.Event; +import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Label; +import javafx.scene.input.MouseEvent; +import sarsoo.fmframework.fx.AlbumTab; +import sarsoo.fmframework.fx.ArtistTab; +import sarsoo.fmframework.fx.FmFramework; +import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.Maths; @@ -13,7 +21,7 @@ import sarsoo.fmframework.util.Reference; import javafx.scene.layout.*; public class FMObjListPaneController { - + @FXML private Label labelTotalScrobbles; @@ -22,33 +30,49 @@ public class FMObjListPaneController { @FXML private GridPane gridPaneFMObjs; - + public void populate(FMObjList list) { double percent = Maths.getPercentListening(list, Reference.getUserName()); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); - - labelTotalScrobbles.setText("Σ" + list.getTotalUserScrobbles()); + + labelTotalScrobbles.setText("Σ " + list.getTotalUserScrobbles()); labelPercent.setText(String.format("%.2f%%", percent)); - + Collections.sort(list); Collections.reverse(list); - + int counter; - for(counter = 0; counter < list.size(); counter++) { - + for (counter = 0; counter < list.size(); counter++) { + FMObj obj = list.get(counter); - + Label name = new Label(obj.getName().toLowerCase()); + + name.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() { + + @Override + public void handle(Event event) { + + try { + FmFramework.getController().addTab(new ArtistTab((Artist) obj)); + } catch (IOException e) { + + e.printStackTrace(); + } + + } + + }); + Label userScrobbles = new Label(numberFormat.format(obj.getUserPlayCount())); Label totalScrobbles = new Label(numberFormat.format(obj.getPlayCount())); - + gridPaneFMObjs.add(name, 0, counter); gridPaneFMObjs.add(userScrobbles, 1, counter); gridPaneFMObjs.add(totalScrobbles, 2, counter); - - + } - + } } diff --git a/fmframework/src/sarsoo/fmframework/fx/controller/TrackPaneController.java b/fmframework/src/sarsoo/fmframework/fx/controller/TrackPaneController.java new file mode 100644 index 0000000..a2bad73 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/fx/controller/TrackPaneController.java @@ -0,0 +1,146 @@ +package sarsoo.fmframework.fx.controller; + +import java.io.IOException; +import java.text.NumberFormat; +import java.util.Locale; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import sarsoo.fmframework.fx.AlbumTab; +import sarsoo.fmframework.fx.ArtistTab; +import sarsoo.fmframework.fx.FmFramework; +import sarsoo.fmframework.music.Track; +import sarsoo.fmframework.music.Wiki; +import sarsoo.fmframework.net.Network; +import sarsoo.fmframework.util.Maths; +import sarsoo.fmframework.util.Reference; +import javafx.scene.control.*; + +public class TrackPaneController { + + @FXML + private Label labelTrackName; + + @FXML + private Label labelAlbumName; + + @FXML + private Label labelArtistName; + + @FXML + private Label labelUserScrobbles; + + @FXML + private Label labelRatio; + + @FXML + private Label labelTotalListeners; + + @FXML + private Label labelTotalScrobbles; + + @FXML + private TextArea textAreaWiki; + + @FXML + private Button buttonViewAlbum; + + @FXML + public void initialize() { + + } + + Track track; + + public void populate(Track track) { + + this.track = track; + + NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); + + labelTrackName.setText(track.getName()); + + if (track.getAlbum() == null) + labelAlbumName.setVisible(false); + else + labelAlbumName.setText(track.getAlbum().getName()); + + labelArtistName.setText(track.getArtist().getName()); + labelUserScrobbles.setText(numberFormat.format(track.getUserPlayCount()) + + String.format(" Scrobbles (%.2f%%)", Maths.getPercentListening(track, Reference.getUserName()))); + + labelTotalListeners.setText(numberFormat.format(track.getListeners()) + " Listeners"); + labelTotalScrobbles.setText(numberFormat.format(track.getPlayCount()) + " Total Scrobbles"); + + double ratio = track.getTimeListenRatio(); + + if (ratio > 1) { + labelRatio.setText(String.format("listen every %.2f days", ratio)); + } else if (ratio == 1) { + labelRatio.setText("listen every day"); + } else { + labelRatio.setText(String.format("%.2f times a day", 1 / ratio)); + } + + Wiki wiki = track.getWiki(); + + if (wiki != null) { + + textAreaWiki.setText(wiki.getContent() + "\n\n" + wiki.getDate()); + } + + if (track.getAlbum() == null) { + buttonViewAlbum.setVisible(false); + } + } + + @FXML + protected void handleRefresh(ActionEvent event) { + refresh(); + } + + @FXML + protected void viewOnline(ActionEvent event) { + Network.openURL(track.getUrl()); + } + + @FXML + protected void viewArtist(ActionEvent event) throws IOException { + FmFramework.getController().addTab(new ArtistTab(track.getArtist())); + } + + @FXML + protected void viewAlbum(ActionEvent event) throws IOException { + FmFramework.getController().addTab(new AlbumTab(track.getAlbum())); + } + + public void refresh() { + track.refresh(); + + NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); + + labelUserScrobbles.setText(numberFormat.format(track.getUserPlayCount()) + + String.format(" Scrobbles (%.2f%%)", Maths.getPercentListening(track, Reference.getUserName()))); + + labelTotalListeners.setText(numberFormat.format(track.getListeners()) + " Listeners"); + labelTotalScrobbles.setText(numberFormat.format(track.getPlayCount()) + " Total Scrobbles"); + + double ratio = track.getTimeListenRatio(); + + if (ratio > 1) { + labelRatio.setText(String.format("listen every %.2f days", ratio)); + } else if (ratio == 1) { + labelRatio.setText("listen every day"); + } else { + labelRatio.setText(String.format("%.2f times a day", 1 / ratio)); + } + + Wiki wiki = track.getWiki(); + + if (wiki != null) { + + textAreaWiki.setText(wiki.getContent() + "\n\n" + wiki.getDate()); + } + } + +} diff --git a/fmframework/src/sarsoo/fmframework/fx/ui/TrackPane.fxml b/fmframework/src/sarsoo/fmframework/fx/ui/TrackPane.fxml new file mode 100644 index 0000000..246f052 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/fx/ui/TrackPane.fxml @@ -0,0 +1,91 @@ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +