diff options
author | Jonathan Bauer | 2016-03-15 14:08:22 +0100 |
---|---|---|
committer | Jonathan Bauer | 2016-03-15 14:08:22 +0100 |
commit | 400fc72fc9d606e56a1d20efba37514b556aed30 (patch) | |
tree | 6fc23ba996283b952c5170d5647396506c37a777 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java | |
parent | [client] fix '.part' not beeing removed from TransferPanel when the transfer ... (diff) | |
download | tutor-module-400fc72fc9d606e56a1d20efba37514b556aed30.tar.gz tutor-module-400fc72fc9d606e56a1d20efba37514b556aed30.tar.xz tutor-module-400fc72fc9d606e56a1d20efba37514b556aed30.zip |
[client] reworked lecture details window
now uses tabs for subsets of options - clearing up the gui a bit
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java | 139 |
1 files changed, 106 insertions, 33 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java index 4f66e54d..ff1327bb 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/AdvancedConfigurator.java @@ -5,6 +5,8 @@ import java.awt.Insets; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.EventListener; +import java.util.EventObject; import java.util.Iterator; import java.util.List; @@ -13,8 +15,8 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextPane; +import javax.swing.event.EventListenerList; import javax.swing.text.BadLocationException; -import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; @@ -25,6 +27,7 @@ import org.openslx.bwlp.thrift.iface.NetRule; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.helper.GridManager; import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.dozmod.gui.helper.TextChangeListener; /** * Widget for advanced configuration options for lectures. This handles @@ -35,7 +38,8 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { private static final long serialVersionUID = -3497629601818983994L; private final static Logger LOGGER = Logger .getLogger(AdvancedConfigurator.class); - + private String originalRawRuleText = null; + private String originalRunScript = null; /** * Character defining how the rules are parsed, e.g. for whitespace \\s * Example: "8.8.8.8 80 in" would be split in -hostname "8.8.8.8" -port "80" @@ -45,11 +49,28 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { public AdvancedConfigurator() { super(); + + final TextChangeListener docListener = new TextChangeListener() { + @Override + public void changed() { + fireAdvancedConfigurationChangeEvent(new AdvancedConfigurationChangeEvent( + new Object())); + } + }; + tpNetworkRules.getDocument().addDocumentListener(docListener); + taRunScript.getDocument().addDocumentListener(docListener); + + } + + public boolean hasChanged() { + return !originalRawRuleText.equalsIgnoreCase(tpNetworkRules.getText()) + || !originalRunScript.equalsIgnoreCase(taRunScript.getText()); } /** - * Gets the state of the widget. This will first try to parse the tpNetworkRules - * and taRunScript and build the corresponding AdvancedConfiguration Object returned. + * Gets the state of the widget. This will first try to parse the + * tpNetworkRules and taRunScript and build the corresponding + * AdvancedConfiguration Object returned. * * @return advanced configuration object composed of the parsed network * rules as List<NetRule> and the raw runscript text as String @@ -58,10 +79,6 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { public AdvancedConfiguration getState() { // cleanup the TextPane for network rules if needed String input = tpNetworkRules.getText().trim(); - input = input.replaceAll("(?m)^\\s*", ""); - input = input.replaceAll("(?m)\\s*$", ""); - tpNetworkRules.setText(input); - List<NetRule> rules = parseNetRules(input); if (rules != null) { return new AdvancedConfiguration(rules, taRunScript.getText()); @@ -70,20 +87,25 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { } /** - * Sets the state of this widget to the given AdvancedConfiguration. Basicly this sets - * the content of the text areas to the corresponding network rules/runscript as given - * by the AdvancedConfiguration object - * @param config AdvancedConfiguration to set the state to + * Sets the state of this widget to the given AdvancedConfiguration. Basicly + * this sets the content of the text areas to the corresponding network + * rules/runscript as given by the AdvancedConfiguration object + * + * @param config + * AdvancedConfiguration to set the state to */ public void setState(final AdvancedConfiguration config) { // setText() blanks the text area if null is given, so no null checks - this.tpNetworkRules.setText(decodeNetRulesToText(config.netRulesList)); - this.taRunScript.setText(config.runScriptText); + originalRawRuleText = decodeNetRulesToText(config.netRulesList); + originalRunScript = config.runScriptText != null ? config.runScriptText : ""; + this.tpNetworkRules.setText(originalRawRuleText); + this.taRunScript.setText(originalRunScript); } /** - * "Decodes" the given list of NetRule to a single String. This should be used - * to set the text in the TextPane for the network rules + * "Decodes" the given list of NetRule to a single String. This should be + * used to set the text in the TextPane for the network rules + * * @param netRulesList * list of NetRule to decode * @return String representation of the list of rules @@ -107,7 +129,8 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { currentLine += currentRule.host + " "; currentLine += currentRule.port + " "; currentLine += currentRule.direction.name(); - decodedRules += currentLine + System.lineSeparator(); + decodedRules += currentLine + + (it.hasNext() ? System.lineSeparator() : ""); } return decodedRules; } @@ -129,6 +152,9 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { if (rawNetRules.isEmpty()) { return rulesList; } + // prune the text first + String prunedRawNetRules = rawNetRules.replaceAll("(?m)^\\s*", ""); + prunedRawNetRules = prunedRawNetRules.replaceAll("(?m)\\s*$", ""); // split it line by line final String[] netRules = rawNetRules.split("[" @@ -141,12 +167,12 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { String[] fields = ruleLine.split(FIELD_DELIMITER); if (fields.length != 3) { markText(ruleLine, Color.RED); - // log numbers for fields independently... + // log numbers for fields independently... LOGGER.debug("Invalid number of fields! Expected 3, got: " + fields.length); Gui.showMessageBox( - "Ungültige Syntax: Nutzen Sie: <host> <port> [in|out|in/out]", - MessageType.ERROR, LOGGER, null); + "Ungültige Syntax: Nutzen Sie: <host> <port> [in|out|in/out]", + MessageType.ERROR, LOGGER, null); break; } @@ -159,8 +185,8 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { LOGGER.debug("Invalid net direction! Expected 'in' or out'. Got: " + ruleDirection); Gui.showMessageBox( - "Ungültige Richtung: nur 'in', 'out', 'in/out' und 'out/in' erlaubt!", - MessageType.ERROR, LOGGER, null); + "Ungültige Richtung: nur 'in', 'out', 'in/out' und 'out/in' erlaubt!", + MessageType.ERROR, LOGGER, null); break; } // check port: accept if > -2 and < 65535 @@ -176,8 +202,8 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { markText(ruleLine, Color.RED); LOGGER.debug("Invalid port number! Got: " + port); Gui.showMessageBox( - "Ungültiges Port! Muss zwischen 0 und 65536 sein.", - MessageType.ERROR, LOGGER, null); + "Ungültiges Port! Muss zwischen 0 und 65536 sein.", + MessageType.ERROR, LOGGER, null); break; } // check hostname: bit more to do here @@ -203,11 +229,11 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { LOGGER.debug("Invalid host/IP! Got: " + fields[0]); if (checkHostnameSimple(fields[0])) { markText(ruleLine, Color.ORANGE); - if (!Gui.showMessageBox( - "Konnte '" - + fields[0] - + "' nicht verifizieren. Wollen Sie es trotzdem verwenden?", - MessageType.WARNING_RETRY, LOGGER, null)) { + if (!Gui.showMessageBox( + "Konnte '" + + fields[0] + + "' nicht verifizieren. Wollen Sie es trotzdem verwenden?", + MessageType.WARNING_RETRY, LOGGER, null)) { break; } else { // keep it @@ -224,6 +250,9 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { .toUpperCase()), fields[0], port)); } if (netRules.length == rulesList.size()) { + // pruned rules were successfully parsed so they are valid: set the textpane to it + tpNetworkRules.setText(prunedRawNetRules); + LOGGER.debug("Success"); return rulesList; } return null; @@ -286,7 +315,6 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { // TODO still buggy with text colors: the marking works fine // but when trying to input new text, funny things happen private void markText(final String txt, final Color color) { - MutableAttributeSet origSet = tpNetworkRules.getInputAttributes(); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, color); StyleConstants.setBold(set, color == Color.red); @@ -307,9 +335,12 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { "Failed to set '" + txt + "' to color " + color.toString(), e); } - // resetting the char attr to what they were before (buggy) - tpNetworkRules.setCharacterAttributes(origSet, true); + // resetting the char attr to what they were before (buggy) + tpNetworkRules.setStyledDocument(doc); + StyleConstants.setForeground(set, Color.WHITE); + StyleConstants.setBold(set, false); + tpNetworkRules.setCharacterAttributes(set, true); } /** @@ -328,6 +359,49 @@ public class AdvancedConfigurator extends AdvancedConfiguratorLayout { this.runScriptText = runScriptText; } } + + /** + * Custom event mechanism to detect changes to the user list (Mostly needed + * for the reactToChange() stuff in LectureDetailsWindow) + */ + protected EventListenerList listenerList = new EventListenerList(); + + public class AdvancedConfigurationChangeEvent extends EventObject { + + private static final long serialVersionUID = -8779550754760035845L; + + public AdvancedConfigurationChangeEvent(Object source) { + super(source); + } + } + + public interface AdvancedConfigurationChangeEventListener extends + EventListener { + public void stateChanged(AdvancedConfigurationChangeEvent event); + } + + public void addAdvancedConfigurationChangeEventListener( + AdvancedConfigurationChangeEventListener listener) { + listenerList.add(AdvancedConfigurationChangeEventListener.class, + listener); + } + + public void removeAdvancedConfigurationChangeEventListener( + AdvancedConfigurationChangeEventListener listener) { + listenerList.remove(AdvancedConfigurationChangeEventListener.class, + listener); + } + + void fireAdvancedConfigurationChangeEvent( + AdvancedConfigurationChangeEvent evt) { + Object[] listeners = listenerList.getListenerList(); + for (int i = 0; i < listeners.length; i++) { + if (listeners[i] == AdvancedConfigurationChangeEventListener.class) { + ((AdvancedConfigurationChangeEventListener) listeners[i + 1]) + .stateChanged(evt); + } + } + } } /** @@ -398,6 +472,5 @@ class AdvancedConfiguratorLayout extends JPanel { grid.nextRow(); grid.add(pnlRunScript).fill(true, true).expand(true, true); grid.finish(false); - } } |