Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.makery.address.model;
- import javafx.beans.property.StringProperty;
- public class Book {
- private static final StringProperty isbn=null;
- private static final StringProperty name=null;
- private static final StringProperty authors=null;
- private static final StringProperty publ=null;
- private static final StringProperty genre=null;
- private static final StringProperty date=null;
- static public String getIsbn() {
- return isbn.get();
- }
- public static void setIsbn(String isbn) {
- Book.isbn.set(isbn);
- }
- public StringProperty isbnProperty() {
- return isbn;
- }
- public static String getName() {
- return name.get();
- }
- public static void setName(String name) {
- Book.name.set(name);
- }
- public StringProperty nameProperty() {
- return name;
- }
- public static String getAuthors() {
- return authors.get();
- }
- public static void setAuthors(String authors) {
- Book.authors.set(authors);
- }
- public StringProperty authorsProperty() {
- return authors;
- }
- public static String getPubl() {
- return publ.get();
- }
- public static void setPubl(String publ) {
- Book.publ.set(publ);
- }
- public StringProperty publProperty() {
- return publ;
- }
- public static String getGenre() {
- return genre.get();
- }
- public static void setGenre(String genre) {
- Book.genre.set(genre);
- }
- public StringProperty genreProperty() {
- return genre;
- }
- public static String getDate() {
- return date.get();
- }
- public static void setDate(String date) {
- Book.date.set(date);
- }
- public StringProperty dateProperty() {
- return date;
- }
- }
- package ch.makery.address;
- import java.io.IOException;
- import ch.makery.address.model.Book;
- import ch.makery.address.view.BookEditDialogController;
- import ch.makery.address.view.BookOverviewController;
- import javafx.application.Application;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Scene;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.BorderPane;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- public class MainApp extends Application {
- private Stage primaryStage;
- private BorderPane rootLayout;
- @Override
- public void start(Stage primaryStage) {
- this.primaryStage = primaryStage;
- this.primaryStage.setTitle("OOP LAB 8");
- initRootLayout();
- showBookOverview();
- }
- public void initRootLayout() {
- try {
- FXMLLoader loader = new FXMLLoader();
- loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
- rootLayout = (BorderPane) loader.load();
- Scene scene = new Scene(rootLayout);
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void showBookOverview() {
- try {
- FXMLLoader loader = new FXMLLoader();
- loader.setLocation(MainApp.class.getResource("view/BookOverview.fxml"));
- AnchorPane personOverview = (AnchorPane) loader.load();
- rootLayout.setCenter(personOverview);
- BookOverviewController controller = loader.getController();
- controller.setMainApp(this);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Stage getPrimaryStage() {
- return primaryStage;
- }
- private ObservableList<Book> library = FXCollections.observableArrayList();
- public ObservableList<Book> getBook() {
- return library;
- }
- public boolean showBookEditDialog(Book book) {
- try {
- // load fxml-file and create a new scene
- // for popup dialog.
- FXMLLoader loader = new FXMLLoader();
- loader.setLocation(MainApp.class.getResource("view/BookEditDialog.fxml"));
- AnchorPane page = (AnchorPane) loader.load();
- // Create dialog window Stage.
- Stage dialogStage = new Stage();
- dialogStage.setTitle("Edit Book");
- dialogStage.initModality(Modality.WINDOW_MODAL);
- dialogStage.initOwner(primaryStage);
- Scene scene = new Scene(page);
- dialogStage.setScene(scene);
- // Send book to the controller.
- BookEditDialogController controller = loader.getController();
- controller.setDialogStage(dialogStage);
- controller.setPerson(book);
- // Display dialog window and wait, till it will be closed
- dialogStage.showAndWait();
- return controller.isOkClicked();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- package ch.makery.address.view;
- import javafx.fxml.FXML;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.TextField;
- import javafx.stage.Stage;
- import ch.makery.address.model.Book;
- //Window for changing information about book
- public class BookEditDialogController {
- @FXML
- private TextField isbnField;
- @FXML
- private TextField nameField;
- @FXML
- private TextField authorsField;
- @FXML
- private TextField publField;
- @FXML
- private TextField genreField;
- @FXML
- private TextField dateField;
- private Stage dialogStage;
- private Book book;
- private boolean okClicked = false;
- /**
- * Initialize class-container. This method is called automatically
- * after the file is loaded.
- */
- @FXML
- private void initialize() {
- }
- /**
- * Set the scene for this window.
- *
- * @param dialogStage
- */
- public void setDialogStage(Stage dialogStage) {
- this.dialogStage = dialogStage;
- }
- /**
- * Set the book and info .
- *
- * @param book
- */
- public void setPerson(Book book) {
- this.book = book;
- isbnField.setText(Book.getIsbn());
- nameField.setText(Book.getName());
- authorsField.setText(Book.getAuthors());
- publField.setText(Book.getPubl());
- genreField.setText(Book.getGenre());
- dateField.setText(Book.getDate());
- }
- /**
- * Returns true, if user clicked OK, else false.
- *
- * @return
- */
- public boolean isOkClicked() {
- return okClicked;
- }
- /**
- *Called when user clicked OK
- */
- @FXML
- private void handleOk() {
- if (isInputValid()) {
- Book.setIsbn(isbnField.getText());
- Book.setName(nameField.getText());
- Book.setAuthors(authorsField.getText());
- Book.setPubl(publField.getText());
- Book.setGenre(genreField.getText());
- Book.setDate(dateField.getText());
- okClicked = true;
- dialogStage.close();
- }
- }
- /**
- * Called when user clicked Cancel.
- */
- @FXML
- private void handleCancel() {
- dialogStage.close();
- }
- /**
- * check user input in text fields.
- *
- * @return true, if user input is correct
- */
- private boolean isInputValid() {
- String errorMessage = "";
- if (isbnField.getText() == null || isbnField.getText().length() == 0) {
- errorMessage += "No valid isbn!n";
- }
- if (nameField.getText() == null || nameField.getText().length() == 0) {
- errorMessage += "No valid name!n";
- }
- if (authorsField.getText() == null || authorsField.getText().length() == 0) {
- errorMessage += "No valid authors!n";
- }
- if (publField.getText() == null || publField.getText().length() == 0) {
- errorMessage += "No valid publishing!n";
- }
- if (genreField.getText() == null || genreField.getText().length() == 0) {
- errorMessage += "No valid genre!n";
- }
- if (dateField.getText() == null || dateField.getText().length() == 0) {
- errorMessage += "No valid birthday!n";
- }
- if (errorMessage.length() == 0) {
- return true;
- } else {
- // Show message about error.
- Alert alert = new Alert(AlertType.ERROR);
- alert.initOwner(dialogStage);
- alert.setTitle("Invalid Fields");
- alert.setHeaderText("Please correct invalid fields");
- alert.setContentText(errorMessage);
- alert.showAndWait();
- return false;
- }
- }
- }
- package ch.makery.address.view;
- import javafx.fxml.FXML;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.Label;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import ch.makery.address.MainApp;
- import ch.makery.address.model.Book;
- public class BookOverviewController {
- @FXML
- private TableView<Book> bookTable;
- @FXML
- private TableColumn<Book, String> booksName;
- @FXML
- private Label isbnLabel;
- @FXML
- private Label nameLabel;
- @FXML
- private Label authorsLabel;
- @FXML
- private Label publLabel;
- @FXML
- private Label genreLabel;
- @FXML
- private Label dateLabel;
- private MainApp mainApp;
- public BookOverviewController() {
- }
- @FXML
- private void initialize() {
- // Initialization of the table with the names of books.
- booksName.setCellValueFactory(
- cellData -> cellData.getValue().nameProperty());
- // Clear additional information about the book.
- showPersonDetails(null);
- // Listen to the selection changes, and when changing, display
- // additional information about the book.
- bookTable.getSelectionModel().selectedItemProperty().addListener(
- (observable, oldValue, newValue) ->
- showPersonDetails(newValue));
- }
- public void setMainApp(MainApp mainApp) {
- this.mainApp = mainApp;
- bookTable.setItems(mainApp.getBook());
- }
- private void showPersonDetails(Book person) {
- if (person != null) {
- isbnLabel.setText(Book.getIsbn());
- nameLabel.setText(Book.getName());
- authorsLabel.setText(Book.getAuthors());
- publLabel.setText(Book.getPubl());
- genreLabel.setText(Book.getGenre());
- dateLabel.setText(Book.getDate());
- } else {
- isbnLabel.setText("");
- nameLabel.setText("");
- authorsLabel.setText("");
- publLabel.setText("");
- genreLabel.setText("");
- dateLabel.setText("");
- }
- }
- @FXML
- private void handleDeleteBook() {
- int selectedIndex = bookTable.getSelectionModel().getSelectedIndex();
- if(selectedIndex >= 0){
- bookTable.getItems().remove(selectedIndex);
- }
- else {
- // Nothing selected.
- Alert alert = new Alert(AlertType.WARNING);
- alert.initOwner(mainApp.getPrimaryStage());
- alert.setTitle("No Selection");
- alert.setHeaderText("No Book Selected");
- alert.setContentText("Please select a book in the table.");
- alert.showAndWait();
- }
- }
- /*
- *Called when the user clicks the New button
- *Opens a dialog box with additional information about the new book.
- */
- @FXML
- private void handleNewBook() {
- Book tempBook = new Book();
- boolean okClicked = mainApp.showBookEditDialog(tempbook);
- if(okCliked) {
- mainApp.getbook().add(tempbook);
- }
- }
- //Called when the user clicks the Edit button
- //Opens a dialog box for changing the selected book.
- @FXML
- private void handleEditBook() {
- Book selectBook = bookTable.getSelectionModel().getSelectedItem();
- if(selectedBook != null) {
- boolean okClikced = mainApp.showBookEditDialog(selectedBook);
- if(okClikced) {
- showPersonDetails(selectedBook);
- }
- } else {
- //Nothing selected.
- Alert alert = new Alert(AlertType.WARNING);
- alert.initOwner(mainApp.getPrimaryStage());
- alert.setTitle("No Selection");
- alert.setHeaderText("No Book selected");
- alert.setContentText("Please select a book in the table.");
- alert.showAndWait();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement