# Source code for tabletree.src.api.table

    # sphinx bug workaround for python 3.11. see grm:86663
    from __future__ import annotations

    from typing import TYPE_CHECKING, Self

    from ..lib.i18n import I18nResourcesWriter, I18nWord
    from ..lib.table import TableBuilder
    from .feature import Feature
    from .view import View, ViewEventType, ViewKeySuffix

    if TYPE_CHECKING:
        from collections.abc import Sequence

        from .column import Column, NameColumn

    class Table(Feature):
        _all_tables = list[Self]()
        _builder: TableBuilder
        _views: list[View]

        def __init__(self, name: str | I18nWord):
            """Create a table.

            Args:
                name:
                    The name of the table.
            """
            Table._all_tables.append(self)  # type: ignore[misc]
            self._builder = TableBuilder()
            if isinstance(name, str):
                self._builder.set_name(name)
            else:
                self._builder.set_name_i18n(name)
                I18nResourcesWriter.get_instance().add_word(name)
            self._views = []

        def add_view(
            self,
            view: View,
            default_on_event: Sequence[ViewEventType] | None = None,
        ) -> Self:
            """Add a 3D view to the table.

            Args:
                view:
                    The 3D view.
                default_on_event:
                    `None`:
                        The view is not triggered by default.
                    'on_single_clicked':
                        The view is triggered on a single click.
                        If more than one views are added with this option,
                        the last one is triggered on a single click by default.
                    'on_double_clicked':
                        The view is triggered on a double click.
                        If more than one views are added with this option,
                        the last one is triggered on a single click by default.
            """
            op = view._get_data()
            self._builder.add_operation(op)
            if default_on_event is not None:
                if "on_single_clicked" in default_on_event:
                    self._builder.set_operation_on_select(op)
                if "on_double_clicked" in default_on_event:
                    self._builder.set_operation_on_double_click(op)
            self._views.append(view)
            return self

        def add_item(self, item: Feature | Self) -> Self:
            """Add an item to the table.

            Args:
                item:
                    The item to be added.
            """
            self._builder.add_child_builder(item._builder)
            return self

        def set_item_name_column(self, name_column: NameColumn) -> Self:
            """Set a NameColumn to the table.

            Args:
                name_column:
                    The NameColumn to be set.
            """
            self._builder.set_row_header_column(name_column._get_data())
            return self

        def add_column(self, column: Column) -> Self:
            """Add a column to the table.

            Args:
                column:
                    The column to be added.
            """
            self._builder.add_column(column._get_data())
            return self

        def _write_zoom_property(self) -> Self:
            for view in self._views:
                for key in view._zoomed_keys:
                    for child in self._builder.children:
                        child.set_property(
                            f"{key}_{view._op_bdr.id}{ViewKeySuffix.zoomed}", True
                        )
            return self

        def _get_data(self) -> TableBuilder:
            """Obtain the data."""
            for table in Table._all_tables:  # type: ignore[misc]
                table._write_zoom_property()
            return self._builder