Search in sources :

Example 1 with BuildFileKey

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

the class NamedObjectPanel method init.

@Override
public void init() {
    super.init();
    myPanelGroup.myPanes.add(myDetailsPane);
    if (myGradleBuildFile == null) {
        return;
    }
    List<NamedObject> namedObjects = (List<NamedObject>) myGradleBuildFile.getValue(myBuildFileKey);
    if (namedObjects != null) {
        for (NamedObject object : namedObjects) {
            // consistent between the case where you've customized it and you haven't.
            if (myBuildFileKey == BuildFileKey.BUILD_TYPES && HARDCODED_BUILD_TYPES.contains(object.getName())) {
                object = new UndeletableNamedObject(object);
            }
            addElement(object);
        }
    }
    // If this is a flavor panel, add a synthetic flavor entry for defaultConfig.
    if (myBuildFileKey == BuildFileKey.FLAVORS) {
        GrStatementOwner defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
        NamedObject obj = new UndeletableNamedObject(DEFAULT_CONFIG);
        if (defaultConfig != null) {
            for (BuildFileKey key : DEFAULT_CONFIG_KEYS) {
                Object value = myGradleBuildFile.getValue(defaultConfig, key);
                obj.setValue(key, value);
            }
        }
        addElement(obj);
    }
    NamedObject.Factory objectFactory = (NamedObject.Factory) myBuildFileKey.getValueFactory();
    if (objectFactory == null) {
        throw new IllegalArgumentException("Can't instantiate a NamedObjectPanel for BuildFileKey " + myBuildFileKey.toString());
    }
    Collection<BuildFileKey> properties = objectFactory.getProperties();
    // Query the model for its view of the world, and merge it with the build file-based view.
    for (NamedObject obj : getObjectsFromModel(properties)) {
        boolean found = false;
        for (NamedObject o : myListModel) {
            if (o.getName().equals(obj.getName())) {
                found = true;
            }
        }
        if (!found) {
            NamedObject namedObject = new UndeletableNamedObject(obj.getName());
            addElement(namedObject);
            // Keep track of objects that are only in the model and not in the build file. We want to avoid creating them in the build file
            // unless some value in them is changed to non-default.
            myModelOnlyObjects.add(namedObject);
        }
        myModelObjects.put(obj.getName(), obj.getValues());
    }
    myList.updateUI();
    myDetailsPane.init(myGradleBuildFile, properties);
    if (myListModel.getSize() > 0) {
        myList.setSelectedIndex(0);
    }
    updateUiFromCurrentObject();
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            updatePanelGroup();
        }
    }, ModalityState.any());
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) ValueFactory(com.android.tools.idea.gradle.parser.ValueFactory) JBList(com.intellij.ui.components.JBList) SortedList(com.intellij.util.containers.SortedList) List(java.util.List) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Example 2 with BuildFileKey

use of com.android.tools.idea.gradle.parser.BuildFileKey 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)

Example 3 with BuildFileKey

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

the class KeyValuePane method getReferencePrefix.

@NotNull
private static String getReferencePrefix(@NotNull BuildFileKey key) {
    BuildFileKey containerType = key.getContainerType();
    if (containerType != null) {
        String path = containerType.getPath();
        String lastLeaf = path.substring(path.lastIndexOf('/') + 1);
        return lastLeaf + ".";
    } else {
        return "";
    }
}
Also used : AndroidTargetHash.getAddonHashString(com.android.sdklib.AndroidTargetHash.getAddonHashString) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with BuildFileKey

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

the class NamedObjectPanel method apply.

@Override
public void apply() {
    if ((!myModified && myModifiedKeys.isEmpty()) || myGradleBuildFile == null) {
        return;
    }
    List<NamedObject> objects = Lists.newArrayList();
    for (NamedObject obj : myListModel) {
        // Save the defaultConfig separately and don't write it out as a regular flavor.
        if (myBuildFileKey == BuildFileKey.FLAVORS && obj.getName().equals(DEFAULT_CONFIG)) {
            GrStatementOwner defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
            if (defaultConfig == null) {
                myGradleBuildFile.setValue(BuildFileKey.DEFAULT_CONFIG, "{}");
                defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
            }
            assert defaultConfig != null;
            for (BuildFileKey key : DEFAULT_CONFIG_KEYS) {
                if (!isModified(obj, key)) {
                    continue;
                }
                Object value = obj.getValue(key);
                if (value != null) {
                    myGradleBuildFile.setValue(defaultConfig, key, value);
                } else {
                    myGradleBuildFile.removeValue(defaultConfig, key);
                }
            }
        } else if (myModelOnlyObjects.contains(obj) && isObjectEmpty(obj)) {
            // If this object wasn't in the build file to begin with and doesn't have non-default values, don't write it out.
            continue;
        } else {
            objects.add(obj);
        }
    }
    myGradleBuildFile.setValue(myBuildFileKey, objects, new ValueFactory.KeyFilter() {

        @Override
        public boolean shouldWriteKey(BuildFileKey key, Object object) {
            return isModified((NamedObject) object, key);
        }
    });
    myModified = false;
    myModifiedKeys.clear();
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) ValueFactory(com.android.tools.idea.gradle.parser.ValueFactory) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Example 5 with BuildFileKey

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

the class KeyValuePane method updateReferenceValues.

public void updateReferenceValues(@NotNull BuildFileKey containerProperty, @NotNull Iterable<String> values) {
    BuildFileKey itemType = containerProperty.getItemType();
    if (itemType == null) {
        return;
    }
    ComboBox comboBox = (ComboBox) myProperties.get(itemType);
    if (comboBox == null) {
        return;
    }
    myIsUpdating = true;
    try {
        String currentValue = comboBox.getEditor().getItem().toString();
        comboBox.removeAllItems();
        for (String value : values) {
            comboBox.addItem(value);
        }
        comboBox.setSelectedItem(currentValue);
    } finally {
        myIsUpdating = false;
    }
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) AndroidTargetHash.getAddonHashString(com.android.sdklib.AndroidTargetHash.getAddonHashString) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Aggregations

BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)9 AndroidTargetHash.getAddonHashString (com.android.sdklib.AndroidTargetHash.getAddonHashString)5 ComboBox (com.intellij.openapi.ui.ComboBox)4 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)3 JBTextField (com.intellij.ui.components.JBTextField)3 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)2 ValueFactory (com.android.tools.idea.gradle.parser.ValueFactory)2 GradleWrapper (com.android.tools.idea.gradle.util.GradleWrapper)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 BuildFileKeyType (com.android.tools.idea.gradle.parser.BuildFileKeyType)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)1 TextBrowseFolderListener (com.intellij.openapi.ui.TextBrowseFolderListener)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 JBLabel (com.intellij.ui.components.JBLabel)1 JBList (com.intellij.ui.components.JBList)1 GridConstraints (com.intellij.uiDesigner.core.GridConstraints)1 GridLayoutManager (com.intellij.uiDesigner.core.GridLayoutManager)1 SortedList (com.intellij.util.containers.SortedList)1