summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util
diff options
context:
space:
mode:
authorSimon Rettberg2018-06-20 17:01:59 +0200
committerSimon Rettberg2018-06-20 17:01:59 +0200
commite30422516ba4f22b1c5ff67c1f0e00521d602b62 (patch)
treeef5b16f3d76e25ea3ff336da990e84245933973c /dozentenmodul/src/main/java/org/openslx/dozmod/util
parent[server] Don't use shared instance in multi-threaded app (diff)
downloadtutor-module-e30422516ba4f22b1c5ff67c1f0e00521d602b62.tar.gz
tutor-module-e30422516ba4f22b1c5ff67c1f0e00521d602b62.tar.xz
tutor-module-e30422516ba4f22b1c5ff67c1f0e00521d602b62.zip
[client] Add GUI change handling classes
Code to track if a dialog contains modified controls has been messy, error prone and all over the place. Adding a control to a dialog required adding multiple fields to the dialog class and adding new code in multiple places. This is an approach to create a centralized facility that would only require adding all controls of a dialog to the monitor in one place, and then defining a callback to get informed when the validity or content of the whole form changes. The monitor class will also remember the original state of the dialog, so you can tell if the user undoes their changes manually.
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java32
1 files changed, 20 insertions, 12 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java
index 0e92bc6d..61133cd9 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java
@@ -12,24 +12,32 @@ public class MapHelper {
private MapHelper() {
}
- public static <T> boolean hasChanged(final Map<String, T> oldMap, final Map<String, T> newMap) {
+ public static <K, T> boolean hasChanged(final Map<K, T> oldMap, final Map<K, T> newMap) {
+ return compare(oldMap, newMap) != 0;
+ }
+
+ public static <K, T> int compare(final Map<K, T> oldMap, final Map<K, T> newMap) {
if (oldMap == null && newMap == null)
- return false;
- if ((oldMap == null && newMap != null) || (oldMap != null && newMap == null))
- return true;
- // build list of users that were added, if any return true
+ return 0;
+ if (oldMap == null)
+ return -1;
+ if (newMap == null)
+ return 1;
if (oldMap.size() != newMap.size())
- return true;
+ return oldMap.size() - newMap.size();
- // no changes in the users, lets check for changes in each users permissions
- for (Entry<String, T> entry : oldMap.entrySet()) {
+ // Same amount of entries, check if any entry changed
+ int retval = 0;
+ for (Entry<K, T> entry : oldMap.entrySet()) {
T current = entry.getValue();
T other = newMap.get(entry.getKey());
- if (!current.equals(other))
- return true;
+ if (other == null) {
+ retval -= 1;
+ continue;
+ }
+ retval += current.hashCode() - other.hashCode();
}
- // everything was the same if we are still here
- return false;
+ return retval;
}
}