1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.scene.control;
  27 
  28 import javafx.beans.value.ChangeListener;
  29 import javafx.beans.value.ObservableValue;
  30 import javafx.beans.value.WeakChangeListener;
  31 
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.function.Consumer;
  38 
  39 public final class LambdaMultiplePropertyChangeListenerHandler {
  40 
  41     private final Map<ObservableValue<?>, List<Consumer<ObservableValue<?>>>> propertyReferenceMap;
  42     private final ChangeListener<Object> propertyChangedListener;
  43     private final WeakChangeListener<Object> weakPropertyChangedListener;
  44 
  45     public LambdaMultiplePropertyChangeListenerHandler() {
  46         this.propertyReferenceMap = new HashMap<>();
  47         this.propertyChangedListener = (observable, oldValue, newValue) -> {
  48             List<Consumer<ObservableValue<?>>> callbacks = propertyReferenceMap.getOrDefault(observable, Collections.emptyList());
  49             for (Consumer<ObservableValue<?>> callback : callbacks) {
  50                 callback.accept(observable);
  51             }
  52         };
  53         this.weakPropertyChangedListener = new WeakChangeListener<>(propertyChangedListener);
  54     }
  55     
  56     /**
  57      * Subclasses can invoke this method to register that we want to listen to
  58      * property change events for the given property.
  59      *
  60      * @param property
  61      */
  62     public final void registerChangeListener(ObservableValue<?> property, Consumer<ObservableValue<?>> consumer) {
  63         if (consumer == null) return;
  64         List<Consumer<ObservableValue<?>>> callbacks = propertyReferenceMap.computeIfAbsent(property, p -> new ArrayList<>());
  65         callbacks.add(consumer);
  66 
  67         // we only add a listener if the callbacks list contains only one element
  68         // (that is, we've added a consumer to this specific property for the first
  69         // time).
  70         if (callbacks.size() == 1) {
  71             property.addListener(weakPropertyChangedListener);
  72         }
  73     }
  74 
  75     // need to be careful here - removing all listeners on the specific property!
  76     public final void unregisterChangeListener(ObservableValue<?> property) {
  77         if (propertyReferenceMap.containsKey(property)) {
  78             propertyReferenceMap.remove(property);
  79             property.removeListener(weakPropertyChangedListener);
  80         }
  81     }
  82 
  83     public void dispose() {
  84         // unhook listeners
  85         for (ObservableValue<?> value : propertyReferenceMap.keySet()) {
  86             value.removeListener(weakPropertyChangedListener);
  87         }
  88         propertyReferenceMap.clear();
  89     }
  90 }