shifted genre pie charts to separate tab

This commit is contained in:
aj 2019-05-07 21:20:53 +01:00
parent 903f474b8c
commit 7ae40b1c8c
5 changed files with 341 additions and 256 deletions

View File

@ -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<GenreHierarchy> genreHierarchies = new ArrayList<GenreHierarchy>();
@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<String> hierarchyTagNameList = new ArrayList<String>();
String hierarchyName = hierarchyJsonObj.getString("name");
JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags");
ArrayList<String> hierarchyTagNameList = new ArrayList<String>();
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<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@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<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@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<TitledPane> paneList = new ArrayList<TitledPane>();
ArrayList<String> allTags = new ArrayList<String>();
for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) {
JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter);
JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags");
ArrayList<String> hierarchyTagNameList = new ArrayList<String>();
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<String> tagNames;
public GenreHierarchy(String name, ArrayList<String> tagNames) {
this.name = name;
this.tagNames = tagNames;
}
public String getName() {
return name;
}
public ArrayList<String> getTagNames() {
return tagNames;
}
@Override
public String toString() {
return name;
}
}

View File

@ -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<String> hierarchyTagNameList = new ArrayList<String>();
String hierarchyName = hierarchyJsonObj.getString("name");
JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags");
ArrayList<String> hierarchyTagNameList = new ArrayList<String>();
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<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// TAG ITEM HANDLER SERVICE
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@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<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@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<TitledPane> paneList = new ArrayList<TitledPane>();
ArrayList<String> allTags = new ArrayList<String>();
for (counter = 0; counter < hierarchiesJsonArray.length(); counter++) {
JSONObject hierarchyJsonObj = (JSONObject) hierarchiesJsonArray.get(counter);
JSONArray hierarchyTagsJsonArray = hierarchyJsonObj.getJSONArray("tags");
ArrayList<String> hierarchyTagNameList = new ArrayList<String>();
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<Tab> selectionModel = tabPane.getSelectionModel();
@ -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<Tag> 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;
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sarsoo.fmframework.fx.controller.GenrePieChartPaneController">
<children>
<BorderPane fx:id="borderPane" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<bottom>
<ToolBar fx:id="toolbar" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button fx:id="buttonLoadAll" mnemonicParsing="false" onAction="#handleLoadAll" text="load all" />
<ChoiceBox fx:id="choiceBox" prefWidth="150.0" />
<Button fx:id="buttonLoad" mnemonicParsing="false" onAction="#handleLoad" text="load" />
</items>
</ToolBar>
</bottom>
<center>
<Accordion fx:id="accordionCharts" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</AnchorPane>

View File

@ -27,7 +27,7 @@
<Menu fx:id="menuChart" mnemonicParsing="false" text="chart">
<items>
<MenuItem fx:id="menuItemScrobbleChart" mnemonicParsing="false" onAction="#handleScrobbleChart" text="daily scrobbles" />
<Menu fx:id="menuPieChart" mnemonicParsing="false" text="load pie chart" visible="false" />
<MenuItem fx:id="menuItemGenrePieChartTab" mnemonicParsing="false" onAction="#handleGenrePieTab" text="genre pie chart" />
</items>
</Menu>
<Menu fx:id="menuTag" mnemonicParsing="false" text="tags" />
@ -60,26 +60,7 @@
<tabs>
<Tab fx:id="tabHome" closable="false" text="home">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<BorderPane prefHeight="354.0" prefWidth="211.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<Accordion fx:id="accordionCharts" prefHeight="325.0" prefWidth="222.0" BorderPane.alignment="CENTER">
<panes>
</panes>
</Accordion>
</center>
<bottom>
<ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button fx:id="buttonPieChartRefresh" mnemonicParsing="false" onAction="#handleRefreshPieChart" text="refresh" />
</items>
</ToolBar>
</bottom>
</BorderPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>