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);
}
}