Search in sources :

Example 26 with ComboBox

use of com.intellij.openapi.ui.ComboBox in project android by JetBrains.

the class KeyValuePane method createComboBox.

private ComboBox createComboBox(boolean editable) {
    ComboBox comboBox = new ComboBox();
    comboBox.addItemListener(this);
    comboBox.setEditor(new FixedComboBoxEditor());
    comboBox.setEditable(true);
    // Default is only 20 chars
    comboBox.setMinLength(60);
    JBTextField editorComponent = (JBTextField) comboBox.getEditor().getEditorComponent();
    editorComponent.setEditable(editable);
    editorComponent.getDocument().addDocumentListener(this);
    return comboBox;
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) JBTextField(com.intellij.ui.components.JBTextField) FixedComboBoxEditor(com.intellij.openapi.ui.FixedComboBoxEditor)

Example 27 with ComboBox

use of com.intellij.openapi.ui.ComboBox 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 28 with ComboBox

use of com.intellij.openapi.ui.ComboBox in project intellij-community by JetBrains.

the class TargetOptionsComponent method createTargetOptionsCombo.

private static ComboBox createTargetOptionsCombo() {
    final ComboBox combo = new ComboBox(new TargetLevelComboboxModel());
    //combo.setRenderer(new DefaultListCellRenderer() {
    //  @Override
    //  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    //    try {
    //      return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    //    }
    //    finally {
    //      //if ("".equals(value)) {
    //      //  setText(COMPILER_DEFAULT);
    //      //}
    //    }
    //  }
    //});
    combo.setEditable(true);
    combo.setEditor(new BasicComboBoxEditor() {

        @Override
        protected JTextField createEditorComponent() {
            HintTextField editor = new HintTextField(COMPILER_DEFAULT, 12);
            editor.setBorder(null);
            return editor;
        }
    });
    return combo;
}
Also used : BasicComboBoxEditor(javax.swing.plaf.basic.BasicComboBoxEditor) ComboBox(com.intellij.openapi.ui.ComboBox)

Example 29 with ComboBox

use of com.intellij.openapi.ui.ComboBox 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)

Example 30 with ComboBox

use of com.intellij.openapi.ui.ComboBox 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)

Aggregations

ComboBox (com.intellij.openapi.ui.ComboBox)46 Nullable (org.jetbrains.annotations.Nullable)12 ActionListener (java.awt.event.ActionListener)11 ActionEvent (java.awt.event.ActionEvent)10 JBLabel (com.intellij.ui.components.JBLabel)7 NotNull (org.jetbrains.annotations.NotNull)7 JBTextField (com.intellij.ui.components.JBTextField)6 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)5 AndroidTargetHash.getAddonHashString (com.android.sdklib.AndroidTargetHash.getAddonHashString)4 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)4 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)4 Project (com.intellij.openapi.project.Project)4 javax.swing (javax.swing)4 Module (com.intellij.openapi.module.Module)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 JBCheckBox (com.intellij.ui.components.JBCheckBox)3 java.awt (java.awt)3 ItemEvent (java.awt.event.ItemEvent)3 ItemListener (java.awt.event.ItemListener)3 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)2