1 /*
   2  * Copyright (c) 2012, 2013, 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 java.util.HashMap;
  29 import java.util.Map;
  30 import javafx.beans.value.ChangeListener;
  31 import javafx.beans.value.ObservableValue;
  32 import javafx.beans.value.WeakChangeListener;
  33 import javafx.util.Callback;
  34 
  35 public final class MultiplePropertyChangeListenerHandler {
  36     
  37     private final Callback<String, Void> propertyChangedHandler;
  38     
  39     public MultiplePropertyChangeListenerHandler(Callback<String, Void> propertyChangedHandler) {
  40         this.propertyChangedHandler = propertyChangedHandler;
  41     }
  42     
  43     /**
  44      * This is part of the workaround introduced during delomboking. We probably will
  45      * want to adjust the way listeners are added rather than continuing to use this
  46      * map (although it doesn't really do much harm).
  47      */
  48     private Map<ObservableValue<?>,String> propertyReferenceMap =
  49             new HashMap<ObservableValue<?>,String>();
  50     
  51     private final ChangeListener<Object> propertyChangedListener = new ChangeListener<Object>() {
  52         @Override public void changed(ObservableValue<?> property, 
  53                 @SuppressWarnings("unused") Object oldValue, 
  54                 @SuppressWarnings("unused") Object newValue) {
  55             propertyChangedHandler.call(propertyReferenceMap.get(property));
  56         }
  57     };
  58     
  59     private final WeakChangeListener<Object> weakPropertyChangedListener = 
  60             new WeakChangeListener<Object>(propertyChangedListener);
  61     
  62     /**
  63      * Subclasses can invoke this method to register that we want to listen to
  64      * property change events for the given property.
  65      *
  66      * @param property
  67      * @param reference
  68      */
  69     public final void registerChangeListener(ObservableValue<?> property, String reference) {
  70         if (!propertyReferenceMap.containsKey(property)) {
  71             propertyReferenceMap.put(property, reference);
  72             property.addListener(weakPropertyChangedListener);
  73         }
  74     }
  75     
  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 }