View Javadoc

1   /*
2    * Created on Mar 12, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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  		// fire the messages change listener if appropriate.
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  	 * (non-Javadoc)
82  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#addModifyListener(sk.uniba.euromath.editor.widgets.IModifyListener)
83  	 */
84  	public void addModifyListener(IModifyListener listener) {
85  		listeners.add(listener);
86  	}
87  	/*
88  	 * (non-Javadoc)
89  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#removeModifyListener(sk.uniba.euromath.editor.widgets.IModifyListener)
90  	 */
91  	public void removeModifyListener(IModifyListener listener) {
92  		listeners.remove(listener);
93  	}
94  	/*
95  	 * (non-Javadoc)
96  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#addMessageListener(sk.uniba.euromath.editor.widgets.IMessagesChangeListener)
97  	 */
98  	public void addMessageListener(IMessagesChangeListener listener) {
99  		msgListeners.add(listener);
100 	}
101 	/*
102 	 * (non-Javadoc)
103 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#removeMessageListener(sk.uniba.euromath.editor.widgets.IMessagesChangeListener)
104 	 */
105 	public void removeMessageListener(IMessagesChangeListener listener) {
106 		msgListeners.remove(listener);
107 	}
108 }