summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java
blob: 9b475bcbcc97d78c68631f01200d9188ce951a2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.openslx.dozmod.gui.changemonitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor.ValidationConstraint;

/**
 * Control/element wrapper
 */
public abstract class AbstractControlWrapper<T> {
	private final DialogChangeMonitor dcm;
	protected boolean wasEverChanged;
	protected boolean isCurrentlyChanged;
	private T originalContent;
	private List<ValidationConstraint<T>> constraints;
	private final Comparator<T> cmp;
	protected String currentError = null;
	/**
	 * If muted, this wrapper will not react to changes of the underlying
	 * control.
	 */
	private boolean muted = false;

	protected AbstractControlWrapper(DialogChangeMonitor dcm, Comparator<T> comp) {
		this.dcm = dcm;
		this.cmp = comp;
	}

	public boolean hasChangedSinceInit() {
		return wasEverChanged;
	}

	public boolean isCurrentlyChanged() {
		return isCurrentlyChanged;
	}

	/**
	 * Returns the current error message for the first failed constraint. If no
	 * constraint currently yields invalid, null is returned.
	 * 
	 * @return
	 */
	public String currentConstraintError() {
		return currentError;
	}
	
	public AbstractControlWrapper<T> addConstraint(ValidationConstraint<T> constraint) {
		if (constraints == null) {
			constraints = new ArrayList<>();
		}
		constraints.add(constraint);
		return this;
	}

	/**
	 * Resets the change state of this control
	 * and removes the "muted" flag, if set.
	 */
	public void reset() {
		boolean wasChanged = wasEverChanged;
		resetChangeState();
		if (wasChanged) {
			// It's possible that the global change state changed
			dcm.contentChanged(this);
		}
	}
	
	protected final void resetChangeState() {
		muted = false;
		isCurrentlyChanged = wasEverChanged = false;
		originalContent = getCurrentValue();
		checkValid(originalContent);
	}

	/**
	 * To be called by the implementation whenever the content of the control
	 * changed and according checks should be triggered.
	 */
	protected final void contentChanged() {
		T text = getCurrentValue();
		checkChanged(text);
		checkValid(text);
	}

	/**
	 * Ignore any changes, don't trigger callbacks regarding
	 * this component. This flag can be removed by calling
	 * reset() on this ControlWrapper or the enclosing
	 * DialogChangeMonitor.
	 */
	public void mute() {
		muted = true;
	}

	/**
	 * Method MUST return the current value of the monitored component, so
	 * that the current state can be considered as the original unmodified
	 * state.
	 */
	abstract T getCurrentValue();

	protected void checkChanged(T newContent) {
		if (muted)
			return;
		final boolean changed;
		if (newContent == originalContent) {
			changed = false; // This also covers null == null
		} else if (newContent == null || originalContent == null) {
			changed = true; // So here we know either/or is null, not both
		} else if (newContent instanceof Collection
				&& ((Collection<?>) newContent).size() != ((Collection<?>) originalContent).size()) {
			changed = true;
		} else if (newContent instanceof Map
				&& ((Map<?, ?>) newContent).size() != ((Map<?, ?>) originalContent).size()) {
			changed = true;
		} else if (cmp == null) {
			changed = !originalContent.equals(newContent);
		} else {
			// User supplied comparator
			changed = cmp.compare(originalContent, newContent) != 0;
		}
		if (isCurrentlyChanged != changed) {
			isCurrentlyChanged = changed;
			dcm.contentChanged(this);
		}
	}

	protected void checkValid(T text) {
		if (muted || constraints == null || constraints.isEmpty())
			return;
		String error = null;
		for (ValidationConstraint<T> i : constraints) {
			error = i.checkStateValid(text);
			if (error != null)
				break;
		}
		if ((error == null && currentError != null) || (error != null && currentError == null)
				|| (error != null && !error.equals(currentError))) {
			currentError = error;
			dcm.validityChanged(this);
		}
	}

}