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);
}
}
}
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());
}
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());
}
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());
}
Aggregations