1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.widgets;
13 import java.util.HashSet;
14 import java.util.Set;
15 /***
16 * Abstract implementation of widget.
17 * @author Martin Vysny
18 */
19 public abstract class AbstractUserInputWidget implements IUserInputWidget {
20 /***
21 * Constructor.
22 */
23 protected AbstractUserInputWidget() {
24 super();
25 }
26 /***
27 * Modify listeners.
28 */
29 protected final Set<IModifyListener> listeners = new HashSet<IModifyListener>();
30 /***
31 * Message change listeners.
32 */
33 protected final Set<IMessagesChangeListener> msgListeners = new HashSet<IMessagesChangeListener>();
34 /***
35 * If non-zero, data-modify event is not fired.
36 */
37 private int suppressModifyEvent = 0;
38 /***
39 * Calls all modify listeners.
40 */
41 protected void fireDataModified() {
42 if (suppressModifyEvent == 0) {
43 for (final IModifyListener listener : listeners) {
44 listener.dataModified();
45 }
46 }
47
48 final ValidityMessages newMessages = getMessages();
49 if (!ValidityMessages.equalsTopLevelMsg(lastMessages, newMessages)) {
50 lastMessages = new ValidityMessages(newMessages);
51 for (final IMessagesChangeListener listener : msgListeners) {
52 listener.messagesChanged(newMessages);
53 }
54 }
55 }
56 /***
57 * Last known validity messages.
58 */
59 private ValidityMessages lastMessages = null;
60 /***
61 * Suppresses the data-modify event. This method can be called multiple
62 * times. To allow data-modify event to be fired, each call to
63 * <code>suppressModifyEvent()</code> must have
64 * <code>allowModifyEvent()</code> called.
65 */
66 protected final void suppressModifyEvent() {
67 suppressModifyEvent++;
68 }
69 /***
70 * Allows the data-modify event to be called. The data-modify event may
71 * still be left suppressed, depending on the number of previous
72 * <code>suppressModifyEvent()</code> calls.
73 */
74 protected final void allowModifyEvent() {
75 if (suppressModifyEvent > 0)
76 suppressModifyEvent--;
77 }
78
79
80
81
82 public void addModifyListener(IModifyListener listener) {
83 listeners.add(listener);
84 }
85
86
87
88
89 public void removeModifyListener(IModifyListener listener) {
90 listeners.remove(listener);
91 }
92
93
94
95
96 public void addMessageListener(IMessagesChangeListener listener) {
97 msgListeners.add(listener);
98 }
99
100
101
102
103 public void removeMessageListener(IMessagesChangeListener listener) {
104 msgListeners.remove(listener);
105 }
106 }