Search in sources :

Example 1 with CollectionListModel

use of com.intellij.ui.CollectionListModel 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 CollectionListModel

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

the class PsiElementListNavigator method navigateOrCreatePopup.

/**
   * listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
   */
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
    if (targets.length == 0)
        return null;
    if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
        consumer.consume(targets);
        return null;
    }
    final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
    final JBList list = new JBList(model);
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setTransferHandler(new TransferHandler() {

        @Nullable
        @Override
        protected Transferable createTransferable(JComponent c) {
            final Object[] selectedValues = list.getSelectedValues();
            final PsiElement[] copy = new PsiElement[selectedValues.length];
            for (int i = 0; i < selectedValues.length; i++) {
                copy[i] = (PsiElement) selectedValues[i];
            }
            return new PsiCopyPasteManager.MyTransferable(copy);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return COPY;
        }
    });
    list.setCellRenderer(listRenderer);
    list.setFont(EditorUtil.getEditorFont());
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (listRenderer instanceof PsiElementListCellRenderer) {
        ((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
    }
    PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        consumer.consume(selectedElements);
    }).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        if (listUpdaterTask != null) {
            listUpdaterTask.cancelTask();
        }
        return true;
    });
    final Ref<UsageView> usageView = new Ref<>();
    if (findUsagesTitle != null) {
        popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
            final List<NavigatablePsiElement> items = model.getItems();
            usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
            popup.cancel();
            return false;
        });
    }
    final JBPopup popup = popupChooserBuilder.createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (listUpdaterTask != null) {
        listUpdaterTask.init((AbstractPopup) popup, list, usageView);
    }
    return popup;
}
Also used : EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) AbstractPopup(com.intellij.ui.popup.AbstractPopup) CollectionListModel(com.intellij.ui.CollectionListModel) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) ListBackgroundUpdaterTask(com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) List(java.util.List) FindUtil(com.intellij.find.FindUtil) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Consumer(com.intellij.util.Consumer) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List) CollectionListModel(com.intellij.ui.CollectionListModel) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with CollectionListModel

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

the class CodeInsightTestUtil method gotoImplementation.

@NotNull
@TestOnly
public static GotoTargetHandler.GotoData gotoImplementation(Editor editor, PsiFile file) {
    GotoTargetHandler.GotoData data = new GotoImplementationHandler().getSourceAndTargetElements(editor, file);
    if (data.listUpdaterTask != null) {
        JBList list = new JBList();
        CollectionListModel model = new CollectionListModel(new ArrayList());
        list.setModel(model);
        list.setModel(new NameFilteringListModel(list, Function.ID, Condition.FALSE, String::new));
        JBPopup popup = new ComponentPopupBuilderImpl(list, null).createPopup();
        data.listUpdaterTask.init((AbstractPopup) popup, list, new Ref<>());
        data.listUpdaterTask.queue();
        try {
            while (!data.listUpdaterTask.isFinished()) {
                UIUtil.dispatchAllInvocationEvents();
            }
        } finally {
            Disposer.dispose(popup);
        }
    }
    return data;
}
Also used : NameFilteringListModel(com.intellij.ui.speedSearch.NameFilteringListModel) GotoTargetHandler(com.intellij.codeInsight.navigation.GotoTargetHandler) GotoImplementationHandler(com.intellij.codeInsight.navigation.GotoImplementationHandler) ArrayList(java.util.ArrayList) JBList(com.intellij.ui.components.JBList) CollectionListModel(com.intellij.ui.CollectionListModel) JBPopup(com.intellij.openapi.ui.popup.JBPopup) ComponentPopupBuilderImpl(com.intellij.ui.popup.ComponentPopupBuilderImpl) TestOnly(org.jetbrains.annotations.TestOnly) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CollectionListModel

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

the class ProjectTemplateList method restoreSelection.

void restoreSelection() {
    final String templateName = PropertiesComponent.getInstance().getValue(PROJECT_WIZARD_TEMPLATE);
    if (templateName != null && myList.getModel() instanceof CollectionListModel) {
        @SuppressWarnings("unchecked") List<ProjectTemplate> list = ((CollectionListModel<ProjectTemplate>) myList.getModel()).toList();
        ProjectTemplate template = ContainerUtil.find(list, template1 -> templateName.equals(template1.getName()));
        if (template != null) {
            myList.setSelectedValue(template, true);
        }
    }
    myList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            ProjectTemplate template = getSelectedTemplate();
            if (template != null) {
                PropertiesComponent.getInstance().setValue(PROJECT_WIZARD_TEMPLATE, template.getName());
            }
        }
    });
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) CollectionListModel(com.intellij.ui.CollectionListModel) ProjectTemplate(com.intellij.platform.ProjectTemplate) ArchivedProjectTemplate(com.intellij.platform.templates.ArchivedProjectTemplate) ListSelectionListener(javax.swing.event.ListSelectionListener)

Example 5 with CollectionListModel

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

the class SelectPluginsStep method fillPlugins.

public void fillPlugins() {
    Collections.sort(myPlugins, (o1, o2) -> StringUtil.compare(o1.getName(), o2.getName(), true));
    myPluginsList.setModel(new CollectionListModel(myPlugins));
    myPluginsList.setSelectedIndex(0);
}
Also used : CollectionListModel(com.intellij.ui.CollectionListModel)

Aggregations

CollectionListModel (com.intellij.ui.CollectionListModel)6 JBList (com.intellij.ui.components.JBList)4 NotNull (org.jetbrains.annotations.NotNull)3 JBPopup (com.intellij.openapi.ui.popup.JBPopup)2 List (java.util.List)2 javax.swing (javax.swing)2 Nullable (org.jetbrains.annotations.Nullable)2 GotoImplementationHandler (com.intellij.codeInsight.navigation.GotoImplementationHandler)1 GotoTargetHandler (com.intellij.codeInsight.navigation.GotoTargetHandler)1 ListBackgroundUpdaterTask (com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask)1 ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1 FindUtil (com.intellij.find.FindUtil)1 AllIcons (com.intellij.icons.AllIcons)1 PsiCopyPasteManager (com.intellij.ide.PsiCopyPasteManager)1 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)1 Module (com.intellij.openapi.module.Module)1 Configurable (com.intellij.openapi.options.Configurable)1