import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;

public class BindingTest {
	public static void main(String[] arguments) 
	   { 
	      // Set up the content binding and listeners. 
	      ObservableMap<Integer, String> source = FXCollections.observableHashMap(); 
	      ObservableMap<Integer, String> destination = FXCollections.observableHashMap(); 
	      Bindings.bindContent(destination, source); 
	      destination.addListener(s_destinationMapListener); 

	      // First set, add some things. 
	      System.out.println("Adding things"); 
	      source.put(1, "Thing 1"); 
	      source.put(2, "Thing 2"); 
	      source.put(3, "Thing 3"); 
	      source.put(4, "Thing 4"); 
	      source.put(5, "Thing 5"); 
	      System.out.println("Destination: " + destination); 
	      System.out.println(); 

	      // Next, remove some things. 
	      System.out.println("Removing things"); 
	      source.remove(3); 
	      source.remove(5); 
	      System.out.println("Destination: " + destination); 
	      System.out.println(); 

	      // Next, add and modify. 
	      System.out.println("Adding/modifying things"); 
	      source.put(1, "Thing 1-1"); 
	      source.put(2, "Thing 2-2"); 
	      source.put(3, "Thing 3-3"); 
	      source.put(4, "Thing 4-4"); 
	      source.put(5, "Thing 5-5"); 
	      System.out.println("Destination: " + destination); 
	      System.out.println(); 

	      // Next, remove a bunch. 
	      System.out.println("Batch remove"); 
	      source.keySet().removeAll(Arrays.asList(new Integer[]{0, 1, 5, 6, 7})); 
	      System.out.println("Destination: " + destination); 
	      System.out.println(); 

	      // Next, add a bunch at once. 
	      System.out.println("Batch add/modify"); 
	      Map<Integer, String> next = new HashMap<>(); 
	      next.put(1, "Thing 1-1-1"); 
	      next.put(3, "Thing 3-3-3"); 
	      next.put(5, "Thing 5-5-5"); 
	      next.put(6, "Thing 6-6-6"); 
	      source.putAll(next); 
	      System.out.println("Destination: " + destination); 
	      System.out.println(); 
	   } 

	   private static MapChangeListener<Integer, String> s_destinationMapListener = 
	      new MapChangeListener<Integer, String>() 
	      { 
	         @Override 
	         public void onChanged(Change<? extends Integer, ? extends String> change) 
	         { 
	            if (change.wasAdded()) 
	            { 
	               if (change.wasRemoved()) 
	               { 
	                  System.out.println("Modified: " + change.getKey() + " from " + change.getValueRemoved() 
	                     + " to " + change.getValueAdded()); 
	               } 
	               else 
	               { 
	                  System.out.println("Added: " + change.getKey() + " -> " + change.getValueAdded()); 
	               } 
	            } 
	            else if (change.wasRemoved()) 
	            { 
	               System.out.println( 
	                  "Removed: " + change.getKey() + " ( was " + change.getValueRemoved() + " )"); 
	            } 
	         } 
	      }; 
}
