package org.helioviewer.jhv.gui;

import java.util.AbstractList;
import java.util.ConcurrentModificationException;
import java.util.LinkedList;

import org.helioviewer.base.logging.Log;
import org.helioviewer.viewmodel.changeevent.ChangeEvent;
import org.helioviewer.viewmodel.changeevent.ViewChainChangedReason;
import org.helioviewer.viewmodel.view.View;
import org.helioviewer.viewmodel.view.ViewListener;

/**
 * This class distributes changes from the associated view chain to all
 * listeners which are part of the GUI.
 * 
 * Sometime components or parts of the GUI have to react on changes which
 * belongs to the view chain. The view listener distributer acts as a
 * registration point for these components.
 * <p>
 * It is guaranteed when something changed to the topmost view of the view chain
 * the view listener distributer will recognize it. There is no guaranteed for
 * that a component which registers it as view listener at a view itself
 * recognize changes to the view chain.
 * 
 * @author Markus Langenberg
 */
public class ViewListenerDistributor implements ViewListener {

    private static ViewListenerDistributor singletonObject = new ViewListenerDistributor();
    private View view;
    private AbstractList<ViewListener> listeners = new LinkedList<ViewListener>();

    /**
     * Private default constructor to implement the singleton pattern.
     */
    private ViewListenerDistributor() {
    }

    /**
     * Returns the only instance of this class.
     * 
     * @return the only instance of this class.
     */
    public static ViewListenerDistributor getSingletonInstance() {
        return singletonObject;
    }

    /**
     * Sets the view where to listen for changes.
     * 
     * This changes will be distributed to all listeners of the distributer.
     * 
     * @param newView
     *            view where to listen for changes.
     */
    public void setView(View newView) {
        if (view != null) {
            view.removeViewListener(this);
        }

        view = newView;

        if (newView != null) {
            view.addViewListener(this);
        }

        viewChanged(null, new ChangeEvent(new ViewChainChangedReason(null)));
    }

    /**
     * Adds a view listener.
     * 
     * This listener will be called on every change from views deeper in the
     * view chain.
     * 
     * @param l
     *            the listener to add
     * @see #removeViewListener(ViewListener)
     */
    public void addViewListener(ViewListener l) {
        synchronized (listeners) {
            listeners.add(l);
        }
    }

    /**
     * Removes a view listener.
     * 
     * The listener no longer will be informed about changes from views deeper
     * in the view chain.
     * 
     * @param l
     *            the listener to remove
     * @see #addViewListener(ViewListener)
     */
    public void removeViewListener(ViewListener l) {

        synchronized (listeners) {
            listeners.remove(l);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void viewChanged(View sender, ChangeEvent aEvent) {
        notifyViewListener(sender, aEvent);
    }

    /**
     * Informs all listeners of changes made to previous views.
     * 
     * For the reason that this is just a distributor it forwards the events and
     * the sender.
     * 
     * @param sender
     *            last view which sent the change event or forwarded it.
     * @param event
     *            event which contains the associated reasons.
     */
    private void notifyViewListener(View sender, ChangeEvent event) {
        try {
            synchronized (listeners) {
                for (ViewListener listener : listeners) {
                    listener.viewChanged(sender, event);
                }
            }
        } catch (ConcurrentModificationException e) {
            e.printStackTrace();
        }
    }

    class ViewChangedNotifier implements Runnable {
        private boolean interrupted = false;

        public void interrupt() {
            interrupted = true;
        }

        public void run() {
            synchronized (this) {
                if (interrupted)
                    return;
                this.notify();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    Log.error("", e);
                }
            }
        }

    }

}

@@ -79,68 +79,76 @@
 til.
-ConcurrentModificationException;%0Aimport java.util.LinkedList
+LinkedList;%0Aimport java.util.concurrent.locks.ReentrantReadWriteLock
 ;%0A%0Ai
@@ -1215,16 +1215,17 @@
 w view;%0A
+%0A
     priv
@@ -1228,16 +1228,99 @@
 private 
+final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();%0A    private final 
 Abstract
@@ -2118,25 +2118,24 @@
  = newView;%0A
-%0A
         if (
@@ -2608,62 +2608,131 @@
     
-synchronized (listeners) %7B%0A            listeners.add(l
+rwl.writeLock().lock();%0A        try %7B%0A            listeners.add(l);%0A        %7D finally %7B%0A            rwl.writeLock().unlock(
 );%0A 
@@ -3069,25 +3069,24 @@
 er l) %7B%0A
-%0A
         
 synchron
@@ -3081,65 +3081,134 @@
     
-synchronized (listeners) %7B%0A            listeners.remove(l
+rwl.writeLock().lock();%0A        try %7B%0A            listeners.remove(l);%0A        %7D finally %7B%0A            rwl.writeLock().unlock(
 );%0A 
@@ -3829,57 +3829,45 @@
     
-try %7B%0A            synchronized (listeners) %7B%0A    
+rwl.readLock().lock();%0A        try %7B%0A
     
@@ -3924,36 +3924,32 @@
                 
-    
 listener.viewCha
@@ -3969,34 +3969,16 @@
 event);%0A
-                %7D%0A
         
@@ -3997,617 +3997,64 @@
   %7D 
-catch (ConcurrentModificationException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    class ViewChangedNotifier implements Runnable %7B%0A        private boolean interrupted = false;%0A%0A        public void interrupt() %7B%0A            interrupted = true;%0A        %7D%0A%0A        public void run() %7B%0A            synchronized (this) %7B%0A                if (interrupted)%0A                    return;%0A                this.notify();%0A                try %7B%0A                    this.wait();%0A                %7D catch (InterruptedException e) %7B%0A                    Log.error(%22%22, e);%0A                %7D%0A            %7D%0A        %7D%0A
+finally %7B%0A            rwl.readLock().unlock();%0A        %7D
 %0A   

Improve locking in ViewListenerDistributor