This Question is Answered

15 "helpful" answers available (3 pts)
3 Replies Last post: Jul 20, 2008 6:00 AM by Poseidon.UD

how to realize AutoSearch in TreeControl

Jul 18, 2008 12:26 AM

Click to view Poseidon.UD's profile Level 2 Poseidon.UD 37 posts since
Jul 3, 2008

about TreeControl,

for example , i press the key "A". i want to set focus to node of "abc".

thanks

Click to view Poseidon.UD's profile Level 2 Poseidon.UD 37 posts since
Jul 3, 2008
1. Re: how to realize AutoSearch in TreeControl Jul 18, 2008 12:29 AM
press "a" again,set focus to another node which "a" is begin word ,for example, "abde"
Click to view Kamal's profile Curl Kamal 139 posts since
Oct 17, 2007
2. Re: how to realize AutoSearch in TreeControl Jul 18, 2008 6:26 AM
You basically want your TreeControl to listen to KeyPress events and react accordingly. I am putting some code here to show how. You can extend on this and add say wrapping. Also you can have some kind of Alarm and use it to search multi chars when a person types fast (for example if there are nodes like Tree, Tamato, Apple), if one press 't' and then 'a' within a specified time period, then the tree control should not select "Apple", it should select Tamato).


{curl 5.0, 6.0 applet}
 
{define-class public CustomTreeControl {inherits TreeControl}
 
  {constructor public {default
                          data-model:TreeModel = {TreeModel},
                          tree-item-creation-proc:{proc-type {TreeNode}:TreeItem} = TreeControl.default-tree-item-creation-proc,
                          selection-policy:SelectionPolicy = SelectionPolicy.single,
                          ui-object:#TreeControlUI = null,
                          ...
                      }
    {construct-super
        data-model = data-model,
        tree-item-creation-proc = tree-item-creation-proc,
        selection-policy = selection-policy,
        ui-object = ui-object,
        ...
    }
  }
 
  {method public {on-key-press e:KeyPress}:void
    let node:#TreeNode = self.current-node 
    let constant val:String = {String e.value}
    {if e.insertable? then
        {while node != null do
            set node = {self.next-node {non-null node}}
            {if-non-null node then
                let node-text:#String =
                    {type-switch node.node-label
                     case si:StringInterface do
                        {si.to-String}
                     case g:Graphic do
                        {g.get-text}
                     else
                        null
                    }
                {if-non-null node-text then
                    {if {node-text.prefix? val, ignore-case? = true} then
                        set self.current-node = node
                        {e.consume}
                        {break}
                    }
                }
            }
        }
    }
    {super.on-key-press e}
  }
}
 
{CustomTreeControl
    data-model =
        {TreeModel
            root = 
                {TreeNode node-data="Food",
                    {TreeNode node-data="Fruit",
                        {TreeNode node-data="Apples",
                            {TreeNode node-data="Macintosh"},
                            {TreeNode node-data="Cortland"},
                            {TreeNode node-data="Gala"},
                            {TreeNode node-data="Delicious"}
                        },
                        {TreeNode node-data="Oranges"}
                    },
                    {TreeNode node-data="Vegetables",
                        {TreeNode node-data="Squash"},
                        {TreeNode node-data="Tomatoes"},
                        {TreeNode node-data="Cucumbers"}
                    }
                }
        }
}
 
Click to view Poseidon.UD's profile Level 2 Poseidon.UD 37 posts since
Jul 3, 2008
3. Re: how to realize AutoSearch in TreeControl Jul 20, 2008 6:00 AM
in response to: Kamal
Great! thank u