ppforge/internal/ui/protocolfilehandler.go

91 lines
2.0 KiB
Go
Raw Normal View History

2023-01-05 14:16:41 +01:00
package ui
import (
2023-04-11 11:32:22 +02:00
"fmt"
2023-03-31 15:38:20 +02:00
"fyne.io/fyne/v2"
2023-04-11 11:32:22 +02:00
"fyne.io/fyne/v2/container"
2023-05-12 09:10:24 +02:00
"gitea.mmo.to/ppForge/ppforge/protocol"
"gitea.mmo.to/ppForge/ppforge/protocolctl"
2023-01-05 14:16:41 +01:00
)
2023-04-11 16:05:24 +02:00
// implements FileHandler, TabProvider
2023-01-05 14:16:41 +01:00
type ProtocolFileHandler struct {
ProtocolEditor *ProtocolEditor
2023-03-31 15:38:20 +02:00
name string
path string
changed bool
2023-04-11 11:32:22 +02:00
tab *container.TabItem
2023-01-05 14:16:41 +01:00
Reference *protocol.ProtocolStructure
}
func NewProtocolFileHandler() *ProtocolFileHandler {
pfh := ProtocolFileHandler{}
2023-03-31 15:38:20 +02:00
pfh.name = "*new"
pfh.changed = true
pfh.ProtocolEditor = NewProtocolEditor(protocolctl.NewProtocolStructure())
2023-04-11 11:32:22 +02:00
pfh.Reference = pfh.ProtocolEditor.Reference
2023-01-05 14:16:41 +01:00
return &pfh
}
2023-03-31 15:38:20 +02:00
func (pfh *ProtocolFileHandler) Load() error {
prot, err := protocolctl.LoadNew(pfh.Path())
2023-01-05 14:16:41 +01:00
if err != nil {
// process error
err = nil
2023-01-05 14:16:41 +01:00
}
pfh.ProtocolEditor = NewProtocolEditor(prot)
pfh.Reference = prot
2023-03-31 15:38:20 +02:00
pfh.name = prot.Metadata.Name
return err
}
func (pfh *ProtocolFileHandler) GetWorkarea() *fyne.Container {
return pfh.ProtocolEditor.Representation
}
func (pfh *ProtocolFileHandler) Name() string {
return pfh.name
}
func (pfh *ProtocolFileHandler) Path() string {
return pfh.path
}
func (pfh *ProtocolFileHandler) Open(path string) error {
pfh.path = path
return pfh.Load()
}
func (pfh *ProtocolFileHandler) Save() error {
2023-04-11 11:32:22 +02:00
err := protocolctl.Save(pfh.Reference, pfh.path)
if err != nil {
fmt.Printf("%v\n", err)
}
2023-03-31 15:38:20 +02:00
pfh.changed = false
2023-04-11 11:32:22 +02:00
return err
2023-03-31 15:38:20 +02:00
}
func (pfh *ProtocolFileHandler) SaveAs(path string) error {
pfh.path = path
2023-04-11 11:32:22 +02:00
return pfh.Save()
2023-03-31 15:38:20 +02:00
}
func (pfh *ProtocolFileHandler) Close() error {
return pfh.Save()
}
func (pfh *ProtocolFileHandler) SetChanged() {
pfh.changed = true
2023-01-05 14:16:41 +01:00
}
2023-03-31 15:38:20 +02:00
func (pfh *ProtocolFileHandler) HasChanged() bool {
return pfh.changed
2023-01-05 14:16:41 +01:00
}
2023-03-31 15:38:20 +02:00
func (pfh *ProtocolFileHandler) GetReference() interface{} {
return pfh.Reference
2023-01-05 14:16:41 +01:00
}
2023-04-11 11:32:22 +02:00
func (pfh *ProtocolFileHandler) Tab() *container.TabItem {
return pfh.tab
}
func (pfh *ProtocolFileHandler) SetTab(tab *container.TabItem) {
pfh.tab = tab
}