Commit d3792580 authored by Arne Styve's avatar Arne Styve
Browse files

Minor updates/adjustments. Ready for release...

parent 8d2119ef
......@@ -3,15 +3,26 @@ package no.ntnu.idata2001.contacts.controllers;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableView;
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;
......@@ -35,6 +46,8 @@ import no.ntnu.idata2001.contacts.views.ContactDetailsDialog;
* See: https://stackoverflow.com/questions/30210170/is-fxml-needed-for-every-declaration
*/
public class ContactsAppFXMLController {
private final Logger logger = Logger.getLogger(getClass().toString());
@FXML
private TableView<ContactDetails> contactDetailsTableView;
......@@ -71,6 +84,13 @@ public class ContactsAppFXMLController {
this.observableContactsList.setAll(addressBook.getAllContacts());
}
/**
* Display the input dialog to get input to create a new Contact.
* If the user confirms creating a new contact, a new instance
* of ContactDetails is created and added to the AddressBook provided.
*
* @param actionEvent the actionevent triggering this call
*/
public void addContact(ActionEvent actionEvent) {
ContactDetailsDialog contactsDialog = new ContactDetailsDialog();
Optional<ContactDetails> result = contactsDialog.showAndWait();
......@@ -84,6 +104,8 @@ public class ContactsAppFXMLController {
/**
* Import contacts from a .CSV-file chosen by the user.
*
* @param actionEvent the actionevent triggering this call
*/
public void importFromCSV(ActionEvent actionEvent) {
FileChooser fileChooser = new FileChooser();
......@@ -111,6 +133,8 @@ public class ContactsAppFXMLController {
/**
* Export all contacts in the address book to a CSV-file specified by the user.
*
* @param actionEvent the actionevent triggering this call
*/
public void exportToCSV(ActionEvent actionEvent) {
FileChooser fileChooser = new FileChooser();
......@@ -137,6 +161,8 @@ public class ContactsAppFXMLController {
/**
* Exit the application. Displays a confirmation dialog.
*
* @param actionEvent the actionevent triggering this call
*/
public void exitApplication(ActionEvent actionEvent) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
......@@ -152,10 +178,32 @@ public class ContactsAppFXMLController {
}
}
/**
* Removes the Contact selected in the table. If no Contact is
* selected, nothing is deleted, and the user is informed that he/she must
* select which Contact to delete.
*
* @param actionEvent the actionevent triggering this call
*/
public void removeContact(ActionEvent actionEvent) {
System.out.println("removeContact() was called...");
ContactDetails selectedContact = this.contactDetailsTableView
.getSelectionModel()
.getSelectedItem();
if (selectedContact == null) {
showPleaseSelectItemDialog();
} else {
if (showDeleteConfirmationDialog()) {
this.addressBook.removeContact(selectedContact.getPhone());
this.updateObservableList(this.addressBook);
}
}
}
/**
* Edit the contact details selected in the table view.
*
* @param actionEvent the actionevent triggering this call
*/
public void editContact(ActionEvent actionEvent) {
System.out.println("editContact() was called...");
}
......@@ -164,6 +212,8 @@ public class ContactsAppFXMLController {
/**
* Displays an example of an alert (info) dialog. In this case an "about"
* type of dialog.
*
* @param actionEvent the actionevent triggering this call
*/
public void showAboutDialog(ActionEvent actionEvent) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
......@@ -176,7 +226,101 @@ public class ContactsAppFXMLController {
alert.showAndWait();
}
/**
* Displays a login dialog using a custom dialog.
* Just to demonstrate the {@link javafx.scene.control.PasswordField}-control.
*
* @param actionEvent the actionevent triggering this call
*/
public void showLoginDialog(ActionEvent actionEvent) {
System.out.println("showLoginDialog() was called...");
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation .
username.textProperty().addListener((observable, oldValue, newValue) ->
loginButton.setDisable(newValue.trim().isEmpty()));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(username::requestFocus);
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(
dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(
usernamePassword -> logger.log(Level.INFO, () -> "Username=" + usernamePassword.getKey()
+ ", Password=" + usernamePassword.getValue()));
}
/**
* Displays a warning informing the user that an item must be selected from
* the table.
*/
public void showPleaseSelectItemDialog() {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("No items selected");
alert.setContentText("No item is selected from the table.\n"
+ "Please select an item from the table.");
alert.showAndWait();
}
/**
* Displays a delete confirmation dialog. If the user confirms the delete,
* <code>true</code> is returned.
*
* @return <code>true</code> if the user confirms the delete
*/
public boolean showDeleteConfirmationDialog() {
boolean deleteConfirmed = false;
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete confirmation");
alert.setHeaderText("Delete confirmation");
alert.setContentText("Are you sure you want to delete this item?");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent()) {
deleteConfirmed = (result.get() == ButtonType.OK);
}
return deleteConfirmed;
}
}
......@@ -107,7 +107,6 @@ public class AddressBookDBHandler implements AddressBook {
eman.close();
this.logger.log(Level.INFO, () ->
"Read all contacts from the DB, a total of " + contactsList.size());
return contactsList;
......
......@@ -22,7 +22,7 @@ public class ContactsAppFXML extends Application {
public void start(Stage primaryStage) throws Exception {
ContactsAppFXMLController contactsAppFXMLController = new ContactsAppFXMLController();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ContactsMain.fxml"));
fxmlLoader.setController(contactsAppFXMLController);
//fxmlLoader.setController(contactsAppFXMLController);
Parent root = fxmlLoader.load();
......
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