-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerEditDialog.cs
151 lines (128 loc) · 3.95 KB
/
ServerEditDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using Xwt;
namespace ginger
{
public class ServerEditDialog
: Dialog
{
BrowserContext _context;
ListBox _listBox;
TextEntry _name = new TextEntry();
TextEntry _hostname = new TextEntry();
SpinButton _port = new SpinButton {
MaximumValue = 65535,
IncrementValue = 1.0,
Digits = 0
};
Button _addButton;
Button _deleteButton;
bool _updating;
public ServerEditDialog(BrowserContext context)
{
_context = context;
var vbox = new VBox();
vbox.WidthRequest = 300;
var hbox = new HBox();
_listBox = new ListBox();
_listBox.SelectionChanged += (sender, e) => {
if (!_updating)
Update();
};
PopulateListBox();
var commandBox = CommandBox();
hbox.PackStart(_listBox, true, true);
hbox.PackStart(commandBox, false, WidgetPlacement.Center);
vbox.PackStart(hbox, true, true);
var table = new Table();
table.Add(new Label("名前"), 0, 0);
table.Add(new Label("ホスト名"), 0, 1);
table.Add(new Label("ポート番号"), 0, 2);
table.Add(_name, 1, 0, 1, 1, true, true);
table.Add(_hostname, 1, 1, 1, 1, true, true);
table.Add(_port, 1, 2, 1, 1, false, false, WidgetPlacement.Start);
_name.Changed += (sender, e) => {
_updating = true;
var server = ((Server) _listBox.SelectedItem);
server.Name = _name.Text;
int row = _listBox.SelectedRow;
_listBox.Items.RemoveAt(row);
_listBox.Items.Insert(row, server, server.Name);
_listBox.SelectRow(row);
_updating = false;
};
_hostname.Changed += (sender, e) => {
var server = (Server) _listBox.SelectedItem;
if (!_updating && server != null)
server.Hostname = _hostname.Text;
};
_port.ValueChanged += (sender, e) => {
var server = (Server) _listBox.SelectedItem;
if (!_updating && server != null)
server.Port = (int) _port.Value;
};
vbox.PackStart(table);
var okButton = new DialogButton(Command.Ok);
okButton.Clicked += (sender, e) => {
_context.Ginger.Servers.Clear();
foreach (Server server in _listBox.Items) {
_context.Ginger.Servers.Add(server);
}
Respond(Command.Ok);
Close();
};
Buttons.Add(okButton);
Content = vbox;
Update();
Closed += (sender, e) => {
_context.Ginger.SaveSettings();
};
}
void PopulateListBox()
{
_listBox.Items.Clear();
foreach (var server in _context.Ginger.Servers) {
_listBox.Items.Add(server, server.Name);
}
}
void Update()
{
var server = (Server) _listBox.SelectedItem;
if (server == null) {
_name.Text = "";
_hostname.Text = "";
_port.Value = 0;
_name.Sensitive = _hostname.Sensitive = _port.Sensitive = false;
}
else {
_name.Text = server.Name;
_hostname.Text = server.Hostname;
_port.Value = server.Port;
_name.Sensitive = _hostname.Sensitive = _port.Sensitive = true;
}
}
Widget CommandBox()
{
var vbox = new VBox { WidthRequest = 80 };
_addButton = new Button("追加");
_deleteButton = new Button("削除");
_addButton.Clicked += (sender, e) => {
var server = new Server("新規サーバー", "", 7144);
_context.Ginger.Servers.Add(server);
PopulateListBox();
_listBox.SelectRow(_listBox.Items.Count - 1);
};
_deleteButton.Clicked += (sender, e) => {
var server = (Server) _listBox.SelectedItem;
if (server != null) {
int row = _listBox.SelectedRow;
_context.Ginger.Servers.Remove(server);
PopulateListBox();
_listBox.SelectRow(Math.Min(row, _listBox.Items.Count - 1));
}
};
vbox.PackStart(_addButton);
vbox.PackStart(_deleteButton);
return vbox;
}
}
}