I do sometimes show Dialog boxes in my Java application.
Currently the Controller classes are (expect some exceptions where only getters are called on my model) used like mediators between my model and my UI.
But my UI knows my controllers and my controllers know my UI.
Whenever I add a new dialog I add a method in a controller and in the view class.
Is there a more elegant way to extend my program with new user dialogs by using a design pattern?
To illustrate you how my interaction looks right now I will append some code snippets.
Code from my UI
itmEmailSettings.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.showEmailSettingsDialog();
}
} );
More UI Code
public void showEmailSettingsDialog(String host, int port, int authMode,
String user, String pass, String fromEMail, String fromName) {
EmailSettingsDialog d = new EmailSettingsDialog(
host, port, authMode,
user, pass, fromEMail, fromName
);
d.createJDialog( mainFrame.getFrame() ).setVisible(true);
if(d.isValid()){
controller.changeEmailSettings( d.getHost(), d.getPort(), d.getAuthMode(), d.getFromEMail(), d.getFromName(), d.getUser(), d.getPass() );
}
}
Controller code:
public void showEmailSettingsDialog() {
try{
if(!pm.hasProjectFileAccess()){
mainFrame.showNoProjectfileAccess();
return;
}
ProgrammSettingsRepository pr = Utils.getProgrammSettingsRepository(pm);
String host = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_HOST);
int port = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PORT)==null?0:Integer.parseInt( pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PORT) );
int authMode = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE)==null?0:Integer.parseInt( pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE) );
String user = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_USER);
String pass = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PASSWORD);
String fromEMail = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_FROM_EMAIL);
String fromName = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_FROM_NAME);
menuView.showEmailSettingsDialog(host, port, authMode, user, pass, fromEMail, fromName);
}catch(SQLException e){
throw new RuntimeException(e.getMessage(), e);
}
}
public void changeEmailSettings(String host, int port, int authMode,
String fromEMail, String fromName, String user, String pass) {
try {
ProgrammSettingsRepository pr = Utils.getProgrammSettingsRepository(pm);
pr.store( ProgrammSettingsRepository.KEY_EMAIL_HOST , String.valueOf( host ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_PORT , String.valueOf( port ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE , String.valueOf( authMode ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_USER , String.valueOf( user ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_PASSWORD, String.valueOf( pass ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_FROM_EMAIL , String.valueOf( fromEMail ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_FROM_NAME , String.valueOf( fromName ));
pr.store(ProgrammSettingsRepository.KEY_EMAIL_SETTINGS_CONFIGURED, "true");
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
There are 3 types of dialog boxes: modeless, modal, and system modal.
A dialog box is a temporary window an application creates to retrieve user input. An application typically uses dialog boxes to prompt the user for additional information for menu items.
Knowing how to design dialogs will allow you to use them in a way that doesn’t annoy your users. What is Dialog? A dialog is an overlay that requires the user to interact with it and designed to elicit a response from the user. Dialogs inform users about critical information, require users to make decisions, or involve multiple tasks.
This topic provides information about the Dialog form pattern. A dialog box represents an action or activity that users can explicitly commit or cancel. It's used when a user initiates a specific task or process, and the system requires user input about how or whether to proceed.
They consist of the following parts, which can be assembled in a variety of combinations: A title bar to identify the application or system feature where the dialog box came from. A main instruction, with an optional icon, to identify the user's objective with the dialog.
Much of the guidance still applies in principle, but the presentation and examples do not reflect our current design guidance. A dialog box is a secondary window that allows users to perform a command, asks users a question, or provides users with information or progress feedback. A typical dialog box.
I understand that from UI you call Controller and then you call UI from Controller to show dialog. The controller does some calculations and then call UI to show dialog.
If you implement IReportable to the UI classes.
public interface IReportable {
public void showYesNoDialog(//all needed params);
public void showSimpleDialog(//all needed params);
.
.
.
}
public class DialogController() {
private IRportable _reportable;//delegator
public DialogController(IRportable _reportable) {//bridge pattern.
_reportable = reportable;
}
public void showEmailDialog() {
//calculations
_reportable.showSimpleDialog(//params);
}
public void showCustomerDialog() {
//calculations
_reportable.showYesNoDialog(//params);
}
}
public class UIClass implements IReportable {
private DialogController _dialogController;
public UIClass() {
_dialogController = new DialogController();
}
public void someMethod() {
if() {
}
...
_dialogController.showEmailDialog();
}
public void someOtherMethod() {
if() {
}
...
_dialogController.showCustomerDialog();
}
@Override
public void showYesNoDialog(//all needed params) {
//code here to show dialog according to params.
}
@Override
public void showSimpleDialog(//all needed params) {
//code here to show dialog according to params.
}
}
That way you have dependancy for the interface and not to single ui components. You can alter interface to abstract class to have the same functionality to all of the UI classes...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With