Search in sources :

Example 11 with CollectionComboBoxModel

use of com.intellij.ui.CollectionComboBoxModel in project android by JetBrains.

the class ServicePanelBuilder method addComboBox.

public JComboBox addComboBox(@NotNull final ObservableList<String> backingList) {
    final CollectionComboBoxModel<String> model = new CollectionComboBoxModel<String>(backingList) {

        @NotNull
        @Override
        public List<String> getItems() {
            return backingList;
        }
    };
    final ComboBox comboBox = new ComboBox(model);
    InvalidationListener onListModified = new InvalidationListener() {

        @Override
        public void onInvalidated(@NotNull ObservableValue<?> sender) {
            model.update();
            if (backingList.size() > 0 && comboBox.getSelectedIndex() < 0) {
                comboBox.setSelectedIndex(0);
            }
        }
    };
    addComponent(comboBox);
    backingList.addWeakListener(onListModified);
    // Keep weak listener alive as long as the combobox is alive
    comboBox.putClientProperty("onListModified", onListModified);
    return comboBox;
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) ObservableValue(com.android.tools.idea.ui.properties.ObservableValue) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) InvalidationListener(com.android.tools.idea.ui.properties.InvalidationListener) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with CollectionComboBoxModel

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

the class ExternalToolsCheckinHandlerFactory method createHandler.

@NotNull
@Override
public CheckinHandler createHandler(@NotNull final CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
    final ToolsProjectConfig config = ToolsProjectConfig.getInstance(panel.getProject());
    return new CheckinHandler() {

        @Override
        public RefreshableOnComponent getAfterCheckinConfigurationPanel(Disposable parentDisposable) {
            final JLabel label = new JLabel(ToolsBundle.message("tools.after.commit.description"));
            ComboboxWithBrowseButton listComponent = new ComboboxWithBrowseButton();
            final JComboBox comboBox = listComponent.getComboBox();
            comboBox.setModel(new CollectionComboBoxModel(getComboBoxElements(), null));
            comboBox.setRenderer(new ListCellRendererWrapper<Object>() {

                @Override
                public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
                    if (value instanceof ToolsGroup) {
                        setText(StringUtil.notNullize(((ToolsGroup) value).getName(), ToolsBundle.message("tools.unnamed.group")));
                    } else if (value instanceof Tool) {
                        setText("  " + StringUtil.notNullize(((Tool) value).getName()));
                    } else {
                        setText(ToolsBundle.message("tools.list.item.none"));
                    }
                }
            });
            listComponent.getButton().addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    final Object item = comboBox.getSelectedItem();
                    String id = null;
                    if (item instanceof Tool) {
                        id = ((Tool) item).getActionId();
                    }
                    final ToolSelectDialog dialog = new ToolSelectDialog(panel.getProject(), id, new ToolsPanel());
                    if (!dialog.showAndGet()) {
                        return;
                    }
                    comboBox.setModel(new CollectionComboBoxModel(getComboBoxElements(), dialog.getSelectedTool()));
                }
            });
            BorderLayout layout = new BorderLayout();
            layout.setVgap(3);
            final JPanel panel = new JPanel(layout);
            panel.add(label, BorderLayout.NORTH);
            panel.add(listComponent, BorderLayout.CENTER);
            listComponent.setBorder(BorderFactory.createEmptyBorder(0, 0, 3, 0));
            if (comboBox.getItemCount() == 0 || (comboBox.getItemCount() == 1 && comboBox.getItemAt(0) == NONE_TOOL)) {
                return null;
            }
            return new RefreshableOnComponent() {

                @Override
                public JComponent getComponent() {
                    return panel;
                }

                @Override
                public void refresh() {
                    String id = config.getAfterCommitToolsId();
                    if (id == null) {
                        comboBox.setSelectedIndex(-1);
                    } else {
                        for (int i = 0; i < comboBox.getItemCount(); i++) {
                            final Object itemAt = comboBox.getItemAt(i);
                            if (itemAt instanceof Tool && id.equals(((Tool) itemAt).getActionId())) {
                                comboBox.setSelectedIndex(i);
                                return;
                            }
                        }
                    }
                }

                @Override
                public void saveState() {
                    Object item = comboBox.getSelectedItem();
                    config.setAfterCommitToolId(item instanceof Tool ? ((Tool) item).getActionId() : null);
                }

                @Override
                public void restoreState() {
                    refresh();
                }
            };
        }

        @Override
        public void checkinSuccessful() {
            final String id = config.getAfterCommitToolsId();
            if (id == null) {
                return;
            }
            DataManager.getInstance().getDataContextFromFocus().doWhenDone(new Consumer<DataContext>() {

                @Override
                public void consume(final DataContext context) {
                    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

                        @Override
                        public void run() {
                            ToolAction.runTool(id, context);
                        }
                    });
                }
            });
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) DataContext(com.intellij.openapi.actionSystem.DataContext) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) Disposable(com.intellij.openapi.Disposable) CheckinHandler(com.intellij.openapi.vcs.checkin.CheckinHandler) ActionListener(java.awt.event.ActionListener) ComboboxWithBrowseButton(com.intellij.ui.ComboboxWithBrowseButton) RefreshableOnComponent(com.intellij.openapi.vcs.ui.RefreshableOnComponent) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with CollectionComboBoxModel

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

the class ReadOnlyStatusDialog method initFileList.

private void initFileList() {
    //noinspection unchecked
    myFileList.setModel(new AbstractListModel() {

        public int getSize() {
            return myFiles.length;
        }

        public Object getElementAt(final int index) {
            return myFiles[index].getFile();
        }
    });
    boolean hasVcs = false;
    for (FileInfo info : myFiles) {
        if (info.hasVersionControl()) {
            hasVcs = true;
            HandleType handleType = info.getSelectedHandleType();
            List<String> changelists = handleType.getChangelists();
            final String defaultChangelist = handleType.getDefaultChangelist();
            //noinspection unchecked
            myChangelist.setModel(new CollectionComboBoxModel(changelists, defaultChangelist));
            //noinspection unchecked
            myChangelist.setRenderer(new ColoredListCellRendererWrapper<String>() {

                @Override
                protected void doCustomize(JList list, String value, int index, boolean selected, boolean hasFocus) {
                    if (value == null)
                        return;
                    String trimmed = StringUtil.first(value, 50, true);
                    if (value.equals(defaultChangelist)) {
                        append(trimmed, selected ? SELECTED_BOLD_ATTRIBUTES : BOLD_ATTRIBUTES);
                    } else {
                        append(trimmed, selected ? SimpleTextAttributes.SELECTED_SIMPLE_CELL_ATTRIBUTES : SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES);
                    }
                }
            });
            break;
        }
    }
    myUsingVcsRadioButton.setEnabled(hasVcs);
}
Also used : CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel)

Example 14 with CollectionComboBoxModel

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

the class RepositoryLibraryPropertiesEditor method initVersionKindSelector.

private void initVersionKindSelector() {
    List<String> versionKinds = Arrays.asList(ProjectBundle.message("maven.version.kind.selector.release"), ProjectBundle.message("maven.version.kind.selector.latest"), ProjectBundle.message("maven.version.kind.selector.select"));
    CollectionComboBoxModel<String> versionKindSelectorModel = new CollectionComboBoxModel<>(versionKinds);
    //noinspection unchecked
    versionKindSelector.setModel(versionKindSelectorModel);
    versionKindSelector.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            VersionKind newVersionKind = getSelectedVersionKind();
            if (newVersionKind != versionKind) {
                versionKind = newVersionKind;
                versionKindChanged();
            }
        }
    });
    setSelectedVersionKind(getVersionKind(model.getVersion()));
}
Also used : ItemEvent(java.awt.event.ItemEvent) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) ItemListener(java.awt.event.ItemListener)

Example 15 with CollectionComboBoxModel

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

the class RepositoryAttachDialog method updateComboboxSelection.

private void updateComboboxSelection(boolean force) {
    final String prevFilter = myFilterString;
    final JTextComponent field = (JTextComponent) myCombobox.getEditor().getEditorComponent();
    final int caret = field.getCaretPosition();
    myFilterString = field.getText();
    if (!force && Comparing.equal(myFilterString, prevFilter))
        return;
    int prevSize = myShownItems.size();
    myShownItems.clear();
    myInUpdate = true;
    final boolean itemSelected = myCoordinates.containsKey(myFilterString) && Comparing.strEqual((String) myCombobox.getSelectedItem(), myFilterString, false);
    final boolean filtered;
    if (itemSelected) {
        myShownItems.addAll(myCoordinates.keySet());
        filtered = false;
    } else {
        final String[] parts = myFilterString.split(" ");
        main: for (String coordinate : myCoordinates.keySet()) {
            for (String part : parts) {
                if (!StringUtil.containsIgnoreCase(coordinate, part))
                    continue main;
            }
            myShownItems.add(coordinate);
        }
        filtered = !myShownItems.isEmpty();
        if (!filtered) {
            myShownItems.addAll(myCoordinates.keySet());
        }
        myCombobox.setSelectedItem(null);
    }
    // use maven version sorter
    ArrayList<Comparable> comparables = new ArrayList<>(myShownItems.size());
    for (String item : myShownItems) {
        comparables.add(new MavenVersionComparable(item));
    }
    Collections.sort(comparables);
    myShownItems.clear();
    for (Comparable comparable : comparables) {
        myShownItems.add(comparable.toString());
    }
    ((CollectionComboBoxModel) myCombobox.getModel()).update();
    myInUpdate = false;
    field.setText(myFilterString);
    field.setCaretPosition(caret);
    updateInfoLabel();
    if (filtered) {
        if (prevSize < 10 && myShownItems.size() > prevSize && myCombobox.isPopupVisible()) {
            myCombobox.setPopupVisible(false);
        }
        if (!myCombobox.isPopupVisible()) {
            myCombobox.setPopupVisible(true);
        }
    }
}
Also used : MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) JTextComponent(javax.swing.text.JTextComponent)

Aggregations

CollectionComboBoxModel (com.intellij.ui.CollectionComboBoxModel)16 ArrayList (java.util.ArrayList)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ItemEvent (java.awt.event.ItemEvent)2 ItemListener (java.awt.event.ItemListener)2 NotNull (org.jetbrains.annotations.NotNull)2 InvalidationListener (com.android.tools.idea.ui.properties.InvalidationListener)1 ObservableValue (com.android.tools.idea.ui.properties.ObservableValue)1 CommonProgramParametersPanel (com.intellij.execution.ui.CommonProgramParametersPanel)1 Artifact (com.intellij.facet.frameworks.beans.Artifact)1 Disposable (com.intellij.openapi.Disposable)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 ModalityState (com.intellij.openapi.application.ModalityState)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 MultiLineLabelUI (com.intellij.openapi.ui.MultiLineLabelUI)1 CheckinHandler (com.intellij.openapi.vcs.checkin.CheckinHandler)1 RefreshableOnComponent (com.intellij.openapi.vcs.ui.RefreshableOnComponent)1