From 186969b115df3e905a83bfa68c2e8e3a478156d5 Mon Sep 17 00:00:00 2001 From: aj Date: Tue, 4 Dec 2018 23:28:01 +0000 Subject: [PATCH] auto find pie chart json and view pie chart option in menu --- piechart.json | 2 + .../fx/controller/ControllerMain.java | 138 ++++++++++++++++-- .../sarsoo/fmframework/fx/ui/main.fxml | 1 + 3 files changed, 129 insertions(+), 12 deletions(-) diff --git a/piechart.json b/piechart.json index 47eee72..8168e4d 100644 --- a/piechart.json +++ b/piechart.json @@ -25,6 +25,8 @@ "rap", "classic rap", "grime", + "trap", + "uk hip-hop" ], "name":"rap" }, diff --git a/src/main/java/sarsoo/fmframework/fx/controller/ControllerMain.java b/src/main/java/sarsoo/fmframework/fx/controller/ControllerMain.java index c76fd7f..34db76c 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/ControllerMain.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/ControllerMain.java @@ -89,6 +89,7 @@ public class ControllerMain { // refreshPieCharts(); refreshTagMenu(); + refreshPieChartMenu(); } finally { latch.countDown(); @@ -132,9 +133,9 @@ public class ControllerMain { 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 @@ -161,12 +162,111 @@ public class ControllerMain { } } - public void refreshPieCharts() { - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("open pie chart json"); - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON", "*.json")); - File file = fileChooser.showOpenDialog(FmFramework.getStage()); + 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"); + + 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)); + System.out.println(hierarchyTagsJsonArray.getString(i)); + } + + System.out.println("hierarchy: " + hierarchyName); + System.out.println(hierarchyTagNameList); + +// 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) { +// 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()); +// } Service service = new Service() { @Override protected Task createTask() { @@ -185,7 +285,7 @@ public class ControllerMain { sb.append(jsonLine); jsonLine = br.readLine(); } - + br.close(); jsonString = sb.toString(); @@ -204,7 +304,7 @@ public class ControllerMain { 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"); @@ -221,10 +321,10 @@ public class ControllerMain { System.out.println("hierarchy: " + hierarchyName); System.out.println(hierarchyTagNameList); - + paneList.add(new GenrePieChartTitledPane(hierarchyName, hierarchyTagNameList)); } - + JSONArray totalPieTags = pieJson.getJSONArray("tags"); int i; for (i = 0; i < totalPieTags.length(); i++) { @@ -465,7 +565,18 @@ public class ControllerMain { @FXML protected void handleRefreshPieChart(ActionEvent event) throws IOException { - refreshPieCharts(); + 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); } @@ -541,6 +652,9 @@ public class ControllerMain { @FXML private Menu menuTag; + + @FXML + private Menu menuPieChart; // // @FXML // private PieChart pieChartRap; diff --git a/src/main/resources/sarsoo/fmframework/fx/ui/main.fxml b/src/main/resources/sarsoo/fmframework/fx/ui/main.fxml index 3bad9ab..9c6047a 100644 --- a/src/main/resources/sarsoo/fmframework/fx/ui/main.fxml +++ b/src/main/resources/sarsoo/fmframework/fx/ui/main.fxml @@ -14,6 +14,7 @@ +