summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java
blob: 5ae805db00f71a348aeef7c4e278e5704a1eb2aa (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package org.openslx.dozmod.gui.control;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.NetShare;
import org.openslx.bwlp.thrift.iface.NetShareAuth;
import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
import org.openslx.dozmod.gui.control.table.NetshareTable;
import org.openslx.dozmod.gui.control.table.QScrollPane;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.util.FormatHelper;

/**
 * Widget for network share configuration of lectures
 */
public class NetshareConfigurator extends NetshareConfiguratorLayout {

	private static final long serialVersionUID = -3336605759245603655L;
	private final static Logger LOGGER = Logger.getLogger(NetshareConfigurator.class);
	private List<NetShare> tblNetshareData = null;

	// mount points / win drive letters - ideally, we would check whether the image is linux or windows based
	// and either show a drive selection list like this one, or a textfield for a user-defined mount point...
	private Character[] mountPoints = { 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	public NetshareConfigurator() {

		super();

		tblNetshare.getModel().addTableModelListener(new TableModelListener() {
			@Override
			public void tableChanged(TableModelEvent e) {
				fireNetshareConfigurationChangeEvent(new NetshareConfigurationChangeEvent(NetshareConfigurator.this));
			}
		});

		tblNetshare.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				NetShare item = tblNetshare.getSelectedItem();
				// ugly block-wise sets, but only one test needed compared to
				// doing lots of item != null ? ... : ...
				if (item != null) {
					// share from the list is selected: fill bottom form and change "Add" to "Apply"
					btnDel.setEnabled(true);
					tfSharePath.setText(item.path);
					tfShareName.setText(item.displayname);
					tfUsername.setText(item.username);
					tfPassword.setText(item.password);
					cboNetshareAuth.setSelectedItem(item.auth);
					cboNetshareMountPoint.setSelectedItem(Character.valueOf(item.mountpoint.charAt(0)));
					btnAdd.setText("Ändern");
				} else {
					btnDel.setEnabled(false);
					tfSharePath.setText(null);
					tfShareName.setText(null);
					tfUsername.setText(null);
					tfPassword.setText(null);
					cboNetshareAuth.setSelectedItem(null);
					cboNetshareMountPoint.setSelectedItem(null);
					btnAdd.setText("Hinzufügen");
				}
			}
		});

		cboNetshareMountPoint.setModel(new DefaultComboBoxModel<Character>(mountPoints));
		cboNetshareMountPoint.setSelectedItem(null);

		// combobox for share authentication types
		cboNetshareAuth.setModel(new DefaultComboBoxModel<NetShareAuth>(NetShareAuth.values()));
		cboNetshareAuth.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				NetShareAuth selectedAuth = cboNetshareAuth.getItemAt(cboNetshareAuth.getSelectedIndex());
				if (selectedAuth == null)
					return;
				boolean activate = selectedAuth == NetShareAuth.OTHER_USER;
				// username field is needed to either special or guest user 
				tfUsername.setEnabled(activate);
				lblUsername.setEnabled(activate);
				tfPassword.setEnabled(activate);
				lblPassword.setEnabled(activate);
				chkShowPass.setEnabled(activate);
			}
		});
		cboNetshareAuth.setSelectedItem(null);

		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// check if we are editing an existing share entry or
				// creating a new one, check for input either way
				NetShare input = new NetShare();

				String inputShareName = tfShareName.getText();
				if (inputShareName.isEmpty()) {
					lblError.setText("Kein Anzeigename angegeben!");
					return;
				}
				input.displayname = inputShareName;

				NetShareAuth inputNetShareAuth = cboNetshareAuth.getItemAt(cboNetshareAuth.getSelectedIndex());
				if (inputNetShareAuth == null) {
					lblError.setText("Kein Authentifizierungstyp angegeben!");
					return;
				}
				input.auth = inputNetShareAuth;
				input.path = tfSharePath.getText();
				if (input.path == null || input.path.isEmpty()) {
					lblError.setText("Kein Pfad angegeben!");
					return;
				}
				switch (inputNetShareAuth) {
				case LOGIN_USER:
					// this uses the bwLehrpool client's logged in user
					// we don't need to have anything
					break;
				case OTHER_USER:
					// save given username/password
					input.username = tfUsername.getText();
					char[] inputPassword = tfPassword.getPassword();
					input.password = new String(inputPassword);
					Arrays.fill(inputPassword, '0'); // prolly not relevant/sufficiant...
					// if password is set but no username, error
					if (!input.password.isEmpty() && input.username.isEmpty()) {
						lblError.setText("Kein Nutzername angegeben!");
						return;
					}
					break;
				default:
					input = null;
					break;
				}
				if (input == null) {
					lblError.setText("Interner Fehler");
					LOGGER.debug("Bad input, aborting.");
					return;
				}
				// now check for optional mount path
				Character inputMountPoint = cboNetshareMountPoint.getItemAt(cboNetshareMountPoint.getSelectedIndex());
				if (inputMountPoint != null) {
					input.mountpoint = inputMountPoint.toString() + ":";
				}
				// now decide whether to create a new entry or update existing one
				NetShare oldEntry = tblNetshare.getSelectedItem();
				if (oldEntry != null) {
					// editing existing one, delete it from the internal data
					if (!tblNetshareData.remove(oldEntry)) {
						lblError.setText("Änderung fehlgeschlagen!");
						LOGGER.debug("Failed to remove selected share for replacement!");
						return;
					}
				}
				// either we delete the existing share from the data or we are
				// creating a new one, either way add it to the list and update
				// the table, if its not present already
				if (tblNetshareData.contains(input)) {
					lblError.setText("Existiert bereits!");
					LOGGER.error("Network share already in the list, aborting.");
					return;
				}
				lblError.setText(null);
				tblNetshareData.add(input);
				tblNetshare.getModel().setData(tblNetshareData);
				}
			}
		);

		btnDel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// try to delete the selected share
				NetShare selection = tblNetshare.getSelectedItem();
				if (selection == null) {
					return;
				}
				try {
					if (!tblNetshareData.remove(selection)) {
						// false if it was not found
						LOGGER.error("Could not remove non-existant network share '" + selection.toString()
								+ "' from the table data: " + tblNetshareData.toString());
						return;
					}
					// refresh table data
					tblNetshare.getModel().setData(tblNetshareData);
				} catch (Exception ex) {
					LOGGER.debug("Failed to remove " + selection.toString() + " from the table data.", ex);
					return;
				}
			}
		});

		chkShowPass.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() != ItemEvent.SELECTED) {
					tfPassword.setEchoChar('*');
				} else {
					tfPassword.setEchoChar((char) 0);
				}
			}
		});
		chkShowPass.setEnabled(false);
		tfUsername.setEnabled(false);
		lblUsername.setEnabled(false);
		tfPassword.setEnabled(false);
		lblPassword.setEnabled(false);
	}

	public List<NetShare> getState() {
		return tblNetshareData;
	}

	public boolean setState(List<NetShare> data) {
		if (data == null)
			return false;
		if (tblNetshareData == null) {
			tblNetshareData = new ArrayList<>();
		}
		tblNetshareData = data;
		tblNetshare.getModel().setData(tblNetshareData);
		return true;
	}

	protected EventListenerList listenerList = new EventListenerList();

	public class NetshareConfigurationChangeEvent extends EventObject {

		private static final long serialVersionUID = -511509960878320591L;

		public NetshareConfigurationChangeEvent(Object source) {
			super(source);
		}
	}

	public interface NetshareConfigurationChangeEventListener extends EventListener {
		public void stateChanged(NetshareConfigurationChangeEvent event);
	}

	public void addNetshareConfigurationChangeEventListener(NetshareConfigurationChangeEventListener listener) {
		listenerList.add(NetshareConfigurationChangeEventListener.class, listener);
	}

	public void removeNetshareConfigurationChangeEventListener(NetshareConfigurationChangeEventListener listener) {
		listenerList.remove(NetshareConfigurationChangeEventListener.class, listener);
	}

	void fireNetshareConfigurationChangeEvent(NetshareConfigurationChangeEvent evt) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = 0; i < listeners.length; i++) {
			if (listeners[i] == NetshareConfigurationChangeEventListener.class) {
				((NetshareConfigurationChangeEventListener) listeners[i + 1]).stateChanged(evt);
			}
		}
	}
}

/**
 * Internal layout class for this widget
 */
class NetshareConfiguratorLayout extends JPanel {

	private static final long serialVersionUID = 6479525981542743622L;

	private final static String txtNetshareDesc = "Hier können Sie Netzlaufwerke angeben,"
			+ " die automatisch beim Start der Veranstaltung eingebunden werden sollen.";

	protected QLabel lblShareAuth, lblSharePath, lblShareName, lblMountPoint, lblUsername, lblPassword, lblError;
	protected NetshareTable tblNetshare = new NetshareTable();
	protected JTextField tfSharePath, tfShareName, tfUsername;
	protected JPasswordField tfPassword;
	protected JButton btnAdd, btnDel;
	protected JCheckBox chkShowPass;
	protected ComboBox<NetShareAuth> cboNetshareAuth = new ComboBox<>(new ComboBoxRenderer<NetShareAuth>() {
		@Override
		public String renderItem(NetShareAuth item) {
			if (item == null)
				return null;
			return FormatHelper.netShareAuthName(item);
		}
	});
	protected ComboBox<Character> cboNetshareMountPoint = new ComboBox<>(new ComboBoxRenderer<Character>() {
		@Override
		public String renderItem(Character letter) {
			if (letter == null)
				return null;
			return letter.toString() + ":";
		}
	});

	public NetshareConfiguratorLayout() {
		GridManager grid = new GridManager(this, 5, true, new Insets(3, 3, 3, 3));
		// top info panel
		grid.add(new WordWrapLabel(txtNetshareDesc), 5).fill(true, false).expand(true, false);
		grid.nextRow();
		// middle netshare list
		grid.add(new QScrollPane(tblNetshare), 5).fill(true, true).expand(true, true);
		grid.nextRow();

		JPanel pnlNewShare = new JPanel();
		GridManager gridNewShare = new GridManager(pnlNewShare, 5, true);
		pnlNewShare.setBorder(BorderFactory.createTitledBorder("Details"));
		lblSharePath = new QLabel("Pfad");
		gridNewShare.add(lblSharePath);
		tfSharePath = new JTextField();
		gridNewShare.add(tfSharePath, 4).fill(true, false).expand(true, false);
		gridNewShare.nextRow();
		// bottom form to add a new share
		lblShareName = new QLabel("Anzeigename");
		lblMountPoint = new QLabel("Ziel");
		tfShareName = new JTextField();

		gridNewShare.add(lblShareName);
		gridNewShare.add(tfShareName, 2).fill(true, false).expand(true, false);
		gridNewShare.add(lblMountPoint);
		gridNewShare.add(cboNetshareMountPoint);
		gridNewShare.nextRow();
		lblShareAuth = new QLabel("Authentifizierung");
		gridNewShare.add(lblShareAuth);
		gridNewShare.add(cboNetshareAuth, 4).fill(true, false).expand(true, false);
		gridNewShare.nextRow();
		lblUsername = new QLabel("Username");
		gridNewShare.add(lblUsername);
		tfUsername = new JTextField(20);
		gridNewShare.add(tfUsername, 1).fill(true, false).expand(true, false);
		lblPassword = new QLabel("Passwort");
		gridNewShare.add(lblPassword);
		tfPassword = new JPasswordField(20);
		gridNewShare.add(tfPassword, 2).fill(true, false).expand(true, false);
		gridNewShare.nextRow();
		chkShowPass = new JCheckBox("Passwort anzeigen");
		JPanel pnlShowPass = new JPanel();
		pnlShowPass.setLayout(new BoxLayout(pnlShowPass, BoxLayout.LINE_AXIS));
		pnlShowPass.add(Box.createGlue());
		pnlShowPass.add(chkShowPass, BorderLayout.LINE_END);
		gridNewShare.add(pnlShowPass, 5).fill(true, false).expand(true, false);
		gridNewShare.nextRow();
		grid.add(pnlNewShare, 5).fill(true, false).expand(true, false);
		grid.nextRow();
		// bottom panel for right-aligned button...
		JPanel pnlButtonAdd = new JPanel();
		pnlButtonAdd.setLayout(new BoxLayout(pnlButtonAdd, BoxLayout.LINE_AXIS));
		btnAdd = new JButton("Hinzufügen");
		btnDel = new JButton("Entfernen");
		lblError = new QLabel("");
		lblError.setForeground(Color.RED);
		pnlButtonAdd.add(lblError);
		pnlButtonAdd.add(Box.createGlue());
		pnlButtonAdd.add(btnAdd, BorderLayout.LINE_END);
		pnlButtonAdd.add(btnDel, BorderLayout.LINE_END);
		grid.add(pnlButtonAdd, 5).fill(true, false).expand(true, false);
		grid.finish(false);
	}
}