package no.ntnu.capgeminitest.data;

import java.util.List;
import java.util.ArrayList;

import no.ntnu.capgeminitest.event.PropertyChangeListener;

/**
 * Hold a property for a class.
 * 
 * A sophisticated wrapper for a property, supporting {@code PropertyChangeListener}s.
 * 
 * Used with the data binding framework.
 * 
 * @param <T>
 *            The type of this property.
 */
public class Property<T> {
    
    private T data;
    private List<PropertyChangeListener<T>> listeners = new ArrayList<PropertyChangeListener<T>>();

    public Property(T data) {
        this.data = data;
    }

    /**
     * @return The data contained in this property.
     */
    public T get() {
        return data;
    }

    /**
     * Set the data for this property.
     * 
     * Notifies listeners that the data was changed.
     */
    public void set(T data) {
        this.data = data;
        notifyListeners();
    }

    protected void notifyListeners() {
        for (PropertyChangeListener<T> listener : listeners) {
            listener.propertyChanged(this);
        }
    }

    public void addListener(PropertyChangeListener<T> listener) {
        listeners.add(listener);
    }

    public void removeListener(PropertyChangeListener<T> listener) {
        listeners.remove(listener);
    }
    
    /**
     * Override this method to receive property changes.
     * 
     * Called before other bindings are notified.
     * 
     * Note: Changing the property inside {@code onChange} triggers a recursive call.
     */
    protected void onChange(T newValue) {
        // Empty.
    }
}

@@ -405,24 +405,455 @@
 y%3CT%3E %7B%0A    %0A
+    /**%0A     * A listener that is used to update this property.%0A     * %0A     * When this property is bound to another property, it will listen to%0A     * changes to the other property and update itself on change.%0A     */%0A    private class OnPropertyChangeUpdater implements PropertyChangeListener%3CT%3E %7B%0A        @Override public void propertyChanged(Property%3CT%3E property) %7B%0A            set(property.get());%0A        %7D      %0A    %7D%0A    %0A
     private 
@@ -856,24 +856,24 @@
 ate T data;%0A
-
     private 
@@ -960,17 +960,105 @@
 %3CT%3E%3E();%0A
+    private OnPropertyChangeUpdater updateListener = new OnPropertyChangeUpdater();%0A    
 %0A
-
     publ
@@ -1221,32 +1221,328 @@
 rn data;%0A    %7D%0A%0A
+    /**%0A     * Override this method to receive property changes.%0A     * %0A     * Called before other bindings are notified.%0A     * %0A     * Note: Changing the property inside %7B@code onChange%7D triggers a recursive call.%0A     */%0A    protected void onChange(T newValue) %7B%0A        // Empty.%0A    %7D%0A    %0A
     /**%0A     * S
@@ -2124,288 +2124,95 @@
     
-/**%0A     * Override this method to receive p
+P
 roperty
- c
+C
 hange
-s.%0A     * %0A     * Called before other bindings are notified.%0A     * %0A     * Note: Changing the property inside %7B@code onChange%7D triggers a recursive call.%0A     */%0A    protected void onChange(T newValue) %7B%0A        // Empty.
+Listener%3CT%3E getOnPropertyChangeUpdater() %7B%0A        return updateListener;
 %0A   

Add dependency for updating properties.