package view;

import components.main.left.TitanLeftToolBar;
import components.main.left.TitanTree;
import components.main.right.TitanTable;
import controller.TitanMainController;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.ArrayList;
import java.util.Enumeration;

public class TitanDataView {
    private TitanMainController controller;
    private TitanTree tree;
    private TitanTable table;
    private TitanLeftToolBar toolBar;
    private Container parent;

    public TitanDataView(TitanMainController controller, TitanTree tree, TitanTable table, TitanLeftToolBar toolBar, Container parent) {
        this.controller = controller;
        this.tree = tree;
        this.table = table;
        this.toolBar = toolBar;
        this.parent = parent;

        initListeners();
    }

    public DefaultMutableTreeNode[] getSelectedRows() {
        ArrayList<DefaultMutableTreeNode> rows = new ArrayList<>();

        TreePath[] paths = tree.getSelectionPaths();

        if (paths != null) {
            for (TreePath path : paths) {
                rows.add((DefaultMutableTreeNode) path.getLastPathComponent());
            }
        }

        return rows.toArray(new DefaultMutableTreeNode[rows.size()]);
    }

    public DefaultMutableTreeNode[] getVisibleRows(DefaultMutableTreeNode root, boolean excludeExpanded) {
        ArrayList<DefaultMutableTreeNode> rows = new ArrayList<>();

        Enumeration<DefaultMutableTreeNode> nodes = root.preorderEnumeration();
        nodes.nextElement(); // exclude root

        while (nodes.hasMoreElements()) {
            DefaultMutableTreeNode node = nodes.nextElement();

            TreePath path = new TreePath(node.getPath());

            if (tree.isVisible(path) && !(excludeExpanded && tree.isExpanded(path))) {
                rows.add(node);
            }
        }

        return rows.toArray(new DefaultMutableTreeNode[rows.size()]);
    }

    public void setToolBarEnabled(boolean enabled) {
        toolBar.setEnabledAll(enabled);
    }

    public void setToolBarPartialEnabled(boolean group, boolean ungroup, boolean up, boolean down, boolean delete) {
        toolBar.getGroupButton().setEnabled(group);
        toolBar.getUngroupButton().setEnabled(ungroup);
        toolBar.getMoveUpButton().setEnabled(up);
        toolBar.getMoveDownButton().setEnabled(down);
        toolBar.getDeleteButton().setEnabled(delete);
    }

    public void setTreeModel(TreeModel treeModel) {
        tree.setModel(treeModel);
    }
    public TreeModel getTreeModel() {
        return tree.getModel();
    }

    public boolean isExpanded(TreePath path) {
        return tree.isExpanded(path);
    }

    public void drawTable(String[] names, boolean[][] data, int[][] color) {
        table.setTableContents(names, data, color);
    }

    private void initListeners() {
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                controller.checkSelection(tree.getSelectionPaths());
            }
        });
    }
}

@@ -403,16 +403,89 @@
 .awt.*;%0A
+import java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A
 import j
@@ -3075,255 +3075,1001 @@
    p
-rivate void initListeners() %7B%0A        tree.addTreeSelectionListener(new TreeSelectionListener() %7B%0A            @Override%0A            public void valueChanged(TreeSelectionEvent e) %7B%0A                controller.checkSelection(tree.getSelectionPaths()
+ublic void collapseAll() %7B%0A        int size = tree.getRowCount();%0A%0A        for (int i = 0; i %3C size; i++) %7B%0A            tree.collapseRow(i);%0A        %7D%0A    %7D%0A%0A    public void expandAll() %7B%0A        int size = tree.getRowCount();%0A%0A        for (int i = 0; i %3C size; i++) %7B%0A            tree.expandRow(i);%0A        %7D%0A    %7D%0A%0A    private void initListeners() %7B%0A        tree.addTreeSelectionListener(new TreeSelectionListener() %7B%0A            @Override%0A            public void valueChanged(TreeSelectionEvent e) %7B%0A                controller.checkSelection(tree.getSelectionPaths());%0A            %7D%0A        %7D);%0A%0A        toolBar.getCollapseButton().addActionListener(new ActionListener() %7B%0A            @Override%0A            public void actionPerformed(ActionEvent e) %7B%0A                collapseAll();%0A            %7D%0A        %7D);%0A%0A        toolBar.getExpandButton().addActionListener(new ActionListener() %7B%0A            @Override%0A            public void actionPerformed(ActionEvent e) %7B%0A                expandAll(
 );%0A 

implement collpase/expand all