Skip to content

Commit

Permalink
GUI Updated
Browse files Browse the repository at this point in the history
- Added a detailed window with tabs and TableView<Book>
- Added a searching form (must be debugged)
  • Loading branch information
Cristian committed Sep 5, 2022
1 parent a6fe6dc commit c0af7b3
Show file tree
Hide file tree
Showing 6 changed files with 509 additions and 5 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'it.books'
version '1.0'
version '1.0.2'

repositories {
mavenCentral()
Expand All @@ -21,7 +21,9 @@ jar{
}

dependencies {
// https://mvnrepository.com/artifact/net.sf.ucanaccess/ucanaccess
// Microsoft Database Handler (JDBC Driver)
implementation 'net.sf.ucanaccess:ucanaccess:5.0.1'

// .ini file writer/parser
implementation 'org.ini4j:ini4j:0.5.4'
}
41 changes: 41 additions & 0 deletions src/main/java/it/books/MicrosoftDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,45 @@ public static ArrayList<Book> connectAndGet(File dbFile){
}

}

/** Connect and search on the entire list of books from a given database
* @param dbFile specify the database file to work on
* @param author specify the book's author
* @param code specify the code of book
* @param genre specify the genre of book (could be Decimal/Dewey)
* @param title specify the book's title
* @return the list of books which matches with specified fields.
* **/
public static ArrayList<Book> connectAndSearch(File dbFile, String title, String code, String author, String genre){
String db = dbFile.getAbsolutePath().replace("\\", "/");
try(Connection conn = DriverManager.getConnection("jdbc:ucanaccess://"+db+";memory=true")){
Statement statement = conn.createStatement();
String query;
query = "SELECT * FROM LIBRI WHERE " + ((title.isEmpty())? "" : "titolo LIKE '%" + title + "%' ");
query += (title.isEmpty()&&(!code.isEmpty()))? "codice LIKE '%"+code+"%' " : (!code.isEmpty())? "AND WHERE codice LIKE '%" + code + "%' " : "";
query += (title.isEmpty()&&code.isEmpty()&&(!author.isEmpty()))? "autore LIKE '%" + author + "%' " : (!author.isEmpty())? "AND WHERE autore = '%" + author + "%' " : "";
query += (title.isEmpty()&&code.isEmpty()&&author.isEmpty()&&(!genre.isEmpty()))? "genere LIKE '%" + genre + "%' " : (!genre.isEmpty())? "AND WHERE genere LIKE '%" + genre + "%'" : "";
ResultSet res = statement.executeQuery(query);
ArrayList<Book> bookList = new ArrayList<>();
//Fetch results
while(res.next()){
Book book = new Book(res.getString("codice"), res.getString("titolo"));
book.setDetails(res.getString("autore"), res.getBoolean("unico_autore"), res.getString("collana"), res.getString("genere"), res.getString("editore"), res.getString("edizione"), res.getString("anno"));
book.setDetails(res.getString("scaffale"), (res.getString("pro_dal_gg") +"/" + (res.getString("pro_dal_mm") + "/" + res.getString("pro_dal_aa"))), res.getString("commento"));
book.setLocale(res.getString("nazione"), res.getString("traduttore"), res.getString("titolo_orig"));
book.setPages(res.getInt("pagine"), res.getString("formato"));
if(res.getBoolean("prestito")){
book.setLeasing(true, (res.getString("pre_dal_gg") + "/" + res.getString("pre_dal_mm") + "/" + res.getString("pre_dal_aa")), res.getString("prestitario"));
}else{
book.setLeasing(false, "", "");
}
bookList.add(book);
}
return bookList;

}catch (SQLException e){
return null;
}

}
}
54 changes: 51 additions & 3 deletions src/main/java/it/books/gcon/DeskG.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/* ========================================
* Author: Cristian Capraro
* September 2022
* This is controller class of desk.fxml
* ======================================== */



package it.books.gcon;

import it.books.base.Book;
import it.books.MicrosoftDB;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
Expand All @@ -13,6 +23,7 @@
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -72,9 +83,11 @@ private void AboutBtn() {
ToggleGroup view;
@FXML
RadioMenuItem RadioMenuTLoadButton, RadioMenuILoadButton;

@FXML
private void OpenBtn(){
RadioMenuItem RadioMenuTViewModeButton, RadioMenuTCViewModeButton;

@FXML @SuppressWarnings("ConstantConditions")
private void OpenBtn() throws IOException {
//Selecting files
FileChooser dChooser = new FileChooser();
dChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Database Microsoft", "*.mdb", "*.accdb"));
Expand All @@ -86,10 +99,14 @@ private void OpenBtn(){
}else{
//Loading and operating with database files
ArrayList<Book> book;
//Share info
OldDeskDetailG.getDBFile(dbFile);

if(view.getToggles().get(0).isSelected()){ //Incremental load
RadioMenuILoadButton.setDisable(true);
RadioMenuTLoadButton.setDisable(true);
RadioMenuTCViewModeButton.setDisable(true);
RadioMenuTViewModeButton.setDisable(true);

book = MicrosoftDB.connectAndGet(dbFile, off, limit);
//Update offset
Expand All @@ -98,6 +115,8 @@ private void OpenBtn(){
else{ //Full load
RadioMenuILoadButton.setDisable(true);
RadioMenuTLoadButton.setDisable(true);
RadioMenuTCViewModeButton.setDisable(true);
RadioMenuTViewModeButton.setDisable(true);

book = MicrosoftDB.connectAndGet(dbFile);

Expand All @@ -122,7 +141,34 @@ private void OpenBtn(){
table.getColumns().addAll(columns);
table.getItems().addAll(book);
table.setFixedCellSize(70);
MainPane.setCenter(table);

//Detailed View
if(RadioMenuTCViewModeButton.isSelected()){
//Load Win
Parent root = FXMLLoader.load(OldDeskDetailG.class.getResource("oldDeskDetails.fxml"));
BorderPane pane = (BorderPane) root;

Stage MainStage = (Stage) MainPane.getScene().getWindow();

MainStage.widthProperty().addListener((obs, ov, nv) -> {
double valueX = MainStage.getWidth();

MainStage.heightProperty().addListener((bso, vo, vn) -> {
double valueY = MainStage.getHeight() - 40;
pane.setPrefSize(MainStage.getWidth(), valueY);
});
pane.setPrefSize(valueX, MainStage.getHeight() - 40);
});


//pane.setPrefSize(MainPane.getWidth(), MainPane.getHeight());
pane.setCenter(table);
pane.setPrefSize(MainStage.getWidth(),MainStage.getHeight() - 40);
MainPane.setCenter(pane);
}else{
MainPane.setCenter(table);
}

CloseViewButton.setDisable(false);
OpenButton.setDisable(true);

Expand Down Expand Up @@ -168,6 +214,8 @@ private void CloseViewBtn(){
RadioMenuILoadButton.setDisable(false);
RadioMenuTLoadButton.setDisable(false);
OpenButton.setDisable(false);
RadioMenuTCViewModeButton.setDisable(false);
RadioMenuTViewModeButton.setDisable(false);
System.gc(); //Try optimization
//Reset offset
off = 0; finished = false;
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/it/books/gcon/OldDeskDetailG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* ========================================
* Author: Cristian Capraro
* September 2022
* This is controller class of
* oldDeskDetails.fxml
* ======================================== */

package it.books.gcon;

import it.books.MicrosoftDB;
import it.books.base.Book;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class OldDeskDetailG {
//Use this to get required share between G classes information
@FXML
BorderPane MainPane;

private static TableView<Book> copy;

private static List<Book> clonedBook = null;

private static File ldFile;

/**
* This method is used to share information between controller classes.
* @param rdFile specify the database file on which actually works.
* **/
static void getDBFile(File rdFile){
ldFile = rdFile;
}
@FXML
private void initialize(){
//Wait table rendering
Task<Void> setTable = new Task<>() {
@Override @SuppressWarnings("unchecked")
protected Void call() throws Exception {
Thread.sleep(512);
copy = (TableView<Book>) MainPane.getCenter();

copy.setOnMouseClicked(i -> {
Book selected;
if((selected = copy.getFocusModel().getFocusedItem()) != null){
CodeTextBox.setText(selected.getCode());
AuthorsTextBox.setText(selected.getAuthor());
TitleTextBox.setText(selected.getTitle());
EditionTextBox.setText(selected.getEdition());
EditorTextBox.setText(selected.getEditor());
CommentTextArea.setText(selected.getComments());
GenreTextBox.setText(selected.getGenre());
YearTextBox.setText(selected.getYear());
PagesTextBox.setText(String.valueOf(selected.getPages()));
SeriesTextBox.setText(selected.getSeries());
OrigTitleTextBox.setText(selected.getOriginalTitle());
CountryTextBox.setText(selected.getNation());
PagesFormatTextBox.setText(selected.getPageFormat());
TranslatorTextBox.setText(selected.getTranslator());
}else{
CodeTextBox.setText("");
AuthorsTextBox.setText("");
TitleTextBox.setText("");
EditionTextBox.setText("");
EditorTextBox.setText("");
CommentTextArea.setText("");
GenreTextBox.setText("");
YearTextBox.setText("");
PagesTextBox.setText("");
SeriesTextBox.setText("");
OrigTitleTextBox.setText("");
CountryTextBox.setText("");
PagesFormatTextBox.setText("");
TranslatorTextBox.setText("");
}
});

return null;
}
};
new Thread(setTable).start();
}

//Tab #1
@FXML
TextField CodeTextBox, AuthorsTextBox, TitleTextBox, EditorTextBox, GenreTextBox, YearTextBox, PagesTextBox, EditionTextBox;
//Tab #2
@FXML
TextArea CommentTextArea;
@FXML
TextField SeriesTextBox, OrigTitleTextBox, CountryTextBox, PagesFormatTextBox, TranslatorTextBox;
//Tab #3
@FXML
TextField TitleSearchTextBox, CodeSearchTextBox, GenreSearchTextBox, AuthorSearchTextBox;

@FXML
@SuppressWarnings("unchecked")
private void SearchBtn(){
TableView<Book> table = (TableView<Book>) MainPane.getCenter();

if(clonedBook == null){
clonedBook = new ArrayList<>(table.getItems().stream().toList());
}
table.getItems().remove(0, table.getItems().size()); //Remove all
//Search params
ArrayList<Book> books = MicrosoftDB.connectAndSearch(ldFile, TitleSearchTextBox.getText(), CodeSearchTextBox.getText(), AuthorSearchTextBox.getText(), GenreSearchTextBox.getText());
if(books == null){
new Alert(Alert.AlertType.WARNING, "Nessun libro corrisponde ai criteri di ricerca. ", ButtonType.OK).showAndWait();
}else{
table.getItems().addAll(books);
}
}

@FXML @SuppressWarnings("unchecked")
private void ResetBtn(){
if(!(clonedBook == null)){
TableView<Book> table = (TableView<Book>) MainPane.getCenter();
table.getItems().remove(0, table.getItems().size());
table.getItems().addAll(clonedBook);
clonedBook = null;
System.gc();
}
}

}
6 changes: 6 additions & 0 deletions src/main/resources/it/books/gcon/desk.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
</RadioMenuItem>
<RadioMenuItem fx:id="RadioMenuTLoadButton" mnemonicParsing="false" text="Caricamento totale" toggleGroup="$view" />
<SeparatorMenuItem mnemonicParsing="false" />
<RadioMenuItem fx:id="RadioMenuTViewModeButton" mnemonicParsing="false" selected="true" text="Vista tabella">
<toggleGroup>
<ToggleGroup fx:id="viewType" />
</toggleGroup>
</RadioMenuItem>
<RadioMenuItem fx:id="RadioMenuTCViewModeButton" mnemonicParsing="false" text="Vista tabella + dettagli" toggleGroup="$viewType" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Altro">
Expand Down
Loading

0 comments on commit c0af7b3

Please sign in to comment.