From 711928b5b8730663bacd9c83541c3d42791e0f77 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 19 Apr 2018 16:25:45 -0700 Subject: [PATCH] almost fully threaded --- .../fx/controller/ControllerMain.java | 411 +++++++++++++----- 1 file changed, 291 insertions(+), 120 deletions(-) diff --git a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java index f7c0aea..90635c4 100644 --- a/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java +++ b/fmframework/src/sarsoo/fmframework/fx/controller/ControllerMain.java @@ -61,72 +61,94 @@ public class ControllerMain { @FXML public void initialize() { Reference.setUserName("sarsoo"); - + NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); - + Service service = new Service() { - @Override - protected Task createTask() { - return new Task() { - @Override - protected Void call() throws Exception { - + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { - String scrobblesToday = numberFormat.format(Getter.getScrobblesToday(Reference.getUserName())); - String scrobbles = numberFormat.format(Getter.getScrobbles(Reference.getUserName())); - tags = Getter.getUserTags(Reference.getUserName()); - - 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); - - refreshPieCharts(); - - int counter; - for (counter = 0; counter < tags.size(); counter++) { + String scrobblesToday = numberFormat.format(Getter.getScrobblesToday(Reference.getUserName())); + String scrobbles = numberFormat.format(Getter.getScrobbles(Reference.getUserName())); + tags = Getter.getUserTags(Reference.getUserName()); - String name = tags.get(counter).getName().toLowerCase(); + final CountDownLatch latch = new CountDownLatch(1); + Platform.runLater(new Runnable() { + @Override + public void run() { + try { - // System.out.println(name); + labelStatsScrobblesToday.setText(scrobblesToday); + labelStatsUsername.setText(Reference.getUserName()); + labelStatsScrobblesTotal.setText(scrobbles); - MenuItem item = new MenuItem(name); + refreshPieCharts(); - item.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent e) { - try { - FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name)); + int counter; + for (counter = 0; counter < tags.size(); counter++) { - tabPane.getTabs().add(tab); - // System.out.println("tab added"); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - }); + String name = tags.get(counter).getName().toLowerCase(); - menuTag.getItems().add(item); - } - - }finally{ - latch.countDown(); - } - } - }); - latch.await(); - //Keep with the background work - return null; - } - }; - } - }; - service.start(); + // 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(Getter + .getUserTag(Reference.getUserName(), 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(); + } + }); + + menuTag.getItems().add(item); + } + + } finally { + latch.countDown(); + } + } + }); + latch.await(); + // Keep with the background work + return null; + } + }; + } + }; + service.start(); } @@ -144,20 +166,114 @@ public class ControllerMain { @FXML protected void handleLookupAlbum(ActionEvent event) throws IOException { + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + + Album album = Getter.getAlbum(); + + if (album != null) { + AlbumTab tab = new AlbumTab(album); + + 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(); - tabPane.getTabs().add(new AlbumTab(Getter.getAlbum())); } @FXML protected void handleLookupArtist(ActionEvent event) throws IOException { + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { - tabPane.getTabs().add(new ArtistTab(Getter.getArtist())); + Artist artist = Getter.getArtist(); + + if (artist != null) { + ArtistTab tab = new ArtistTab(artist); + + 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(); + + // tabPane.getTabs().add(new ArtistTab(Getter.getArtist())); } @FXML protected void handleLookupTrack(ActionEvent event) throws IOException { - tabPane.getTabs().add(new TrackTab(Getter.getTrack())); + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + + Track track = Getter.getTrack(); + + 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(); } @FXML @@ -174,42 +290,43 @@ public class ControllerMain { private ArrayList tags; - @FXML - protected void handleTagClick(ActionEvent event) throws IOException { - // System.out.println("clicked"); - - if (tags == null) { - // System.out.println("list null"); - - tags = Getter.getUserTags(Reference.getUserName()); - - 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) { - try { - FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name)); - - tabPane.getTabs().add(tab); - System.out.println("tab added"); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - }); - - menuTag.getItems().add(item); - } - } - } + // @FXML + // protected void handleTagClick(ActionEvent event) throws IOException { + // // System.out.println("clicked"); + // + // if (tags == null) { + // // System.out.println("list null"); + // + // tags = Getter.getUserTags(Reference.getUserName()); + // + // 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) { + // try { + // FMObjListTab tab = new + // FMObjListTab(Getter.getUserTag(Reference.getUserName(), name)); + // + // tabPane.getTabs().add(tab); + // System.out.println("tab added"); + // } catch (IOException e1) { + // e1.printStackTrace(); + // } + // } + // }); + // + // menuTag.getItems().add(item); + // } + // } + // } @FXML protected void handleKeyShortcut(KeyEvent event) throws IOException { @@ -221,40 +338,94 @@ public class ControllerMain { } public void refresh() { - labelStatsScrobblesToday.setText(Integer.toString(Getter.getScrobblesToday(Reference.getUserName()))); + NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); - labelStatsUsername.setText(Reference.getUserName()); + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { - labelStatsScrobblesTotal - .setText(NumberFormat.getInstance().format(Getter.getScrobbles(Reference.getUserName()))); + String scrobblesToday = numberFormat.format(Getter.getScrobblesToday(Reference.getUserName())); + String scrobbles = numberFormat.format(Getter.getScrobbles(Reference.getUserName())); + tags = Getter.getUserTags(Reference.getUserName()); - tags = Getter.getUserTags(Reference.getUserName()); - menuTag.getItems().clear(); + final CountDownLatch latch = new CountDownLatch(1); + Platform.runLater(new Runnable() { + @Override + public void run() { + try { - int counter; - for (counter = 0; counter < tags.size(); counter++) { + labelStatsScrobblesToday.setText(scrobblesToday); + labelStatsUsername.setText(Reference.getUserName()); + labelStatsScrobblesTotal.setText(scrobbles); - String name = tags.get(counter).getName(); + refreshPieCharts(); + menuTag.getItems().clear(); - // System.out.println(name); + int counter; + for (counter = 0; counter < tags.size(); counter++) { - MenuItem item = new MenuItem(name); + String name = tags.get(counter).getName().toLowerCase(); - item.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent e) { - try { - FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name)); + // System.out.println(name); - tabPane.getTabs().add(tab); - System.out.println("tab added"); - } catch (IOException e1) { - e1.printStackTrace(); + 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(Getter + .getUserTag(Reference.getUserName(), 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(); + } + }); + + menuTag.getItems().add(item); + } + + } finally { + latch.countDown(); + } + } + }); + latch.await(); + // Keep with the background work + return null; } - } - }); - menuTag.getItems().add(item); - } + }; + } + }; + service.start(); } public void addTab(Tab tab) {