summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java
blob: 5472ea427aabcdc32d66cd432a779de2ad858450 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                             
 
                      
                          




                               

                             
                           




                                  

                                       
                               
                                                  
                                  

                                                                
                                                 
                                              
 
                           
                                                         
 

                                                                                       
                                                      
                                          


                                  
 
                                    
 
                                                  
                                     
                 
         
 

                                                          
 



                                                                              
                           

                                                                  
                           




                                                         
 

                                       
           
                           
                                                     
           

                                                                          










                                                                                    












                                                                                













                                                                                


                                                     





                                                                             
 
                                             


                                                                              
 
                                             




















                                                                                             
         
 
           

                                                                                

                                           
                     
                                                                                             




                                                                                                 





                                                     

                                                                                                                        
                                     
                                       
                                                            
                 
                            
         
 
package org.openslx.dozmod.gui.window.layout;

import java.awt.Frame;
import java.awt.Rectangle;

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.JLabel;
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.gui.Gui;
import org.openslx.dozmod.gui.control.ComboBox;
import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
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 = "bwSuite - Login";
	private static final String AUTH_TYPE_LABEL = "Authentifizierungsart";
	private static final String LOGIN_FORM_LABEL = "Zugangsdaten";

	// login type panel
	protected JRadioButton[] loginTypes = new JRadioButton[3];

	// login form panel
	protected final JComboBox<Organization> idpCombo;
	protected final JTextField usernameField;
	protected final JPasswordField passwordField;
	protected final JCheckBox saveUsernameCheck;
	protected final JButton loginButton;

	/**
	 * 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 JLabel(getScaledLogo()), 2);
		grid.nextRow();

		loginTypes[0] = new JRadioButton("Authentifizierung über bwIDM");
		loginTypes[1] = new JRadioButton("Test-Zugang mit festem Benutzer");
		loginTypes[2] = new JRadioButton("Direkter Zugang zum Satelliten");

		idpCombo = 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...";
			}
		});
		usernameField = new JTextField();
		passwordField = new JPasswordField();
		loginButton = new JButton("Login");
		saveUsernameCheck = new JCheckBox("Benutzername speichern");

		grid.add(makeLoginTypePanel()).expand(0.25, 1).fill(true, true);
		grid.add(makeLoginFormPanel()).expand(0.75, 1).fill(true, true);
		grid.nextRow();

		grid.finish(true);

	}

	private JPanel makeLoginFormPanel() {

		// login form panel
		JPanel loginFormPanel = new JPanel();
		loginFormPanel.setBorder(new TitledBorder(LOGIN_FORM_LABEL));
		GridManager grid = new GridManager(loginFormPanel, 2);

		grid.add(new JLabel("Identity Provider"));
		grid.add(idpCombo).expand(true, false).fill(true, false);
		grid.nextRow();

		// label + field for username
		grid.add(new JLabel("Benutzername"));
		grid.add(usernameField).expand(true, false).fill(true, false);
		grid.nextRow();

		// label + field for password
		grid.add(new JLabel("Passwort"));
		grid.add(passwordField).expand(true, false).fill(true, false);
		grid.nextRow();

		grid.add(loginButton);
		grid.add(saveUsernameCheck);
		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 < loginTypes.length; i++) {
			loginTypeButtonGroup.add(loginTypes[i]);
			loginTypePanel.add(loginTypes[i]);
		}
		return loginTypePanel;
	}

	/**
	 * @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");
			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;
	}
}