diff options
author | Jonathan Bauer | 2017-09-06 13:48:33 +0200 |
---|---|---|
committer | Jonathan Bauer | 2017-09-06 13:48:33 +0200 |
commit | e310fa0738149f20b9de6b173d3d175857b0c748 (patch) | |
tree | 5857811d25cc53c51590cc0158ec84e049a3ed88 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java | |
parent | [client] Update apache httpclient, pass custom client to ecp-client-lean (diff) | |
download | tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.tar.gz tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.tar.xz tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.zip |
[client/server] network share feature [WIP]
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java | 330 |
1 files changed, 330 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java new file mode 100644 index 00000000..ac10e2bb --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/NetshareConfigurator.java @@ -0,0 +1,330 @@ +package org.openslx.dozmod.gui.control; + +import java.awt.BorderLayout; +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 netrules 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; + + public NetshareConfigurator() { + super(); + tblNetshare.getModel().addTableModelListener(new TableModelListener() { + @Override + public void tableChanged(TableModelEvent e) { + fireNetshareConfigurationChangeEvent(new NetshareConfigurationChangeEvent(NetshareConfigurator.this)); + } + }); + + // 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()); + boolean activate = selectedAuth == NetShareAuth.SPECIAL_USER; + // username field is needed to either special or guest user + tfUsername.setEnabled(activate || selectedAuth == NetShareAuth.GUEST_USER); + lblUsername.setEnabled(activate || selectedAuth == NetShareAuth.GUEST_USER); + tfPassword.setEnabled(activate); + lblPassword.setEnabled(activate); + chkShowPass.setEnabled(activate); + } + }); + + tfUsername.setEnabled(false); + lblUsername.setEnabled(false); + tfPassword.setEnabled(false); + lblPassword.setEnabled(false); + chkShowPass.setEnabled(false); + 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(); + NetShareAuth inputNetShareAuth = cboNetshareAuth.getItemAt(cboNetshareAuth.getSelectedIndex()); + if (inputNetShareAuth == null) { + LOGGER.error("Could not get the network share auth type from the combobox!"); + return; + } + input.auth = inputNetShareAuth; + input.path = tfSharePath.getText(); + if (input.path == null || input.path.isEmpty()) { + LOGGER.error("Missing/empty network share URI!"); + return; + } + switch (inputNetShareAuth) { + case GUEST_USER: + // guest mode requires guest account but not password + input.username = tfUsername.getText(); + break; + case LOGIN_USER: + break; + case SPECIAL_USER: + // save given username/password + input.username = tfUsername.getText(); + char[] inputPassword = tfPassword.getPassword(); + input.password = new String(inputPassword); + Arrays.fill(inputPassword, '0'); + break; + default: + input = null; + break; + } + if (input == null) { + LOGGER.debug("Input bad, aborting."); + return; + } + + // now decide whether to create a new entry or update existing one + if (tblNetshare.getSelectedItem() != null) { + // editing existing one, delete it from the internal data + if (!tblNetshareData.remove(tblNetshare.getSelectedItem())) { + 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)) { + LOGGER.error("Network share already in the list, aborting."); + return; + } + 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); + } + } + }); + + 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); + tfUsername.setText(item.username); + tfPassword.setText(item.password); + cboNetshareAuth.setSelectedItem(item.auth); + btnAdd.setText("Ändern"); + } else { + btnDel.setEnabled(false); + tfSharePath.setText(null); + tfUsername.setText(null); + tfPassword.setText(null); + cboNetshareAuth.setSelectedItem(null); + btnAdd.setText("Hinzufügen"); + } + } + }); + } + + 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, lblUsername, lblPassword; + + protected NetshareTable tblNetshare = new NetshareTable(); + protected JTextField tfSharePath, 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); + } + }); + + + + 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 pnlButtonDelete = new JPanel(); + pnlButtonDelete.setLayout(new BoxLayout(pnlButtonDelete, BoxLayout.LINE_AXIS)); + btnDel = new JButton("Entfernen"); + pnlButtonDelete.add(Box.createGlue()); + pnlButtonDelete.add(btnDel, BorderLayout.LINE_END); + grid.add(pnlButtonDelete, 5).fill(true, false).expand(true, false); + grid.nextRow(); + JPanel pnlNewShare = new JPanel(); + GridManager gridNewShare = new GridManager(pnlNewShare, 5, true); + pnlNewShare.setBorder(BorderFactory.createTitledBorder("Neues Netzlaufwerk")); + // bottom form to add a new share + lblShareAuth = new QLabel("Authentifizierung"); + gridNewShare.add(lblShareAuth); + gridNewShare.add(cboNetshareAuth, 4).fill(true, false).expand(true, false); // User (optional) + gridNewShare.nextRow(); + lblSharePath = new QLabel("URI"); + gridNewShare.add(lblSharePath); + tfSharePath = new JTextField(); + gridNewShare.add(tfSharePath, 4).fill(true, false).expand(true, false); // User (optional) + gridNewShare.nextRow(); + lblUsername = new QLabel("Username"); + gridNewShare.add(lblUsername); + tfUsername = new JTextField(); + gridNewShare.add(tfUsername, 1).fill(true, false).expand(true, false); // Password (optional) + lblPassword = new QLabel("Passwort:"); + gridNewShare.add(lblPassword); + tfPassword = new JPasswordField(); + gridNewShare.add(tfPassword, 2).fill(true, false).expand(true, false); // Password (optional) + gridNewShare.nextRow(); + chkShowPass = new JCheckBox("Passwort anzeigen"); + JPanel pnlHidePass = new JPanel(); + pnlHidePass.setLayout(new BoxLayout(pnlHidePass, BoxLayout.LINE_AXIS)); + pnlHidePass.add(Box.createGlue()); + pnlHidePass.add(chkShowPass, BorderLayout.LINE_END); + gridNewShare.add(pnlHidePass, 5).fill(true, false).expand(true, false); + gridNewShare.nextRow(); + grid.add(pnlNewShare, 5).fill(true, false).expand(true, false); + grid.nextRow(); + // bottom panels for right-aligned button... + JPanel pnlButtonAdd = new JPanel(); + pnlButtonAdd.setLayout(new BoxLayout(pnlButtonAdd, BoxLayout.LINE_AXIS)); + btnAdd = new JButton("Hinzufügen"); + pnlButtonAdd.add(Box.createGlue()); + pnlButtonAdd.add(btnAdd, BorderLayout.LINE_END); + grid.add(pnlButtonAdd, 5).fill(true, false).expand(true, false); + grid.finish(false); + } +} |