package org.openslx.dozmod.gui.window.layout;
import java.awt.Frame;
import java.awt.Rectangle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.dozmod.Branding;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.control.ComboBox;
import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
import org.openslx.dozmod.gui.control.QLabel;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.util.ResourceLoader;
@SuppressWarnings("serial")
public abstract class LoginWindowLayout extends JDialog {
private static final Logger LOGGER = Logger.getLogger(LoginWindowLayout.class);
// TODO This has nothing to to with the layout
protected static enum LOGIN_TYPE {
ECP(0),
TEST_ACCOUNT(1),
DIRECT_CONNECT(2);
public final int id;
private LOGIN_TYPE(final int id) {
this.id = id;
}
}
// authentication method to use for login attempts
protected LOGIN_TYPE loginType = null;
private static final String TITLE = Branding.getApplicationName() + " - Login";
private static final String AUTH_TYPE_LABEL = "Authentifizierungsart";
private static final String LOGIN_FORM_LABEL = "Zugangsdaten";
private static final String ADVANCED_LABEL = "Erweitert";
// login type panel
protected final JRadioButton[] rdoLoginType = new JRadioButton[3];
// login form panel
protected final JComboBox<Organization> cboOrganization;
protected final JTextField txtUsername;
protected final JPasswordField txtPassword;
protected final JCheckBox chkSaveUsername;
protected final JButton btnOpenRegistration;
protected final JButton btnLogin;
protected final JPanel pnlLoginType;
protected final JPanel pnlLoginForm;
protected final JPanel pnlAdvanced;
// advanced panel
protected final JButton btnSettings;
protected final JButton btnLogDir;
/**
* Create a new login composite
*
* @param mainShell
* The shell it should be added to
*/
public LoginWindowLayout(Frame modalParent) {
super(modalParent, TITLE, ModalityType.APPLICATION_MODAL);
setResizable(false);
GridManager grid = new GridManager(this, 2);
grid.add(new QLabel(getScaledLogo()), 2);
grid.nextRow();
rdoLoginType[0] = new JRadioButton("Authentifizierung über " + Branding.getMasterServerIdm());
rdoLoginType[1] = new JRadioButton("Test-Zugang mit festem Benutzer");
rdoLoginType[2] = new JRadioButton("Direkter Zugang zum Satelliten");
btnSettings = new JButton("Einstellungen");
btnLogDir = new JButton("Logverzeichnis");
cboOrganization = new ComboBox<>(new ComboBoxRenderer<Organization>() {
@Override
public String renderItem(Organization item) {
if (item == null)
return null;
return item.getDisplayName();
}
@Override
public String getEmptyText() {
return "Wird geladen...";
}
});
txtUsername = new JTextField();
txtPassword = new JPasswordField();
btnLogin = new JButton("Login");
chkSaveUsername = new JCheckBox("Benutzername speichern");
btnOpenRegistration = new JButton("Registrieren");
pnlLoginType = makeLoginTypePanel();
grid.add(pnlLoginType).expand(0.25, 1).fill(true, true);
pnlLoginForm = makeLoginFormPanel();
grid.add(pnlLoginForm,1,2).expand(0.75, 1).fill(true, true);
grid.nextRow();
pnlAdvanced = makeAdvancedPanel();
grid.add(pnlAdvanced).expand(true, true).fill(true, true);
grid.finish(false);
}
private JPanel makeLoginFormPanel() {
// login form panel
JPanel loginFormPanel = new JPanel();
loginFormPanel.setBorder(new TitledBorder(LOGIN_FORM_LABEL));
GridManager grid = new GridManager(loginFormPanel, 4);
grid.add(new QLabel("Identity Provider"));
grid.add(cboOrganization, 3).expand(true, false).fill(true, false);
grid.nextRow();
// label + field for username
grid.add(new QLabel("Benutzername"));
grid.add(txtUsername, 3).expand(true, false).fill(true, false);
grid.nextRow();
// label + field for password
grid.add(new QLabel("Passwort"));
grid.add(txtPassword, 3).expand(true, false).fill(true, false);
grid.nextRow();
grid.add(Box.createGlue());
grid.add(chkSaveUsername).expand(true, false);
grid.add(btnOpenRegistration);
grid.add(btnLogin);
grid.nextRow();
grid.finish(true);
return loginFormPanel;
}
private JPanel makeLoginTypePanel() {
JPanel loginTypePanel = new JPanel();
loginTypePanel.setLayout(new BoxLayout(loginTypePanel, BoxLayout.PAGE_AXIS));
loginTypePanel.setBorder(new TitledBorder(AUTH_TYPE_LABEL));
ButtonGroup loginTypeButtonGroup = new ButtonGroup();
for (int i = 0; i < rdoLoginType.length; i++) {
loginTypeButtonGroup.add(rdoLoginType[i]);
loginTypePanel.add(rdoLoginType[i]);
}
return loginTypePanel;
}
private JPanel makeAdvancedPanel() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.setBorder(new TitledBorder(ADVANCED_LABEL));
p.add(btnSettings);
p.add(btnLogDir);
return p;
}
/**
* @return ImageIcon of the standard service logo scaled to the login
* window size
*/
private ImageIcon getScaledLogo() {
try {
ImageIcon image = ResourceLoader.getIcon("/img/service-logo.png");
Rectangle screenSize = Gui.getMonitorFromRectangle(getBounds(), true)
.getDefaultConfiguration()
.getBounds();
float scaleX = (float) screenSize.width / (float) image.getIconWidth();
float scaleY = (float) screenSize.height / (float) image.getIconHeight();
final float scaling;
if (scaleX < scaleY) {
scaling = scaleX / 2;
} else {
scaling = scaleY / 2;
}
image = new ImageIcon(image.getImage().getScaledInstance((int) (image.getIconWidth() * scaling),
(int) (image.getIconHeight() * scaling), 0));
return image;
} catch (Exception e) {
LOGGER.warn("Cannot load image", e);
}
return null;
}
}