Search in sources :

Example 1 with Key

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

the class TemplateParameterStep2 method setSelectedTemplate.

private void setSelectedTemplate(@Nullable TemplateEntry template) {
    if (template == null || Objects.equal(myCurrentTemplate, template)) {
        return;
    }
    myCurrentTemplate = template;
    TemplateMetadata metadata = myCurrentTemplate.getMetadata();
    myTemplateIcon.setText(myCurrentTemplate.getTitle());
    String string = ImportUIUtil.makeHtmlString(metadata.getDescription());
    myTemplateDescription.setText(string);
    myParameterDefaultValues.clear();
    for (Key addedState : myAddedState) {
        myState.remove(addedState);
    }
    Set<Key> initialState = myState.getAllKeys();
    updateStateWithDefaults(myCurrentTemplate.getParameters());
    myAddedState = Sets.difference(myState.getAllKeys(), initialState);
    for (Component component : myTemplateParameters.getComponents()) {
        myTemplateParameters.remove(component);
        if (component instanceof JComponent) {
            deregister((JComponent) component);
        }
    }
    Set<Parameter> parameters = ImmutableSet.copyOf(filterNonUIParameters(myCurrentTemplate));
    int lastRow = addParameterComponents(parameters.size() + 1, parameters);
    addSourceSetControls(lastRow);
}
Also used : ScopedStateStore.createKey(com.android.tools.idea.wizard.dynamic.ScopedStateStore.createKey) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Example 2 with Key

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

the class IconStep method computeResourceName.

@Nullable
private String computeResourceName() {
    String resourceName = null;
    TemplateEntry templateEntry = myState.get(myTemplateKey);
    String nameExpression;
    if (templateEntry != null) {
        nameExpression = templateEntry.getMetadata().getIconName();
        if (!StringUtil.isEmpty(nameExpression)) {
            Set<Key> allKeys = myState.getAllKeys();
            Map<String, Object> parameters = Maps.newHashMapWithExpectedSize(allKeys.size());
            for (Key key : allKeys) {
                parameters.put(key.name, myState.get(key));
            }
            resourceName = myStringEvaluator.evaluate(nameExpression, parameters);
        }
    }
    return resourceName;
}
Also used : ScopedStateStore.createKey(com.android.tools.idea.wizard.dynamic.ScopedStateStore.createKey) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Key

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

the class ScopedDataBinderTest method testRegisterColorPanel.

public void testRegisterColorPanel() throws Exception {
    Key<Color> colorKey = myState.createKey("colorPanel", Color.class);
    Key<Color> colorKey2 = myState.createKey("boundSecond", Color.class);
    final Key<String> triggerKey = myState.createKey("triggerKey", String.class);
    ColorPanel colorPanel = new ColorPanel();
    myScopedDataBinder.register(colorKey, colorPanel);
    myScopedDataBinder.register(colorKey2, colorPanel);
    // Test binding UI -> Store
    colorPanel.setSelectedColor(Color.BLUE);
    // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here
    myScopedDataBinder.saveState(colorPanel);
    assertEquals(Color.BLUE, myState.get(colorKey));
    assertEquals(Color.BLUE, myState.get(colorKey2));
    // Test binding Store -> UI
    myState.put(colorKey, Color.RED);
    // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here
    myScopedDataBinder.saveState(colorPanel);
    assertEquals(Color.RED, colorPanel.getSelectedColor());
    assertEquals(Color.RED, myState.get(colorKey2));
    myState.put(colorKey2, Color.GREEN);
    // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here
    myScopedDataBinder.saveState(colorPanel);
    assertEquals(Color.GREEN, colorPanel.getSelectedColor());
    assertEquals(Color.GREEN, myState.get(colorKey));
    final AtomicBoolean respectsUserEdits = new AtomicBoolean(true);
    // Test value derivation
    myScopedDataBinder.registerValueDeriver(colorKey, new ValueDeriver<Color>() {

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

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

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

Example 4 with Key

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

the class ScopedDataBinderTest method testRegisterTextFieldWithBrowseButton.

public void testRegisterTextFieldWithBrowseButton() 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);
    TextFieldWithBrowseButton textField = new TextFieldWithBrowseButton();
    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) TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) Set(java.util.Set) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) Key(com.android.tools.idea.wizard.dynamic.ScopedStateStore.Key)

Example 5 with Key

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

the class ScopedDataBinderTest method testRegisterCheckbox.

public void testRegisterCheckbox() throws Exception {
    Key<Boolean> booleanKey = myState.createKey("checkBox", 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)

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