From 924957d00d49dc8c9dca32a2d09298af9afacbe6 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 23 Jun 2024 14:55:21 +1200 Subject: [PATCH] main: abstract the nil selection into a virtual database type --- db_none.go | 35 +++++++++++++++++++++++++++++++++++ main.go | 22 +++++++++++++--------- 2 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 db_none.go diff --git a/db_none.go b/db_none.go new file mode 100644 index 0000000..f2ddaf5 --- /dev/null +++ b/db_none.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/ying32/govcl/vcl" +) + +type noLoadedDatabase struct{} + +func (n *noLoadedDatabase) DisplayName() string { + return "yvbolt" +} + +func (n *noLoadedDatabase) DriverName() string { + return "No database selected" +} + +func (n *noLoadedDatabase) RootElement() *vcl.TTreeNode { + return nil +} + +func (n *noLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) { + f.propertiesBox.SetText("Open a database to get started...") + f.contentBox.SetEnabled(false) + f.contentBox.Clear() +} + +func (n *noLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { +} + +func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { + return []string{}, nil +} + +func (n *noLoadedDatabase) Keepalive(ndata *navData) { +} diff --git a/main.go b/main.go index 972eb8a..c60d43c 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,8 @@ type TMainForm struct { queryInput *vcl.TMemo queryResult *vcl.TListView - dbs []loadedDatabase + none *noLoadedDatabase + dbs []loadedDatabase } var ( @@ -137,7 +138,6 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) { f.propertiesBox.SetReadOnly(true) f.propertiesBox.SetEnabled(false) f.propertiesBox.SetBorderStyle(types.BsNone) - f.propertiesBox.SetText("Open a database to get started...") dataTab := vcl.NewTabSheet(f.Tabs) dataTab.SetParent(f.Tabs) @@ -199,6 +199,9 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) { f.queryResult.BorderSpacing().SetLeft(MY_SPACING) f.queryResult.BorderSpacing().SetRight(MY_SPACING) f.queryResult.BorderSpacing().SetBottom(MY_SPACING) + + f.none = &noLoadedDatabase{} + f.OnNavChange(f, nil) // calls f.none.RenderForNav and sets up status bar content } func (f *TMainForm) OnMnuFileOpenClick(sender vcl.IObject) { @@ -281,17 +284,18 @@ func (f *TMainForm) OnQueryExecute(sender vcl.IObject) { func (f *TMainForm) OnNavChange(sender vcl.IObject, node *vcl.TTreeNode) { - if node.Data() == nil { - vcl.ShowMessage("unexpected nil data") - return + var ld loadedDatabase = f.none + var ndata *navData = nil + + if node != nil && node.Data() != nil { + ndata = (*navData)(node.Data()) + ld = ndata.ld } - ndata := (*navData)(node.Data()) - - ndata.ld.RenderForNav(f, ndata) // Handover to the database type's own renderer function + ld.RenderForNav(f, ndata) // Handover to the database type's own renderer function // We're in charge of common status bar text updates - f.StatusBar.SetSimpleText(ndata.ld.DisplayName() + " | " + ndata.ld.DriverName()) + f.StatusBar.SetSimpleText(ld.DisplayName() + " | " + ld.DriverName()) } func (f *TMainForm) OnNavExpanding(sender vcl.IObject, node *vcl.TTreeNode, allowExpansion *bool) {