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  		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  	 * (non-Javadoc)
80  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#addModifyListener(sk.uniba.euromath.editor.widgets.IModifyListener)
81  	 */
82  	public void addModifyListener(IModifyListener listener) {
83  		listeners.add(listener);
84  	}
85  	/*
86  	 * (non-Javadoc)
87  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#removeModifyListener(sk.uniba.euromath.editor.widgets.IModifyListener)
88  	 */
89  	public void removeModifyListener(IModifyListener listener) {
90  		listeners.remove(listener);
91  	}
92  	/*
93  	 * (non-Javadoc)
94  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#addMessageListener(sk.uniba.euromath.editor.widgets.IMessagesChangeListener)
95  	 */
96  	public void addMessageListener(IMessagesChangeListener listener) {
97  		msgListeners.add(listener);
98  	}
99  	/*
100 	 * (non-Javadoc)
101 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#removeMessageListener(sk.uniba.euromath.editor.widgets.IMessagesChangeListener)
102 	 */
103 	public void removeMessageListener(IMessagesChangeListener listener) {
104 		msgListeners.remove(listener);
105 	}
106 }