Search in sources :

Example 6 with NamedObject

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

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

the class NamedObjectPanel method addObject.

private void addObject() {
    int num = 1;
    String name = myNewItemName;
    while (getNamedItem(name) != null) {
        name = myNewItemName + num++;
    }
    int index = addElement(new NamedObject(name));
    // Select newly added element.
    myList.getSelectionModel().setSelectionInterval(index, index);
    updateUiFromCurrentObject();
    // Make sure we scroll to selected element
    myList.ensureIndexIsVisible(index);
    myList.updateUI();
    myModified = true;
    updatePanelGroup();
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject)

Example 8 with NamedObject

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

Aggregations

NamedObject (com.android.tools.idea.gradle.parser.NamedObject)8 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 ValueFactory (com.android.tools.idea.gradle.parser.ValueFactory)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 File (java.io.File)2 List (java.util.List)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 SourceFile (com.android.ide.common.blame.SourceFile)1 GradleSettingsFile (com.android.tools.idea.gradle.parser.GradleSettingsFile)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 JBList (com.intellij.ui.components.JBList)1 SortedList (com.intellij.util.containers.SortedList)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1