Java Code Examples for javafx.beans.property.IntegerProperty#set()

The following examples show how to use javafx.beans.property.IntegerProperty#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VarFromValTest.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void test() {
    IntegerProperty src = new SimpleIntegerProperty(0);
    IntegerBinding twice = src.multiply(2);
    Var<Number> twiceVar = Var.fromVal(twice, n -> src.set(n.intValue() / 2));

    List<Number> values = new ArrayList<>();
    EventStreams.valuesOf(twiceVar).subscribe(values::add);

    src.set(1);
    twiceVar.setValue(4);
    twiceVar.setValue(5); // no effect
    twiceVar.setValue(7); // will become 6

    assertEquals(Arrays.asList(0, 2, 4, 6), values);
}
 
Example 2
Source File: ValTest.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void changesTest() {
    IntegerProperty src = new SimpleIntegerProperty(0);
    Val<Number> val = Val.wrap(src);

    List<Change<Number>> changes = new ArrayList<>();
    val.changes().subscribe(changes::add);

    src.set(1);
    src.set(2);
    src.set(3);

    assertArrayEquals(Arrays.asList(0, 1, 2).toArray(),
        changes.stream().map(change -> change.getOldValue()).toArray());
    assertArrayEquals(Arrays.asList(1, 2, 3).toArray(),
        changes.stream().map(change -> change.getNewValue()).toArray());
}
 
Example 3
Source File: Rubik.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void doSequence(String list){
    onScrambling.set(true);
    sequence = Utils.unifyNotation(list);
    
    /*
    This is the way to perform several rotations from a list, waiting till each of
    them ends properly. A listener is added to onRotation, so only when the last rotation finishes
    a new rotation is performed. The end of the list is used to stop the listener, by adding 
    a new listener to the index property. Note the size+1, to allow for the last rotation to end.
    */
    
    IntegerProperty index = new SimpleIntegerProperty(1);
    ChangeListener<Boolean> lis = (ov, b, b1) -> {
        if (!b1) {
            if (index.get()<sequence.size()) {
                rotateFace(sequence.get(index.get()));
            } else {
                // save transforms
                for (Map.Entry<String, MeshView> entry : mapMeshes.entrySet()) {
                    mapTransformsScramble.put(entry.getKey(), entry.getValue().getTransforms().get(0));
                }

                orderScramble = new ArrayList<>();
                for (Integer i : reorder) {
                    orderScramble.add(i);
                }
            } 
            index.set(index.get()+1);
        }
    };
    index.addListener((ov, v, v1) -> {
        if (v1.intValue() == sequence.size()+1) {
            onScrambling.set(false);
            onRotation.removeListener(lis);
            count.set(-1);
        }
    });
    onRotation.addListener(lis);
    rotateFace(sequence.get(0));
}
 
Example 4
Source File: Rubik.java    From RubikFX with GNU General Public License v3.0 5 votes vote down vote up
public void doSequence(String list){
    onScrambling.set(true);
    sequence=Utils.unifyNotation(list);
    
    /*
    This is the way to perform several rotations from a list, waiting till each of
    them ends properly. A listener is added to onRotation, so only when the last rotation finishes
    a new rotation is performed. The end of the list is used to stop the listener, by adding 
    a new listener to the index property. Note the size+1, to allow for the last rotation to end.
    */
    
    IntegerProperty index=new SimpleIntegerProperty(1);
    ChangeListener<Boolean> lis=(ov,b,b1)->{
        if(!b1){
            if(index.get()<sequence.size()){
                rotateFace(sequence.get(index.get()));
            } else {
                // save transforms
                mapMeshes.forEach((k,v)->mapTransformsScramble.put(k, v.getTransforms().get(0)));
                orderScramble=reorder.stream().collect(Collectors.toList());
            } 
            index.set(index.get()+1);
        }
    };
    index.addListener((ov,v,v1)->{
        if(v1.intValue()==sequence.size()+1){
            onScrambling.set(false);
            onRotation.removeListener(lis);
            count.set(-1);
        }
    });
    onRotation.addListener(lis);
    rotateFace(sequence.get(0));
}
 
Example 5
Source File: Rubik.java    From RubikFX with GNU General Public License v3.0 5 votes vote down vote up
public void doReplay(List<Move> moves){
    if(moves.isEmpty()){
        return;
    }
    content.resetCam();
    //restore scramble
    if(mapTransformsScramble.size()>0){
        System.out.println("Restoring scramble");
        mapMeshes.forEach((k,v)->v.getTransforms().setAll(mapTransformsScramble.get(k)));
        order=orderScramble.stream().collect(Collectors.toList());
        rot.setCube(order);
        count.set(-1);
    } else {
        // restore original
        doReset();
    }
    onReplaying.set(true);
    
    IntegerProperty index=new SimpleIntegerProperty(1);
    ChangeListener<Boolean> lis=(ov,v,v1)->{
        if(!v1 && moves.size()>1){
            if(index.get()<moves.size()){
                timestamp.set(moves.get(index.get()).getTimestamp());
                rotateFace(moves.get(index.get()).getFace());
            }
            index.set(index.get()+1);
        }
    };
    index.addListener((ov,v,v1)->{
        if(v1.intValue()==moves.size()+1){
            onReplaying.set(false);
            onRotation.removeListener(lis);
        }
    });
    onRotation.addListener(lis);
    timestamp.set(moves.get(0).getTimestamp());
    rotateFace(moves.get(0).getFace());
}
 
Example 6
Source File: RecursionTest.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This is not a test of ReactFX functionality, but rather a showcase of
 * JavaFX disfunctionality.
 */
@Test
public void failingRecursionForJavaFxProperty() {
    IntegerProperty var = new SimpleIntegerProperty(0);
    IntegerProperty lastObserved1 = new SimpleIntegerProperty(var.get());
    IntegerProperty lastObserved2 = new SimpleIntegerProperty(var.get());
    BooleanProperty failedAsExpected = new SimpleBooleanProperty(false);
    var.addListener((obs, oldVal, newVal) -> {
        if(lastObserved1.get() != oldVal.intValue()) {
            failedAsExpected.set(true);
        }
        lastObserved1.set(newVal.intValue());
        if(newVal.intValue() == 1) {
            var.set(2);
        }
    });
    var.addListener((obs, oldVal, newVal) -> {
        if(lastObserved1.get() != oldVal.intValue()) {
            failedAsExpected.set(true);
        }
        lastObserved2.set(newVal.intValue());
        if(newVal.intValue() == 1) {
            var.set(2);
        }
    });
    var.set(1);
    assertTrue(failedAsExpected.get());
}
 
Example 7
Source File: ValidatorUtils.java    From kafka-message-tool with MIT License 4 votes vote down vote up
private static Consumer<String> stringConsumer(IntegerProperty referenceProperty) {
    return (v) -> referenceProperty.set(Integer.valueOf(v));
}
 
Example 8
Source File: Rubik.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void doReplay(List<Move> moves){
        if (moves.isEmpty()) {
            return;
        }
        content.resetCam();
        //restore scramble
        if (mapTransformsScramble.size() > 0) {
//            System.out.println("Restoring scramble");
            for (Map.Entry<String, MeshView> entry : mapMeshes.entrySet()) {
                entry.getValue().getTransforms().setAll(mapTransformsScramble.get(entry.getKey()));
            }

            order = new ArrayList<>();
            for (Integer i : orderScramble) {
                order.add(i);
            }
            rot.setCube(order);
            count.set(-1);
        } else {
            // restore original
            doReset();
        }
        onReplaying.set(true);
        
        IntegerProperty index = new SimpleIntegerProperty(1);
        ChangeListener<Boolean> lis = (ov, v, v1) -> {
            if (!v1 && moves.size() > 1) {
                if (index.get() < moves.size()) {
                    timestamp.set(moves.get(index.get()).getTimestamp());
                    rotateFace(moves.get(index.get()).getFace());
                }
                index.set(index.get() + 1);
            }
        };
        index.addListener((ov, v, v1) -> {
            if (v1.intValue() == moves.size() + 1) {
                onReplaying.set(false);
                onRotation.removeListener(lis);
            }
        });
        onRotation.addListener(lis);
        timestamp.set(moves.get(0).getTimestamp());
        rotateFace(moves.get(0).getFace());
    }
 
Example 9
Source File: FXBinderTest.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
@Test
public void testJavaFXIntegerBidirectional() {
    Property<Integer> integerDolphinProperty = new MockedProperty<>();
    Property<Number> numberDolphinProperty = new MockedProperty<>();
    IntegerProperty integerJavaFXProperty = new SimpleIntegerProperty();

    integerDolphinProperty.set(47);
    assertNotEquals(integerJavaFXProperty.get(), 47);

    Binding binding = FXBinder.bind(integerJavaFXProperty).bidirectionalToNumeric(integerDolphinProperty);
    assertEquals(integerJavaFXProperty.get(), 47);
    integerDolphinProperty.set(100);
    assertEquals(integerJavaFXProperty.get(), 100);
    integerDolphinProperty.set(null);
    assertEquals(integerJavaFXProperty.get(), 0);

    integerJavaFXProperty.set(12);
    assertEquals(integerDolphinProperty.get().intValue(), 12);
    integerJavaFXProperty.setValue(null);
    assertEquals(integerDolphinProperty.get().intValue(), 0);

    binding.unbind();
    integerDolphinProperty.set(100);
    assertEquals(integerJavaFXProperty.get(), 0);


    numberDolphinProperty.set(12);
    binding = FXBinder.bind(integerJavaFXProperty).bidirectionalTo(numberDolphinProperty);
    assertEquals(integerJavaFXProperty.get(), 12);
    numberDolphinProperty.set(null);
    assertEquals(integerJavaFXProperty.get(), 0);

    integerJavaFXProperty.set(12);
    assertEquals(numberDolphinProperty.get().intValue(), 12);
    integerJavaFXProperty.setValue(null);
    assertEquals(numberDolphinProperty.get().intValue(), 0);

    binding.unbind();
    numberDolphinProperty.set(100);
    assertEquals(integerJavaFXProperty.get(), 0);
}
 
Example 10
Source File: MemoizationListTest.java    From ReactFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {
    ObservableList<String> source = new LiveArrayList<>("1", "22", "333");
    IntegerProperty counter = new SimpleIntegerProperty(0);
    MemoizationList<Integer> memoizing = LiveList.map(source, s -> {
        counter.set(counter.get() + 1);
        return s.length();
    }).memoize();
    LiveList<Integer> memoized = memoizing.memoizedItems();
    List<Integer> memoMirror = new ArrayList<>();
    memoized.observeModifications(mod -> {
        memoMirror.subList(mod.getFrom(), mod.getFrom() + mod.getRemovedSize()).clear();
        memoMirror.addAll(mod.getFrom(), mod.getAddedSubList());
    });

    assertEquals(0, memoized.size());

    source.add("4444");
    assertEquals(Collections.emptyList(), memoized);
    assertEquals(0, memoMirror.size());
    assertEquals(0, counter.get());

    memoizing.get(2);
    assertEquals(Arrays.asList(3), memoized);
    assertEquals(Arrays.asList(3), memoMirror);
    assertEquals(1, counter.get());

    counter.set(0);
    memoizing.get(0);
    assertEquals(Arrays.asList(1, 3), memoized);
    assertEquals(Arrays.asList(1, 3), memoMirror);
    assertEquals(1, counter.get());

    counter.set(0);
    source.subList(2, 4).replaceAll(s -> s + s);
    assertEquals(Arrays.asList(1), memoized);
    assertEquals(Arrays.asList(1), memoMirror);
    assertEquals(0, counter.get());

    counter.set(0);
    memoizing.observeModifications(mod -> {
        if(mod.getAddedSize() == 3) { // when three items added
            mod.getAddedSubList().get(0); // force evaluation of the first
            mod.getAddedSubList().get(1); // and second one
            source.remove(0); // and remove the first element from source
        }
    });
    source.remove(1, 4);
    assertEquals(Arrays.asList(1), memoized);
    assertEquals(Arrays.asList(1), memoMirror);
    assertEquals(0, counter.get());
    source.addAll("22", "333", "4444");
    assertEquals(Arrays.asList(2, 3), memoized);
    assertEquals(Arrays.asList(2, 3), memoMirror);
    assertEquals(2, counter.get());

    assertEquals(Arrays.asList(2, 3, 4), memoizing);
    assertEquals(3, counter.get());
    assertEquals(Arrays.asList(2, 3, 4), memoized);
    assertEquals(Arrays.asList(2, 3, 4), memoMirror);
}
 
Example 11
Source File: ListRecursionTest.java    From ReactFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Tests that list changes are accumulated on recursion.
 */
@Test
public void testChangeAccumulation() {
    ObservableList<String> strings = new LiveArrayList<>("1", "22", "333");
    LiveList<Integer> lengths = LiveList.map(strings, String::length);

    IntegerProperty firstListener = new SimpleIntegerProperty(0);

    List<Integer> first1Removed = new ArrayList<>();
    List<Integer> first1Added = new ArrayList<>();
    List<Integer> first2Removed = new ArrayList<>();
    List<Integer> first2Added = new ArrayList<>();
    List<Integer> secondRemoved = new ArrayList<>();
    List<Integer> secondAdded = new ArrayList<>();

    IntFunction<ListChangeListener<Integer>> listenerFactory = id -> ch -> {
        while(ch.next()) {
            if(firstListener.get() == 0) {
                firstListener.set(id);
                first1Removed.addAll(ch.getRemoved());
                first1Added.addAll(ch.getAddedSubList());
                strings.add(2, "55555");
            } else if(firstListener.get() == id) {
                first2Removed.addAll(ch.getRemoved());
                first2Added.addAll(ch.getAddedSubList());
            } else {
                secondRemoved.addAll(ch.getRemoved());
                secondAdded.addAll(ch.getAddedSubList());
            }
        }
    };

    lengths.addListener(listenerFactory.apply(1));
    lengths.addListener(listenerFactory.apply(2));

    strings.set(1, "4444");

    assertEquals(Arrays.asList(2), first1Removed);
    assertEquals(Arrays.asList(4), first1Added);
    assertEquals(Arrays.asList(), first2Removed);
    assertEquals(Arrays.asList(5), first2Added);
    assertEquals(Arrays.asList(2), secondRemoved);
    assertEquals(Arrays.asList(4, 5), secondAdded);
}