<commit_before>/*
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.plugin.idea.ui;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.popup.*;
import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.ui.components.JBList;
import com.intellij.util.Function;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

import javax.swing.*;
import java.util.*;

/**
 * Lists all UI commands.
 *
 * @author <NAME>
 */
public class CommandListPopup
{
    // TODO Design and implement CommandListPopup

    private static volatile boolean active;

    private final UIContext uiContext;

    public CommandListPopup(UIContext uiContext)
    {
        this.uiContext = uiContext;
    }

    public static boolean isActive()
    {
        return active;
    }

    public void show()
    {
        if (active)
            return;
        active = true;

        final JBList list = new JBList();
        DefaultListModel model = new DefaultListModel();

        final Map<String, List<UICommand>> categories = categorizeCommands(getAllCandidates());
        final List<Object> elements = categoriesToList(organizeIntoCategories(categories));
        model.setSize(elements.size());

        list.setCellRenderer(new ListCellRendererWrapper<Object>()
        {
            @Override
            public void customize(JList list, Object data, int index,
                                  boolean selected, boolean hasFocus)
            {
                if (data instanceof UICommand)
                {
                    setIcon(AllIcons.Nodes.Plugin);

                    UICommand command = (UICommand) data;
                    UICommandMetadata metadata = command.getMetadata(uiContext);

                    setText(metadata.getName());
                    setToolTipText(metadata.getDescription());
                }
                else if (data instanceof String)
                {
                    String string = (String) data;

                    setText(string);
                    setSeparator();
                }
            }
        });

        for (int i = 0; i < elements.size(); i++)
        {
            model.set(i, elements.get(i));
        }

        list.setModel(model);

        final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
        listPopupBuilder.setTitle("Run a Forge command");
        listPopupBuilder.setResizable(true);
        listPopupBuilder.addListener(new JBPopupAdapter()
        {
            @Override
            public void onClosed(LightweightWindowEvent event)
            {
                CommandListPopup.this.active = false;
            }
        });
        listPopupBuilder.setItemChoosenCallback(new Runnable()
        {
            @Override
            public void run()
            {
                int selectedIndex = list.getSelectedIndex();
                Object selectedObject = elements.get(selectedIndex);
                if (selectedObject instanceof UICommand)
                {
                    UICommand selectedCommand = (UICommand) selectedObject;
                    openWizard(selectedCommand);
                }
            }
        });
        listPopupBuilder.setFilteringEnabled(new Function<Object, String>()
        {
            @Override
            public String fun(Object object)
            {
                if (object instanceof UICommand)
                {
                    UICommand command = (UICommand) object;
                    UICommandMetadata metadata = command.getMetadata(uiContext);

                    return metadata.getCategory().toString() + " " + metadata.getName();
                }
                else if (object instanceof String)<commit_msg>Fix selecting commands from filtered list<commit_after>/*
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.plugin.idea.ui;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.popup.*;
import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.ui.components.JBList;
import com.intellij.util.Function;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

import javax.swing.*;
import java.util.*;

/**
 * Lists all UI commands.
 *
 * @author <NAME>
 */
public class CommandListPopup
{
    // TODO Design and implement CommandListPopup

    private static volatile boolean active;

    private final UIContext uiContext;

    public CommandListPopup(UIContext uiContext)
    {
        this.uiContext = uiContext;
    }

    public static boolean isActive()
    {
        return active;
    }

    public void show()
    {
        if (active)
            return;
        active = true;

        final JBList list = new JBList();
        DefaultListModel model = new DefaultListModel();

        final Map<String, List<UICommand>> categories = categorizeCommands(getAllCandidates());
        final List<Object> elements = categoriesToList(organizeIntoCategories(categories));
        model.setSize(elements.size());

        list.setCellRenderer(new ListCellRendererWrapper<Object>()
        {
            @Override
            public void customize(JList list, Object data, int index,
                                  boolean selected, boolean hasFocus)
            {
                if (data instanceof UICommand)
                {
                    setIcon(AllIcons.Nodes.Plugin);

                    UICommand command = (UICommand) data;
                    UICommandMetadata metadata = command.getMetadata(uiContext);

                    setText(metadata.getName());
                    setToolTipText(metadata.getDescription());
                }
                else if (data instanceof String)
                {
                    String string = (String) data;

                    setText(string);
                    setSeparator();
                }
            }
        });

        for (int i = 0; i < elements.size(); i++)
        {
            model.set(i, elements.get(i));
        }

        list.setModel(model);

        final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
        listPopupBuilder.setTitle("Run a Forge command");
        listPopupBuilder.setResizable(true);
        listPopupBuilder.addListener(new JBPopupAdapter()
        {
            @Override
            public void onClosed(LightweightWindowEvent event)
            {
                CommandListPopup.this.active = false;
            }
        });
        listPopupBuilder.setItemChoosenCallback(new Runnable()
        {
            @Override
            public void run()
            {
                Object selectedObject = list.getSelectedValue();
                if (selectedObject instanceof UICommand)
                {
                    UICommand selectedCommand = (UICommand) selectedObject;
                    openWizard(selectedCommand);
                }
            }
        });
        listPopupBuilder.setFilteringEnabled(new Function<Object, String>()
        {
            @Override
            public String fun(Object object)
            {
                if (object instanceof UICommand)
                {
                    UICommand command = (UICommand) object;
                    UICommandMetadata metadata = command.getMetadata(uiContext);

                    return metadata.getCategory().toString() + " " + metadata.getName();
                }
                else if (object instanceof String)
{'fraction_non_alphanumeric': 0.08972660484162351, 'fraction_numerical': 0.0021678911236902325, 'mean_word_length': 1.9024816497728068, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5044489', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2093, 'n_words': 451}