tree/action.go

131 lines
3.6 KiB
Go

// 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.TablePB != nil {
log.Log(TREEWARN, "tree: got a TablePB")
me.doTable(a)
return
}
n := treeRoot.FindWidgetId(a.WidgetId)
switch a.ActionType {
case widget.Add:
if n == nil {
n := me.AddNode(&a)
me.Add(n)
return
}
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)
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:
me.Enable(n)
case widget.Disable:
me.Disable(n)
case widget.Delete:
me.Hide(n)
log.Info("tree: todo: remove child from parent")
n.DeleteNode()
// 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
}