Class Tree

  • All Implemented Interfaces:
    EventHandler, KeyListener, PenListener, PressListener, Scrollable

    public class Tree
    extends Container
    implements PressListener, PenListener, KeyListener, Scrollable
    This class is a simple implementation of a tree widget. Since it's natural to render the tree in rows, this class borrows most of the code from ListBox. Features:
    • similar to Microsoft Windows Explorer tree
    • horizontal and vertical scrolling
    • allows setting of folder and leaf icons.
    • expands and collapse of folder
    • allowsChildren flag to determine if the node is a leaf or folder
    • delete, insert, and modify (user object) of a node
    • clicking on leaf node will swap leaf icon (like hyperlink)
    • allows creation of tree to show or hide root node.
    You should use TreeModel class to modify the tree after. Here's a sample:
     TreeModel tmodel = new TreeModel();
     Tree tree = new Tree(tmodel);
     add(tree,LEFT,TOP,FILL,FILL);
     Node root = new Node("Tree");
     tmodel.setRoot(root);
     Node n;
     root.add(n = new Node("Branch1"));
     n.add(new Node("SubBranch1"));
     n.add(new Node("SubBranch2"));
     
    You can also see the FileChooserBox control and FileChooserTest (in UIGadgets sample).
    See Also:
    Node.userObject
    • Field Detail

      • multipleSelection

        public boolean multipleSelection
        Set to true to allow multiple selections using a Check drawn before the nodes.
        Since:
        TotalCross 1.15
      • expandClickingOnText

        public boolean expandClickingOnText
        Set to false to only expand or collapse if you click on the +- buttons.
        Since:
        TotalCross 1.2
      • useFullWidthOnSelection

        public boolean useFullWidthOnSelection
        If true, all Tree will have the selection bar drawn in the full width instead of the selected's text width
        Since:
        TotalCross 1.27
      • flick

        protected Flick flick
        The Flick object listens and performs flick animations on PenUp events when appropriate.
      • imgPlus

        protected Image imgPlus
      • imgMinus

        protected Image imgMinus
      • imgOpen

        protected Image imgOpen
      • imgClose

        protected Image imgClose
      • imgFile

        protected Image imgFile
      • items

        protected Vector items
      • offset

        protected int offset
      • hsOffset

        protected int hsOffset
      • selectedIndex

        protected int selectedIndex
      • itemCount

        protected int itemCount
      • hsCount

        protected int hsCount
      • visibleItems

        protected int visibleItems
      • btnX

        protected int btnX
      • lineH

        @Deprecated
        public int lineH
        Deprecated.
        Use setLineHeight and getLineHeight
    • Constructor Detail

      • Tree

        public Tree()
        Constructs a new Tree based on an empty TreeModel.
      • Tree

        public Tree​(TreeModel model)
        Constructs a new Tree based on the given parameters.
        Parameters:
        model - the TreeModel to be used
      • Tree

        public Tree​(TreeModel model,
                    boolean showRoot)
        Constructs a new Tree based on the given parameters.
        Parameters:
        model - the TreeModel to be used
        showRoot - if true, the root node is shown
    • Method Detail

      • setLineHeight

        public void setLineHeight​(int k)
        Default line height.
      • getLineHeight

        public int getLineHeight()
      • flickStarted

        public boolean flickStarted()
        Description copied from interface: Scrollable
        Called when the flick animation is started.
        Specified by:
        flickStarted in interface Scrollable
      • flickEnded

        public void flickEnded​(boolean atPenDown)
        Description copied from interface: Scrollable
        Called when the flick animation is ended.
        Specified by:
        flickEnded in interface Scrollable
        Parameters:
        atPenDown - Flag indicating if the animation ended due to a pendown event.
      • canScrollContent

        public boolean canScrollContent​(int direction,
                                        java.lang.Object target)
        Description copied from interface: Scrollable
        Checks if there is room to flick this container in the given direction.
        Specified by:
        canScrollContent in interface Scrollable
        Parameters:
        direction - The direction we want to flick given by one of the following constants:
        target - The target of the series of PEN events that triggered this flick attempt.
        Returns:
        true if the container can flick in the indicated direction.
      • scrollContent

        public boolean scrollContent​(int dx,
                                     int dy,
                                     boolean fromFlick)
        Description copied from interface: Scrollable
        Performs a relative move.
        Specified by:
        scrollContent in interface Scrollable
        Parameters:
        dx - The relative amount of pixels to move in the X axis.
        dy - The relative amount of pixels to move in the Y axis.
      • getScrollPosition

        public int getScrollPosition​(int direction)
        Description copied from interface: Scrollable
        Returns the current position given the direction. Used on page scrolls.
        Specified by:
        getScrollPosition in interface Scrollable
      • getFlick

        public Flick getFlick()
        Description copied from interface: Scrollable
        Returns the current flick object.
        Specified by:
        getFlick in interface Scrollable
      • wasScrolled

        public boolean wasScrolled()
        Description copied from interface: Scrollable
        Returns true if the control was scrolled since last pen down
        Specified by:
        wasScrolled in interface Scrollable
      • dontShowFileAndFolderIcons

        public void dontShowFileAndFolderIcons()
        Call this method to hide the file and folder icons.
      • setModel

        public void setModel​(TreeModel model)
        Method to set the tree model.
        Parameters:
        model - the tree model.
      • initTree

        public void initTree​(Node root)
        Method to set the tree root node with the new root node. If the new root node is null, the tree is unchanged.
        Parameters:
        root - the new tree root node.
      • onFontChanged

        public void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • initScrollBars

        protected void initScrollBars()
        Method to initialize the vertical and horizontal scrollbars maximum.
      • initImage

        protected void initImage()
        Method to load icons use for the tree. You can change the icon by using the setIcon(int iconType, Filename imageFilename).
      • setIcon

        public void setIcon​(int iconType,
                            Image img)
                     throws ImageException
        Method to set the icon of the tree based on the icon type. Note: You should not change the plus and minus icons. You can set the open/close to null (setting one will null out the other).
        Parameters:
        iconType - one of the ICON_xxx constants.
        img - The image to be used.
        Throws:
        ImageException
        See Also:
        ICON_PLUS, ICON_MINUS, ICON_OPEN, ICON_CLOSE, ICON_FILE
      • getItemWidth

        protected int getItemWidth​(int index)
        Method to return the width of the given item index with the current fontmetrics. Note: if you override this class you must implement this method.
      • removeAll

        public void removeAll()
        Method to empties this Tree, setting all elements of the array to null, so they can be garbage collected.
        Overrides:
        removeAll in class Container
      • clear

        public void clear()
        Same as removeAll() method. Just more clearer method name
        Overrides:
        clear in class Container
      • remove

        public void remove​(int index)
        Method to remove the given index from the Tree items vector. This method will not remove the node from the original node.
        Parameters:
        index - the item index in the items vector.
      • remove

        public void remove​(java.lang.Object item)
        Method to remove an Object from the Tree's items vector.
        Parameters:
        item - the Node to delete from the tree's item vector.
      • expand

        public boolean expand​(Node node)
        Method to expand a collapsed node.
        Parameters:
        node - the collapse node to expand.
        Returns:
        True if the item was expanded, false otherwise.
      • expandTo

        public void expandTo​(java.lang.String filePath)
        Expands all nodes until the given path is reached
      • collapse

        public boolean collapse​(Node node)
        Method to collapse an expanded node.
        Parameters:
        node - the expanded node to collapse.
        Returns:
        True if the item was collapsed, false otherwise.
      • setItemAt

        public void setItemAt​(int i,
                              java.lang.Object s)
        Method to set the Object at the given Index, starting from 0.
        Parameters:
        i - the index
        s - the object to set.
      • getItemAt

        public java.lang.Object getItemAt​(int i)
        Method to get the Object at the given Index. Returns an empty string in case of error.
        Parameters:
        i - the index.
      • getSelectedItem

        public Node getSelectedItem()
        Method to return the selected item of the Tree or an empty String if no selection has been made.
        Returns:
        the selected object, or null is no selection has been made.
      • getSelectedIndex

        public int getSelectedIndex()
        Method to return the position of the selected item of the Tree or -1 if the Tree has no selected index yet.
        Returns:
        the selected index or -1 if no selection has been made.
      • getItems

        public java.lang.Object[] getItems()
        Method to return all items in the items vector as an array of object. The objects are of the class Node.
        Returns:
        all items in items vector as an array of Objects.
      • indexOf

        public int indexOf​(java.lang.Object name)
        Method to return the index of the item specified by the name, or -1 if not found.
        Parameters:
        name - the object to find.
        Returns:
        the index of the item specified by the name, or -1 if not found.
      • setSelectedItem

        public void setSelectedItem​(java.lang.Object name)
        Method to select the given name. If the name is not found, the current selected item is not changed.
        Parameters:
        name - the object to select.
        Since:
        SuperWaba 4.01
      • setSelectedIndex

        public void setSelectedIndex​(int i)
        Method to select the given index and scroll to it if necessary. Note: select must be called only after the control has been added to the container and its rect has been set.
        Parameters:
        i - the index of the item.
      • size

        public int size()
        Returns the number of items (Nodes)
      • getPreferredWidth

        public int getPreferredWidth()
        Method to return the preferred width, ie, size of the largest item plus 20.
        Overrides:
        getPreferredWidth in class Control
        Returns:
        the preferred width of this control.
      • getPreferredHeight

        public int getPreferredHeight()
        Method to return the number of items multiplied by the font metrics height
        Overrides:
        getPreferredHeight in class Control
        Returns:
        the preferred height of this control.
      • find

        protected void find​(char c)
        Method to search this Tree for an item with the first letter matching the given char. The search is made case insensitive. Note: if you override this class you must implement this method.
      • setEnabled

        public void setEnabled​(boolean enabled)
        Method to enable this control if the specified enabled flag is true.
        Overrides:
        setEnabled in class Container
      • setBackColor

        public void setBackColor​(int c)
        Description copied from class: Control
        Set the background color of this control.
        Overrides:
        setBackColor in class Control
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Container
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Method to recalculate the box size for the selected item if the control is resized by the main application .
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • nodeRemoved

        public void nodeRemoved​(Node node)
        Method to notify the tree that a node has been removed from the tree model and to repaint the tree to reflect the changes, if necessary
        Parameters:
        node - the node that has been removed from the tree model
      • nodeInserted

        public void nodeInserted​(Node parent,
                                 Node child,
                                 int index)
        Method to notify the tree that a node has been added to the tree model and to repaint the tree to reflect the changes.
        Parameters:
        parent - the parent node of the new added node
        child - the new ly added node
        index - the index of the new node
      • nodeModified

        public void nodeModified​(Node node)
        Method to notify the tree that a node in the tree model has been modified (currently - only changing the user object)
        Parameters:
        node - the node that has been modified
      • checkClicked

        public void checkClicked​(int sel)
      • handleGeographicalFocusChangeKeys

        public Control handleGeographicalFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Control
        Used by the main event loop to give the currently focused control an opportunity to act directly on the KeyEvent.
        Overrides:
        handleGeographicalFocusChangeKeys in class Control
        Parameters:
        ke - The KeyEvent to be processed
        Returns:
        The control that should get focus as a result of this KeyEvent. Null if this control did not handle the KeyEvent.
        See Also:
        Settings.geographicalFocus
      • onPaint

        public void onPaint​(Graphics g)
        Description copied from class: Container
        Draws the border (if any). If you override this method, be sure to call super.onPaint(g);, or the border will not be drawn.
        Overrides:
        onPaint in class Container
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • drawNode

        protected void drawNode​(Graphics g,
                                int index,
                                int dx,
                                int dy)
        Method to draw the icons and node text
      • drawFileImage

        protected void drawFileImage​(Graphics g,
                                     Node node,
                                     int x,
                                     int y)
        Allows the draw of customized file images.
      • drawConnector

        protected void drawConnector​(Graphics g,
                                     int index,
                                     int dx,
                                     int dy,
                                     Node node)
        Method to draw the (line connector) angled line.
      • drawCursor

        protected void drawCursor​(Graphics g,
                                  int sel)
        Method to draw the highlight box when user select a listbox's item.
      • setCursorColor

        public void setCursorColor​(int color)
        Method to set the cursor color for this Tree. The default is equal to the background slightly darker.
      • reload

        public void reload()
        Method to reload the tree. Use this method when the tree model has made a drastic change.
      • unload

        public void unload()
        Method to clear the tree and release the tree model references.
      • getFocusableControls

        public void getFocusableControls​(Vector v)
        Description copied from class: Container
        Get a list of child controls of this container which are focus candidates
        Overrides:
        getFocusableControls in class Container
        Parameters:
        v - A vector into which to add the focus candidates.