Commit 8d2119ef authored by Arne Styve's avatar Arne Styve
Browse files

Finishing the FXML-based implementation

parent bb50feff
package no.ntnu.idata2001.contacts;
/**
* A simple class to hold the version of the application.
*/
public class AppVersion {
public static String VERSION = "0.5";
}
package no.ntnu.idata2001.contacts.controllers;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableView;
import javafx.stage.FileChooser;
import no.ntnu.idata2001.contacts.AppVersion;
import no.ntnu.idata2001.contacts.model.AddressBook;
import no.ntnu.idata2001.contacts.model.AddressBookDBHandler;
import no.ntnu.idata2001.contacts.model.AddressBookFileHandler;
import no.ntnu.idata2001.contacts.model.ContactDetails;
import no.ntnu.idata2001.contacts.views.ContactDetailsDialog;
/**
* The controller class mapped to the FXML main class.
......@@ -26,34 +38,118 @@ public class ContactsAppFXMLController {
@FXML
private TableView<ContactDetails> contactDetailsTableView;
// The observable list to be used with the TableView
ObservableList<ContactDetails> observableContactsList;
// The address book to be used to store the contact details in.
private AddressBook addressBook;
/**
* This method will be called by the FXML-loader after having loaded
* all the GUI-components and initialized them.
*/
@FXML
private void initialize() {
ObservableList<ContactDetails> observableContactsList = FXCollections.observableArrayList();
observableContactsList.add(new ContactDetails("Arne Styve", "Phone", "Address"));
observableContactsList.add(new ContactDetails("Eskil Styve", "Phone", "Address"));
observableContactsList.add(new ContactDetails("Siw Styve", "Phone", "Address"));
this.observableContactsList = FXCollections.observableArrayList();
//observableContactsList.add(new ContactDetails("Arne Styve", "Phone", "Address"));
//observableContactsList.add(new ContactDetails("Eskil Styve", "Phone", "Address"));
//observableContactsList.add(new ContactDetails("Siw Styve", "Phone", "Address"));
this.addressBook = new AddressBookDBHandler();
this.observableContactsList.setAll(this.addressBook.getAllContacts());
this.contactDetailsTableView.setItems(observableContactsList);
}
/**
* Updates the ObservableArray wrapper with the current content in the
* Literature register. Call this method whenever changes are made to the
* underlying LiteratureRegister.
*
* @param addressBook the address book to use for updating the observable list
*/
public void updateObservableList(AddressBook addressBook) {
this.observableContactsList.setAll(addressBook.getAllContacts());
}
public void addContact(ActionEvent actionEvent) {
System.out.println("addContact() was called...");
ContactDetailsDialog contactsDialog = new ContactDetailsDialog();
Optional<ContactDetails> result = contactsDialog.showAndWait();
if (result.isPresent()) {
ContactDetails newContactDetails = result.get();
this.addressBook.addContact(newContactDetails);
this.updateObservableList(this.addressBook);
}
}
/**
* Import contacts from a .CSV-file chosen by the user.
*/
public void importFromCSV(ActionEvent actionEvent) {
System.out.println("importFromCSV() was called...");
FileChooser fileChooser = new FileChooser();
// Set extension filter for .csv-file
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
fileChooser.getExtensionFilters().add(extFilter);
// Show save open dialog
File file = fileChooser.showOpenDialog(null);
if (file != null) {
try {
AddressBookFileHandler.importFromCsv(this.addressBook, file);
this.updateObservableList(this.addressBook);
} catch (IOException ioe) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("File Import Error");
alert.setHeaderText("Error during CSV-import.");
alert.setContentText("Details: " + ioe.getMessage());
alert.showAndWait();
}
}
}
/**
* Export all contacts in the address book to a CSV-file specified by the user.
*/
public void exportToCSV(ActionEvent actionEvent) {
System.out.println("exportToCSV() was called...");
FileChooser fileChooser = new FileChooser();
// Set extension filter for .csv-file
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
fileChooser.getExtensionFilters().add(extFilter);
// Show save file dialog
File file = fileChooser.showSaveDialog(null);
if (file != null) {
try {
AddressBookFileHandler.exportToCsv(this.addressBook, file);
} catch (IOException ioe) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("File Export Error");
alert.setHeaderText("Error during CSV-export.");
alert.setContentText("Details: " + ioe.getMessage());
alert.showAndWait();
}
}
}
/**
* Exit the application. Displays a confirmation dialog.
*/
public void exitApplication(ActionEvent actionEvent) {
System.out.println("exitApplication() was called...");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Exit Application ?");
alert.setContentText("Are you sure you want to exit this application?");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && (result.get() == ButtonType.OK)) {
// ... user choose OK
Platform.exit();
}
}
public void removeContact(ActionEvent actionEvent) {
......@@ -64,8 +160,20 @@ public class ContactsAppFXMLController {
System.out.println("editContact() was called...");
}
/**
* Displays an example of an alert (info) dialog. In this case an "about"
* type of dialog.
*/
public void showAboutDialog(ActionEvent actionEvent) {
System.out.println("showAboutDialog() was called...");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog - About");
alert.setHeaderText("Contacts Register\nv" + AppVersion.VERSION);
alert.setContentText("A brilliant application created by\n"
+ "(C)Arne Styve\n"
+ "2020-03-16");
alert.showAndWait();
}
public void showLoginDialog(ActionEvent actionEvent) {
......
......@@ -19,6 +19,7 @@ import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.util.Pair;
import no.ntnu.idata2001.contacts.AppVersion;
import no.ntnu.idata2001.contacts.model.AddressBook;
import no.ntnu.idata2001.contacts.model.AddressBookDBHandler;
import no.ntnu.idata2001.contacts.model.AddressBookFileHandler;
......@@ -237,13 +238,11 @@ public class MainController {
/**
* Displays an example of an alert (info) dialog. In this case an "about"
* type of dialog.
*
* @param version the version of the application, to be displayed in the dialog.
*/
public void showAboutDialog(String version) {
public void showAboutDialog() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog - About");
alert.setHeaderText("Contacts Register\nv" + version);
alert.setHeaderText("Contacts Register\nv" + AppVersion.VERSION);
alert.setContentText("A brilliant application created by\n"
+ "(C)Arne Styve\n"
+ "2020-03-16");
......
......@@ -28,6 +28,7 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import no.ntnu.idata2001.contacts.AppVersion;
import no.ntnu.idata2001.contacts.controllers.MainController;
import no.ntnu.idata2001.contacts.model.AddressBook;
import no.ntnu.idata2001.contacts.model.ContactDetails;
......@@ -37,8 +38,6 @@ import no.ntnu.idata2001.contacts.model.ContactDetails;
*/
public class ContactsApp extends Application {
private static final String VERSION = "0.5";
private MainController mainController;
// The JavaFX ObservableListWrapper used to connect tot he underlying AddressBook
......@@ -100,7 +99,7 @@ public class ContactsApp extends Application {
Scene scene = new Scene(root, 600, 500);
// Set title of the stage (window) and add the scene
primaryStage.setTitle("Contacts v" + VERSION);
primaryStage.setTitle("Contacts v" + AppVersion.VERSION);
primaryStage.setScene(scene);
// And initialize the controller
......@@ -276,7 +275,7 @@ public class ContactsApp extends Application {
// ----- The Help-menu ------
Menu menuHelp = new Menu("Help");
MenuItem about = new MenuItem("About");
about.setOnAction(event -> mainController.showAboutDialog(VERSION));
about.setOnAction(event -> mainController.showAboutDialog());
menuHelp.getItems().add(about);
// ----- The LogIn-menu ------
......
......@@ -14,13 +14,10 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<!--BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="no.ntnu.idata2001.contacts.controllers.ContactsAppFXMLControllerller"-->
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1">
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="no.ntnu.idata2001.contacts.controllers.ContactsAppFXMLController">
<top>
<VBox>
<children>
......@@ -71,7 +68,7 @@
</HBox>
</bottom>
<center>
<TableView fx:id="contactDetailsTableView">
<TableView fx:id="contactDetailsTableView" >
<columns>
<TableColumn maxWidth="-1.0" minWidth="200.0" prefWidth="-1.0" text="Name">
<cellValueFactory>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment