Search in sources :

Example 6 with Key

use of com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key in project android by JetBrains.

the class ScopedDataBinder method deriveValues.

private <T> void deriveValues(@Nullable Key<T> changedKey) {
    // Loop over our value derivers and call them if necessary
    for (Key key : myValueDerivers.keySet()) {
        // Don't derive values that have already been updated this round
        if (!myGuardedKeys.contains(key)) {
            ValueDeriver deriver = myValueDerivers.get(key);
            // Don't overwrite values that the user has manually entered
            if (myUserEditedKeys.contains(key) && deriver.respectUserEdits()) {
                continue;
            }
            @SuppressWarnings("unchecked") Collection<Key<?>> triggerKeys = deriver.getTriggerKeys();
            // Respect the deriver's filter of triggers
            if (changedKey != null && triggerKeys != null && !triggerKeys.contains(changedKey)) {
                continue;
            }
            // noinspection unchecked
            deriveValue(key, deriver, changedKey);
        }
    }
}
Also used : Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Example 7 with Key

use of com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key in project android by JetBrains.

the class ScopedDataBinderTest method testRegisterRadioButton.

public void testRegisterRadioButton() throws Exception {
    Key<Boolean> booleanKey = myState.createKey("textField", Boolean.class);
    Key<Boolean> booleanKey2 = myState.createKey("boundSecond", Boolean.class);
    final Key<String> triggerKey = myState.createKey("triggerKey", String.class);
    JCheckBox checkBox = new JCheckBox();
    myScopedDataBinder.register(booleanKey, checkBox);
    myScopedDataBinder.register(booleanKey2, checkBox);
    // Test binding UI -> Store
    checkBox.setSelected(true);
    assertEquals(Boolean.TRUE, myState.get(booleanKey));
    assertEquals(Boolean.TRUE, myState.get(booleanKey2));
    // Test binding Store -> UI
    myState.put(booleanKey, Boolean.FALSE);
    assertEquals(false, checkBox.isSelected());
    assertEquals(Boolean.FALSE, myState.get(booleanKey2));
    myState.put(booleanKey2, true);
    assertEquals(true, checkBox.isSelected());
    assertEquals(Boolean.TRUE, myState.get(booleanKey));
    final AtomicBoolean respectsUserEdits = new AtomicBoolean(true);
    // Test value derivation
    myScopedDataBinder.registerValueDeriver(booleanKey, new ValueDeriver<Boolean>() {

        @Nullable
        @Override
        public Set<Key<?>> getTriggerKeys() {
            return makeSetOf(triggerKey);
        }

        @Override
        public boolean respectUserEdits() {
            return respectsUserEdits.get();
        }

        @Override
        public Boolean deriveValue(ScopedStateStore state, Key changedKey, @Nullable Boolean currentValue) {
            String trigger = state.get(triggerKey);
            if (trigger == null) {
                return null;
            } else {
                return Boolean.parseBoolean(trigger);
            }
        }
    });
    myState.put(triggerKey, "Not A Value");
    // The deriver does not fire because user edits are respected
    assertEquals(true, checkBox.isSelected());
    respectsUserEdits.set(false);
    myState.put(triggerKey, "false");
    // The deriver fires because user edits are not respected
    assertEquals(false, checkBox.isSelected());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nullable(org.jetbrains.annotations.Nullable) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Example 8 with Key

use of com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key in project android by JetBrains.

the class ScopedDataBinderTest method testRegisterTextField.

public void testRegisterTextField() throws Exception {
    Key<String> textKey = myState.createKey("textField", String.class);
    Key<String> textKey2 = myState.createKey("boundSecond", String.class);
    final Key<String> triggerKey = myState.createKey("triggerKey", String.class);
    JTextField textField = new JTextField();
    myScopedDataBinder.register(textKey, textField);
    myScopedDataBinder.register(textKey2, textField);
    // Test binding UI -> Store
    textField.setText("Hello World!");
    assertEquals("Hello World!", myState.get(textKey));
    assertEquals("Hello World!", myState.get(textKey2));
    // Test binding Store -> UI
    myState.put(textKey, "Awesome");
    assertEquals("Awesome", textField.getText());
    assertEquals("Awesome", myState.get(textKey2));
    myState.put(textKey2, "Goodbye");
    assertEquals("Goodbye", textField.getText());
    assertEquals("Goodbye", myState.get(textKey));
    final AtomicBoolean respectsUserEdits = new AtomicBoolean(true);
    // Test value derivation
    myScopedDataBinder.registerValueDeriver(textKey, new ValueDeriver<String>() {

        @Nullable
        @Override
        public Set<Key<?>> getTriggerKeys() {
            return makeSetOf(triggerKey);
        }

        @Override
        public boolean respectUserEdits() {
            return respectsUserEdits.get();
        }

        @NotNull
        @Override
        public String deriveValue(ScopedStateStore state, Key changedKey, @Nullable String currentValue) {
            String trigger = state.get(triggerKey);
            if (trigger == null) {
                return "UNEXPECTED NULL!";
            } else {
                return trigger.toUpperCase();
            }
        }
    });
    myState.put(triggerKey, "Some value to trigger update");
    // The deriver does not fire because user edits are respected
    assertEquals("Goodbye", textField.getText());
    respectsUserEdits.set(false);
    myState.put(triggerKey, "the quick brown fox");
    // The deriver fires because user edits are not respected
    assertEquals("THE QUICK BROWN FOX", textField.getText());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Set(java.util.Set) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Example 9 with Key

use of com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key in project android by JetBrains.

the class ScopedDataBinderTest method testRegisterSlider.

public void testRegisterSlider() throws Exception {
    Key<Integer> integerKey = myState.createKey("slider", Integer.class);
    Key<Integer> integerKey2 = myState.createKey("boundSecond", Integer.class);
    final Key<String> triggerKey = myState.createKey("triggerKey", String.class);
    JSlider slider = new JSlider();
    myScopedDataBinder.register(integerKey, slider);
    myScopedDataBinder.register(integerKey2, slider);
    // Test binding UI -> Store
    slider.setValue(25);
    assertEquals(Integer.valueOf(25), myState.get(integerKey));
    assertEquals(Integer.valueOf(25), myState.get(integerKey2));
    // Test binding Store -> UI
    myState.put(integerKey, 75);
    assertEquals(75, slider.getValue());
    assertEquals(Integer.valueOf(75), myState.get(integerKey2));
    myState.put(integerKey2, 33);
    assertEquals(33, slider.getValue());
    assertEquals(Integer.valueOf(33), myState.get(integerKey));
    final AtomicBoolean respectsUserEdits = new AtomicBoolean(true);
    // Test value derivation
    myScopedDataBinder.registerValueDeriver(integerKey, new ValueDeriver<Integer>() {

        @Nullable
        @Override
        public Set<Key<?>> getTriggerKeys() {
            return makeSetOf(triggerKey);
        }

        @Override
        public boolean respectUserEdits() {
            return respectsUserEdits.get();
        }

        @Override
        public Integer deriveValue(ScopedStateStore state, Key changedKey, @Nullable Integer currentValue) {
            String trigger = state.get(triggerKey);
            if (trigger == null) {
                return null;
            } else {
                return Integer.parseInt(trigger);
            }
        }
    });
    myState.put(triggerKey, "24");
    // The deriver does not fire because user edits are respected
    assertEquals(33, slider.getValue());
    respectsUserEdits.set(false);
    myState.put(triggerKey, "42");
    // The deriver fires because user edits are not respected
    assertEquals(42, slider.getValue());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Set(java.util.Set) Nullable(org.jetbrains.annotations.Nullable) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Aggregations

Key (com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)9 Nullable (org.jetbrains.annotations.Nullable)7 Set (java.util.Set)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ScopedStateStore.createKey (com.android.tools.idea.wizard.dynamic.ScopedStateStore.createKey)2 NotNull (org.jetbrains.annotations.NotNull)2 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)1 ColorPanel (com.intellij.ui.ColorPanel)1