Last active
June 19, 2018 20:34
-
-
Save ityulkanov/e5f9046490b73557d0a1d0f2b6a68b2b to your computer and use it in GitHub Desktop.
controller for book catalog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import eu.hansolo.tilesfx.Tile; | |
import eu.hansolo.tilesfx.TileBuilder; | |
import eu.hansolo.tilesfx.skins.BarChartItem; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.collections.transformation.FilteredList; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.stage.FileChooser; | |
import javafx.stage.Stage; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class Controller { | |
@FXML | |
private TextField tfId; | |
@FXML | |
private TextField tfTitle; | |
@FXML | |
private TextField tfAuthor; | |
@FXML | |
private TextField tfYear; | |
@FXML | |
private Pane barChartPane; | |
@FXML | |
private Pane barChartPane1; | |
@FXML | |
private Tile barChartTile; | |
@FXML | |
private Tile barChartTile1; | |
private BarChartItem barChartItemOld; | |
private BarChartItem barChartItem18; | |
private BarChartItem barChartItem19; | |
private BarChartItem barChartItem20; | |
private ArrayList<BarChartItem> namesCharts = new ArrayList<>(); | |
@FXML | |
private Button btnAdd; | |
@FXML | |
private Button btnOpen; | |
@FXML | |
private Button btnChoose; | |
@FXML | |
private TableView<Book> tvData; | |
@FXML | |
private TableColumn<Book, Integer> tcId; | |
@FXML | |
private TableColumn<Book, String> tcTitle; | |
@FXML | |
private TableColumn<Book, String> tcAuthor; | |
@FXML | |
private TableColumn<Book, Integer> tcYear; | |
@FXML | |
private ObservableList<Book> bookData = | |
FXCollections.observableArrayList(); | |
private FilteredList<Book> fileredData; | |
@FXML | |
public void initialize() { | |
tcId.setCellValueFactory(new PropertyValueFactory<Book, Integer>("id")); | |
tcTitle.setCellValueFactory(new PropertyValueFactory<Book, String>("title")); | |
tcAuthor.setCellValueFactory(new PropertyValueFactory<Book, String>("author")); | |
tcYear.setCellValueFactory(new PropertyValueFactory<Book, Integer>("year")); | |
tvData.setItems(bookData); | |
barChartItemOld = new BarChartItem("Until 18c", 15, Tile.ORANGE); | |
barChartItem18 = new BarChartItem("From 18 till 19", 25, Tile.BLUE); | |
barChartItem19 = new BarChartItem("From 19 till 20", 35, Tile.GREEN); | |
barChartItem20 = new BarChartItem("From 20 and now", 45, Tile.MAGENTA); | |
barChartTile = TileBuilder.create() | |
.prefSize(150, 150) | |
.skinType(Tile.SkinType.BAR_CHART) | |
.title("Statistics") | |
.text("") | |
.textColor(Color.BLACK) | |
.titleColor(Color.BLACK) | |
.valueColor(Color.BLUE) | |
.backgroundColor(Color.rgb(244, 244, 244)) | |
.barChartItems(barChartItemOld, barChartItem18, barChartItem19, barChartItem20) | |
.build(); | |
barChartPane.getChildren().add(barChartTile); | |
barChartTile1 = TileBuilder.create() | |
.prefSize(150, 150) | |
.skinType(Tile.SkinType.BAR_CHART) | |
.title("Authors count") | |
.text("") | |
.textColor(Color.BLACK) | |
.titleColor(Color.BLACK) | |
.valueColor(Color.BLUE) | |
.backgroundColor(Color.rgb(244, 244, 244)) | |
.barChartItems(namesCharts) | |
.build(); | |
barChartPane1.getChildren().add(barChartTile1); | |
} | |
@FXML | |
public void onClickAdd() { | |
bookData.add(new Book(Integer.parseInt(tfId.getText()), | |
tfTitle.getText(), | |
tfAuthor.getText(), | |
Integer.parseInt(tfYear.getText()))); | |
} | |
@FXML | |
public void onclickOpen() { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); | |
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON", "*.json")); | |
File selectedFile = fileChooser.showOpenDialog(new Stage()); | |
importJson(selectedFile.getAbsolutePath()); | |
} | |
public void importJson(String filename) { | |
int id = 1; | |
JSONParser parser = new JSONParser(); | |
try { | |
Object obj = parser.parse(new FileReader(filename)); | |
JSONArray books = (JSONArray) obj; | |
Iterator bookIterator = books.iterator(); | |
while(bookIterator.hasNext()) { | |
JSONObject book = (JSONObject) bookIterator.next(); | |
String title = book.get("title").toString(); | |
String author = book.get("author").toString(); | |
int year = Integer.parseInt(book.get("year").toString()); | |
bookData.add(new Book(id++, title, author, year)); | |
} | |
} | |
catch (FileNotFoundException e ) { | |
System.out.println("File not found" + e); | |
} | |
catch (IOException e) { | |
System.out.println("Input exception" + e); | |
} | |
catch (ParseException e) { | |
System.out.println("Parse exception" + e); | |
} | |
} | |
public void onClickSelect() { | |
barChartItemOld.setValue(bookData.stream().filter(book->book.getYear()<1700).count()); | |
barChartItem18.setValue(bookData.stream().filter(book->book.getYear()>=1700 && book.getYear() <1800).count()); | |
barChartItem19.setValue(bookData.stream().filter(book->book.getYear()>=1800 && book.getYear() <1900).count()); | |
barChartItem20.setValue(bookData.stream().filter(book->book.getYear()>=1900 && book.getYear() <=2000).count()); | |
Set<String> authorNames = bookData.stream().map(Book::getTitle).collect(Collectors.toSet()); | |
Map<String, Integer> authorsCount = new HashMap<>(); | |
bookData.stream().forEach(book -> | |
authorsCount.put(book.getTitle(), authorsCount.containsKey(book.getTitle()) ? authorsCount.get(book.getTitle()) + 1 : 1)); | |
for (String name: authorsCount.keySet()){ | |
String key =name; | |
Integer value = authorsCount.get(name); | |
if (value > 2) { | |
namesCharts.add(new BarChartItem(key, value, Tile.ORANGE)); | |
} | |
} | |
barChartTile1 = TileBuilder.create() | |
.prefSize(150, 150) | |
.skinType(Tile.SkinType.BAR_CHART) | |
.title("Authors count") | |
.text("") | |
.textColor(Color.BLACK) | |
.titleColor(Color.BLACK) | |
.valueColor(Color.BLUE) | |
.backgroundColor(Color.rgb(244, 244, 244)) | |
.barChartItems(namesCharts) | |
.build(); | |
barChartPane1.getChildren().add(barChartTile1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment