001    package org.LiveGraph.settings;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    /**
007     * Base class for all settings classes which can hadle observers to listen to
008     * settings events. This class handles all observer and event handling.
009     * 
010     * <p style="font-size:smaller;">This product includes software developed by the
011     *    <strong>LiveGraph</strong> project and its contributors.<br />
012     *    (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>)<br />
013     *    Copyright (c) 2007 G. Paperin.<br />
014     *    All rights reserved.
015     * </p>
016     * <p style="font-size:smaller;">File: ObservableSettings.java</p> 
017     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
018     *    without modification, are permitted provided that the following terms and conditions are met:
019     * </p>
020     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
021     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
022     *    this list of conditions and the following disclaimer.<br />
023     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
024     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
025     *    and the following disclaimer in the documentation and/or other materials provided with
026     *    the distribution.<br />
027     *    3. All advertising materials mentioning features or use of this software or any derived
028     *    software must display the following acknowledgement:<br />
029     *    <em>This product includes software developed by the LiveGraph project and its
030     *    contributors.<br />(http://www.live-graph.org)</em><br />
031     *    4. All advertising materials distributed in form of HTML pages or any other technology
032     *    permitting active hyper-links that mention features or use of this software or any
033     *    derived software must display the acknowledgment specified in condition 3 of this
034     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
035     *    homepage (http://www.live-graph.org).
036     * </p>
037     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
038     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
039     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
040     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
041     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
042     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
043     * </p>
044     * 
045     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
046     * @version {@value org.LiveGraph.LiveGraph#version}
047     */
048    public class ObservableSettings {
049    
050    private List<SettingsObserver> observers = new ArrayList<SettingsObserver>(); 
051    
052    public boolean addObserver(SettingsObserver observer) {
053            if (hasObserver(observer))
054                    return false;
055            return observers.add(observer);
056    }
057    
058    public boolean hasObserver(SettingsObserver observer) {
059            return observers.contains(observer);    
060    }
061    
062    public boolean removeObserver(SettingsObserver observer) {
063            return observers.remove(observer);
064    }
065    
066    public int countObservers() {
067            return observers.size();
068    }
069    
070    public void notifyObservers(Object info) {
071            ErrorWhileSettingHasChangedProcessingException err = null;
072            for (SettingsObserver observer : observers) {
073                    try {
074                            observer.settingHasChanged(this, info);
075                    } catch (ErrorWhileSettingHasChangedProcessingException e) {
076                            if (!e.getDontBreakObserverNotification())
077                                    throw e;
078                            if (null == err)
079                                    err = e;
080                    }
081            }
082            if (null != err)
083                    throw err;
084    }
085    
086    public void notifyObservers() {
087            notifyObservers(null);
088    }
089    
090    }