×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: abdul qayyum
Added: Aug 13, 2015 12:10 AM
Views: 5
Tags: swdd
File_1
  1. package training1;
  2.  
  3. import java.util.List;
  4. import javafx.collections.ListChangeListener;
  5.  
  6. public class PersonListChangeListener implements ListChangeListener<Person> {
  7.  
  8.     @Override
  9.     public void onChanged(Change<? extends Person> change) {
  10.         while(change.next()){
  11.             if(change.wasPermutated()){
  12.                 handlePermutated(change);
  13.             }else if(change.wasUpdated()){
  14.                 handleUpdated(change);
  15.             }else if(change.wasReplaced()){
  16.                 handleReplaced(change);
  17.             }else if(change.wasRemoved()){
  18.                 handleRemoved(change);
  19.             }else if(change.wasAdded()){
  20.                 handleAdded(change);
  21.             }
  22.     }
  23.    
  24. }
  25.  
  26.     private void handlePermutated(Change<? extends Person> change) {
  27.         System.out.print("Change Type: Permutated");
  28.         System.out.println("Permutated Range:"+getRangeText(change));
  29.         int start = change.getFrom();
  30.         int end = change.getTo();
  31.         for(int oldIndex = start;oldIndex < end;oldIndex++){
  32.             int newIndex = change.getPermutation(oldIndex);
  33.             System.out.println("index["+oldIndex+"] moved to"+"index["+newIndex+"]");
  34.         }
  35.     }
  36.  
  37.     private void handleUpdated(Change<? extends Person> change) {
  38.         System.out.println("Change Type: Updated");
  39.         System.out.println("Updated Range:"+getRangeText(change));
  40.         System.out.println("Updated Element are:"+change.getList().subList(change.getFrom(),change.getTo()));
  41.     }
  42.    
  43.  
  44.  
  45.  
  46.     private void handleReplaced(Change<? extends Person> change) {
  47.         System.out.println("Change type:Replaced");
  48.        
  49.         // Replace is the same as a remove followed with an "add"
  50.         handleRemoved(change);
  51.         handleAdded(change);
  52.     }
  53.    
  54.     private void handleRemoved(Change<? extends Person> change) {
  55.         System.out.println("Change Type: Removed");
  56.        
  57.         int removedSize = change.getRemovedSize();
  58.         List<? extends Person> subList = change.getRemoved();
  59.        
  60.         System.out.println("Removed Size:"+removedSize);
  61.         System.out.println("Remove Range:"+this.getRangeText(change));
  62.         System.out.println("Remove List:"+subList);
  63.        
  64.     }
  65.  
  66.     private void handleAdded(Change<? extends Person> change) {
  67.         System.out.println("Change Type: Added");
  68.        
  69.         int addedSize = change.getAddedSize();
  70.         List<? extends Person> subList = change.getAddedSubList();
  71.         System.out.println("Added Size:"+addedSize);
  72.         System.out.println("Added List:"+subList);
  73.     }
  74.    
  75.     private String getRangeText(Change<? extends Person> change) {
  76.         return "["+change.getFrom()+","+change.getTo()+"]";
  77.     }
  78.  
  79.  
  80. }
  81.