diff --git a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java index 55c66ea..d5737aa 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java @@ -21,6 +21,11 @@ import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.fx.chart.GenrePieChartTitledPane; import sarsoo.fmframework.fx.chart.PieChartTitledPane; +import sarsoo.fmframework.fx.service.GetLastTrackService; +import sarsoo.fmframework.fx.service.GetScrobbleCountService; +import sarsoo.fmframework.fx.service.GetTagMenuItemsService; +import sarsoo.fmframework.fx.service.GetTagsService; +import sarsoo.fmframework.fx.service.ScrobbleCount; import sarsoo.fmframework.fx.tab.AlbumTab; import sarsoo.fmframework.fx.tab.ArtistTab; import sarsoo.fmframework.fx.tab.ConsoleTab; @@ -55,116 +60,100 @@ public class RootController { @FXML public void initialize() { -// Reference.setUserName("sarsoo"); - Logger.setLog(new Log(TextAreaConsole.getInstance(), false)); -// ConsoleHandler.setVerbose(TextAreaConsole.getInstance()); - refresh(); } public void refresh() { + labelStatsUsername.setText(Reference.getUserName()); + refreshScrobbleCounts(); + addLastTrackTab(); + refreshTagMenu(); + } + + public void refreshScrobbleCounts() { + NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK); + + GetScrobbleCountService getScrobbles = new GetScrobbleCountService(); + getScrobbles.setOnSucceeded(new EventHandler() { - Service service = new Service() { @Override - protected Task createTask() { - return new Task() { + public void handle(WorkerStateEvent t) { + Platform.runLater(new Runnable() { + @Override - protected Void call() throws Exception { - NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); - - FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); - - String scrobblesToday = numberFormat.format(net.getScrobblesToday()); - String scrobbles = numberFormat.format(net.getUserScrobbleCount()); - - TrackTab tab = new TrackTab(net.getLastTrack()); - - final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - - labelStatsScrobblesToday.setText(scrobblesToday); - labelStatsUsername.setText(Reference.getUserName()); - labelStatsScrobblesTotal.setText(scrobbles); - - addTab(tab); - - // refreshPieCharts(); - refreshTagMenu(); - refreshPieChartMenu(); - - } finally { - latch.countDown(); - } - } - }); - latch.await(); - // Keep with the background work - return null; + public void run() { + labelStatsScrobblesToday.setText( + numberFormat.format(((ScrobbleCount) t.getSource().getValue()).getDailyCount())); + labelStatsScrobblesTotal.setText( + numberFormat.format(((ScrobbleCount) t.getSource().getValue()).getTotalCount())); } - }; + + }); } - }; - service.start(); + }); + getScrobbles.start(); + } + + public void addLastTrackTab() { + GetLastTrackService getLastTrack = new GetLastTrackService(); + getLastTrack.setOnSucceeded(new EventHandler() { + @Override + public void handle(WorkerStateEvent t) { + try { + TrackTab tab = new TrackTab(((Track) t.getSource().getValue())); + + Platform.runLater(new Runnable() { + @Override + public void run() { + addTab(tab); + } + + }); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + getLastTrack.start(); } public void refreshTagMenu() { - FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); - tags = net.getTags(); + GetTagsService getTags = new GetTagsService(); + getTags.setOnSucceeded(new EventHandler() { + @Override + public void handle(WorkerStateEvent t) { - Collections.sort(tags); + tags = (ArrayList) t.getSource().getValue(); - int counter; - for (counter = 0; counter < tags.size(); counter++) { + Collections.sort(tags); - String name = tags.get(counter).getName().toLowerCase(); + GetTagMenuItemsService getTagMenuItems = new GetTagMenuItemsService(tags); - // System.out.println(name); + getTagMenuItems.setOnSucceeded(new EventHandler() { + @Override + public void handle(WorkerStateEvent t) { - MenuItem item = new MenuItem(name); + Platform.runLater(new Runnable() { - item.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent e) { + @Override + public void run() { + menuTag.getItems().setAll((ArrayList) t.getSource().getValue()); + } - // TAG ITEM HANDLER SERVICE - Service service = new Service() { - @Override - protected Task createTask() { - return new Task() { - @Override - protected Void call() throws Exception { + }); - FMObjListTab tab = new FMObjListTab(TagPool.getPool().getTag(name)); + } + }); - final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - tabPane.getTabs().add(tab); - } finally { - latch.countDown(); - } - } - }); - latch.await(); - // Keep with the background work - return null; - } - }; - } - }; - service.start(); - } - }); + getTagMenuItems.start(); + + } + }); + + getTags.start(); - menuTag.getItems().add(item); - } } public void refreshPieChartMenu() { @@ -189,8 +178,8 @@ public class RootController { JSONArray hierarchiesJsonArray = rootParsedJsonObj.getJSONObject("genrehierarchy") .getJSONArray("genres"); - - if(hierarchiesJsonArray.length() > 0) { + + if (hierarchiesJsonArray.length() > 0) { menuPieChart.setVisible(true); } @@ -229,18 +218,18 @@ public class RootController { GenrePieChartTitledPane pane = new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList); - final CountDownLatch latch = new CountDownLatch(1); +// final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { try { accordionCharts.getPanes().add(pane); } finally { - latch.countDown(); +// latch.countDown(); } } }); - latch.await(); +// latch.await(); // Keep with the background work return null; } @@ -262,9 +251,9 @@ public class RootController { } public void refreshPieCharts(File file) { - + Logger.getLog().log(new LogEntry("refreshPieCharts")); - + Service service = new Service() { @Override protected Task createTask() { @@ -325,7 +314,7 @@ public class RootController { } paneList.add(new PieChartTitledPane("total", allTags)); - final CountDownLatch latch = new CountDownLatch(1); +// final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { @@ -336,11 +325,11 @@ public class RootController { accordionCharts.getPanes().add(paneList.get(i)); } } finally { - latch.countDown(); +// latch.countDown(); } } }); - latch.await(); +// latch.await(); return null; } }; @@ -366,13 +355,6 @@ public class RootController { @FXML protected void handleChangeUsername(ActionEvent event) throws IOException { -// System.out.println("USERNAME"); -// String username = JOptionPane.showInputDialog("enter username:"); -// if(username != null) { -// Reference.setUserName(username); -// } -// refresh(); - Service service = new Service() { @Override protected Task createTask() { @@ -380,25 +362,17 @@ public class RootController { @Override protected Void call() throws Exception { - System.out.println("USERNAME"); String username = JOptionPane.showInputDialog("enter username:"); if (username != null) { Reference.setUserName(username); - final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { - try { - refresh(); - } finally { - latch.countDown(); - } + refresh(); } }); - latch.await(); } - // Keep with the background work return null; } }; @@ -421,18 +395,18 @@ public class RootController { if (album != null) { AlbumTab tab = new AlbumTab(album); - final CountDownLatch latch = new CountDownLatch(1); +// final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { try { tabPane.getTabs().add(tab); } finally { - latch.countDown(); +// latch.countDown(); } } }); - latch.await(); +// latch.await(); } // Keep with the background work return null; @@ -458,20 +432,19 @@ public class RootController { if (artist != null) { ArtistTab tab = new ArtistTab(artist); - final CountDownLatch latch = new CountDownLatch(1); +// final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { try { tabPane.getTabs().add(tab); } finally { - latch.countDown(); +// latch.countDown(); } } }); - latch.await(); +// latch.await(); } - // Keep with the background work return null; } }; @@ -495,18 +468,18 @@ public class RootController { if (track != null) { TrackTab tab = new TrackTab(track); - final CountDownLatch latch = new CountDownLatch(1); +// final CountDownLatch latch = new CountDownLatch(1); Platform.runLater(new Runnable() { @Override public void run() { try { tabPane.getTabs().add(tab); } finally { - latch.countDown(); +// latch.countDown(); } } }); - latch.await(); +// latch.await(); } // Keep with the background work return null; @@ -519,39 +492,7 @@ public class RootController { @FXML protected void handleCurrentTrack(ActionEvent event) throws IOException { - Service service = new Service() { - @Override - protected Task createTask() { - return new Task() { - @Override - protected Void call() throws Exception { - - Track track = new FmUserNetwork(Key.getKey(), Reference.getUserName()).getLastTrack(); - - if (track != null) { - TrackTab tab = new TrackTab(track); - - final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - tabPane.getTabs().add(tab); - } finally { - latch.countDown(); - } - } - }); - latch.await(); - } - // Keep with the background work - return null; - } - }; - } - }; - service.start(); - + addLastTrackTab(); } @FXML @@ -597,15 +538,15 @@ public class RootController { protected void handleOpenConsole(ActionEvent event) { addTab(new ConsoleTab()); } - + @FXML protected void handleScrobbleChart(ActionEvent event) { - try { - addTab(new ScrobbleChartTab()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + try { + addTab(new ScrobbleChartTab()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } @@ -658,7 +599,7 @@ public class RootController { @FXML private Menu menuPieChart; - + @FXML private Menu menuChart; // diff --git a/src/main/java/sarsoo/fmframework/fx/service/GetLastTrackService.java b/src/main/java/sarsoo/fmframework/fx/service/GetLastTrackService.java new file mode 100644 index 0000000..d02e1c3 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/service/GetLastTrackService.java @@ -0,0 +1,27 @@ +package sarsoo.fmframework.fx.service; + +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import sarsoo.fmframework.fm.FmUserNetwork; +import sarsoo.fmframework.music.Track; +import sarsoo.fmframework.net.Key; +import sarsoo.fmframework.util.Reference; + +public class GetLastTrackService extends Service{ + + @Override + protected Task createTask() { + return new Task() { + + @Override + protected Track call() throws Exception { + + FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); + + return net.getLastTrack(); + } + + }; + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/service/GetScrobbleCountService.java b/src/main/java/sarsoo/fmframework/fx/service/GetScrobbleCountService.java new file mode 100644 index 0000000..0ca6f1d --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/service/GetScrobbleCountService.java @@ -0,0 +1,26 @@ +package sarsoo.fmframework.fx.service; + +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import sarsoo.fmframework.fm.FmUserNetwork; +import sarsoo.fmframework.net.Key; +import sarsoo.fmframework.util.Reference; + +public class GetScrobbleCountService extends Service{ + + @Override + protected Task createTask() { + return new Task() { + + @Override + protected ScrobbleCount call() throws Exception { + + FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); + + return new ScrobbleCount(net.getScrobblesToday(), net.getUserScrobbleCount()); + } + + }; + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java b/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java new file mode 100644 index 0000000..2db0cee --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java @@ -0,0 +1,81 @@ +package sarsoo.fmframework.fx.service; + +import java.util.ArrayList; + +import javafx.application.Platform; +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.control.MenuItem; +import sarsoo.fmframework.fx.FmFramework; +import sarsoo.fmframework.fx.tab.FMObjListTab; +import sarsoo.fmframework.music.Tag; +import sarsoo.fmframework.util.tagpool.TagPool; + +public class GetTagMenuItemsService extends Service> { + + private ArrayList tags; + + public GetTagMenuItemsService(ArrayList tags) { + super(); + this.tags = tags; + } + + @Override + protected Task> createTask() { + return new Task>() { + + @Override + protected ArrayList call() throws Exception { + + ArrayList items = new ArrayList(); + + int counter; + for (counter = 0; counter < tags.size(); counter++) { + + String name = tags.get(counter).getName().toLowerCase(); + + // System.out.println(name); + + MenuItem item = new MenuItem(name); + + item.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent e) { + + // TAG ITEM HANDLER SERVICE + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + + FMObjListTab tab = new FMObjListTab(TagPool.getPool().getTag(name)); + + Platform.runLater(new Runnable() { + @Override + public void run() { + FmFramework.getController().addTab(tab); + } + }); + return null; + } + }; + } + }; + service.start(); + } + }); + + items.add(item); + } + + return items; + } + + }; + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/service/GetTagsService.java b/src/main/java/sarsoo/fmframework/fx/service/GetTagsService.java new file mode 100644 index 0000000..432c93a --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/service/GetTagsService.java @@ -0,0 +1,29 @@ +package sarsoo.fmframework.fx.service; + +import java.util.ArrayList; + +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import sarsoo.fmframework.fm.FmUserNetwork; +import sarsoo.fmframework.music.Tag; +import sarsoo.fmframework.net.Key; +import sarsoo.fmframework.util.Reference; + +public class GetTagsService extends Service>{ + + @Override + protected Task> createTask() { + return new Task>() { + + @Override + protected ArrayList call() throws Exception { + + FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); + return net.getTags(); + + } + + }; + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/service/ScrobbleCount.java b/src/main/java/sarsoo/fmframework/fx/service/ScrobbleCount.java new file mode 100644 index 0000000..dd9327e --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/service/ScrobbleCount.java @@ -0,0 +1,20 @@ +package sarsoo.fmframework.fx.service; + +public class ScrobbleCount { + + private int dailyCount; + private int totalCount; + + public ScrobbleCount(int dailyCount, int totalCount) { + this.dailyCount = dailyCount; + this.totalCount = totalCount; + } + + public int getDailyCount() { + return dailyCount; + } + + public int getTotalCount() { + return totalCount; + } +}