Tabular IQ

Overview

The TreeView class represents a tree view control.

Base Class

FormControl

Constructor

TreeView(x_pos : Integer, y_pos : Integer, width : Integer, height : Integer)

Arguments

x_pos
The x position of the control.
y_pos
The y position of the control.
width
The width of the control.
height
The height of the control.

Events

TreeView.itemActivate
Fired when a tree item is activated.
TreeView.itemBeginLabelEdit
Fired when a tree item's label editing begins.
TreeView.itemCollapsed
Fired after a tree item is collapsed.
TreeView.itemCollapsing
Fired when a tree item is about to be collapsed.
TreeView.itemDelete
Fired when a tree item is deleted.
TreeView.itemEndLabelEdit
Fired when a tree item's label editing ends.
TreeView.itemExpanded
Fired after a tree item is collapsed.
TreeView.itemExpanding
Fired when a tree item is about to be expanded.
TreeView.itemRightClick
Fired when a tree item is right-clicked.
TreeView.keyDown
Fired when the tool bar item is clicked.
TreeView.selectionChanged
Fired when the tree's item selection has changed.
TreeView.selectionChanging
Fired when the tree's item selection is about to change.

Methods

TreeView.addItem
Adds a treeview item to the treeview.
TreeView.clear
Clears the treeview of all items.
TreeView.collapseItem
Collapses a treeview item in the treeview.
TreeView.deleteItem
Deletes a treeview item from the treeview.
TreeView.deselectAllItems
Deselects all items in the treeview.
TreeView.deselectItem
Deselects a treeview item in the treeview.
TreeView.editItemLabel
Causes a treeview item to enter into editing mode.
TreeView.expandItem
Expands a treeview item in the treeview.
TreeView.getItemCount
Returns the number of items in the treeview.
TreeView.getRootItem
Gets the root item of the treeview.
TreeView.getSelectedItem
Gets selected treeview item from the treeview.
TreeView.getSelectedItems
Gets an array of the selected treeview items from the treeview.
TreeView.scrollToItem
Scrolls the treeview control to a given treeview item.
TreeView.selectItem
Selects a treeview item in the treeview.
TreeView.setRootItem
Sets the root item of the treeview.
TreeView.toggleItemExpanded
Expands or collapses a treeview item.
TreeView.toggleItemSelected
Selects or deselects a treeview item.

Inherited Methods

FormControl.captureMouse
Captures the mouse on this form control.
FormControl.disablePaint
Disables the window from redrawing itself.
FormControl.enablePaint
Enables the window to redraw itself.
FormControl.getBackgroundColor
Gets the background color of the form control.
FormControl.getClientSize
Gets the client size of the form control.
FormControl.getEnabled
Indicates whether or not a form control is enabled.
FormControl.getFont
Gets the default font for the text of form control.
FormControl.getForegroundColor
Gets the foreground color of the form control.
FormControl.getMaxSize
Gets the maximum size of the form control.
FormControl.getMinSize
Gets the minimum size of the form control.
FormControl.getMousePosition
Gets the mouse position relative to this form control.
FormControl.getNativeHandle
Gets the native handle of the window/control
FormControl.getPosition
Gets the position of a form control.
FormControl.getSize
Gets the size of the form control.
FormControl.invalidate
Invalidates a form control, which will cause it to be repainted on the next paint event.
FormControl.refresh
Refreshes a form control, which immediately repaints the entire form control.
FormControl.releaseMouse
Releases the mouse from being captured on this form control.
FormControl.setBackgroundColor
Sets the background color of the form control.
FormControl.setClientSize
Sets the client size of a form control.
FormControl.setEnabled
Enables or disables the form control.
FormControl.setFocus
Sets the focus to the form control.
FormControl.setFont
Sets the default font for the text of the form control.
FormControl.setForegroundColor
Sets the foreground color of the form control.
FormControl.setMaxSize
Sets the maximum size of a form control.
FormControl.setMinSize
Sets the minimum size of a form control.
FormControl.setPosition
Sets the position of a form control relative to the the form control's parent.
FormControl.setSize
Sets the size of a form control.
FormControl.show
Shows or hides the form control.
FormControl.update
Updates a form control, which will immediately repaint any invalid areas.

Example

// A TreeView shows a hierarchy of TreeViewItem objects.  Every tree has
// one root: setRootItem() names it, addItem(item, parent) hangs the rest
// off it.  (addItem() with no root set makes that first item the root.)
//
// Every event but keyDown carries the item it concerns as
// event_args.item.

class MyForm extends Form {
    tree = null;
    root = null;

    constructor() {
        super("TreeView Example", 100, 100, 360, 400);

        this.tree = new TreeView();

        this.root = new TreeViewItem("Data Sources");
        this.tree.setRootItem(this.root);

        for (const name of ["Databases", "Files"]) {
            const folder = new TreeViewItem(name);
            this.tree.addItem(folder, this.root);

            for (const leaf of ["alpha", "beta", "gamma"]) {
                this.tree.addItem(new TreeViewItem(`${name}/${leaf}`), folder);
            }
        }

        this.tree.expandItem(this.root);

        this.tree.selectionChanged.connect(this, this.onSelectionChanged);
        this.tree.itemActivate.connect(this, this.onItemActivate);
        this.tree.itemRightClick.connect(this, this.onItemRightClick);
        this.tree.itemEndLabelEdit.connect(this, this.onItemEndLabelEdit);

        const layout = new BoxLayout(Layout.Vertical);
        layout.add(this.tree, 1, Layout.Expand | Layout.All, 8);
        this.setLayout(layout);
    }

    onSelectionChanged(sender, event_args) {
        const item = event_args.item;

        // getChildCount(false) counts direct children; true -- the
        // default -- counts the whole subtree
        this.setCaption(`${item.getLabel()}:` +
                        ` ${item.getChildCount(false)} children,` +
                        ` expanded=${item.isExpanded()}`);
    }

    onItemActivate(sender, event_args) {
        // a double-click or Enter
        this.tree.toggleItemExpanded(event_args.item);
    }

    onItemRightClick(sender, event_args) {
        // carry the clicked item on the menu item, so the handler does
        // not depend on the selection still being where it was
        const rename_item = new MenuItem("Rename");
        rename_item.tree_item = event_args.item;
        rename_item.click.connect(this, this.onRename);

        const menu = new Menu();
        menu.add(rename_item);
        menu.popup(this.tree);
    }

    onRename(sender, event_args) {
        // starts an in-place edit; the result arrives below
        this.tree.editItemLabel(sender.tree_item);
    }

    onItemEndLabelEdit(sender, event_args) {
        this.setCaption(`renamed to ${event_args.item.getLabel()}`);
    }
}


const form = new MyForm();
form.show();
Application.run();

TreeView.addItem

function TreeView.addItem(item : TreeViewItem, parent : TreeViewItem)

Arguments

item
The item to add to the treeview.
parent
The node to which to add the item.

Description

Adds a treeview item, specified by item, to the node specified by parent. If parent isn't specified, then item is added to the root item of the treeview.

TreeView.clear

function TreeView.clear()

Description

Clears the treeview of all items.

TreeView.collapseItem

function TreeView.collapseItem(item : TreeViewItem)

Arguments

item
The treeview item to collapse.

Description

Collapses a treeview item, specified by item, so that it no longer shows any children it may have.

TreeView.deleteItem

function TreeView.deleteItem(item : TreeViewItem)

Arguments

item
The item to delete from the treeview.

Description

Deletes the treeview item, specified by item, from the treeview.

TreeView.deselectAllItems

function TreeView.deselectAllItems()

Description

Deselects all items in the treeview.

TreeView.deselectItem

function TreeView.deselectItem(item : TreeViewItem)

Arguments

item
The treeview item to deselect.

Description

Deselects a treeview item, specified by item.

TreeView.editItemLabel

function TreeView.editItemLabel(item : TreeViewItem)

Arguments

item
The treeview item that should enter into editing mode.

Description

This function triggers an event that causes a treeview item, specified by item, to enter into editing mode.

TreeView.expandItem

function TreeView.expandItem(item : TreeViewItem)

Arguments

item
The treeview item to expand.

Description

Expands a treeview item, specified by item, so that it shows its children, if they exist.

TreeView.getItemCount

function TreeView.getItemCount() : Integer

Returns

Returns the number of items in the treeview.

Description

Returns the number of items in the treeview.

TreeView.getRootItem

function TreeView.getRootItem() : TreeViewItem

Returns

Returns the root item of the treeview.

Description

Gets the root item of the treeview.

TreeView.getSelectedItem

function TreeView.getSelectedItem() : TreeViewItem

Returns

Returns the selected treeview item, if a treeview item is selected. Otherwise, the function returns null.

Description

Gets the selected treeview item from the treeview. If the treeview doesn't have a selection, the function returns null.

TreeView.getSelectedItems

function TreeView.getSelectedItems() : Array(TreeViewItem)

Returns

Returns an array of the selected treeview items in the treeview.

Description

Gets an array of the selected treeview items from the treeview.

TreeView.scrollToItem

function TreeView.scrollToItem(item : TreeViewItem)

Arguments

item
The item to which the treeview control should scroll.

Description

Scrolls the treeview control to a treeview item, which is specified by item.

TreeView.selectItem

function TreeView.selectItem(item : TreeViewItem)

Arguments

item
The treeview item to select.

Description

Selects a treeview item, specified by item.

TreeView.setRootItem

function TreeView.setRootItem(item : TreeViewItem)

Arguments

item
The item to use as the root item.

Description

Sets the root item of the treeview from the specified item.

TreeView.toggleItemExpanded

function TreeView.toggleItemExpanded(item : TreeViewItem)

Arguments

item
The treeview item to expand or collapse.

Description

Expands or collapses a treeview item, depending on whether it is currently expanded or collapsed. If the treeview item is currently expanded, the function will collapse it. If the treeview item is currently collapsed, the function will expand it.

TreeView.toggleItemSelected

function TreeView.toggleItemSelected(item : TreeViewItem)

Arguments

item
The treeview item to select or deselect.

Description

Selects or deselects a treeview item, depending on whether it is currently selected or deselected. If the treeview item is currently selected, the function will deselect it. If the treeview item is currently deselected, the function will select it.

TreeView.itemActivate

TreeView.itemBeginLabelEdit

TreeView.itemCollapsed

TreeView.itemCollapsing

TreeView.itemDelete

TreeView.itemEndLabelEdit

TreeView.itemExpanded

TreeView.itemExpanding

TreeView.itemRightClick

TreeView.keyDown

Arguments

keyCode
Key code of the key pressed.
altDown
True if the Alt key is pressed, false otherwise.
controlDown
True if the Control key is pressed, false otherwise.
shiftDown
True if the Shift key is pressed, false otherwise.

TreeView.selectionChanged

TreeView.selectionChanging