summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java
blob: e5970b9f7f062c7452401915feb74f3fe359f576 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
	protected final JButton btnUpdateCheck;

	/**
	 * 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");
		btnUpdateCheck = new JButton("Update");

		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);
		p.add(btnUpdateCheck);
		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;
	}
}