Last active
April 18, 2016 07:50
-
-
Save tschulte/2e270070b9f1ae17860ce82f173adcc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import javafx.beans.property.ListProperty; | |
import javafx.beans.property.Property; | |
import javafx.beans.property.SimpleListProperty; | |
import javafx.beans.property.SimpleObjectProperty; | |
import javafx.beans.value.WritableObjectValue; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
public class FXObservableWithList | |
{ | |
private Object theList; | |
public List<String> getTheList() | |
{ | |
if (theList == null) | |
{ | |
theList = new ArrayList<String>(); | |
} | |
if (theList instanceof WritableObjectValue) | |
{ | |
return ((WritableObjectValue<ObservableList<String>>)theList).get(); | |
} | |
return (List<String>)theList; | |
} | |
public void setTheList(List<String> theList) | |
{ | |
if (this.theList instanceof Property) | |
{ | |
((Property<ObservableList<String>>)this.theList).setValue(toObservableList(theList)); | |
} | |
else | |
{ | |
this.theList = theList; | |
} | |
} | |
public ObservableList<String> theListObservables() | |
{ | |
if (this.theList instanceof Property) | |
{ | |
return ((Property<ObservableList<String>>)this.theList).getValue(); | |
} | |
theList = toObservableList((List<String>)theList); | |
return (ObservableList<String>)theList; | |
} | |
public Property<ObservableList<String>> theListProperty() | |
{ | |
if (!(theList instanceof WritableObjectValue)) | |
{ | |
theList = new SimpleObjectProperty<List<String>>(this, "theList", theListObservables()); | |
} | |
return (Property<ObservableList<String>>)theList; | |
} | |
public ListProperty<String> theListListProperty() | |
{ | |
if (!(theList instanceof ListProperty)) | |
{ | |
Property<ObservableList<String>> oldProperty = null; | |
if (theList instanceof Property) | |
{ | |
oldProperty = (Property<ObservableList<String>>)theList; | |
} | |
theList = new SimpleListProperty<String>(this, "theList", theListObservables()); | |
if (oldProperty != null) | |
{ | |
oldProperty.bind((ListProperty<String>)theList); | |
} | |
} | |
return (ListProperty<String>)theList; | |
} | |
private ObservableList<String> toObservableList(List<String> list) | |
{ | |
if (list instanceof ObservableList) | |
{ | |
return (ObservableList<String>)list; | |
} | |
if (list != null) | |
{ | |
return FXCollections.observableList(list); | |
} | |
return FXCollections.observableArrayList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.beans.property.* | |
import javafx.collections.* | |
import spock.lang.Specification | |
class FXObservableWithListSpec extends Specification { | |
FXObservableWithList observable = new FXObservableWithList() | |
def "list is never null"() { | |
expect: | |
observable.getTheList() == [] | |
!(observable.getTheList() instanceof ObservableList) | |
} | |
def "can add and remove a list"() { | |
when: | |
observable.setTheList(['foo']) | |
then: | |
observable.getTheList() == ['foo'] | |
} | |
def "can get the observable list"() { | |
expect: | |
observable.theListObservables() == [] | |
(observable.theListObservables() instanceof ObservableList) | |
(observable.getTheList() instanceof ObservableList) | |
} | |
def "can get the Property"() { | |
expect: | |
observable.theListProperty() instanceof ObjectProperty | |
observable.getTheList() == [] | |
} | |
def "can get the ListProperty"() { | |
expect: | |
observable.theListListProperty() instanceof ListProperty | |
observable.theListProperty() instanceof ListProperty | |
} | |
def "when bound to the property changing to listProperty notifies the listener"() { | |
given: | |
Property<ObservableList<String>> property = observable.theListProperty() | |
ListProperty listProperty = new SimpleListProperty<String>() | |
expect: | |
listProperty.get() == null | |
when: | |
listProperty.bind(property) | |
then: | |
listProperty.get() == [] | |
when: | |
observable.setTheList(['foo']) | |
then: | |
listProperty.get() == ['foo'] | |
when: | |
observable.theListListProperty() | |
observable.setTheList(['bar']) | |
then: | |
listProperty.get() == ['bar'] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment