summaryrefslogtreecommitdiff
path: root/action.go
blob: cfd629ff28bab21c6de8b32a8202cd5b7c83bdbb (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Although most code from WIT.COM Inc is under the GPL
// This code is more generic because it must be able
// to be used in any GUI plugin

package tree

import (
	"go.wit.com/lib/protobuf/guipb"
	"go.wit.com/log"
	"go.wit.com/widget"
)

// everything from the application goes through here
func (me *TreeInfo) doAction(a widget.Action) {
	if a.ActionType == widget.ToolkitInit {
		log.Log(TREE, "tree.doAction() trapped ToolkitInit finally!")
		a.WidgetType = widget.Root
		n := addNode(&a)
		me.Add(n)
		log.Log(TREE, "tree.doAction() init() me.treeRoot")
		if me.ToolkitInit == nil {
			log.Log(TREE, "tree.doAction() ToolkitInit() was called before plugin had a chance to initialize")
			log.Log(TREE, "tree.doAction() TODO: fix channel to pause")
			return
		}
		log.Log(TREE, "tree.doAction() doing ToolkitInit()")
		me.ToolkitInit()
		return
	}
	if a.TablePB != nil {
		log.Log(TREE, "tree: got a TablePB")
		me.doTable(a)
		return
	}
	if a.WidgetId == 0 {
		if treeRoot == nil {
			log.Log(TREE, "tree.doAction() yes, treeRoot is nil. add here")
		}
	}
	n := treeRoot.FindWidgetId(a.WidgetId)
	switch a.ActionType {
	case widget.Add:
		if n == nil {
			n := me.AddNode(&a)
			me.Add(n)
			return
		}
		if a.WidgetId == 0 {
			// this is ok. This is the binary tree base and it's already initialized. This happens on startup
			return
		}
		// this shouldn't really happen. It's good to print a warning so the plugin code can be debugged
		log.Log(TREEWARN, "attempting to re-add widget", a.WidgetId, a.WidgetType, a.ActionType)
		return
	}
	if n == nil {
		// log.Log(TREEWARN, "tree.FindWidgetId() n == nil", a.WidgetId, a.WidgetType, a.ActionType)
		// log.Log(TREEWARN, "tree.FindWidgetId() n == nil", a.State.CurrentS)
		// log.Log(TREEWARN, "tree.FindWidgetId() n == nil. A bug in your application?")
		log.Log(TREEWARN, "tree.doAction() bug in gui. trying to do action", a.ActionType, "before widget init() wId =", a.WidgetId)
		if a.WidgetId == 0 {
			log.Log(TREEWARN, "tree.doAction() bug in gui. on wId zero. is treeRoot nil?")
			if treeRoot == nil {
				log.Log(TREEWARN, "tree.doAction() yes, treeRoot is nil")
			}
		}
		return
	}

	switch a.ActionType {
	case widget.SetText:
		log.Log(TREE, "tree.SetText() a.State.CurrentS =", a.State.CurrentS)
		log.Log(TREE, "tree.SetText() a.State.DefaultS =", a.State.DefaultS)
		log.Log(TREE, "tree.SetText() a.State.NewString =", a.State.NewString)
		switch n.WidgetType {
		case widget.Dropdown:
			me.SetText(n, a.State.NewString)
		case widget.Combobox:
			me.SetText(n, a.State.NewString)
		case widget.Textbox:
			me.SetText(n, a.State.NewString)
		case widget.Window:
			me.SetTitle(n, a.State.Label)
		default:
			// buttons, checkboxes, groups, etc
			me.SetLabel(n, a.State.Label)
		}
	case widget.AddText:
		switch n.WidgetType {
		case widget.Dropdown:
			n.ddStrings = append(n.ddStrings, a.State.NewString)
			me.AddText(n, a.State.NewString)
		case widget.Combobox:
			n.ddStrings = append(n.ddStrings, a.State.NewString)
			me.AddText(n, a.State.NewString)
		default:
			log.Log(TREEWARN, "AddText() not supported on widget", n.WidgetType, n.String())
		}
	case widget.Checked:
		switch n.WidgetType {
		case widget.Checkbox:
			if me.SetChecked == nil {
				log.Log(TREEWARN, "SetChecked() == nil in toolkit", me.PluginName)
			} else {
				me.SetChecked(n, a.State.Checked)
			}
		default:
			log.Log(TREEWARN, "SetChecked() not supported on widget", n.WidgetType, n.String())
		}
	case widget.Show:
		if n.WidgetType == widget.Table {
			t, err := loadTable(&a)
			if err != nil {
				log.Info("unmarshal data failed", err)
				return
			}
			if t == nil {
				log.Info("unmarshal data failed table == nil")
			} else {
				me.ShowTable(nil)
			}
		} else {
			n.State.Hidden = false
			me.Show(n)
		}
	case widget.Hide:
		n.State.Hidden = true
		me.Hide(n)
		log.Info("tree: doing hide here on", a.WidgetId, n.WidgetType)
	case widget.Enable:
		n.State.Enable = true
		me.Enable(n)
	case widget.Disable:
		n.State.Enable = false
		me.Disable(n)
	case widget.Delete:
		if me.Hide == nil {
			log.Info("toolkit doesn't know how to Hide() widgets")
		} else {
			me.Hide(n)
		}
		me.DeleteNode(n)
		// now remove the child from the parent
	default:
		log.Log(TREEWARN, "tree.Action() unknown action", a.ActionType, "on wId", a.WidgetId)
		// me.NodeAction(n, a.ActionType)
	}
}

func loadTable(a *widget.Action) (*guipb.Tables, error) {
	var t *guipb.Tables
	err := t.Unmarshal(a.TablePB)
	/*
		test := NewRepos()
		if test.Uuid != all.Uuid {
			log.Log(WARN, "uuids do not match", test.Uuid, all.Uuid)
			deleteProtobufFile(cfgname)
		}
		if test.Version != all.Version {
			log.Log(WARN, "versions do not match", test.Version, all.Version)
			deleteProtobufFile(cfgname)
		}
	*/
	// log.Log(INFO, cfgname, "protobuf versions and uuid match", all.Uuid, all.Version)
	return t, err
}