1
|
|
|
package ui |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"regexp" |
6
|
|
|
"strings" |
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 fyne.Window |
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
|
|
|
type OnNewConnection func(client files.FileSystemClient) |
30
|
|
|
|
31
|
|
|
func NewFileRepoManagerDialog() *FileRepoManagerDialog { |
32
|
|
|
repository, err := files.GetFileSystemRepository() |
33
|
|
|
|
34
|
|
|
if err != nil { |
35
|
|
|
log.Error.Fatal(err) |
36
|
|
|
} |
37
|
|
|
dialog := &FileRepoManagerDialog{ |
38
|
|
|
Title: "Connections", |
39
|
|
|
popup: nil, |
40
|
|
|
fileSystemRepo: repository, |
41
|
|
|
fsList: binding.NewStringList(), |
42
|
|
|
name: binding.NewString(), |
43
|
|
|
description: binding.NewString(), |
44
|
|
|
addresses: binding.NewString(), |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
dialog.fsList.Set(dialog.fileSystemNames()) |
48
|
|
|
|
49
|
|
|
return dialog |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
func (d *FileRepoManagerDialog) edit(label string) error { |
53
|
|
|
fs, err := d.fileSystemRepo.FindByName(label) |
54
|
|
|
|
55
|
|
|
if err != nil { |
56
|
|
|
return err |
57
|
|
|
} |
58
|
|
|
d.name.Set(fs.Name) |
59
|
|
|
d.description.Set(fs.Description) |
60
|
|
|
d.addresses.Set(strings.Join(fs.Addresses, "\n")) |
61
|
|
|
return nil |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
func (d *FileRepoManagerDialog) new() { |
65
|
|
|
d.listUI.ClearSelected() |
66
|
|
|
d.name.Set("") |
67
|
|
|
d.description.Set("") |
68
|
|
|
d.addresses.Set("") |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func (d *FileRepoManagerDialog) save() error { |
72
|
|
|
name, _ := d.name.Get() |
73
|
|
|
desc, _ := d.description.Get() |
74
|
|
|
addr, _ := d.addresses.Get() |
75
|
|
|
|
76
|
|
|
if len(name) == 0 || len(desc) == 0 || len(addr) == 0 { |
77
|
|
|
return errors.New("Missing field data") |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
re := regexp.MustCompile(`\s+`) |
81
|
|
|
split := re.Split(addr, -1) |
82
|
|
|
addresses := make([]string, 0) |
83
|
|
|
if split == nil { |
84
|
|
|
addresses = []string{strings.TrimSpace(addr)} |
85
|
|
|
} else { |
86
|
|
|
for _, v := range split { |
87
|
|
|
addresses = append(addresses, strings.TrimSpace(v)) |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
fs := files.FileSystem{ |
92
|
|
|
Name: name, |
93
|
|
|
Description: desc, |
94
|
|
|
Addresses: addresses, |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
err := d.fileSystemRepo.Store(fs) |
98
|
|
|
if err == nil { |
99
|
|
|
d.fsList.Set(d.fileSystemNames()) |
100
|
|
|
} |
101
|
|
|
d.new() |
102
|
|
|
return err |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
func (d *FileRepoManagerDialog) fileSystemNames() []string { |
106
|
|
|
list, err := d.fileSystemRepo.List() |
107
|
|
|
if err != nil { |
108
|
|
|
ShowFatalError(err) |
109
|
|
|
} |
110
|
|
|
names := make([]string, len(list)) |
111
|
|
|
for i, fileSystem := range list { |
112
|
|
|
names[i] = fileSystem.Name |
113
|
|
|
} |
114
|
|
|
return names |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func (d *FileRepoManagerDialog) Close() { |
118
|
|
|
if d.popup != nil { |
119
|
|
|
d.popup.Close() |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
func (d *FileRepoManagerDialog) Open(onNewConnection OnNewConnection) { |
124
|
|
|
if d.popup == nil { |
125
|
|
|
fsList := d.fileSystemList(onNewConnection) |
126
|
|
|
form := d.createForm() |
127
|
|
|
content := container.NewBorder(nil, nil, nil, form, fsList, widget.NewSeparator()) |
128
|
|
|
newButton := widget.NewButton("New", func() { |
129
|
|
|
d.new() |
130
|
|
|
}) |
131
|
|
|
saveButton := widget.NewButton("Save", func() { |
132
|
|
|
if err := d.save(); err != nil { |
133
|
|
|
dialog.ShowError(err, Window) |
134
|
|
|
} |
135
|
|
|
}) |
136
|
|
|
d.popup = NewCustomDialog( |
137
|
|
|
d.Title, |
138
|
|
|
content, |
139
|
|
|
newButton, |
140
|
|
|
saveButton, |
141
|
|
|
) |
142
|
|
|
} |
143
|
|
|
d.popup.Show() |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
func (d *FileRepoManagerDialog) createForm() *fyne.Container { |
147
|
|
|
nameLabel := widget.NewLabel("Name: ") |
148
|
|
|
nameEntry := widget.NewEntryWithData(d.name) |
149
|
|
|
descriptionLabel := widget.NewLabel("Description: ") |
150
|
|
|
descriptionEntry := widget.NewEntryWithData(d.description) |
151
|
|
|
descriptionEntry.MultiLine = true |
152
|
|
|
addressLabel := widget.NewLabel("Addresses (new line sperated):") |
153
|
|
|
addressEntry := widget.NewEntryWithData(d.addresses) |
154
|
|
|
addressEntry.MultiLine = true |
155
|
|
|
|
156
|
|
|
return container.NewVBox( |
157
|
|
|
nameLabel, |
158
|
|
|
nameEntry, |
159
|
|
|
descriptionLabel, |
160
|
|
|
descriptionEntry, |
161
|
|
|
addressLabel, |
162
|
|
|
addressEntry, |
163
|
|
|
) |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
func (d *FileRepoManagerDialog) fileSystemList(onNewConnection OnNewConnection) *fyne.Container { |
167
|
|
|
d.listUI = NewSelectableList(d.fsList) |
168
|
|
|
d.listUI.list.Resize(fyne.NewSize(400, 200)) |
169
|
|
|
|
170
|
|
|
d.listUI.AddListener(func(event Event) { |
171
|
|
|
label := event.EventSource |
172
|
|
|
if event.EventType == "click" { |
173
|
|
|
if err := d.edit(label); err != nil { |
174
|
|
|
dialog.ShowError(err, Window) |
175
|
|
|
} |
176
|
|
|
} else if event.EventType == "dblclick" { |
177
|
|
|
fs, _ := d.fileSystemRepo.FindByName(event.EventSource) |
178
|
|
|
client, err := fs.Connect() |
179
|
|
|
d.popup.Hide() |
180
|
|
|
if err != nil { |
181
|
|
|
dialog.ShowError(err, Window) |
182
|
|
|
} else { |
183
|
|
|
onNewConnection(client) |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
}) |
187
|
|
|
|
188
|
|
|
return container.New(layout.NewMaxLayout(), d.listUI.CanvasObject()) |
189
|
|
|
} |
190
|
|
|
|