main: abstract the nil selection into a virtual database type

This commit is contained in:
mappu 2024-06-23 14:55:21 +12:00
parent d674078071
commit 924957d00d
2 changed files with 48 additions and 9 deletions

35
db_none.go Normal file
View File

@ -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) {
}

20
main.go
View File

@ -22,6 +22,7 @@ type TMainForm struct {
queryInput *vcl.TMemo
queryResult *vcl.TListView
none *noLoadedDatabase
dbs []loadedDatabase
}
@ -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) {