package org.openslx.dozmod.gui.window.layout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.util.ResourceLoader;
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 = "bwSuite - Login";
private static final String AUTH_TYPE_LABEL = "Authentifizierungsart";
private static final String LOGIN_FORM_LABEL = "Zugangsdaten";
// login type panel
protected JPanel loginTypePanel;
protected ButtonGroup loginTypeButtonGroup;
protected JRadioButton radioButtonSat;
protected JRadioButton radioButtonECP;
protected JRadioButton radioButtonTest;
// login form panel
protected JLabel labelIdp;
protected JComboBox<Organization> idpCombo;
protected JTextField loginUsernameField;
protected JPasswordField loginPasswordField;
protected JCheckBox saveUsernameCheckbox;
protected JButton loginButton;
/**
* Create a new login composite
*
* @param mainShell
* The shell it should be added to
*/
@SuppressWarnings("serial")
public LoginWindowLayout() {
super();
setTitle(TITLE);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
GridBagLayout bag = new GridBagLayout();
GridBagConstraints con = new GridBagConstraints();
setLayout(bag);
con.fill = GridBagConstraints.BOTH;
con.gridwidth = GridBagConstraints.REMAINDER;
con.weightx = 1;
con.weighty = 1;
JLabel pic = new JLabel(getScaledLogo());
bag.setConstraints(pic, con);
add(pic);
loginTypePanel = new JPanel();
loginTypePanel.setLayout(new BoxLayout(loginTypePanel, BoxLayout.PAGE_AXIS));
loginTypePanel.setBorder(new TitledBorder(UIManager
.getBorder("TitledBorder.border"), AUTH_TYPE_LABEL,
TitledBorder.LEADING, TitledBorder.TOP, null, Color.WHITE));
loginTypeButtonGroup = new ButtonGroup();
radioButtonECP = new JRadioButton("Authentifizierung über bwIDM");
radioButtonTest = new JRadioButton("Test-Zugang mit festem Benutzer");
radioButtonSat = new JRadioButton("Direkter Zugang zum Satelliten");
loginTypeButtonGroup.add(radioButtonECP);
loginTypeButtonGroup.add(radioButtonTest);
loginTypeButtonGroup.add(radioButtonSat);
loginTypePanel.add(radioButtonECP);
loginTypePanel.add(radioButtonTest);
loginTypePanel.add(radioButtonSat);
con.gridwidth = 1;
bag.setConstraints(loginTypePanel, con);
add(loginTypePanel);
// login form panel
JPanel loginFormPanel = new JPanel();
GridBagLayout formBag = new GridBagLayout();
GridBagConstraints formCon = new GridBagConstraints();
formCon.fill = GridBagConstraints.BOTH;
formCon.gridwidth = 1;
loginFormPanel.setLayout(new BoxLayout(loginFormPanel, BoxLayout.PAGE_AXIS));
loginFormPanel.setBorder(new TitledBorder(UIManager
.getBorder("TitledBorder.border"), LOGIN_FORM_LABEL,
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
labelIdp = new JLabel("Identity Provider:");
formBag.setConstraints(labelIdp, formCon);
loginFormPanel.add(labelIdp);
idpCombo = new JComboBox<Organization>();
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(idpCombo, formCon);
loginFormPanel.add(idpCombo);
// label + field for username
JLabel labelUsername = new JLabel("Benutzername:");
loginUsernameField = new JTextField();
loginUsernameField.setToolTipText("Bitte geben Sie Ihren Benutzernamen ein.");
loginUsernameField.setColumns(10);
formCon.gridwidth = 1;
formBag.setConstraints(labelUsername, formCon);
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(loginUsernameField, formCon);
loginFormPanel.add(labelUsername);
loginFormPanel.add(loginUsernameField);
loginFormPanel.setLayout(formBag);
// label + field for password
JLabel labelPassword = new JLabel("Passwort:");
loginPasswordField = new JPasswordField();
loginPasswordField.setToolTipText("Bitte geben Sie Ihren Passwort ein.");
loginPasswordField.setColumns(10);
formCon.gridwidth = 1;
formBag.setConstraints(labelPassword, formCon);
loginFormPanel.add(labelPassword);
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(loginPasswordField, formCon);
loginFormPanel.add(loginPasswordField);
loginButton = new JButton("Login");
formCon.gridwidth = 1;
formCon.gridx = 1;
formBag.setConstraints(loginButton, formCon);
loginFormPanel.add(loginButton);
saveUsernameCheckbox = new JCheckBox("Benutzername speichern");
formCon.gridwidth = 1;
formCon.gridx = 2;
formBag.setConstraints(saveUsernameCheckbox, formCon);
loginFormPanel.add(saveUsernameCheckbox);
con.gridwidth = GridBagConstraints.REMAINDER;
bag.setConstraints(loginFormPanel, con);
add(loginFormPanel);
pack();
}
/**
* @return ImageIcon of the standard bwLehrpool logo scaled to the login window size
*/
private ImageIcon getScaledLogo() {
try {
ImageIcon image = ResourceLoader.getIcon("/img/Logo_bwLehrpool.png");
java.awt.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;
}
}