summaryrefslogtreecommitdiff
path: root/addNode.go
blob: ab0e6871cf2ccda32a40b25a7d79393e71ee570b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package tree

import (
	"errors"

	"go.wit.com/log"
	"go.wit.com/widget"
)

// this is in common.go, do not move it
func (me *TreeInfo) AddNode(a *widget.Action) *Node {
	n := new(Node)
	n.WidgetType = a.WidgetType
	n.WidgetId = a.WidgetId
	n.ParentId = a.ParentId

	n.State = a.State
	n.Strings = make(map[string]int)

	if a.WidgetType == widget.Root {
		log.Info("AddNode() Root")
		n.Parent = n
		me.treeRoot = n
		return n
	}

	if me.treeRoot.FindWidgetId(a.WidgetId) != nil {
		log.Warn("AddNode() WidgetId already exists", a.WidgetId)
		log.Warn("probably this is a Show() / Hide() issue")
		log.Warn("TODO: figure out what to do here")
		return me.treeRoot.FindWidgetId(a.WidgetId)
	}

	// add this new widget on the binary tree
	p := me.treeRoot.FindWidgetId(a.ParentId)
	n.Parent = p
	if n.Parent == nil {
		log.Error(errors.New("tree.AddNode() ERROR n.Parent == nil"), a.WidgetId, a.ParentId, a.ActionType)
		log.Warn("AddNode() ERROR n.Parent == nil", a.WidgetId, a.ParentId, a.ActionType)
		log.Warn("AddNode() ERROR n.Parent == nil", a.WidgetId, a.ParentId, a.WidgetType)
		return n
	}
	log.Warn("AddNode() Adding to parent =", p.ParentId, p.WidgetType, p.GetProgName())
	log.Warn("AddNode() Adding     child =", n.ParentId, n.WidgetType, n.GetProgName())
	p.children = append(p.children, n)
	return n
}