diff options
author | Simon Rettberg | 2018-06-21 15:41:44 +0200 |
---|---|---|
committer | Simon Rettberg | 2018-06-21 15:41:44 +0200 |
commit | c0003a559a36dfca1bdc4add0034e67bd22824ed (patch) | |
tree | d0f03daa4eb8b94cbfb9472213a109eade52a0dc /dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java | |
parent | [client] Refactor change monitor classes, better error message handling (diff) | |
download | tutor-module-c0003a559a36dfca1bdc4add0034e67bd22824ed.tar.gz tutor-module-c0003a559a36dfca1bdc4add0034e67bd22824ed.tar.xz tutor-module-c0003a559a36dfca1bdc4add0034e67bd22824ed.zip |
[client] Sanitize class/var names, split up control package
All configurators have moved from *.control to *.configurator
*.control should be used for simple controls that feel like
they're really just one thing. The configurators are more like
a group of controls.
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java | 372 |
1 files changed, 372 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java new file mode 100644 index 00000000..6141992c --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/NetshareConfigurator.java @@ -0,0 +1,372 @@ +package org.openslx.dozmod.gui.configurator; + +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.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.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +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.Gui; +import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor; +import org.openslx.dozmod.gui.control.ComboBox; +import org.openslx.dozmod.gui.control.QLabel; +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.gui.helper.MessageType; +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> shareList = 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.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) { + clearInputFields(); + return; + } + // 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"); + } + }); + + 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(); + + input.path = tfSharePath.getText(); + if (input.path == null || input.path.isEmpty()) { + lblError.setText("Kein Pfad angegeben!"); + return; + } + 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; + 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(); + input.password = new String(tfPassword.getPassword()); + if (input.username.isEmpty()) { + lblError.setText("Kein Nutzername angegeben!"); + return; + } + break; + default: + input = null; + break; + } + if (input == null) { + lblError.setText("Fehlerhafte Eingabe"); + LOGGER.debug("Bad input, aborting."); + return; + } + // now check for optional mount path + Character inputMountPoint = cboNetshareMountPoint + .getItemAt(cboNetshareMountPoint.getSelectedIndex()); + if (inputMountPoint == null) { + lblError.setText("Kein Ziel angegeben!"); + return; + } + 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 (!shareList.remove(oldEntry)) { + lblError.setText("Änderung fehlgeschlagen!"); + LOGGER.debug("Failed to remove selected share for replacement: " + oldEntry); + return; + } + tblNetshare.setData(shareList, false); + } + // 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 (shareList.contains(input)) { + lblError.setText("Existiert bereits!"); + LOGGER.error("Network share already in the list, aborting."); + return; + } + // if a password is set, warn the user about its unsafe storage + // which we might want to implement one day... + if (input.password != null && !input.password.isEmpty()) { + if (!Gui.showMessageBox( + "Das eingebene Passwort wird im Klartext gespeichert " + + "und ist in der VM für jeden Nutzer sichtbar.\n" + + "Verwenden Sie auf keinen Fall sicherheitskritische Passwörter!" + + "\n\nMöchten Sie diesen Netzlaufwerk trotzdem hinzufügen?", + MessageType.QUESTION_YESNO, LOGGER, null)) { + return; + } + } + lblError.setText(null); + shareList.add(input); + tblNetshare.setData(shareList, false); + clearInputFields(); + } + }); + + 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 (!shareList.remove(selection)) { + // false if it was not found + LOGGER.error("Could not remove non-existant network share '" + selection.toString() + + "' from the table data: " + shareList.toString()); + return; + } + // refresh table data + tblNetshare.getModel().setData(shareList); + } 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); + } + + private void clearInputFields() { + 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"); + } + public List<NetShare> getState() { + return shareList; + } + + public boolean setState(List<NetShare> data) { + if (data == null) + return false; + if (shareList == null) { + shareList = new ArrayList<>(); + } + shareList = data; + tblNetshare.setData(shareList, false); + return true; + } + + public void addTableModelListener(TableModelListener listener) { + tblNetshare.getModel().addTableModelListener(listener); + } + + public void addToChangeMonitor(DialogChangeMonitor changeMonitor) { + changeMonitor.add(tblNetshare); + } +} + +/** + * Internal layout class for this widget + */ +class NetshareConfiguratorLayout extends JPanel { + + private static final long serialVersionUID = 6479525981542743622L; + + private final static String txtNetshareDesc = "<html>Hier können Sie Netzlaufwerke angeben," + + " die automatisch beim Start der Veranstaltung eingebunden werden sollen." + + " Der Platzhalter <em>%loginuser%</em> wird im Pfad durch den Loginnamen des Nutzers ersetzt.</html>"; + + protected final QLabel lblShareAuth, lblSharePath, lblShareName, lblMountPoint, lblUsername, lblPassword, + lblError; + protected final NetshareTable tblNetshare = new NetshareTable(); + protected final JTextField tfSharePath, tfShareName, tfUsername; + protected final JPasswordField tfPassword; + protected final JButton btnAdd, btnDel; + protected final JCheckBox chkShowPass; + protected final ComboBox<NetShareAuth> cboNetshareAuth = new ComboBox<>(new ComboBoxRenderer<NetShareAuth>() { + @Override + public String renderItem(NetShareAuth item) { + if (item == null) + return null; + return FormatHelper.netShareAuthName(item); + } + }); + protected final 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 JLabel(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, 6, true); + pnlNewShare.setBorder(BorderFactory.createTitledBorder("Details")); + lblSharePath = new QLabel("Pfad"); + gridNewShare.add(lblSharePath); + tfSharePath = new JTextField(); + gridNewShare.add(tfSharePath, 5).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(); + // extra panel for fancy layout purposes... + JPanel pnlShareName = new JPanel(); + pnlShareName.setLayout(new BoxLayout(pnlShareName, BoxLayout.LINE_AXIS)); + pnlShareName.add(lblShareName); + pnlShareName.add(tfShareName); + pnlShareName.add(lblMountPoint); + pnlShareName.add(cboNetshareMountPoint); + gridNewShare.add(lblShareName); + gridNewShare.add(pnlShareName, 5).fill(true, false).expand(true, false); + gridNewShare.nextRow(); + lblShareAuth = new QLabel("Authentifizierung"); + gridNewShare.add(lblShareAuth); + gridNewShare.add(cboNetshareAuth, 5).fill(true, false).expand(true, false); + gridNewShare.nextRow(); + lblUsername = new QLabel("Username"); + gridNewShare.add(lblUsername); + tfUsername = new JTextField(20); + gridNewShare.add(tfUsername, 2).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, 6).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); + } +} |