From c0af7b333569884e10e6c31a5922baec6fcd8226 Mon Sep 17 00:00:00 2001 From: Cristian Date: Mon, 5 Sep 2022 23:47:21 +0200 Subject: [PATCH] GUI Updated - Added a detailed window with tabs and TableView - Added a searching form (must be debugged) --- build.gradle | 6 +- src/main/java/it/books/MicrosoftDB.java | 41 +++ src/main/java/it/books/gcon/DeskG.java | 54 +++- .../java/it/books/gcon/OldDeskDetailG.java | 130 ++++++++ src/main/resources/it/books/gcon/desk.fxml | 6 + .../it/books/gcon/oldDeskDetails.fxml | 277 ++++++++++++++++++ 6 files changed, 509 insertions(+), 5 deletions(-) create mode 100644 src/main/java/it/books/gcon/OldDeskDetailG.java create mode 100644 src/main/resources/it/books/gcon/oldDeskDetails.fxml diff --git a/build.gradle b/build.gradle index efe847e..2aacf1b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'it.books' -version '1.0' +version '1.0.2' repositories { mavenCentral() @@ -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' } \ No newline at end of file diff --git a/src/main/java/it/books/MicrosoftDB.java b/src/main/java/it/books/MicrosoftDB.java index 74989e5..1063812 100644 --- a/src/main/java/it/books/MicrosoftDB.java +++ b/src/main/java/it/books/MicrosoftDB.java @@ -95,4 +95,45 @@ public static ArrayList 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 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 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; + } + + } } diff --git a/src/main/java/it/books/gcon/DeskG.java b/src/main/java/it/books/gcon/DeskG.java index 6b948c3..1ce48a4 100644 --- a/src/main/java/it/books/gcon/DeskG.java +++ b/src/main/java/it/books/gcon/DeskG.java @@ -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; @@ -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; @@ -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")); @@ -86,10 +99,14 @@ private void OpenBtn(){ }else{ //Loading and operating with database files ArrayList 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 @@ -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); @@ -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); @@ -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; diff --git a/src/main/java/it/books/gcon/OldDeskDetailG.java b/src/main/java/it/books/gcon/OldDeskDetailG.java new file mode 100644 index 0000000..a9cba23 --- /dev/null +++ b/src/main/java/it/books/gcon/OldDeskDetailG.java @@ -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 copy; + + private static List 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 setTable = new Task<>() { + @Override @SuppressWarnings("unchecked") + protected Void call() throws Exception { + Thread.sleep(512); + copy = (TableView) 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 table = (TableView) MainPane.getCenter(); + + if(clonedBook == null){ + clonedBook = new ArrayList<>(table.getItems().stream().toList()); + } + table.getItems().remove(0, table.getItems().size()); //Remove all + //Search params + ArrayList 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 table = (TableView) MainPane.getCenter(); + table.getItems().remove(0, table.getItems().size()); + table.getItems().addAll(clonedBook); + clonedBook = null; + System.gc(); + } + } + +} diff --git a/src/main/resources/it/books/gcon/desk.fxml b/src/main/resources/it/books/gcon/desk.fxml index a0838ba..513a749 100644 --- a/src/main/resources/it/books/gcon/desk.fxml +++ b/src/main/resources/it/books/gcon/desk.fxml @@ -43,6 +43,12 @@ + + + + + + diff --git a/src/main/resources/it/books/gcon/oldDeskDetails.fxml b/src/main/resources/it/books/gcon/oldDeskDetails.fxml new file mode 100644 index 0000000..eef5fe0 --- /dev/null +++ b/src/main/resources/it/books/gcon/oldDeskDetails.fxml @@ -0,0 +1,277 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +