<commit_before>package org.yamcs.studio.commanding.stack;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.yamcs.protobuf.Mdb.CommandInfo;
import org.yamcs.protobuf.Mdb.SignificanceInfo;
import org.yamcs.protobuf.Mdb.SignificanceInfo.SignificanceLevelType;
import org.yamcs.protobuf.Yamcs.NamedObjectId;
import org.yamcs.studio.core.model.CommandingCatalogue;
import org.yamcs.studio.core.ui.XtceSubSystemNode;
import org.yamcs.studio.core.ui.utils.CenteredImageLabelProvider;
import org.yamcs.studio.core.ui.utils.RCPUtils;

public class AddToStackWizardPage1 extends WizardPage {

    public static final String COL_PATH = "Command";
    public static final String COL_SIGN = "Sig.";

    private Image level1Image;
    private Image level2Image;
    private Image level3Image;
    private Image level4Image;
    private Image level5Image;

    private StackedCommand command;
    private TreeViewer commandsTreeTable;
    private TreeColumnLayout tcl;

    private List<String> namespaces = new ArrayList<>();

    public AddToStackWizardPage1(StackedCommand command) {
        super("Choose a Command");
        this.command = command;
        setTitle("Choose a Command");
        setPageComplete(false);
    }

    // Add dynamically columns for each alias of a command
    private void addAliasColumn(String namespace) {

        TreeViewerColumn aliasColumn = new TreeViewerColumn(commandsTreeTable, SWT.NONE);
        aliasColumn.getColumn().setText(namespace);

        aliasColumn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                if (element instanceof XtceCommandNode) {
                    XtceCommandNode node = (XtceCommandNode) element;
                    CommandInfo cmd = node.getCommandInfo();
                    List<NamedObjectId> aliases = cmd.getAliasList();
                    for (NamedObjectId aliase : aliases) {
                        if (aliase.getNamespace().equals(namespace)) {
                            return aliase.getName();
                        }
                    }
                }
                return "";
            }
        });
        tcl.setColumnData(aliasColumn.getColumn(), new ColumnPixelData(200));
    }

    @Override
    public void createControl(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        setControl(composite);

        GridLayout gl = new GridLayout();
        gl.marginHeight = 0;
        gl.marginWidth = 0;
        composite.setLayout(gl);

        // add filter box
        Text searchbox = new Text(composite, SWT.SEARCH | SWT.BORDER | SWT.ICON_CANCEL);
        searchbox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // build tree table
        ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), composite);
        level1Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level1s.png"));
        level2Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level2s.png"));
        level3Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level3s.png"));
        level4Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level4s.png"));
        level5Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level5s.png"));

        Composite treeWrapper = new Composite(composite, SWT.NONE);
        tcl = new TreeColumnLayout();
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.widthHint = 800;
        gd.heightHint = 500;
        treeWrapper.setLayoutData(gd);
        treeWrapper.setLayout(tcl);

        commandsTreeTable = new TreeViewer(treeWrapper, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        commandsTreeTable.getTree().setHeaderVisible(true);
        commandsTreeTable.getTree().setLinesVisible(true);

        // column xtce path<commit_msg>Command Stack: add button to expand/collapse the command list<commit_after>package org.yamcs.studio.commanding.stack;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.yamcs.protobuf.Mdb.CommandInfo;
import org.yamcs.protobuf.Mdb.SignificanceInfo;
import org.yamcs.protobuf.Mdb.SignificanceInfo.SignificanceLevelType;
import org.yamcs.protobuf.Yamcs.NamedObjectId;
import org.yamcs.studio.core.model.CommandingCatalogue;
import org.yamcs.studio.core.ui.XtceSubSystemNode;
import org.yamcs.studio.core.ui.utils.CenteredImageLabelProvider;
import org.yamcs.studio.core.ui.utils.RCPUtils;

import com.sun.xml.bind.v2.runtime.reflect.Lister.Pack;

public class AddToStackWizardPage1 extends WizardPage {

    public static final String COL_PATH = "Command";
    public static final String COL_SIGN = "Sig.";

    private Image level1Image;
    private Image level2Image;
    private Image level3Image;
    private Image level4Image;
    private Image level5Image;

    private StackedCommand command;
    private TreeViewer commandsTreeTable;
    private TreeColumnLayout tcl;

    private List<String> namespaces = new ArrayList<>();

    public AddToStackWizardPage1(StackedCommand command) {
        super("Choose a Command");
        this.command = command;
        setTitle("Choose a Command");
        setPageComplete(false);
    }

    // Add dynamically columns for each alias of a command
    private void addAliasColumn(String namespace) {

        TreeViewerColumn aliasColumn = new TreeViewerColumn(commandsTreeTable, SWT.NONE);
        aliasColumn.getColumn().setText(namespace);

        aliasColumn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                if (element instanceof XtceCommandNode) {
                    XtceCommandNode node = (XtceCommandNode) element;
                    CommandInfo cmd = node.getCommandInfo();
                    List<NamedObjectId> aliases = cmd.getAliasList();
                    for (NamedObjectId aliase : aliases) {
                        if (aliase.getNamespace().equals(namespace)) {
                            return aliase.getName();
                        }
                    }
                }
                return "";
            }
        });
        tcl.setColumnData(aliasColumn.getColumn(), new ColumnPixelData(200));
    }

    @Override
    public void createControl(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        setControl(composite);

        GridLayout gl = new GridLayout();
        gl.marginHeight = 0;
        gl.marginWidth = 0;
        composite.setLayout(gl);

        // grid for expand+filter box
        GridLayout gl2 = new GridLayout(3, false);
        gl2.marginHeight = 0;
        gl2.marginWidth = 0;
        Composite row = new Composite(composite, SWT.NONE);
        row.setLayout(gl2);
        row.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        
        // add expand all / collapse all button
        Button expandAll = new Button(row, SWT.ARROW | SWT.DOWN);      
        GridData expandAllData = new GridData(SWT.NONE);
        expandAll.setLayoutData(expandAllData);
        expandAll.setText("Expand All");   
        expandAll.setToolTipText("Expand All");
        expandAll.setVisible(false);
        expandAllData.exclude = true;
        
        
        Button collapseAll = new Button(row, SWT.ARROW | SWT.RIGHT);
        GridData collapseAllData = new GridData(SWT.NONE);
        collapseAll.setLayoutData(collapseAllData);
        collapseAll.setText("Collapse All");
        collapseAll.setToolTipText("Collapse All");
        collapseAll.setVisible(true);
        

        // add filter box        
        Text searchbox = new Text(row, SWT.SEARCH | SWT.BORDER | SWT.ICON_CANCEL);
        searchbox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        
        expandAll.addSelectionListener(new SelectionListener() {
            
            @Override
            public void widgetSelected(SelectionEvent e) {
                commandsTreeTable.expandAll();
                expandAll.setVisible(false);
                collapseAll.setVisible(true); 
                expandAllData.exclude = true;
                collapseAllData.exclude = false;
                expandAll.pack();
                collapseAll.pack();         
            }
            
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                commandsTreeTable.expandAll();
                expandAll.setVisible(false);
                collapseAll.setVisible(true);
                expandAllData.exclude = true;
                collapseAllData.exclude = false;
                expandAll.pack();
                collapseAll.pack();
                
            }
        });
        collapseAll.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                commandsTreeTable.collapseAll();
                expandAll.setVisible(true);
                collapseAll.setVisible(false);
                expandAllData.exclude = false;
                collapseAllData.exclude = true;
                expandAll.pack();
                collapseAll.pack();
              }
            
            @Override
            public void widgetDefaultSelected(SelectionEvent e) { 
                commandsTreeTable.collapseAll();
                expandAll.setVisible(true);
                collapseAll.setVisible(false);
                expandAllData.exclude = false;
                collapseAllData.exclude = true;
                expandAll.pack();
                collapseAll.pack();
            }
        });

        // build tree table
        ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), composite);
        level1Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level1s.png"));
        level2Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level2s.png"));
        level3Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level3s.png"));
        level4Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level4s.png"));
        level5Image = resourceManager
                .createImage(RCPUtils.getImageDescriptor(AddToStackWizardPage1.class, "icons/level5s.png"));

        Composite treeWrapper = new Composite(composite, SWT.NONE);
        tcl = new TreeColumnLayout();
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.widthHint = 800;
        gd.heightHint = 500;
        treeWrapper.setLayoutData(gd);
        treeWrapper.setLayout(tcl);

        commandsTreeTable = new TreeViewer(treeWrapper, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        commandsTreeTable.getTree().setHeaderVisible(true);
        commandsTreeTable.getTree().setLinesVisible(true);

        // column xtce path
{'fraction_non_alphanumeric': 0.09132219290530637, 'fraction_numerical': 0.005423629434183524, 'mean_word_length': 2.7058663769690385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7255740', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3744, 'n_words': 672}