Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. package ch.makery.address.model;
  2. import javafx.beans.property.StringProperty;
  3.  
  4. public class Book {
  5.  
  6. private static final StringProperty isbn=null;
  7. private static final StringProperty name=null;
  8. private static final StringProperty authors=null;
  9. private static final StringProperty publ=null;
  10. private static final StringProperty genre=null;
  11. private static final StringProperty date=null;
  12.  
  13.  
  14. static public String getIsbn() {
  15. return isbn.get();
  16. }
  17.  
  18. public static void setIsbn(String isbn) {
  19. Book.isbn.set(isbn);
  20. }
  21.  
  22. public StringProperty isbnProperty() {
  23. return isbn;
  24. }
  25.  
  26. public static String getName() {
  27. return name.get();
  28. }
  29.  
  30. public static void setName(String name) {
  31. Book.name.set(name);
  32. }
  33.  
  34. public StringProperty nameProperty() {
  35. return name;
  36. }
  37.  
  38. public static String getAuthors() {
  39. return authors.get();
  40. }
  41.  
  42. public static void setAuthors(String authors) {
  43. Book.authors.set(authors);
  44. }
  45.  
  46. public StringProperty authorsProperty() {
  47. return authors;
  48. }
  49.  
  50. public static String getPubl() {
  51. return publ.get();
  52. }
  53.  
  54. public static void setPubl(String publ) {
  55. Book.publ.set(publ);
  56. }
  57.  
  58. public StringProperty publProperty() {
  59. return publ;
  60. }
  61.  
  62. public static String getGenre() {
  63. return genre.get();
  64. }
  65.  
  66. public static void setGenre(String genre) {
  67. Book.genre.set(genre);
  68. }
  69.  
  70. public StringProperty genreProperty() {
  71. return genre;
  72. }
  73.  
  74. public static String getDate() {
  75. return date.get();
  76. }
  77.  
  78. public static void setDate(String date) {
  79. Book.date.set(date);
  80. }
  81.  
  82. public StringProperty dateProperty() {
  83. return date;
  84. }
  85. }
  86.  
  87. package ch.makery.address;
  88.  
  89. import java.io.IOException;
  90.  
  91. import ch.makery.address.model.Book;
  92. import ch.makery.address.view.BookEditDialogController;
  93. import ch.makery.address.view.BookOverviewController;
  94. import javafx.application.Application;
  95. import javafx.collections.FXCollections;
  96. import javafx.collections.ObservableList;
  97. import javafx.fxml.FXMLLoader;
  98. import javafx.scene.Scene;
  99. import javafx.scene.layout.AnchorPane;
  100. import javafx.scene.layout.BorderPane;
  101. import javafx.stage.Modality;
  102. import javafx.stage.Stage;
  103.  
  104. public class MainApp extends Application {
  105.  
  106. private Stage primaryStage;
  107. private BorderPane rootLayout;
  108.  
  109. @Override
  110. public void start(Stage primaryStage) {
  111. this.primaryStage = primaryStage;
  112. this.primaryStage.setTitle("OOP LAB 8");
  113.  
  114. initRootLayout();
  115.  
  116. showBookOverview();
  117. }
  118.  
  119. public void initRootLayout() {
  120. try {
  121. FXMLLoader loader = new FXMLLoader();
  122. loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
  123. rootLayout = (BorderPane) loader.load();
  124.  
  125. Scene scene = new Scene(rootLayout);
  126. primaryStage.setScene(scene);
  127. primaryStage.show();
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132.  
  133.  
  134. public void showBookOverview() {
  135. try {
  136. FXMLLoader loader = new FXMLLoader();
  137. loader.setLocation(MainApp.class.getResource("view/BookOverview.fxml"));
  138. AnchorPane personOverview = (AnchorPane) loader.load();
  139.  
  140. rootLayout.setCenter(personOverview);
  141.  
  142. BookOverviewController controller = loader.getController();
  143. controller.setMainApp(this);
  144.  
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149.  
  150.  
  151.  
  152. public Stage getPrimaryStage() {
  153. return primaryStage;
  154. }
  155.  
  156.  
  157. private ObservableList<Book> library = FXCollections.observableArrayList();
  158.  
  159. public ObservableList<Book> getBook() {
  160. return library;
  161. }
  162.  
  163. public boolean showBookEditDialog(Book book) {
  164. try {
  165. // load fxml-file and create a new scene
  166. // for popup dialog.
  167. FXMLLoader loader = new FXMLLoader();
  168. loader.setLocation(MainApp.class.getResource("view/BookEditDialog.fxml"));
  169. AnchorPane page = (AnchorPane) loader.load();
  170.  
  171. // Create dialog window Stage.
  172. Stage dialogStage = new Stage();
  173. dialogStage.setTitle("Edit Book");
  174. dialogStage.initModality(Modality.WINDOW_MODAL);
  175. dialogStage.initOwner(primaryStage);
  176. Scene scene = new Scene(page);
  177. dialogStage.setScene(scene);
  178.  
  179. // Send book to the controller.
  180. BookEditDialogController controller = loader.getController();
  181. controller.setDialogStage(dialogStage);
  182. controller.setPerson(book);
  183.  
  184. // Display dialog window and wait, till it will be closed
  185. dialogStage.showAndWait();
  186.  
  187. return controller.isOkClicked();
  188. } catch (IOException e) {
  189. e.printStackTrace();
  190. return false;
  191. }
  192. }
  193.  
  194.  
  195. public static void main(String[] args) {
  196. launch(args);
  197. }
  198. }
  199.  
  200. package ch.makery.address.view;
  201.  
  202. import javafx.fxml.FXML;
  203. import javafx.scene.control.Alert;
  204. import javafx.scene.control.Alert.AlertType;
  205. import javafx.scene.control.TextField;
  206. import javafx.stage.Stage;
  207. import ch.makery.address.model.Book;
  208.  
  209.  
  210. //Window for changing information about book
  211.  
  212. public class BookEditDialogController {
  213.  
  214. @FXML
  215. private TextField isbnField;
  216. @FXML
  217. private TextField nameField;
  218. @FXML
  219. private TextField authorsField;
  220. @FXML
  221. private TextField publField;
  222. @FXML
  223. private TextField genreField;
  224. @FXML
  225. private TextField dateField;
  226.  
  227.  
  228. private Stage dialogStage;
  229. private Book book;
  230. private boolean okClicked = false;
  231.  
  232. /**
  233. * Initialize class-container. This method is called automatically
  234. * after the file is loaded.
  235. */
  236. @FXML
  237. private void initialize() {
  238. }
  239.  
  240. /**
  241. * Set the scene for this window.
  242. *
  243. * @param dialogStage
  244. */
  245. public void setDialogStage(Stage dialogStage) {
  246. this.dialogStage = dialogStage;
  247. }
  248.  
  249. /**
  250. * Set the book and info .
  251. *
  252. * @param book
  253. */
  254. public void setPerson(Book book) {
  255. this.book = book;
  256.  
  257. isbnField.setText(Book.getIsbn());
  258. nameField.setText(Book.getName());
  259. authorsField.setText(Book.getAuthors());
  260. publField.setText(Book.getPubl());
  261. genreField.setText(Book.getGenre());
  262. dateField.setText(Book.getDate());
  263. }
  264.  
  265. /**
  266. * Returns true, if user clicked OK, else false.
  267. *
  268. * @return
  269. */
  270. public boolean isOkClicked() {
  271. return okClicked;
  272. }
  273.  
  274. /**
  275. *Called when user clicked OK
  276. */
  277. @FXML
  278. private void handleOk() {
  279. if (isInputValid()) {
  280. Book.setIsbn(isbnField.getText());
  281. Book.setName(nameField.getText());
  282. Book.setAuthors(authorsField.getText());
  283. Book.setPubl(publField.getText());
  284. Book.setGenre(genreField.getText());
  285. Book.setDate(dateField.getText());
  286.  
  287. okClicked = true;
  288. dialogStage.close();
  289. }
  290. }
  291.  
  292. /**
  293. * Called when user clicked Cancel.
  294. */
  295. @FXML
  296. private void handleCancel() {
  297. dialogStage.close();
  298. }
  299.  
  300. /**
  301. * check user input in text fields.
  302. *
  303. * @return true, if user input is correct
  304. */
  305. private boolean isInputValid() {
  306. String errorMessage = "";
  307.  
  308. if (isbnField.getText() == null || isbnField.getText().length() == 0) {
  309. errorMessage += "No valid isbn!n";
  310. }
  311. if (nameField.getText() == null || nameField.getText().length() == 0) {
  312. errorMessage += "No valid name!n";
  313. }
  314. if (authorsField.getText() == null || authorsField.getText().length() == 0) {
  315. errorMessage += "No valid authors!n";
  316. }
  317.  
  318. if (publField.getText() == null || publField.getText().length() == 0) {
  319. errorMessage += "No valid publishing!n";
  320. }
  321.  
  322. if (genreField.getText() == null || genreField.getText().length() == 0) {
  323. errorMessage += "No valid genre!n";
  324. }
  325.  
  326. if (dateField.getText() == null || dateField.getText().length() == 0) {
  327. errorMessage += "No valid birthday!n";
  328. }
  329.  
  330. if (errorMessage.length() == 0) {
  331. return true;
  332. } else {
  333. // Show message about error.
  334. Alert alert = new Alert(AlertType.ERROR);
  335. alert.initOwner(dialogStage);
  336. alert.setTitle("Invalid Fields");
  337. alert.setHeaderText("Please correct invalid fields");
  338. alert.setContentText(errorMessage);
  339.  
  340. alert.showAndWait();
  341.  
  342. return false;
  343. }
  344. }
  345. }
  346.  
  347. package ch.makery.address.view;
  348.  
  349. import javafx.fxml.FXML;
  350. import javafx.scene.control.Alert;
  351. import javafx.scene.control.Alert.AlertType;
  352. import javafx.scene.control.Label;
  353. import javafx.scene.control.TableColumn;
  354. import javafx.scene.control.TableView;
  355. import ch.makery.address.MainApp;
  356. import ch.makery.address.model.Book;
  357.  
  358. public class BookOverviewController {
  359.  
  360. @FXML
  361. private TableView<Book> bookTable;
  362. @FXML
  363. private TableColumn<Book, String> booksName;
  364.  
  365. @FXML
  366. private Label isbnLabel;
  367. @FXML
  368. private Label nameLabel;
  369. @FXML
  370. private Label authorsLabel;
  371. @FXML
  372. private Label publLabel;
  373. @FXML
  374. private Label genreLabel;
  375. @FXML
  376. private Label dateLabel;
  377.  
  378. private MainApp mainApp;
  379.  
  380.  
  381. public BookOverviewController() {
  382. }
  383.  
  384.  
  385. @FXML
  386. private void initialize() {
  387. // Initialization of the table with the names of books.
  388. booksName.setCellValueFactory(
  389. cellData -> cellData.getValue().nameProperty());
  390.  
  391.  
  392. // Clear additional information about the book.
  393. showPersonDetails(null);
  394.  
  395. // Listen to the selection changes, and when changing, display
  396. // additional information about the book.
  397. bookTable.getSelectionModel().selectedItemProperty().addListener(
  398. (observable, oldValue, newValue) ->
  399. showPersonDetails(newValue));
  400. }
  401.  
  402.  
  403. public void setMainApp(MainApp mainApp) {
  404. this.mainApp = mainApp;
  405. bookTable.setItems(mainApp.getBook());
  406. }
  407.  
  408. private void showPersonDetails(Book person) {
  409. if (person != null) {
  410. isbnLabel.setText(Book.getIsbn());
  411. nameLabel.setText(Book.getName());
  412. authorsLabel.setText(Book.getAuthors());
  413. publLabel.setText(Book.getPubl());
  414. genreLabel.setText(Book.getGenre());
  415. dateLabel.setText(Book.getDate());
  416.  
  417.  
  418. } else {
  419. isbnLabel.setText("");
  420. nameLabel.setText("");
  421. authorsLabel.setText("");
  422. publLabel.setText("");
  423. genreLabel.setText("");
  424. dateLabel.setText("");
  425. }
  426. }
  427.  
  428. @FXML
  429. private void handleDeleteBook() {
  430. int selectedIndex = bookTable.getSelectionModel().getSelectedIndex();
  431. if(selectedIndex >= 0){
  432. bookTable.getItems().remove(selectedIndex);
  433. }
  434. else {
  435. // Nothing selected.
  436. Alert alert = new Alert(AlertType.WARNING);
  437. alert.initOwner(mainApp.getPrimaryStage());
  438. alert.setTitle("No Selection");
  439. alert.setHeaderText("No Book Selected");
  440. alert.setContentText("Please select a book in the table.");
  441. alert.showAndWait();
  442. }
  443. }
  444. /*
  445. *Called when the user clicks the New button
  446. *Opens a dialog box with additional information about the new book.
  447. */
  448. @FXML
  449. private void handleNewBook() {
  450. Book tempBook = new Book();
  451. boolean okClicked = mainApp.showBookEditDialog(tempbook);
  452. if(okCliked) {
  453. mainApp.getbook().add(tempbook);
  454. }
  455. }
  456.  
  457. //Called when the user clicks the Edit button
  458. //Opens a dialog box for changing the selected book.
  459. @FXML
  460. private void handleEditBook() {
  461. Book selectBook = bookTable.getSelectionModel().getSelectedItem();
  462. if(selectedBook != null) {
  463. boolean okClikced = mainApp.showBookEditDialog(selectedBook);
  464. if(okClikced) {
  465. showPersonDetails(selectedBook);
  466. }
  467. } else {
  468. //Nothing selected.
  469. Alert alert = new Alert(AlertType.WARNING);
  470. alert.initOwner(mainApp.getPrimaryStage());
  471. alert.setTitle("No Selection");
  472. alert.setHeaderText("No Book selected");
  473. alert.setContentText("Please select a book in the table.");
  474. alert.showAndWait();
  475. }
  476. }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement