/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.components.tree;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;

public class TreeExample extends JPanel {
	public TreeExample() {
		super(new BorderLayout());

		final MyTreeNode rootNode = new MyTreeNode();
		final JTree tree = new JTree(rootNode);
		tree.setRootVisible(true);
		tree.putClientProperty("JTree.lineStyle", "Horizontal"); // Angled | Horizontal | None

		add(new JScrollPane(tree), BorderLayout.CENTER);

		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				if(null==node) return;

				Object nodeInfo = node.getUserObject();
				if (node.isLeaf()){
					System.out.println("It's a page. " + nodeInfo);
				} else {
					System.out.println("It's a folder. " + nodeInfo);
				}

			}
		});
		tree.getModel().addTreeModelListener(new TreeModelListener() {
			@Override
			public void treeNodesChanged(TreeModelEvent treeModelEvent) {

			}

			@Override
			public void treeNodesInserted(TreeModelEvent treeModelEvent) {

			}

			@Override
			public void treeNodesRemoved(TreeModelEvent treeModelEvent) {

			}

			@Override
			public void treeStructureChanged(TreeModelEvent treeModelEvent) {

			}
		});
		tree.addTreeExpansionListener(new TreeExpansionListener() {
			@Override
			public void treeExpanded(TreeExpansionEvent treeExpansionEvent) {

			}

			@Override
			public void treeCollapsed(TreeExpansionEvent treeExpansionEvent) {

			}
		});
		tree.addTreeWillExpandListener(new TreeWillExpandListener() {
			@Override
			public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {

			}

			@Override
			public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {

			}
		});
	}

	static class MyTreeNode extends DefaultMutableTreeNode {
		MyTreeNode(){
			super("The Book");

			final DefaultMutableTreeNode chapter1 = new DefaultMutableTreeNode("Chapter 1");
			chapter1.add(new DefaultMutableTreeNode("Page 1"));
			chapter1.add(new DefaultMutableTreeNode("Page 2"));
			final DefaultMutableTreeNode chapter2 = new DefaultMutableTreeNode("Chapter 2");
			chapter2.add(new DefaultMutableTreeNode("Page 3"));
			chapter2.add(new DefaultMutableTreeNode("Page 4"));
			chapter2.add(new DefaultMutableTreeNode("Page 5"));

			add(chapter1);
			add(chapter2);
		}
	}

	public static void main(String... args) {

		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new TreeExample());

		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});


	}
}
