Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.binding;
import javafx.beans.WeakListener;
import javafx.collections.*;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*/
public class ContentBinding {
private static void checkParameters(Object property1, Object property2) {
if ((property1 == null) || (property2 == null)) {
throw new NullPointerException("Both parameters must be specified.");
}
if (property1 == property2) {
throw new IllegalArgumentException("Cannot bind object to itself");
}
}
public static Object bind(List list1, ObservableList extends E> list2) {
checkParameters(list1, list2);
final ListContentBinding contentBinding = new ListContentBinding<>(list1);
if (list1 instanceof ObservableList) {
((ObservableList) list1).setAll(list2);
} else {
list1.clear();
list1.addAll(list2);
}
list2.removeListener(contentBinding);
list2.addListener(contentBinding);
return contentBinding;
}
public static Object bind(Set set1, ObservableSet extends E> set2) {
checkParameters(set1, set2);
final SetContentBinding contentBinding = new SetContentBinding<>(set1);
set1.clear();
set1.addAll(set2);
set2.removeListener(contentBinding);
set2.addListener(contentBinding);
return contentBinding;
}
public static Object bind(Map map1, ObservableMap extends K, ? extends V> map2) {
checkParameters(map1, map2);
final MapContentBinding contentBinding = new MapContentBinding<>(map1);
map1.clear();
map1.putAll(map2);
map2.removeListener(contentBinding);
map2.addListener(contentBinding);
return contentBinding;
}
public static void unbind(Object obj1, Object obj2) {
checkParameters(obj1, obj2);
if ((obj1 instanceof List) && (obj2 instanceof ObservableList)) {
((ObservableList)obj2).removeListener(new ListContentBinding((List)obj1));
} else if ((obj1 instanceof Set) && (obj2 instanceof ObservableSet)) {
((ObservableSet)obj2).removeListener(new SetContentBinding((Set)obj1));
} else if ((obj1 instanceof Map) && (obj2 instanceof ObservableMap)) {
((ObservableMap)obj2).removeListener(new MapContentBinding((Map)obj1));
}
}
private static class ListContentBinding implements ListChangeListener, WeakListener {
private final WeakReference> listRef;
public ListContentBinding(List list) {
this.listRef = new WeakReference<>(list);
}
@Override
public void onChanged(Change extends E> change) {
final List list = listRef.get();
if (list == null) {
change.getList().removeListener(this);
} else {
while (change.next()) {
if (change.wasPermutated()) {
list.subList(change.getFrom(), change.getTo()).clear();
list.addAll(change.getFrom(), change.getList().subList(change.getFrom(), change.getTo()));
} else {
if (change.wasRemoved()) {
list.subList(change.getFrom(), change.getFrom() + change.getRemovedSize()).clear();
}
if (change.wasAdded()) {
list.addAll(change.getFrom(), change.getAddedSubList());
}
}
}
}
}
@Override
public boolean wasGarbageCollected() {
return listRef.get() == null;
}
@Override
public int hashCode() {
final List list = listRef.get();
return (list == null)? 0 : list.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
final List list1 = listRef.get();
if (list1 == null) {
return false;
}
if (obj instanceof ListContentBinding) {
final ListContentBinding> other = (ListContentBinding>) obj;
final List> list2 = other.listRef.get();
return list1 == list2;
}
return false;
}
}
private static class SetContentBinding implements SetChangeListener, WeakListener {
private final WeakReference> setRef;
public SetContentBinding(Set set) {
this.setRef = new WeakReference<>(set);
}
@Override
public void onChanged(Change extends E> change) {
final Set set = setRef.get();
if (set == null) {
change.getSet().removeListener(this);
} else {
if (change.wasRemoved()) {
set.remove(change.getElementRemoved());
} else {
set.add(change.getElementAdded());
}
}
}
@Override
public boolean wasGarbageCollected() {
return setRef.get() == null;
}
@Override
public int hashCode() {
final Set set = setRef.get();
return (set == null)? 0 : set.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
final Set set1 = setRef.get();
if (set1 == null) {
return false;
}
if (obj instanceof SetContentBinding) {
final SetContentBinding> other = (SetContentBinding>) obj;
final Set> set2 = other.setRef.get();
return set1 == set2;
}
return false;
}
}
private static class MapContentBinding implements MapChangeListener, WeakListener {
private final WeakReference