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 if (msgListeners.size() != 0) {
49 final ValidityMessages newMessages = getMessages();
50 if (!ValidityMessages.equalsTopLevelMsg(lastMessages, newMessages)) {
51 lastMessages = new ValidityMessages(newMessages);
52 for (final IMessagesChangeListener listener : msgListeners) {
53 listener.messagesChanged(newMessages);
54 }
55 }
56 }
57 }
58 /***
59 * Last known validity messages.
60 */
61 private ValidityMessages lastMessages = null;
62 /***
63 * Suppresses the data-modify event. This method can be called multiple
64 * times. To allow data-modify event to be fired, each call to
65 * <code>suppressModifyEvent()</code> must have
66 * <code>allowModifyEvent()</code> called.
67 */
68 protected final void suppressModifyEvent() {
69 suppressModifyEvent++;
70 }
71 /***
72 * Allows the data-modify event to be called. The data-modify event may
73 * still be left suppressed, depending on the number of previous
74 * <code>suppressModifyEvent()</code> calls.
75 */
76 protected final void allowModifyEvent() {
77 if (suppressModifyEvent > 0)
78 suppressModifyEvent--;
79 }
80
81
82
83
84 public void addModifyListener(IModifyListener listener) {
85 listeners.add(listener);
86 }
87
88
89
90
91 public void removeModifyListener(IModifyListener listener) {
92 listeners.remove(listener);
93 }
94
95
96
97
98 public void addMessageListener(IMessagesChangeListener listener) {
99 msgListeners.add(listener);
100 }
101
102
103
104
105 public void removeMessageListener(IMessagesChangeListener listener) {
106 msgListeners.remove(listener);
107 }
108 }