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



                               

                             
                           


                                  
                              
                              

                                       

                                           
                                                  
                                   
                                  

                                                                
                                             
                                                 
                                          
                                              
 
                                                         
 




                                                                          
                                                                                           
 
                                                      
                                          


                                  
 
                                    
 
                                                  
                                     
                 
         
 

                                                          
 
                           
                                                                          
 
                           



                                                                
                                                    


                                            
                                           







                                               


                                            
                                           
 

                                       
           
                           
                                                     
           
                                                     

                                                                                                                     



                                                            
                                                         

                               







                                                                                                                     

                                                                                                      
 
                                                                                                            
                                                                                                   








                                                                     
                                                                                                             
                         
                                       
                                                                                                    
                                               
                                                                                                    
                                                   






                                           


                                                                                                                      



                                                                        
                                                                              
                               
 

                                                                          
 





                                                                                       
 
                                   



                                             


                                                     

                                                                                                          
                                                                      
 
                                          
                                                                                   
                               
 
                                             
                                      
                                                                               
                               
 
                                             
                                      
                                                                               

                               









                                                                            


                                                              
                                   
                               
                                   





                                                                                             

                                                                                                          
                                                                     


                                                                  
                 
 
                                      
         
 


                                                                   
                                                                                                                      



                                   
 
           
                                                                             
                              

                                           
                     
                                                                                          




                                                                                                 





                                                     


                                                                                                     
                                     
                                       
                                                            
                 
                            
         
 
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.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.gui.helper.I18n;
import org.openslx.dozmod.util.ResourceLoader;

public abstract class LoginWindowLayout extends JDialog {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 4666636501698824636L;

	private static final Logger LOGGER = LogManager.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;

	// login type panel
	protected final JRadioButton[] rdoLoginType = new JRadioButton[4];

	// 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;

	protected final JTextField txtUrl;
	protected final QLabel lblError;

	protected final QLabel lblOrganization;
	protected final QLabel lblUsername;
	protected final QLabel lblPassword;

	// advanced panel
	protected final JButton btnSettings;
	protected final JButton btnLogDir;
	protected final JPanel pnlActivity;

	/**
	 * Create a new login composite
	 * 
	 * @param mainShell
	 *            The shell it should be added to
	 */
	public LoginWindowLayout(Frame modalParent) {
		super(modalParent, I18n.WINDOW_LAYOUT.getString("Login.Dialog.title", Branding.getApplicationName()),
				ModalityType.APPLICATION_MODAL);
		setResizable(false);

		GridManager grid = new GridManager(this, 2);

		grid.add(new QLabel(getScaledLogo()), 2);
		grid.nextRow();

		rdoLoginType[0] = new JRadioButton(I18n.WINDOW_LAYOUT.getString("Login.RadioButton.loginType.text.0",
				Branding.getMasterServerIdm()));
		rdoLoginType[1] = new JRadioButton(
				I18n.WINDOW_LAYOUT.getString("Login.RadioButton.loginType.text.1"));
		rdoLoginType[2] = new JRadioButton(
				I18n.WINDOW_LAYOUT.getString("Login.RadioButton.loginType.text.2"));
		rdoLoginType[3] = new JRadioButton(I18n.WINDOW_LAYOUT.getString("Login.RadioButton.loginType.text.3",
				Branding.getMasterServerIdm()));
		btnSettings = new JButton(I18n.WINDOW_LAYOUT.getString("Login.Button.settings.text"));
		btnLogDir = new JButton(I18n.WINDOW_LAYOUT.getString("Login.Button.logDir.text"));

		lblOrganization = new QLabel(I18n.WINDOW_LAYOUT.getString("Login.Label.organization.text"));
		cboOrganization = new ComboBox<Organization>(new ComboBoxRenderer<Organization>() {
			@Override
			public String renderItem(Organization item) {
				if (item == null)
					return null;
				return item.getDisplayName();
			}

			@Override
			public String getEmptyText() {
				return I18n.WINDOW_LAYOUT.getString("Login.ComboBox.organization.emptyText");
			}
		}, Organization.class);
		lblUsername = new QLabel(I18n.WINDOW_LAYOUT.getString("Login.Label.username.text"));
		txtUsername = new JTextField();
		lblPassword = new QLabel(I18n.WINDOW_LAYOUT.getString("Login.Label.password.text"));
		txtPassword = new JPasswordField();
		// Browser
		txtUrl = new JTextField();
		txtUrl.setEditable(false);
		lblError = new QLabel();
		txtUrl.setVisible(false);
		lblError.setVisible(false);
		//
		btnLogin = new JButton(I18n.WINDOW_LAYOUT.getString("Login.Button.login.text"));
		chkSaveUsername = new JCheckBox(I18n.WINDOW_LAYOUT.getString("Login.CheckBox.saveUsername.text"));
		btnOpenRegistration = new JButton(I18n.WINDOW_LAYOUT.getString("Login.Button.openRegistration.text"));

		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.nextRow();
		pnlActivity = new JPanel();
		pnlActivity.setLayout(new BoxLayout(pnlActivity, BoxLayout.PAGE_AXIS));
		pnlActivity.setVisible(false);
		pnlActivity.add(new JSeparator());
		grid.add(pnlActivity, 2).expand(true, false).fill(true, true);

		grid.finish(false);

	}

	private JPanel makeLoginFormPanel() {

		// login form panel
		JPanel loginFormPanel = new JPanel();
		loginFormPanel.setBorder(new TitledBorder(
				I18n.WINDOW_LAYOUT.getString("Login.TitledBorder.loginFormPanel.title")));
		GridManager grid = new GridManager(loginFormPanel, 4);

		grid.add(lblOrganization);
		grid.add(cboOrganization, 3).expand(true, false).fill(true, false);
		grid.nextRow();

		// label + field for username
		grid.add(lblUsername);
		grid.add(txtUsername, 3).expand(true, false).fill(true, false);
		grid.nextRow();

		// label + field for password
		grid.add(lblPassword);
		grid.add(txtPassword, 3).expand(true, false).fill(true, false);
		grid.nextRow();

		// --- Browser-based login

		grid.add(txtUrl, 4).expand(true, false).fill(true, false);
		grid.nextRow();
		grid.add(lblError, 4).expand(true, false).fill(true, false);
		grid.nextRow();
		
		grid.add(Box.createVerticalGlue(), 4).expand(false, true);
		grid.nextRow();

		grid.add(Box.createGlue());
		grid.add(chkSaveUsername).expand(true, false);
		grid.add(btnOpenRegistration);
		grid.add(btnLogin);
		grid.nextRow();
		grid.finish(false);
		return loginFormPanel;
	}

	private JPanel makeLoginTypePanel() {
		JPanel loginTypePanel = new JPanel();
		loginTypePanel.setLayout(new BoxLayout(loginTypePanel, BoxLayout.PAGE_AXIS));
		loginTypePanel.setBorder(new TitledBorder(
				I18n.WINDOW_LAYOUT.getString("Login.TitledBorder.loginTypePanel.title")));
		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(I18n.WINDOW_LAYOUT.getString("Login.TitledBorder.advancedPanel.title")));
		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;
	}
}