Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. package libraryapp;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.prefs.Preferences;
  6.  
  7. import javafx.application.Application;
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. import javafx.fxml.FXMLLoader;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Alert;
  13. import javafx.scene.control.Alert.AlertType;
  14. import javafx.scene.layout.AnchorPane;
  15. import javafx.scene.layout.BorderPane;
  16. import javafx.stage.Stage;
  17. import javax.xml.bind.JAXBContext;
  18. import javax.xml.bind.Marshaller;
  19. import javax.xml.bind.Unmarshaller;
  20. import libraryapp.model.Book;
  21. import libraryapp.model.BookListWrapper;
  22. import libraryapp.view.HomeOverviewController;
  23. import libraryapp.view.RootLayoutController;
  24.  
  25. public class LibraryApp extends Application {
  26.  
  27. private Stage primaryStage;
  28. private BorderPane rootLayout;
  29.  
  30. /**
  31. * The data as an observable list of Books.
  32. */
  33. private ObservableList<Book> bookData = FXCollections.observableArrayList();
  34.  
  35. /**
  36. * Constructor
  37. */
  38. public LibraryApp() {
  39. // Add some sample data
  40. bookData.add(new Book("Hans", "Muster"));
  41. bookData.add(new Book("Ruth", "Mueller"));
  42. bookData.add(new Book("Heinz", "Kurz"));
  43. bookData.add(new Book("Cornelia", "Meier"));
  44. bookData.add(new Book("Werner", "Meyer"));
  45. bookData.add(new Book("Lydia", "Kunz"));
  46. bookData.add(new Book("Anna", "Best"));
  47. bookData.add(new Book("Stefan", "Meier"));
  48. bookData.add(new Book("Martin", "Mueller"));
  49. }
  50.  
  51. /**
  52. * Returns the data as an observable list of Books.
  53. * @return
  54. */
  55. public ObservableList<Book> getBookData() {
  56. return bookData;
  57. }
  58.  
  59.  
  60.  
  61. @Override
  62. public void start(Stage primaryStage) {
  63. this.primaryStage = primaryStage;
  64. this.primaryStage.setTitle("LibraryApp");
  65.  
  66. initRootLayout();
  67. showHomeOverview();
  68.  
  69. }
  70.  
  71.  
  72. /**
  73. * Initializes the root layout and tries to load the last opened
  74. * person file.
  75. */
  76. public void initRootLayout() {
  77. try {
  78. // Load root layout from fxml file.
  79. FXMLLoader loader = new FXMLLoader();
  80. loader.setLocation(LibraryApp.class.getResource("view/RootLayout.fxml"));
  81. rootLayout = (BorderPane) loader.load();
  82.  
  83. // Show the scene containing the root layout.
  84. Scene scene = new Scene(rootLayout);
  85. primaryStage.setScene(scene);
  86.  
  87. // Give the controller access to the main app.
  88. RootLayoutController controller = loader.getController();
  89. controller.setLibraryApp(this);
  90.  
  91. primaryStage.show();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95.  
  96. // Try to load last opened person file.
  97. File file = getBookFilePath();
  98. if (file != null) {
  99. loadBookDataFromFile(file);
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. /**
  108. * Shows the book overview inside the root layout.
  109. */
  110. public void showHomeOverview() {
  111. try {
  112. // Load home overview.
  113. FXMLLoader loader = new FXMLLoader();
  114. loader.setLocation(LibraryApp.class.getResource("view/HomeOverview.fxml"));
  115. AnchorPane homeOverview = (AnchorPane) loader.load();
  116.  
  117.  
  118.  
  119. // Set home overview into the center of root layout.
  120. rootLayout.setCenter(homeOverview);
  121.  
  122. // Give the controller access to the main app.
  123. HomeOverviewController controller = loader.getController();
  124.  
  125.  
  126.  
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131.  
  132. /**
  133. * Returns the main stage.
  134. * @return
  135. */
  136. public Stage getPrimaryStage() {
  137. return primaryStage;
  138. }
  139.  
  140. public static void main(String[] args) {
  141. launch(args);
  142. }
  143.  
  144. /**
  145. * Returns the book file preference, i.e. the file that was last opened.
  146. * The preference is read from the OS specific registry. If no such
  147. * preference can be found, null is returned.
  148. *
  149. * @return
  150. */
  151. public File getBookFilePath() {
  152. Preferences prefs = Preferences.userNodeForPackage(LibraryApp.class);
  153. String filePath = prefs.get("filePath", null);
  154. if (filePath != null) {
  155. return new File(filePath);
  156. } else {
  157. return null;
  158. }
  159. }
  160.  
  161. /**
  162. * Sets the file path of the currently loaded file. The path is persisted in
  163. * the OS specific registry.
  164. *
  165. * @param file the file or null to remove the path
  166. */
  167. public void setBookFilePath(File file) {
  168. Preferences prefs = Preferences.userNodeForPackage(LibraryApp.class);
  169. if (file != null) {
  170. prefs.put("filePath", file.getPath());
  171.  
  172. // Update the stage title.
  173. primaryStage.setTitle("LibraryApp - " + file.getName());
  174. } else {
  175. prefs.remove("filePath");
  176.  
  177. // Update the stage title.
  178. primaryStage.setTitle("LibraryApp");
  179. }
  180. }
  181.  
  182. /**
  183. * Loads book data from the specified file. The current book data will
  184. * be replaced.
  185. *
  186. * @param file
  187. */
  188. public void loadBookDataFromFile(File file) {
  189. try {
  190. JAXBContext context = JAXBContext
  191. .newInstance(BookListWrapper.class);
  192. Unmarshaller um = context.createUnmarshaller();
  193.  
  194. // Reading XML from the file and unmarshalling.
  195. BookListWrapper wrapper = (BookListWrapper) um.unmarshal(file);
  196.  
  197. bookData.clear();
  198. bookData.addAll(wrapper.getBooks());
  199.  
  200. // Save the file path to the registry.
  201. setBookFilePath(file);
  202.  
  203. } catch (Exception e) { // catches ANY exception
  204. Alert alert = new Alert(AlertType.ERROR);
  205. alert.setTitle("Error");
  206. alert.setHeaderText("Could not load data");
  207. alert.setContentText("Could not load data from file:n" + file.getPath());
  208.  
  209. alert.showAndWait();
  210. }
  211. }
  212.  
  213. /**
  214. * Saves the current book data to the specified file.
  215. *
  216. * @param file
  217. */
  218. public void saveBookDataToFile(File file) {
  219. try {
  220. JAXBContext context = JAXBContext
  221. .newInstance(BookListWrapper.class);
  222. Marshaller m = context.createMarshaller();
  223. m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  224.  
  225. // Wrapping our book data.
  226. BookListWrapper wrapper = new BookListWrapper();
  227. wrapper.setBooks(bookData);
  228.  
  229. // Marshalling and saving XML to the file.
  230. m.marshal(wrapper, file);
  231.  
  232. // Save the file path to the registry.
  233. setBookFilePath(file);
  234. } catch (Exception e) { // catches ANY exception
  235. Alert alert = new Alert(AlertType.ERROR);
  236. alert.setTitle("Error");
  237. alert.setHeaderText("Could not save data");
  238. alert.setContentText("Could not save data to file:n" + file.getPath());
  239.  
  240. alert.showAndWait();
  241. }
  242. }
  243.  
  244.  
  245.  
  246. }
  247.  
  248. package libraryapp.view;
  249.  
  250.  
  251.  
  252. import java.io.IOException;
  253. import java.net.URL;
  254. import java.util.ResourceBundle;
  255. import javafx.event.ActionEvent;
  256. import javafx.fxml.FXML;
  257. import javafx.fxml.FXMLLoader;
  258. import javafx.fxml.Initializable;
  259. import javafx.scene.layout.AnchorPane;
  260. import libraryapp.view.BrowseController;
  261.  
  262.  
  263. public class HomeOverviewController implements Initializable {
  264.  
  265.  
  266. @FXML
  267. private AnchorPane homePane;
  268.  
  269.  
  270.  
  271. @FXML
  272. private void goToBrowse(ActionEvent event) throws IOException {
  273.  
  274. AnchorPane pane = FXMLLoader.load(getClass().getResource("Browse.fxml"));
  275. homePane.getChildren().setAll(pane);
  276.  
  277. }
  278.  
  279. @FXML
  280. private void goToManageAccount(ActionEvent event) throws IOException {
  281.  
  282. AnchorPane pane = FXMLLoader.load(getClass().getResource("ManageAccount.fxml"));
  283. homePane.getChildren().setAll(pane);
  284. }
  285.  
  286. @FXML
  287. public void logout(ActionEvent event) throws IOException {
  288. AnchorPane pane = FXMLLoader.load(getClass().getResource("Login.fxml"));
  289. homePane.getChildren().setAll(pane);
  290. }
  291.  
  292.  
  293. @Override
  294. public void initialize(URL url, ResourceBundle rb) {
  295. // TODO
  296. }
  297.  
  298. }
  299.  
  300. package libraryapp.view;
  301.  
  302. import java.io.File;
  303. import java.io.IOException;
  304. import javafx.event.ActionEvent;
  305. import javafx.fxml.FXML;
  306. import javafx.fxml.FXMLLoader;
  307. import javafx.scene.control.Alert;
  308. import javafx.scene.control.Alert.AlertType;
  309. import javafx.scene.control.Label;
  310. import javafx.scene.control.TableColumn;
  311. import javafx.scene.control.TableView;
  312. import javafx.scene.layout.AnchorPane;
  313. import libraryapp.LibraryApp;
  314. import libraryapp.model.Book;
  315.  
  316. public class BrowseController {
  317. @FXML
  318. private TableView<Book> bookTable;
  319. @FXML
  320. private TableColumn<Book, String> titleColumn;
  321.  
  322. @FXML
  323. private Label titleLabel;
  324. @FXML
  325. private Label authorLabel;
  326. @FXML
  327. private Label isbnLabel;
  328. @FXML
  329. private Label quantityLabel;
  330.  
  331. @FXML
  332. private AnchorPane browsePane;
  333.  
  334.  
  335. // Reference to the main application.
  336. private LibraryApp libraryApp;
  337.  
  338. /**
  339. * The constructor.
  340. * The constructor is called before the initialize() method.
  341. */
  342. public BrowseController() {
  343. }
  344.  
  345. /**
  346. * Initializes the controller class. This method is automatically called
  347. * after the fxml file has been loaded.
  348. */
  349. @FXML
  350. private void initialize() {
  351. // Initialize the book table
  352. titleColumn.setCellValueFactory(
  353. cellData -> cellData.getValue().titleProperty());
  354.  
  355. // Clear person details.
  356. showBookDetails(null);
  357.  
  358. // Listen for selection changes and show the person details when changed.
  359. bookTable.getSelectionModel().selectedItemProperty().addListener(
  360. (observable, oldValue, newValue) -> showBookDetails(newValue));
  361. }
  362.  
  363.  
  364.  
  365. /**
  366. * Is called by the main application to give a reference back to itself.
  367. *
  368. * @param libraryApp
  369. */
  370. public void setLibraryApp(LibraryApp libraryApp) {
  371. this.libraryApp = libraryApp;
  372.  
  373. // Add observable list data to the table
  374. bookTable.setItems(libraryApp.getBookData());
  375. }
  376.  
  377.  
  378.  
  379.  
  380. private void showBookDetails(Book book) {
  381. if (book != null) {
  382. // Fill the labels with info from the book object.
  383. titleLabel.setText(book.getTitle());
  384. authorLabel.setText(book.getAuthor());
  385. isbnLabel.setText(book.getIsbn());
  386. quantityLabel.setText(Integer.toString(book.getQuantity()));
  387.  
  388.  
  389. } else {
  390. // Book is null, remove all the text.
  391. titleLabel.setText("");
  392. authorLabel.setText("");
  393. isbnLabel.setText("");
  394. quantityLabel.setText("");
  395.  
  396. }
  397.  
  398.  
  399. }
  400.  
  401.  
  402. /**
  403. * Called when the user clicks on the borrow button.
  404. */
  405. @FXML
  406. private void handleDeleteBook() {
  407. int selectedIndex = bookTable.getSelectionModel().getSelectedIndex();
  408. if (selectedIndex >= 0) {
  409. bookTable.getItems().remove(selectedIndex);
  410. } else {
  411. // Nothing selected.
  412. Alert alert = new Alert(AlertType.WARNING);
  413. alert.initOwner(libraryApp.getPrimaryStage());
  414. alert.setTitle("No Selection");
  415. alert.setHeaderText("No book Selected");
  416. alert.setContentText("Please select a book.");
  417.  
  418. alert.showAndWait();
  419. }
  420. }
  421.  
  422.  
  423. @FXML
  424. public void logout(ActionEvent event) throws IOException {
  425.  
  426. File bookFile = libraryApp.getBookFilePath();
  427. libraryApp.saveBookDataToFile(bookFile);
  428.  
  429.  
  430. AnchorPane pane = FXMLLoader.load(getClass().getResource("Login.fxml"));
  431. browsePane.getChildren().setAll(pane);
  432. }
  433.  
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement