package org.openslx.dozmod.gui.window.layout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListCellRenderer;
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.JList;
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[] loginTypes = new JRadioButton[3];
// login form panel
protected JLabel idpLabel;
protected JComboBox<Organization> idpCombo;
protected JTextField usernameField;
protected JPasswordField passwordField;
protected JCheckBox saveUsernameCheck;
protected JButton loginButton;
/**
* Create a new login composite
*
* @param mainShell
* The shell it should be added to
*/
@SuppressWarnings("serial")
public LoginWindowLayout(Frame modalParent) {
super(modalParent, TITLE, ModalityType.APPLICATION_MODAL);
setResizable(false);
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() {
};
loginTypes[0] = new JRadioButton("Authentifizierung über bwIDM");
loginTypes[1] = new JRadioButton("Test-Zugang mit festem Benutzer");
loginTypes[2] = new JRadioButton("Direkter Zugang zum Satelliten");
for (int i = 0; i < loginTypes.length; i++) {
loginTypeButtonGroup.add(loginTypes[i]);
loginTypePanel.add(loginTypes[i]);
}
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)));
idpLabel = new JLabel("Identity Provider:");
formBag.setConstraints(idpLabel, formCon);
loginFormPanel.add(idpLabel);
idpCombo = new JComboBox<Organization>();
idpCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
setText("Initialisiere...");
return this;
}
});
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(idpCombo, formCon);
loginFormPanel.add(idpCombo);
// label + field for username
JLabel labelUsername = new JLabel("Benutzername:");
usernameField = new JTextField();
usernameField.setToolTipText("Bitte geben Sie Ihren Benutzernamen ein.");
usernameField.setColumns(10);
formCon.gridwidth = 1;
formBag.setConstraints(labelUsername, formCon);
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(usernameField, formCon);
loginFormPanel.add(labelUsername);
loginFormPanel.add(usernameField);
loginFormPanel.setLayout(formBag);
// label + field for password
JLabel labelPassword = new JLabel("Passwort:");
passwordField = new JPasswordField();
passwordField.setToolTipText("Bitte geben Sie Ihren Passwort ein.");
passwordField.setColumns(10);
formCon.gridwidth = 1;
formBag.setConstraints(labelPassword, formCon);
loginFormPanel.add(labelPassword);
formCon.gridwidth = GridBagConstraints.REMAINDER;
formBag.setConstraints(passwordField, formCon);
loginFormPanel.add(passwordField);
loginButton = new JButton("Login");
formCon.gridwidth = 1;
formCon.gridx = 1;
formBag.setConstraints(loginButton, formCon);
loginFormPanel.add(loginButton);
saveUsernameCheck = new JCheckBox("Benutzername speichern");
formCon.gridwidth = 1;
formCon.gridx = 2;
formBag.setConstraints(saveUsernameCheck, formCon);
loginFormPanel.add(saveUsernameCheck);
// finally add the form itself to the main panel
con.gridwidth = GridBagConstraints.REMAINDER;
bag.setConstraints(loginFormPanel, con);
add(loginFormPanel);
pack();
validate();
}
/**
* @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;
}
}