Skip to content

Commit

Permalink
prompter refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
izzat5233 committed May 14, 2023
1 parent 978ddfa commit 0d0585c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
51 changes: 24 additions & 27 deletions ccsm-ui/src/main/java/stu/najah/se/ui/Prompter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,31 @@
/**
* Utility class for (graphically) prompting alerts, inputs texts, dialogs, etc...
*/
public final class Prompter
public abstract class Prompter
implements DatabaseErrorListener, EmailConfirmationListener {

private static Prompter instance;

public static Prompter getInstance() {
if (instance == null) {
instance = new Prompter();
private static final Prompter adapterListener = new Prompter() {
@Override
public void onTransactionError(String message) {
Prompter.error(message);
}
return instance;
}

private Prompter() {
}
@Override
public boolean onEmailConfirmation(String message) {
return Prompter.confirm(message);
}

@Override
public void onTransactionError(String message) {
error(message);
}
@Override
public void onEmailSent(String message) {
Prompter.info(message);
}
};

@Override
public boolean onEmailConfirmation(String message) {
return confirm(message);
public static Prompter getListener() {
return adapterListener;
}

@Override
public void onEmailSent(String message) {
info(message);
private Prompter() {
}

/**
Expand All @@ -46,7 +43,7 @@ public void onEmailSent(String message) {
* @param type only use INFORMATION, WARNING, or ERROR
* because they don't have multiple buttons
*/
private void prompt(String message, Alert.AlertType type) {
private static void prompt(String message, Alert.AlertType type) {
var alert = new Alert(type);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setContentText(message);
Expand All @@ -59,7 +56,7 @@ private void prompt(String message, Alert.AlertType type) {
* @param message to be displayed
* @return true if OK was pressed, false otherwise
*/
public boolean confirm(String message) {
public static boolean confirm(String message) {
var alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setContentText(message);
Expand All @@ -72,7 +69,7 @@ public boolean confirm(String message) {
*
* @param message to be displayed
*/
public void info(String message) {
public static void info(String message) {
prompt(message, Alert.AlertType.INFORMATION);
}

Expand All @@ -81,7 +78,7 @@ public void info(String message) {
*
* @param message to be displayed
*/
public void warning(String message) {
public static void warning(String message) {
prompt(message, Alert.AlertType.WARNING);
}

Expand All @@ -90,7 +87,7 @@ public void warning(String message) {
*
* @param message to be displayed
*/
public void error(String message) {
public static void error(String message) {
prompt(message, Alert.AlertType.ERROR);
}

Expand All @@ -99,12 +96,12 @@ public void error(String message) {
*
* @param e it's message will be displayed
*/
public void error(Exception e) {
public static void error(Exception e) {
prompt(e.getMessage(), Alert.AlertType.ERROR);
}


public void loginError() {
public static void loginError() {
error("Login failed. Please check your username and password and try again.");
}

Expand Down
2 changes: 1 addition & 1 deletion ccsm-ui/src/main/java/stu/najah/se/ui/SceneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static SceneManager getInstance() {
*/
public static void main(String[] args) {
ServiceManager.initializeAdminService();
ServiceManager.initializeEntityServices(Prompter.getInstance(), Prompter.getInstance());
ServiceManager.initializeEntityServices(Prompter.getListener(), Prompter.getListener());
Application.launch(args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void login() {
textFieldPassword.getText()
);
} catch (IllegalArgumentException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void updateOrderProduct() {
try {
orderProductService.updateOrderProduct(createProductFromTextFields());
} catch (NumberFormatException | IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshOrderProductsTable();
}
Expand All @@ -195,7 +195,7 @@ private void deleteOrderProduct() {
try {
orderProductService.deleteOrderProduct();
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshOrderProductsTable();
refreshAvailableProducts();
Expand All @@ -207,7 +207,7 @@ private void createOrderProduct() {
try {
orderProductService.createAndSelectOrderProduct(createProductFromTextFields());
} catch (NumberFormatException | IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshOrderProductsTable();
refreshAvailableProducts();
Expand All @@ -219,7 +219,7 @@ private void createOrder() {
try {
orderService.createAndSelectOrder();
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshOrdersTable();
}
Expand All @@ -230,7 +230,7 @@ private void deleteOrder() {
try {
orderService.deleteOrder();
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshOrdersTable();
}
Expand All @@ -241,7 +241,7 @@ private void notifyCustomer() {
try {
orderService.notifyCustomer();
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void createProduct() {
textFieldDescription.getText()
));
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshTable();
}
Expand All @@ -125,7 +125,7 @@ private void updateProduct() {
textFieldDescription.getText()
));
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshTable();
}
Expand All @@ -136,7 +136,7 @@ private void deleteProduct() {
try {
productService.deleteProduct();
} catch (IllegalStateException e) {
Prompter.getInstance().error(e);
Prompter.error(e);
} finally {
refreshTable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Configuration {
@BeforeAll
public static void launch() throws Exception {
ServiceManager.initializeAdminService();
ServiceManager.initializeEntityServices(Prompter.getInstance(), Prompter.getInstance());
ServiceManager.initializeEntityServices(Prompter.getListener(), Prompter.getListener());
ApplicationTest.launch(SceneManager.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void setHeadlessMode() {
@BeforeAll
public static void launch() throws Exception {
ServiceManager.initializeAdminService();
ServiceManager.initializeEntityServices(Prompter.getInstance(), Prompter.getInstance());
ServiceManager.initializeEntityServices(Prompter.getListener(), Prompter.getListener());
ApplicationTest.launch(SceneManager.class);
}

Expand Down

0 comments on commit 0d0585c

Please sign in to comment.