1
|
|
|
package ui |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"fmt" |
6
|
|
|
"regexp" |
7
|
|
|
|
8
|
|
|
"fyne.io/fyne/v2" |
9
|
|
|
"fyne.io/fyne/v2/container" |
10
|
|
|
"fyne.io/fyne/v2/data/binding" |
11
|
|
|
"fyne.io/fyne/v2/dialog" |
12
|
|
|
"fyne.io/fyne/v2/layout" |
13
|
|
|
"fyne.io/fyne/v2/widget" |
14
|
|
|
"github.com/rchargel/hdfs-explorer/files" |
15
|
|
|
"github.com/rchargel/hdfs-explorer/log" |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
type FileRepoManagerDialog struct { |
19
|
|
|
Title string |
20
|
|
|
popup *widget.PopUp |
21
|
|
|
listUI *ListWithListener |
22
|
|
|
fileSystemRepo files.FileSystemRepository |
23
|
|
|
fsList binding.StringList |
24
|
|
|
name binding.String |
25
|
|
|
description binding.String |
26
|
|
|
addresses binding.String |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
func NewFileRepoManagerDialog() *FileRepoManagerDialog { |
30
|
|
|
repository, err := files.GetFileSystemRepository() |
31
|
|
|
|
32
|
|
|
if err != nil { |
33
|
|
|
log.Error.Fatal(err) |
34
|
|
|
} |
35
|
|
|
dialog := &FileRepoManagerDialog{ |
36
|
|
|
Title: "Connections", |
37
|
|
|
popup: nil, |
38
|
|
|
fileSystemRepo: repository, |
39
|
|
|
fsList: binding.NewStringList(), |
40
|
|
|
name: binding.NewString(), |
41
|
|
|
description: binding.NewString(), |
42
|
|
|
addresses: binding.NewString(), |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
dialog.fsList.Set(dialog.fileSystemNames()) |
46
|
|
|
|
47
|
|
|
return dialog |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func (d *FileRepoManagerDialog) addNew() { |
51
|
|
|
d.listUI.ClearSelected() |
52
|
|
|
d.name.Set("") |
53
|
|
|
d.description.Set("") |
54
|
|
|
d.addresses.Set("") |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (d *FileRepoManagerDialog) save() error { |
58
|
|
|
name, _ := d.name.Get() |
59
|
|
|
desc, _ := d.description.Get() |
60
|
|
|
addr, _ := d.addresses.Get() |
61
|
|
|
|
62
|
|
|
re := regexp.MustCompile(`\s+`) |
63
|
|
|
addresses := re.FindAllString(addr, -1) |
64
|
|
|
|
65
|
|
|
if len(name) == 0 || len(desc) == 0 || addresses == nil { |
66
|
|
|
return errors.New("Missing field data") |
67
|
|
|
} |
68
|
|
|
fs := files.FileSystem{ |
69
|
|
|
Name: name, |
70
|
|
|
Description: desc, |
71
|
|
|
Addresses: addresses, |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
err := d.fileSystemRepo.Store(fs) |
75
|
|
|
if err == nil { |
76
|
|
|
d.fsList.Set(d.fileSystemNames()) |
77
|
|
|
} |
78
|
|
|
d.addNew() |
79
|
|
|
return err |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
func (d *FileRepoManagerDialog) fileSystemNames() []string { |
83
|
|
|
list, err := d.fileSystemRepo.List() |
84
|
|
|
if err != nil { |
85
|
|
|
ShowFatalError(err) |
86
|
|
|
} |
87
|
|
|
names := make([]string, len(list)) |
88
|
|
|
for i, fileSystem := range list { |
89
|
|
|
names[i] = fileSystem.Name |
90
|
|
|
} |
91
|
|
|
return names |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
func (d *FileRepoManagerDialog) Open() { |
95
|
|
|
if d.popup == nil { |
96
|
|
|
fsList := d.fileSystemList() |
97
|
|
|
form := d.createForm() |
98
|
|
|
content := container.NewHBox(fsList, form) |
99
|
|
|
newButton := widget.NewButton("New", func() { |
100
|
|
|
d.addNew() |
101
|
|
|
}) |
102
|
|
|
saveButton := widget.NewButton("Save", func() { |
103
|
|
|
if err := d.save(); err != nil { |
104
|
|
|
dialog.ShowError(err, Window) |
105
|
|
|
} |
106
|
|
|
}) |
107
|
|
|
d.popup = NewCustomDialog( |
108
|
|
|
d.Title, |
109
|
|
|
content, |
110
|
|
|
newButton, |
111
|
|
|
saveButton, |
112
|
|
|
) |
113
|
|
|
} |
114
|
|
|
d.popup.Show() |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func (d *FileRepoManagerDialog) createForm() *fyne.Container { |
118
|
|
|
nameLabel := widget.NewLabel("Name: ") |
119
|
|
|
nameEntry := widget.NewEntryWithData(d.name) |
120
|
|
|
descriptionLabel := widget.NewLabel("Description: ") |
121
|
|
|
descriptionEntry := widget.NewEntryWithData(d.description) |
122
|
|
|
descriptionEntry.MultiLine = true |
123
|
|
|
addressLabel := widget.NewLabel("Addresses (new line sperated):") |
124
|
|
|
addressEntry := widget.NewEntryWithData(d.addresses) |
125
|
|
|
addressEntry.MultiLine = true |
126
|
|
|
|
127
|
|
|
return container.NewVBox( |
128
|
|
|
nameLabel, |
129
|
|
|
nameEntry, |
130
|
|
|
descriptionLabel, |
131
|
|
|
descriptionEntry, |
132
|
|
|
addressLabel, |
133
|
|
|
addressEntry, |
134
|
|
|
) |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
func (d *FileRepoManagerDialog) fileSystemList() *fyne.Container { |
138
|
|
|
d.listUI = NewSelectableList(d.fsList) |
139
|
|
|
|
140
|
|
|
d.listUI.AddListener(func(event interface{}) { |
141
|
|
|
label := event.(string) |
142
|
|
|
fmt.Printf("Selected label %v\n", label) |
143
|
|
|
}) |
144
|
|
|
|
145
|
|
|
return container.New(layout.NewMaxLayout(), d.listUI.CanvasObject()) |
146
|
|
|
} |
147
|
|
|
|