Search in sources :

Example 6 with BuildFileKey

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

the class KeyValuePane method init.

public void init(GradleBuildFile gradleBuildFile, Collection<BuildFileKey> properties) {
    GridLayoutManager layout = new GridLayoutManager(properties.size() + 1, 2);
    setLayout(layout);
    GridConstraints constraints = new GridConstraints();
    constraints.setAnchor(GridConstraints.ANCHOR_WEST);
    constraints.setVSizePolicy(GridConstraints.SIZEPOLICY_FIXED);
    for (BuildFileKey property : properties) {
        constraints.setColumn(0);
        constraints.setFill(GridConstraints.FILL_NONE);
        constraints.setHSizePolicy(GridConstraints.SIZEPOLICY_FIXED);
        JBLabel label = new JBLabel(property.getDisplayName());
        add(label, constraints);
        constraints.setColumn(1);
        constraints.setFill(GridConstraints.FILL_HORIZONTAL);
        constraints.setHSizePolicy(GridConstraints.SIZEPOLICY_WANT_GROW);
        JComponent component;
        switch(property.getType()) {
            case BOOLEAN:
                {
                    constraints.setFill(GridConstraints.FILL_NONE);
                    ComboBox comboBox = createComboBox(false);
                    comboBox.addItem("");
                    comboBox.addItem("true");
                    comboBox.addItem("false");
                    comboBox.setPrototypeDisplayValue("(false) ");
                    component = comboBox;
                    break;
                }
            case FILE:
            case FILE_AS_STRING:
                {
                    JBTextField textField = new JBTextField();
                    TextFieldWithBrowseButton fileField = new TextFieldWithBrowseButton(textField);
                    FileChooserDescriptor d = new FileChooserDescriptor(true, false, false, true, false, false);
                    d.setShowFileSystemRoots(true);
                    fileField.addBrowseFolderListener(new TextBrowseFolderListener(d));
                    fileField.getTextField().getDocument().addDocumentListener(this);
                    component = fileField;
                    break;
                }
            case REFERENCE:
                {
                    constraints.setFill(GridConstraints.FILL_NONE);
                    ComboBox comboBox = createComboBox(true);
                    if (hasKnownValues(property)) {
                        for (String s : myKeysWithKnownValues.get(property).values()) {
                            comboBox.addItem(s);
                        }
                    }
                    // If there are no hardcoded values, the combo box's values will get populated later when the panel for the container reference
                    // type wakes up and notifies us of its current values.
                    component = comboBox;
                    break;
                }
            case CLOSURE:
            case STRING:
            case INTEGER:
            default:
                {
                    if (hasKnownValues(property)) {
                        constraints.setFill(GridConstraints.FILL_NONE);
                        ComboBox comboBox = createComboBox(true);
                        for (String s : myKeysWithKnownValues.get(property).values()) {
                            comboBox.addItem(s);
                        }
                        component = comboBox;
                    } else {
                        JBTextField textField = new JBTextField();
                        textField.getDocument().addDocumentListener(this);
                        component = textField;
                    }
                    break;
                }
        }
        add(component, constraints);
        label.setLabelFor(component);
        myProperties.put(property, component);
        constraints.setRow(constraints.getRow() + 1);
    }
    constraints.setColumn(0);
    constraints.setVSizePolicy(GridConstraints.FILL_VERTICAL);
    constraints.setHSizePolicy(GridConstraints.SIZEPOLICY_FIXED);
    add(new JBLabel(""), constraints);
    updateUiFromCurrentObject();
}
Also used : TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) JBLabel(com.intellij.ui.components.JBLabel) ComboBox(com.intellij.openapi.ui.ComboBox) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) TextBrowseFolderListener(com.intellij.openapi.ui.TextBrowseFolderListener) JBTextField(com.intellij.ui.components.JBTextField) AndroidTargetHash.getAddonHashString(com.android.sdklib.AndroidTargetHash.getAddonHashString) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Example 7 with BuildFileKey

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

the class KeyValuePane method updateUiFromCurrentObject.

/**
   * Updates the form UI objects to reflect the currently selected object. Clears the objects and disables them if there is no selected
   * object.
   */
public void updateUiFromCurrentObject() {
    myIsUpdating = true;
    for (Map.Entry<BuildFileKey, JComponent> entry : myProperties.entrySet()) {
        BuildFileKey key = entry.getKey();
        JComponent component = entry.getValue();
        Object value = myCurrentBuildFileObject != null ? myCurrentBuildFileObject.get(key) : null;
        final Object modelValue = myCurrentModelObject != null ? myCurrentModelObject.get(key) : null;
        switch(key.getType()) {
            case BOOLEAN:
                {
                    ComboBox comboBox = (ComboBox) component;
                    String text = formatDefaultValue(modelValue);
                    comboBox.removeItemAt(0);
                    comboBox.insertItemAt(text, 0);
                    JBTextField editorComponent = (JBTextField) comboBox.getEditor().getEditorComponent();
                    if (Boolean.FALSE.equals(value)) {
                        comboBox.setSelectedIndex(2);
                        editorComponent.setForeground(JBColor.BLACK);
                    } else if (Boolean.TRUE.equals(value)) {
                        comboBox.setSelectedIndex(1);
                        editorComponent.setForeground(JBColor.BLACK);
                    } else {
                        comboBox.setSelectedIndex(0);
                        editorComponent.setForeground(JBColor.GRAY);
                    }
                    break;
                }
            case FILE:
            case FILE_AS_STRING:
                {
                    TextFieldWithBrowseButton fieldWithButton = (TextFieldWithBrowseButton) component;
                    fieldWithButton.setText(value != null ? value.toString() : "");
                    JBTextField textField = (JBTextField) fieldWithButton.getTextField();
                    textField.getEmptyText().setText(formatDefaultValue(modelValue));
                    break;
                }
            case REFERENCE:
                {
                    String stringValue = (String) value;
                    if (hasKnownValues(key) && stringValue != null) {
                        stringValue = getMappedValue(myKeysWithKnownValues.get(key), stringValue);
                    }
                    String prefix = getReferencePrefix(key);
                    if (stringValue == null) {
                        stringValue = "";
                    } else if (stringValue.startsWith(prefix)) {
                        stringValue = stringValue.substring(prefix.length());
                    }
                    ComboBox comboBox = (ComboBox) component;
                    JBTextField textField = (JBTextField) comboBox.getEditor().getEditorComponent();
                    textField.getEmptyText().setText(formatDefaultValue(modelValue));
                    comboBox.setSelectedItem(stringValue);
                    break;
                }
            case CLOSURE:
                if (value instanceof List) {
                    value = Joiner.on(", ").join((List) value);
                }
            // Fall through to INTEGER/STRING/default case
            case INTEGER:
            case STRING:
            default:
                {
                    if (hasKnownValues(key)) {
                        if (value != null) {
                            value = getMappedValue(myKeysWithKnownValues.get(key), value.toString());
                        }
                        ComboBox comboBox = (ComboBox) component;
                        comboBox.setSelectedItem(value != null ? value.toString() : "");
                        JBTextField textField = (JBTextField) comboBox.getEditor().getEditorComponent();
                        textField.getEmptyText().setText(formatDefaultValue(modelValue));
                    } else {
                        JBTextField textField = (JBTextField) component;
                        textField.setText(value != null ? value.toString() : "");
                        textField.getEmptyText().setText(formatDefaultValue(modelValue));
                    }
                    break;
                }
        }
        component.setEnabled(myCurrentBuildFileObject != null);
    }
    myIsUpdating = false;
}
Also used : TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) ComboBox(com.intellij.openapi.ui.ComboBox) JBTextField(com.intellij.ui.components.JBTextField) AndroidTargetHash.getAddonHashString(com.android.sdklib.AndroidTargetHash.getAddonHashString) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Example 8 with BuildFileKey

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

the class AndroidProjectConfigurable method reset.

@Override
public void reset() {
    myProjectProperties.clear();
    if (myGradleBuildFile == null) {
        return;
    }
    for (BuildFileKey key : PROJECT_PROPERTIES) {
        Object value = myGradleBuildFile.getValue(key);
        if (value != null) {
            myProjectProperties.put(key, value);
        }
    }
    try {
        GradleWrapper gradleWrapper = GradleWrapper.find(myProject);
        if (gradleWrapper != null) {
            String wrapperVersion = gradleWrapper.getGradleVersion();
            myProjectProperties.put(BuildFileKey.GRADLE_WRAPPER_VERSION, wrapperVersion);
        }
    } catch (Exception e) {
        LOG.warn("Error while saving Gradle wrapper properties", e);
    }
    myKeyValuePane.setCurrentBuildFileObject(myProjectProperties);
    myKeyValuePane.updateUiFromCurrentObject();
}
Also used : BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

Example 9 with BuildFileKey

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

the class AndroidProjectConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    if (myGradleBuildFile == null) {
        return;
    }
    VirtualFile file = myGradleBuildFile.getFile();
    if (!ReadonlyStatusHandler.ensureFilesWritable(myProject, file)) {
        throw new ConfigurationException(String.format("Build file %1$s is not writable", file.getPath()));
    }
    CommandProcessor.getInstance().runUndoTransparentAction(() -> {
        try {
            ActionRunner.runInsideWriteAction(() -> {
                for (BuildFileKey key : PROJECT_PROPERTIES) {
                    if (key == BuildFileKey.GRADLE_WRAPPER_VERSION || !myModifiedKeys.contains(key)) {
                        continue;
                    }
                    Object value = myProjectProperties.get(key);
                    if (value != null) {
                        myGradleBuildFile.setValue(key, value);
                    } else {
                        myGradleBuildFile.removeValue(null, key);
                    }
                }
                Object wrapperVersion = myProjectProperties.get(BuildFileKey.GRADLE_WRAPPER_VERSION);
                GradleWrapper gradleWrapper = GradleWrapper.find(myProject);
                if (wrapperVersion != null && gradleWrapper != null) {
                    boolean updated = gradleWrapper.updateDistributionUrlAndDisplayFailure(wrapperVersion.toString());
                    if (updated) {
                        VirtualFile virtualFile = gradleWrapper.getPropertiesFile();
                        if (virtualFile != null) {
                            virtualFile.refresh(false, false);
                        }
                    }
                }
                myModifiedKeys.clear();
            });
        } catch (Exception e) {
            LOG.error("Error while applying changes", e);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConfigurationException(com.intellij.openapi.options.ConfigurationException) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

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