From 7ae40b1c8cde51bafc44e880be25cf4543014562 Mon Sep 17 00:00:00 2001 From: aj Date: Tue, 7 May 2019 21:20:53 +0100 Subject: [PATCH] shifted genre pie charts to separate tab --- .../GenrePieChartPaneController.java | 268 ++++++++++++++++++ .../fx/controller/RootController.java | 243 +--------------- .../fmframework/fx/tab/GenrePieChartTab.java | 39 +++ .../fmframework/fx/ui/GenrePieChartPane.fxml | 24 ++ .../sarsoo/fmframework/fx/ui/RootPane.fxml | 23 +- 5 files changed, 341 insertions(+), 256 deletions(-) create mode 100644 src/main/java/sarsoo/fmframework/fx/controller/GenrePieChartPaneController.java create mode 100644 src/main/java/sarsoo/fmframework/fx/tab/GenrePieChartTab.java create mode 100644 src/main/resources/sarsoo/fmframework/fx/ui/GenrePieChartPane.fxml diff --git a/src/main/java/sarsoo/fmframework/fx/controller/GenrePieChartPaneController.java b/src/main/java/sarsoo/fmframework/fx/controller/GenrePieChartPaneController.java new file mode 100644 index 0000000..4fd908b --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/controller/GenrePieChartPaneController.java @@ -0,0 +1,268 @@ +package sarsoo.fmframework.fx.controller; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +import org.json.JSONArray; +import org.json.JSONObject; + +import javafx.application.Platform; +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Accordion; +import javafx.scene.control.Button; +import javafx.scene.control.ChoiceBox; +import javafx.scene.control.TitledPane; +import javafx.scene.control.ToolBar; +import javafx.scene.layout.BorderPane; +import javafx.stage.FileChooser; +import sarsoo.fmframework.fx.FmFramework; +import sarsoo.fmframework.fx.chart.GenrePieChartTitledPane; +import sarsoo.fmframework.fx.chart.PieChartTitledPane; +import sarsoo.fmframework.log.Logger; +import sarsoo.fmframework.log.entry.InfoEntry; +import sarsoo.fmframework.log.entry.LogEntry; + +public class GenrePieChartPaneController { + + @FXML + BorderPane borderPane; + + @FXML + Accordion accordionCharts; + + @FXML + ToolBar toolbar; + + @FXML + Button buttonLoadAll; + + @FXML + Button buttonLoad; + + @FXML + ChoiceBox choiceBox; + + ArrayList genreHierarchies = new ArrayList(); + + @FXML + public void initialize() { + try { + if (new File("./piechart.json").isFile()) { + File file = new File("./piechart.json"); + + BufferedReader br = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + String jsonLine = br.readLine(); + while (jsonLine != null) { + sb.append(jsonLine); + jsonLine = br.readLine(); + } + + br.close(); + + String jsonString = sb.toString(); + + JSONObject rootParsedJsonObj = new JSONObject(jsonString); + + JSONArray hierarchiesJsonArray = rootParsedJsonObj.getJSONObject("genrehierarchy") + .getJSONArray("genres"); + + if (hierarchiesJsonArray.length() > 0) { +// menuPieChart.setVisible(true); + } + + int counter; + for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) { + + JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter); +// JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); +// ArrayList hierarchyTagNameList = new ArrayList(); + + String hierarchyName = hierarchyJsonObj.getString("name"); + JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); + ArrayList hierarchyTagNameList = new ArrayList(); + + int i; + for (i = 0; i < hierarchyTagsJsonArray.length(); i++) { + hierarchyTagNameList.add(hierarchyTagsJsonArray.getString(i)); +// allTags.add(hierarchyTagsJsonArray.getString(i)); + } + + choiceBox.getItems().add(new GenreHierarchy(hierarchyName, hierarchyTagNameList)); +// paneList.add(new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList)); + } + + } + } catch (IOException e) { + + } + } + + @FXML + protected void handleLoad(ActionEvent event) { + GenreHierarchy hier = (GenreHierarchy) choiceBox.getValue(); + + if (hier != null) { + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + + GenrePieChartTitledPane pane = new GenrePieChartTitledPane(hier.getName(), + hier.getTagNames()); + + Platform.runLater(new Runnable() { + @Override + public void run() { + accordionCharts.getPanes().add(pane); + } + }); + return null; + } + }; + } + }; + service.start(); + } + } + + @FXML + protected void handleLoadAll(ActionEvent event) { + + File file = null; + String path = null; + if (new File("./piechart.json").isFile()) { + file = new File("./piechart.json"); + } else { + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("open pie chart json"); + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON", "*.json")); + file = fileChooser.showOpenDialog(FmFramework.getStage()); + } + + refreshPieCharts(file); + + } + + public void refreshPieCharts(File file) { + + Logger.getLog().log(new LogEntry("refreshPieCharts")); + + Service service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + + String jsonString = null; + if (file != null) { + + BufferedReader br = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + String jsonLine = br.readLine(); + while (jsonLine != null) { + sb.append(jsonLine); + jsonLine = br.readLine(); + } + + br.close(); + + jsonString = sb.toString(); + Logger.getLog().logInfo(new InfoEntry("refreshPieCharts").addArg("json read")); + } + + JSONObject rootParsedJsonObj = new JSONObject(jsonString); + + JSONArray hierarchiesJsonArray = rootParsedJsonObj.getJSONObject("genrehierarchy") + .getJSONArray("genres"); + JSONObject pieJson = rootParsedJsonObj.getJSONObject("pie"); + + Logger.getLog().logInfo(new InfoEntry("refreshPieCharts").addArg("arrays parsed")); + + int counter; + ArrayList paneList = new ArrayList(); + + ArrayList allTags = new ArrayList(); + + for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) { + JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter); + JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); + ArrayList hierarchyTagNameList = new ArrayList(); + + String hierarchyName = hierarchyJsonObj.getString("name"); + + int i; + for (i = 0; i < hierarchyTagsJsonArray.length(); i++) { + hierarchyTagNameList.add(hierarchyTagsJsonArray.getString(i)); + allTags.add(hierarchyTagsJsonArray.getString(i)); + } + + paneList.add(new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList)); + } + + JSONArray totalPieTags = pieJson.getJSONArray("tags"); + int i; + for (i = 0; i < totalPieTags.length(); i++) { + allTags.add((totalPieTags).getString(i)); + } + paneList.add(new PieChartTitledPane("total", allTags)); + +// final CountDownLatch latch = new CountDownLatch(1); + Platform.runLater(new Runnable() { + @Override + public void run() { + try { + accordionCharts.getPanes().clear(); + int i; + for (i = 0; i < paneList.size(); i++) { + accordionCharts.getPanes().add(paneList.get(i)); + } + } finally { +// latch.countDown(); + } + } + }); +// latch.await(); + return null; + } + }; + } + }; + service.start(); + } + +} + +class GenreHierarchy { + + private String name; + private ArrayList tagNames; + + public GenreHierarchy(String name, ArrayList tagNames) { + this.name = name; + this.tagNames = tagNames; + } + + public String getName() { + return name; + } + + public ArrayList getTagNames() { + return tagNames; + } + + @Override + public String toString() { + return name; + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java index e27c098..f7a763c 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java @@ -32,6 +32,7 @@ import sarsoo.fmframework.fx.tab.ArtistTab; import sarsoo.fmframework.fx.tab.ConsoleTab; import sarsoo.fmframework.fx.tab.FMObjListEditTab; import sarsoo.fmframework.fx.tab.FMObjListTab; +import sarsoo.fmframework.fx.tab.GenrePieChartTab; import sarsoo.fmframework.fx.tab.ScrobbleChartTab; import sarsoo.fmframework.fx.tab.TrackTab; import sarsoo.fmframework.log.Log; @@ -69,7 +70,6 @@ public class RootController { refreshScrobbleCounts(); addLastTrackTab(); refreshTagMenu(); - refreshPieChartMenu(); } public void refreshScrobbleCounts() { @@ -157,188 +157,6 @@ public class RootController { } - public void refreshPieChartMenu() { - - try { - if (new File("./piechart.json").isFile()) { - File file = new File("./piechart.json"); - - BufferedReader br = new BufferedReader(new FileReader(file)); - StringBuilder sb = new StringBuilder(); - String jsonLine = br.readLine(); - while (jsonLine != null) { - sb.append(jsonLine); - jsonLine = br.readLine(); - } - - br.close(); - - String jsonString = sb.toString(); - - JSONObject rootParsedJsonObj = new JSONObject(jsonString); - - JSONArray hierarchiesJsonArray = rootParsedJsonObj.getJSONObject("genrehierarchy") - .getJSONArray("genres"); - - if (hierarchiesJsonArray.length() > 0) { - menuPieChart.setVisible(true); - } - - int counter; - for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) { - JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter); -// JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); -// ArrayList hierarchyTagNameList = new ArrayList(); - - String hierarchyName = hierarchyJsonObj.getString("name"); - JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); - ArrayList hierarchyTagNameList = new ArrayList(); - - int i; - for (i = 0; i < hierarchyTagsJsonArray.length(); i++) { - hierarchyTagNameList.add(hierarchyTagsJsonArray.getString(i)); -// allTags.add(hierarchyTagsJsonArray.getString(i)); - } - -// paneList.add(new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList)); - - MenuItem item = new MenuItem(hierarchyName); - - 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 { - - GenrePieChartTitledPane pane = new GenrePieChartTitledPane(hierarchyName, - hierarchyTagNameList); - -// final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - accordionCharts.getPanes().add(pane); - } finally { -// latch.countDown(); - } - } - }); -// latch.await(); - // Keep with the background work - return null; - } - }; - } - }; - service.start(); - } - }); - - menuPieChart.getItems().add(item); - } - - } - } catch (IOException e) { - - } - - } - - public void refreshPieCharts(File file) { - - Logger.getLog().log(new LogEntry("refreshPieCharts")); - - Service service = new Service() { - @Override - protected Task createTask() { - return new Task() { - @Override - protected Void call() throws Exception { - - String jsonString = null; - if (file != null) { - - BufferedReader br = new BufferedReader(new FileReader(file)); - StringBuilder sb = new StringBuilder(); - String jsonLine = br.readLine(); - while (jsonLine != null) { - sb.append(jsonLine); - jsonLine = br.readLine(); - } - - br.close(); - - jsonString = sb.toString(); - Logger.getLog().logInfo(new InfoEntry("refreshPieCharts").addArg("json read")); - } - - JSONObject rootParsedJsonObj = new JSONObject(jsonString); - - JSONArray hierarchiesJsonArray = rootParsedJsonObj.getJSONObject("genrehierarchy") - .getJSONArray("genres"); - JSONObject pieJson = rootParsedJsonObj.getJSONObject("pie"); - - Logger.getLog().logInfo(new InfoEntry("refreshPieCharts").addArg("arrays parsed")); - - int counter; - ArrayList paneList = new ArrayList(); - - ArrayList allTags = new ArrayList(); - - for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) { - JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter); - JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags"); - ArrayList hierarchyTagNameList = new ArrayList(); - - String hierarchyName = hierarchyJsonObj.getString("name"); - - int i; - for (i = 0; i < hierarchyTagsJsonArray.length(); i++) { - hierarchyTagNameList.add(hierarchyTagsJsonArray.getString(i)); - allTags.add(hierarchyTagsJsonArray.getString(i)); - } - - paneList.add(new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList)); - } - - JSONArray totalPieTags = pieJson.getJSONArray("tags"); - int i; - for (i = 0; i < totalPieTags.length(); i++) { - allTags.add((totalPieTags).getString(i)); - } - paneList.add(new PieChartTitledPane("total", allTags)); - -// final CountDownLatch latch = new CountDownLatch(1); - Platform.runLater(new Runnable() { - @Override - public void run() { - try { - accordionCharts.getPanes().clear(); - int i; - for (i = 0; i < paneList.size(); i++) { - accordionCharts.getPanes().add(paneList.get(i)); - } - } finally { -// latch.countDown(); - } - } - }); -// latch.await(); - return null; - } - }; - } - }; - service.start(); - } - public void addTab(Tab tab) { tabPane.getTabs().add(tab); SingleSelectionModel selectionModel = tabPane.getSelectionModel(); @@ -352,7 +170,7 @@ public class RootController { if (event.getCode() == KeyCode.F5) { refresh(); } - + // if (event.getCode() == KeyCode.Q && event.isControlDown()) { // System.out.println("control q"); // @@ -474,20 +292,13 @@ public class RootController { 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(); - } + tabPane.getTabs().add(tab); } }); -// latch.await(); } - // Keep with the background work return null; } }; @@ -501,24 +312,6 @@ public class RootController { addLastTrackTab(); } - @FXML - protected void handleRefreshPieChart(ActionEvent event) throws IOException { - - File file = null; - String path = null; - if (new File("./piechart.json").isFile()) { - file = new File("./piechart.json"); - } else { - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("open pie chart json"); - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON", "*.json")); - file = fileChooser.showOpenDialog(FmFramework.getStage()); - } - - refreshPieCharts(file); - - } - @FXML protected void handleCreateList(ActionEvent event) { @@ -550,7 +343,6 @@ public class RootController { try { addTab(new ScrobbleChartTab()); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } @@ -583,6 +375,11 @@ public class RootController { addTab(new ConsoleTab()); } + @FXML + protected void handleGenrePieTab(ActionEvent event) { + addTab(new GenrePieChartTab()); + } + private ArrayList tags; @FXML @@ -593,9 +390,6 @@ public class RootController { @FXML private Label labelStatsScrobblesTotal; -// -// @FXML -// private PieChart pieChartGenres; @FXML private TabPane tabPane; @@ -608,29 +402,8 @@ public class RootController { @FXML private Menu menuChart; -// -// @FXML -// private PieChart pieChartRap; -// -// @FXML -// private PieChart pieChartRapTotal; -// -// @FXML -// private PieChart pieChartRock; -// -// @FXML -// private PieChart pieChartRockTotal; @FXML private Accordion accordionCharts; - @FXML - private TitledPane titledPaneGenres; - - @FXML - private TitledPane titledPaneRap; - - @FXML - private StackPane stackViewGenres; - } diff --git a/src/main/java/sarsoo/fmframework/fx/tab/GenrePieChartTab.java b/src/main/java/sarsoo/fmframework/fx/tab/GenrePieChartTab.java new file mode 100644 index 0000000..46df180 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/fx/tab/GenrePieChartTab.java @@ -0,0 +1,39 @@ +package sarsoo.fmframework.fx.tab; + +import java.io.IOException; + +import javafx.fxml.FXMLLoader; +import javafx.scene.control.Tab; +import javafx.scene.layout.AnchorPane; +import sarsoo.fmframework.fx.controller.GenrePieChartPaneController; + +public class GenrePieChartTab extends Tab{ + + public GenrePieChartTab() { + + setText("genre pie"); + + FXMLLoader loader = new FXMLLoader(getClass().getResource("../ui/GenrePieChartPane.fxml")); + + AnchorPane pane; + try { + 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); + + GenrePieChartPaneController control = (GenrePieChartPaneController) loader.getController(); + + //control.populate(); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } +} diff --git a/src/main/resources/sarsoo/fmframework/fx/ui/GenrePieChartPane.fxml b/src/main/resources/sarsoo/fmframework/fx/ui/GenrePieChartPane.fxml new file mode 100644 index 0000000..84e21b2 --- /dev/null +++ b/src/main/resources/sarsoo/fmframework/fx/ui/GenrePieChartPane.fxml @@ -0,0 +1,24 @@ + + + + + + + + + + + + +