summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ConfigWindow.java
blob: c64fbfca23bb9c5c13b9de94c2e21f3d37cb6125 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package org.openslx.dozmod.gui.window;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.SatelliteUserConfig;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.Config.ProxyMode;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.Language;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.ConfigWindowLayout;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftError;
import org.openslx.thrifthelper.ThriftManager;

public class ConfigWindow extends ConfigWindowLayout implements UiFeedback, ActionListener {

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

	private final static Logger LOGGER = LogManager.getLogger(ConfigWindow.class);
	private SatelliteUserConfig userConfig = null;
	private JRadioButton selectedLookAndFeel = null;

	public ConfigWindow(Window modalParent) {
		super(modalParent);
		btnSave.setEnabled(false);

		// -- PROXY --
		// set proxy mode as saved in config
		switch (Config.getProxyMode()) {
		case NONE:
			btnProxyNone.setSelected(true);
			break;
		case AUTO:
			btnProxyAuto.setSelected(true);
			break;
		case HTTP_CONNECT:
		case SOCKS:
		default:
			break;
		}
		btnProxyNone.addActionListener(this);
		btnProxyAuto.addActionListener(this);
		// -- END PROXY --

		// -- SAT USER CONFIG --
		// get the satellite user config and set the checkbox accordingly
		if (Session.getSatelliteToken() == null) {
			chkSendMeMail.setEnabled(false);
		} else {
			userConfig = getSatUserConfig();
			if (userConfig != null) {
				chkSendMeMail.setSelected(userConfig.isEmailNotifications());
			} else {
				chkSendMeMail.setSelected(false);
			}
			chkSendMeMail.addActionListener(this);
			lblYourAddress.setText(I18n.WINDOW.getString("Config.Label.yourAddress.text", Session.getEMail()));
		}
		// -- END USER CONFIG --

		ChangeListener changeListener = new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				reactToInput();
			}
		};

		// -- FONT CONFIG --
		// set font scaling as saved in config
		sldFontSize.setValue(Config.getFontScaling());
		sldFontSize.addChangeListener(changeListener);
		// -- Look And Feel --
		for (Enumeration<AbstractButton> btn = btnGroupLookAndFeel.getElements(); btn.hasMoreElements();) {
			final JRadioButton b = (JRadioButton) btn.nextElement();
			if (Config.getLookAndFeel() != null && !Config.getLookAndFeel().isEmpty()) {
				if (b.getToolTipText().equals(Config.getLookAndFeel())) {
					selectedLookAndFeel = b;
					b.setSelected(true);
				}
			}
			// check if we set from the config, if not fall back to system look and feel
			if (selectedLookAndFeel == null && b.getToolTipText().equals(UIManager.getSystemLookAndFeelClassName())) {
				selectedLookAndFeel = b;
				b.setSelected(true);
			}
			b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					selectedLookAndFeel = b;
					reactToInput();
				}
			});
		}
		if (selectedLookAndFeel == null) {
			// non-critical but log it anyways
			LOGGER.error("Failed to detect the current look & feel theme.");
		}

		// Transfer connection count
		sldConnections.setValue(Config.getTransferConnectionCount());
		sldConnections.addChangeListener(changeListener);

		// Language
		for (Language language : Language.values()) {
			if (language.value.equals(Config.getPreferredLanguage())) {
				cboLanguage.setSelectedItem(language);
				break;
			}
		}
		cboLanguage.addActionListener(this);

		// -- BOTTOM BUTTONS --
		btnSave.addActionListener(this);
		btnClose.addActionListener(this);
		// -- END BOTTOM BUTTONS --
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == btnProxyAuto || source == btnProxyHttp || source == btnProxyNone
				|| source == btnProxySocks || source == chkSendMeMail || source == cboLanguage) {
			reactToInput();
		}
		if (source == btnSave) {
			saveOptions();
		}
		if (source == btnClose) {
			dispose();
		}
	}

	private void reactToInput() {
		// our flag to en/disable the save button
		boolean changed = false;
		// first check our initial state
		ProxyMode initMode = Config.getProxyMode();
		int fontScaling = Config.getFontScaling();
		Language newLanguage = (Language)cboLanguage.getSelectedItem();

		// now check if the buttons represent another state
		if (btnProxyNone.isSelected() && initMode != ProxyMode.NONE) {
			changed = true;
		} else if (btnProxyAuto.isSelected() && initMode != ProxyMode.AUTO) {
			changed = true;
		} else if (chkSendMeMail.isEnabled() && chkSendMeMail.isSelected() != userConfig.emailNotifications) {
			changed = true;
		} else if ((int) sldFontSize.getValue() != fontScaling) {
			changed = true;
		} else if ((int) sldConnections.getValue() != Config.getTransferConnectionCount()) {
			changed = true;
		} else if (!selectedLookAndFeel.getToolTipText().equals(Config.getLookAndFeel())) {
			changed = true;
		} else if (!newLanguage.value.equals(Config.getPreferredLanguage())) {
			changed = true;
		}

		// en/disable the save button
		btnSave.setEnabled(changed);
	}

	private SatelliteUserConfig getSatUserConfig() {
		SatelliteUserConfig userConfig = null;
		// set mail me stuff as saved on the sat
		try {
			userConfig = ThriftManager.getSatClient().getUserConfig(Session.getSatelliteToken());
		} catch (TException e) {
			ThriftError.showMessage(this, LOGGER, e,
					I18n.WINDOW.getString("Config.Message.error.couldNotGetUserConfFromSat"));
		}
		return userConfig;
	}

	private boolean setSatUserConfig(SatelliteUserConfig config) {
		if (config == null)
			return false;
		// set mail me stuff as saved on the sat
		try {
			ThriftManager.getSatClient().setUserConfig(Session.getSatelliteToken(), config);
			userConfig = config;
		} catch (TException e) {
			ThriftError.showMessage(this, LOGGER, e,
					I18n.WINDOW.getString("Config.Message.error.couldNotSaveUserConfOnSat"));
			return false;
		}
		return true;
	}

	private void saveOptions() {
		boolean restartRequired = false;
		btnSave.setEnabled(false);
		// save proxy mode to config
		ProxyMode selectedMode = null;
		if (btnProxyAuto.isSelected())
			selectedMode = ProxyMode.AUTO;
		if (btnProxyNone.isSelected())
			selectedMode = ProxyMode.NONE;
		if (selectedMode != null) {
			restartRequired = restartRequired || Config.getProxyMode() != selectedMode;
			Config.setProxyMode(selectedMode);
		}

		// save font scaling to config
		int newFontScaling = (int) sldFontSize.getValue();
		restartRequired = restartRequired || Config.getFontScaling() != newFontScaling;
		Config.setFontScaling(newFontScaling);
		// transfers
		Config.setTransferConnectionCount((int) sldConnections.getValue());

		// save mail notification stuff to sat
		if (Session.getSatelliteToken() != null
				&& !setSatUserConfig(new SatelliteUserConfig(chkSendMeMail.isSelected()))) {
			// If saving failed, re-enable button
			btnSave.setEnabled(true);
		}

		// save LookAndFeel
		if (selectedLookAndFeel != null) {
			restartRequired = restartRequired || !selectedLookAndFeel.getToolTipText().equals(Config.getLookAndFeel());
			Config.setLookAndFeel(selectedLookAndFeel.getToolTipText());
		}

		// save language
		Language newLanguage = (Language)cboLanguage.getSelectedItem();
		restartRequired = restartRequired || !newLanguage.value.equals(Config.getPreferredLanguage());
		Config.setPreferredLanguage(newLanguage.value);

		if (restartRequired) {
			// let the user know he needs to restart for the changes to apply
			Gui.showMessageBox(this, I18n.WINDOW.getString("Config.Message.info.restartNeededToApplyChanges"),
					MessageType.INFO, null, null);
		}
	}

	public static boolean shouldBeShown() {
		return true;
	}

	public static void open(Window modalParent) {
		ConfigWindow win = new ConfigWindow(modalParent);
		MainWindow.centerShell(win);
		win.setVisible(true);
	}

	/* 
	 * UIFeedback Implementation
	 */
	@Override
	public boolean wantConfirmQuit() {
		return false;
	}

	@Override
	public void escapePressed() {
		dispose();
	}
}