net.sourceforge.jbizmo.commons.richclient.javafx.dialog.AbstractLogOnDialog Maven / Gradle / Ivy
/*
* This file is part of JBizMo, a set of tools, libraries and plug-ins
* for modeling and creating Java-based enterprise applications.
* For more information visit:
*
* http://sourceforge.net/projects/jbizmo/
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.sourceforge.jbizmo.commons.richclient.javafx.dialog;
import static javafx.scene.layout.Region.USE_COMPUTED_SIZE;
import java.util.HashMap;
import javafx.geometry.HPos;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import net.sourceforge.jbizmo.commons.richclient.javafx.i18n.I18NJavaFX;
import net.sourceforge.jbizmo.commons.richclient.javafx.image.ImageLoader;
import net.sourceforge.jbizmo.commons.richclient.logon.LogOnManager;
import net.sourceforge.jbizmo.commons.richclient.persistence.entity.LastLogOn;
import net.sourceforge.jbizmo.commons.richclient.transport.ServiceLocatorDTO;
/**
*
* Abstract base class for log-on dialogs
*
*
* Copyright 2015 (C) by Martin Ganserer
*
* @author Martin Ganserer
* @version 1.0.0
*/
public abstract class AbstractLogOnDialog extends TitleAreaDialog
{
private static final int MAX_WRONG_INPUTS = 3;
private TextField txtUserName;
private PasswordField txtPassword;
private int wrongInputCount = 0;
private ServiceLocatorDTO serviceLocatorDTO = null;
private final HashMap hostMap;
private ComboBox cboHost;
private boolean hideHostSelection = false;
/**
* Constructor
* @param owner
* @param hostMap
*/
public AbstractLogOnDialog(Window owner, HashMap hostMap)
{
super(owner, I18NJavaFX.getInstance().getString("abstract_log_on_dialog.title"));
this.hostMap = hostMap;
// If the caller provides a list containing only one element we will hide the respective selection component!
if(hostMap.size() == 1)
{
hideHostSelection = true;
setSize(340, 220);
}
else
setSize(340, 250);
// We assume that the caller provides a map containing one item at least!
serviceLocatorDTO = hostMap.values().stream().findFirst().get();
initStyle(StageStyle.UTILITY);
}
/**
* @param settings
* @throws SecurityException if login has failed
*/
public abstract void logOn(ServiceLocatorDTO settings) throws SecurityException;
/**
* @return the selected service locator DTO
*/
public ServiceLocatorDTO getServiceLocatorDTO()
{
return serviceLocatorDTO;
}
/*
* (non-Javadoc)
* @see net.sourceforge.jbizmo.commons.richclient.javafx.dialog.Dialog#createDialogArea()
*/
@Override
protected Node createDialogArea()
{
setTitleMessage(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.title_message"));
setTitleImage(ImageLoader.getImage(ImageLoader.IMG_LOGON));
final GridPane panRoot = new GridPane();
panRoot.setHgap(5.0);
panRoot.setVgap(5.0);
panRoot.getColumnConstraints().add(new ColumnConstraints(USE_COMPUTED_SIZE, USE_COMPUTED_SIZE, USE_COMPUTED_SIZE, Priority.SOMETIMES, HPos.LEFT, false));
panRoot.getColumnConstraints().add(new ColumnConstraints(USE_COMPUTED_SIZE, USE_COMPUTED_SIZE, USE_COMPUTED_SIZE, Priority.ALWAYS, HPos.LEFT, true));
panRoot.add(new Label(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.lbl_user_name")), 0, 0);
panRoot.add(txtUserName = new TextField(), 1, 0);
panRoot.add(new Label(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.lbl_password")), 0, 1);
panRoot.add(txtPassword = new PasswordField(), 1, 1);
if(!hideHostSelection)
{
panRoot.add(new Label(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.lbl_host")), 0, 2);
panRoot.add(cboHost = new ComboBox<>(), 1, 2);
cboHost.getItems().addAll(hostMap.keySet());
cboHost.setValue(hostMap.keySet().stream().findFirst().get());
}
final LastLogOn lastLogOn = LogOnManager.getLastLogOn();
if(lastLogOn != null)
{
txtUserName.setText(lastLogOn.getUserName());
if(!hideHostSelection && hostMap.containsKey(lastLogOn.getHost()))
cboHost.setValue(lastLogOn.getHost());
}
return panRoot;
}
/*
* (non-Javadoc)
* @see net.sourceforge.jbizmo.commons.richclient.javafx.dialog.Dialog#onOKPressed()
*/
@Override
protected void onOKPressed()
{
setReturnCode(DialogButtonType.CANCEL);
if(txtUserName.getText().isEmpty())
{
setErrorMessage(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.msg_missing_user"));
return;
}
if(!hideHostSelection)
serviceLocatorDTO = hostMap.get(cboHost.getValue());
serviceLocatorDTO.setUserName(txtUserName.getText());
serviceLocatorDTO.setPassword(txtPassword.getText());
try
{
logOn(serviceLocatorDTO);
setReturnCode(DialogButtonType.OK);
// Save input
LogOnManager.saveLastLogOn(txtUserName.getText(), serviceLocatorDTO.getAlias());
close();
}
catch (final SecurityException e)
{
wrongInputCount++;
setErrorMessage(I18NJavaFX.getInstance().getString("abstract_log_on_dialog.msg_invalid_credentials"));
if(wrongInputCount == MAX_WRONG_INPUTS)
close();
}
catch (final Throwable t)
{
final String title = I18NJavaFX.getInstance().getString("abstract_log_on_dialog.title_message");
final String message = I18NJavaFX.getInstance().getString("abstract_log_on_dialog.msg_connect_to_host");
setErrorMessage(message);
DialogUtil.openErrorDialog(null, title, message, t);
}
}
}