I do sometimes show Dialoges 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 dialoges by using a design pattern?
To ilustrate 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);
}
}
Aucun commentaire:
Enregistrer un commentaire