Search in sources :

Example 1 with BuildFileKeyType

use of com.android.tools.idea.gradle.parser.BuildFileKeyType in project android by JetBrains.

the class KeyValuePane method updateCurrentObjectFromUi.

/**
   * Reads the state of the UI form objects and writes them into the currently selected object in the list, setting the dirty bit as
   * appropriate.
   */
private void updateCurrentObjectFromUi() {
    if (myIsUpdating || myCurrentBuildFileObject == null) {
        return;
    }
    for (Map.Entry<BuildFileKey, JComponent> entry : myProperties.entrySet()) {
        BuildFileKey key = entry.getKey();
        JComponent component = entry.getValue();
        Object currentValue = myCurrentBuildFileObject.get(key);
        Object newValue;
        BuildFileKeyType type = key.getType();
        switch(type) {
            case BOOLEAN:
                {
                    ComboBox comboBox = (ComboBox) component;
                    JBTextField editorComponent = (JBTextField) comboBox.getEditor().getEditorComponent();
                    int index = comboBox.getSelectedIndex();
                    if (index == 2) {
                        newValue = Boolean.FALSE;
                        editorComponent.setForeground(JBColor.BLACK);
                    } else if (index == 1) {
                        newValue = Boolean.TRUE;
                        editorComponent.setForeground(JBColor.BLACK);
                    } else {
                        newValue = null;
                        editorComponent.setForeground(JBColor.GRAY);
                    }
                    break;
                }
            case FILE:
            case FILE_AS_STRING:
                {
                    newValue = ((TextFieldWithBrowseButton) component).getText();
                    if ("".equals(newValue)) {
                        newValue = null;
                    }
                    if (newValue != null) {
                        newValue = new File(newValue.toString());
                    }
                    break;
                }
            case INTEGER:
                {
                    try {
                        if (hasKnownValues(key)) {
                            String newStringValue = ((ComboBox) component).getEditor().getItem().toString();
                            newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
                            newValue = Integer.valueOf(newStringValue);
                        } else {
                            newValue = Integer.valueOf(((JBTextField) component).getText());
                        }
                    } catch (Exception e) {
                        newValue = null;
                    }
                    break;
                }
            case REFERENCE:
                {
                    newValue = ((ComboBox) component).getEditor().getItem();
                    String newStringValue = (String) newValue;
                    if (hasKnownValues(key)) {
                        newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
                    }
                    if (newStringValue != null && newStringValue.isEmpty()) {
                        newStringValue = null;
                    }
                    String prefix = getReferencePrefix(key);
                    if (newStringValue != null && !newStringValue.startsWith(prefix)) {
                        newStringValue = prefix + newStringValue;
                    }
                    newValue = newStringValue;
                    break;
                }
            case CLOSURE:
            case STRING:
            default:
                {
                    if (hasKnownValues(key)) {
                        String newStringValue = ((ComboBox) component).getEditor().getItem().toString();
                        newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
                        if (newStringValue.isEmpty()) {
                            newStringValue = null;
                        }
                        newValue = newStringValue;
                    } else {
                        newValue = ((JBTextField) component).getText();
                        if ("".equals(newValue)) {
                            newValue = null;
                        }
                    }
                    if (type == BuildFileKeyType.CLOSURE && newValue != null) {
                        List newListValue = new ArrayList();
                        for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split((String) newValue)) {
                            newListValue.add(key.getValueFactory().parse(s, myProject));
                        }
                        newValue = newListValue;
                    }
                    break;
                }
        }
        if (!Objects.equal(currentValue, newValue)) {
            if (newValue == null) {
                myCurrentBuildFileObject.remove(key);
            } else {
                myCurrentBuildFileObject.put(key, newValue);
            }
            if (GradleBuildFile.shouldWriteValue(currentValue, newValue)) {
                myListener.modified(key);
            }
        }
    }
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) JBTextField(com.intellij.ui.components.JBTextField) AndroidTargetHash.getAddonHashString(com.android.sdklib.AndroidTargetHash.getAddonHashString) BuildFileKeyType(com.android.tools.idea.gradle.parser.BuildFileKeyType) TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Aggregations

AndroidTargetHash.getAddonHashString (com.android.sdklib.AndroidTargetHash.getAddonHashString)1 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)1 BuildFileKeyType (com.android.tools.idea.gradle.parser.BuildFileKeyType)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)1 JBTextField (com.intellij.ui.components.JBTextField)1 File (java.io.File)1