Search in sources :

Example 1 with ListSpeedSearch

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

the class ModuleAwareProjectConfigurable method createComponent.

@Override
public JComponent createComponent() {
    if (myProject.isDefault()) {
        T configurable = createDefaultProjectConfigurable();
        if (configurable != null) {
            myModuleConfigurables.put(null, configurable);
            return configurable.createComponent();
        }
    }
    final List<Module> modules = ContainerUtil.filter(ModuleAttachProcessor.getSortedModules(myProject), module -> isSuitableForModule(module));
    final T projectConfigurable = createProjectConfigurable();
    if (modules.size() == 1 && projectConfigurable == null) {
        Module module = modules.get(0);
        final T configurable = createModuleConfigurable(module);
        myModuleConfigurables.put(module, configurable);
        return configurable.createComponent();
    }
    final Splitter splitter = new Splitter(false, 0.25f);
    CollectionListModel<Module> listDataModel = new CollectionListModel<>(modules);
    final JBList moduleList = new JBList(listDataModel);
    new ListSpeedSearch(moduleList, (Function<Object, String>) o -> {
        if (o == null) {
            return getProjectConfigurableItemName();
        } else if (o instanceof Module) {
            return ((Module) o).getName();
        }
        return null;
    });
    moduleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    moduleList.setCellRenderer(new ModuleListCellRenderer() {

        @Override
        public void customize(JList list, Module module, int index, boolean selected, boolean hasFocus) {
            if (module == null) {
                setText(getProjectConfigurableItemName());
                setIcon(getProjectConfigurableItemIcon());
            } else {
                super.customize(list, module, index, selected, hasFocus);
            }
        }
    });
    splitter.setFirstComponent(new JBScrollPane(moduleList));
    final CardLayout layout = new CardLayout();
    final JPanel cardPanel = new JPanel(layout);
    splitter.setSecondComponent(cardPanel);
    if (projectConfigurable != null) {
        myModuleConfigurables.put(null, projectConfigurable);
        final JComponent component = projectConfigurable.createComponent();
        cardPanel.add(component, PROJECT_ITEM_KEY);
        listDataModel.add(0, null);
    }
    for (Module module : modules) {
        final T configurable = createModuleConfigurable(module);
        myModuleConfigurables.put(module, configurable);
        final JComponent component = configurable.createComponent();
        cardPanel.add(component, module.getName());
    }
    moduleList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            final Module value = (Module) moduleList.getSelectedValue();
            layout.show(cardPanel, value == null ? PROJECT_ITEM_KEY : value.getName());
        }
    });
    if (moduleList.getItemsCount() > 0) {
        moduleList.setSelectedIndex(0);
        Module module = listDataModel.getElementAt(0);
        layout.show(cardPanel, module == null ? PROJECT_ITEM_KEY : module.getName());
    }
    return splitter;
}
Also used : ModuleAttachProcessor(com.intellij.platform.ModuleAttachProcessor) AllIcons(com.intellij.icons.AllIcons) HashMap(java.util.HashMap) ContainerUtil(com.intellij.util.containers.ContainerUtil) Nls(org.jetbrains.annotations.Nls) Map(java.util.Map) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Splitter(com.intellij.openapi.ui.Splitter) JBList(com.intellij.ui.components.JBList) UnnamedConfigurable(com.intellij.openapi.options.UnnamedConfigurable) Configurable(com.intellij.openapi.options.Configurable) CollectionListModel(com.intellij.ui.CollectionListModel) JBScrollPane(com.intellij.ui.components.JBScrollPane) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Function(com.intellij.util.Function) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull) ListSelectionListener(javax.swing.event.ListSelectionListener) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) ListSpeedSearch(com.intellij.ui.ListSpeedSearch) javax.swing(javax.swing) Splitter(com.intellij.openapi.ui.Splitter) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ListSelectionListener(javax.swing.event.ListSelectionListener) ListSpeedSearch(com.intellij.ui.ListSpeedSearch) JBList(com.intellij.ui.components.JBList) Module(com.intellij.openapi.module.Module) CollectionListModel(com.intellij.ui.CollectionListModel) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Example 2 with ListSpeedSearch

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

the class TreeGrid method setModel.

private void setModel(@NotNull AbstractTreeStructure model, boolean showSectionHeaders) {
    // using the AbstractTreeStructure instead of the model as the actual TreeModel when used with IJ components
    // works in a very strange way, each time you expand or contract a node it will add or remove all its children.
    Object root = model.getRootElement();
    Object[] sections = model.getChildElements(root);
    mySectionToComponent.clear();
    myLists.clear();
    myHideables.clear();
    removeAll();
    setAutoscrolls(false);
    ListSelectionListener listSelectionListener = e -> {
        if (e.getValueIsAdjusting()) {
            return;
        }
        ListSelectionModel sourceSelectionModel = (ListSelectionModel) e.getSource();
        if (!sourceSelectionModel.isSelectionEmpty()) {
            for (JList<T> aList : myLists) {
                if (sourceSelectionModel != aList.getSelectionModel()) {
                    aList.clearSelection();
                }
            }
        }
    };
    for (Object section : sections) {
        String name = section.toString();
        FilteringListModel<T> listModel = new FilteringListModel<>(new AbstractListModel() {

            @Override
            public int getSize() {
                return model.getChildElements(section).length;
            }

            @Override
            public Object getElementAt(int index) {
                return model.getChildElements(section)[index];
            }
        });
        // Needed as otherwise the filtered list does not show any content.
        listModel.refilter();
        // JBList does not work with HORIZONTAL_WRAP
        //noinspection UndesirableClassUsage,unchecked
        JList<T> list = new JList<>(listModel);
        list.setAutoscrolls(false);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(-1);
        list.getSelectionModel().addListSelectionListener(listSelectionListener);
        // for tests to find the right list
        list.setName(name);
        list.addKeyListener(myKeyListener);
        new ListSpeedSearch(list);
        myLists.add(list);
        if (showSectionHeaders) {
            JPanel panel = new // must be BorderLayout for HideableDecorator to work
            JPanel(// must be BorderLayout for HideableDecorator to work
            new BorderLayout()) {

                @Override
                public Dimension getMaximumSize() {
                    return new Dimension(super.getMaximumSize().width, super.getPreferredSize().height);
                }
            };
            HideableDecorator hidyPanel = new HideableDecorator(panel, name, false);
            myHideables.add(hidyPanel);
            hidyPanel.setContentComponent(list);
            add(panel);
            mySectionToComponent.put(section, panel);
        } else {
            if (getComponentCount() > 0) {
                add(new JSeparator());
            }
            list.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
            add(list);
            mySectionToComponent.put(section, list);
        }
    }
}
Also used : UIUtil(com.intellij.util.ui.UIUtil) KeyListener(java.awt.event.KeyListener) IdentityHashMap(java.util.IdentityHashMap) ModalityState(com.intellij.openapi.application.ModalityState) ContainerUtil(com.intellij.util.containers.ContainerUtil) KeyAdapter(java.awt.event.KeyAdapter) KeyEvent(java.awt.event.KeyEvent) ArrayList(java.util.ArrayList) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Map(java.util.Map) AbstractTreeStructure(com.intellij.ide.util.treeView.AbstractTreeStructure) ApplicationManager(com.intellij.openapi.application.ApplicationManager) FilteringListModel(com.intellij.ui.speedSearch.FilteringListModel) NotNull(org.jetbrains.annotations.NotNull) ListSelectionListener(javax.swing.event.ListSelectionListener) MouseListener(java.awt.event.MouseListener) Condition(com.intellij.openapi.util.Condition) HideableDecorator(com.intellij.ui.HideableDecorator) ListSpeedSearch(com.intellij.ui.ListSpeedSearch) javax.swing(javax.swing) FilteringListModel(com.intellij.ui.speedSearch.FilteringListModel) HideableDecorator(com.intellij.ui.HideableDecorator) ListSelectionListener(javax.swing.event.ListSelectionListener) ListSpeedSearch(com.intellij.ui.ListSpeedSearch)

Aggregations

ListSpeedSearch (com.intellij.ui.ListSpeedSearch)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 java.awt (java.awt)2 List (java.util.List)2 Map (java.util.Map)2 javax.swing (javax.swing)2 ListSelectionListener (javax.swing.event.ListSelectionListener)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 AllIcons (com.intellij.icons.AllIcons)1 AbstractTreeStructure (com.intellij.ide.util.treeView.AbstractTreeStructure)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Module (com.intellij.openapi.module.Module)1 Configurable (com.intellij.openapi.options.Configurable)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 SearchableConfigurable (com.intellij.openapi.options.SearchableConfigurable)1 UnnamedConfigurable (com.intellij.openapi.options.UnnamedConfigurable)1 Project (com.intellij.openapi.project.Project)1 Splitter (com.intellij.openapi.ui.Splitter)1