This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSettingsForm.cs
296 lines (248 loc) · 9.07 KB
/
SettingsForm.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//!CompilerOption:AddRef:SharpSvn.dll
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace repoBuddy;
public class SettingsForm : Form
{
private FlowLayoutPanel controlPanel = new FlowLayoutPanel();
private FlowLayoutPanel buttonPanel = new FlowLayoutPanel();
private DataGridView repoDataGridView = new DataGridView();
private TextBox nameBox = new TextBox();
private ComboBox typeBox = new ComboBox();
private TextBox urlBox = new TextBox();
private Button addNewRowButton = new Button();
private Button deleteRowButton = new Button();
private Button restartRBButton = new Button();
private TabPage repoTab = new TabPage();
private TabPage ddlTab = new TabPage();
private TabControl tabControls = new TabControl();
private DataSet repoDataSet = repoBuddy.repoDataSet;
private Dictionary<string, List<string>> ddlDict = repoBuddy.ddlDict;
private void SetupLayout()
{
Text = "repoBuddy Settings";
Size = new System.Drawing.Size(525, 400);
MaximizeBox = false;
MinimizeBox = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
repoTab.Text = "Repositories";
repoTab.TabIndex = 0;
ddlTab.Text = "One-time Downloads";
ddlTab.TabIndex = 1;
nameBox.Text = "Repo Name";
nameBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
nameBox.GotFocus += new EventHandler(NameBox_GotFocus);
nameBox.LostFocus += new EventHandler(NameBox_LostFocus);
typeBox.Items.AddRange(new string[] { "BotBase", "Plugin", "Profile", "Routine", "Quest Behavior" });
typeBox.DropDownStyle = ComboBoxStyle.DropDownList;
typeBox.SelectedIndex = 0;
urlBox.Text = "Repo URL";
urlBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
urlBox.GotFocus += new EventHandler(urlBox_GotFocus);
urlBox.LostFocus += new EventHandler(urlBox_LostFocus);
addNewRowButton.Text = "Add Row";
addNewRowButton.Click += new EventHandler(AddNewRowButton_Click);
deleteRowButton.Text = "Delete Row";
deleteRowButton.Click += new EventHandler(DeleteRowButton_Click);
restartRBButton.Text = "Restart RebornBuddy";
restartRBButton.Click += new EventHandler(restartRBButton_Click);
restartRBButton.AutoSize = true;
restartRBButton.Width = 495;
restartRBButton.FlatStyle = FlatStyle.Flat;
buttonPanel.Controls.Add(restartRBButton);
foreach (KeyValuePair<string, List<string>> pair in ddlDict)
{
string ddlname = pair.Key;
string ddlmask = pair.Value[0];
string ddluri = pair.Value[1];
string ddldesc = pair.Value[2];
Button button = new Button();
button.Click += new EventHandler(ddlButton_Click);
button.Name = ddlname;
button.Text = ddlname + " - " + ddldesc;
button.AutoSize = true;
button.Width = 495;
buttonPanel.Controls.Add(button);
string resolvedMask;
if (ddlmask.Contains(ddlname.Replace("-CN", "")))
{
resolvedMask = ddlmask;
}
else
{
resolvedMask = ddlmask + @"\" + ddlname.Replace("-CN", "");
}
if (Directory.Exists(resolvedMask))
{
button.Text = $"[INSTALLED] {button.Text}";
button.Enabled = false;
}
button.FlatStyle = FlatStyle.Flat;
}
controlPanel.Controls.Add(nameBox);
controlPanel.Controls.Add(typeBox);
controlPanel.Controls.Add(urlBox);
controlPanel.Controls.Add(addNewRowButton);
controlPanel.Controls.Add(deleteRowButton);
controlPanel.AutoSize = true;
controlPanel.Dock = DockStyle.Bottom;
buttonPanel.AutoScroll = true;
buttonPanel.AutoSize = true;
buttonPanel.Dock = DockStyle.Fill;
tabControls.Dock = DockStyle.Fill;
tabControls.Controls.Add(repoTab);
repoTab.Controls.Add(repoDataGridView); //controls have their Dock setting evaluated from the bottom up, so this is first to prevent overlap with control panel
repoTab.Controls.Add(controlPanel);
tabControls.Controls.Add(ddlTab);
ddlTab.Controls.Add(buttonPanel);
Controls.Add(tabControls);
}
private void SetupDataGridView()
{
DataGridViewTextBoxColumn repoNameColumn = new DataGridViewTextBoxColumn()
{
HeaderText = "Name",
DataPropertyName = "Name",
Width = 75
};
DataGridViewTextBoxColumn repoTypeColumn = new DataGridViewTextBoxColumn()
{
HeaderText = "Type",
DataPropertyName = "Type",
Width = 75
};
DataGridViewTextBoxColumn repoURLColumn = new DataGridViewTextBoxColumn()
{
HeaderText = "URL",
DataPropertyName = "URL",
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
SortMode = DataGridViewColumnSortMode.NotSortable
};
repoDataGridView.Columns.Add(repoNameColumn);
repoDataGridView.Columns.Add(repoTypeColumn);
repoDataGridView.Columns.Add(repoURLColumn);
repoDataGridView.AllowUserToAddRows = false;
repoDataGridView.AllowUserToDeleteRows = false;
repoDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
repoDataGridView.ReadOnly = true;
repoDataGridView.AutoGenerateColumns = false;
repoDataGridView.AllowUserToResizeColumns = false;
repoDataGridView.AllowUserToResizeRows = false;
repoDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
repoDataGridView.Dock = DockStyle.Fill;
repoDataGridView.RowHeadersVisible = false;
}
private void PopulateDataGridView()
{
repoDataGridView.DataSource = repoDataSet;
repoDataGridView.DataMember = "Repo";
}
public SettingsForm()
{
Load += new EventHandler(Form1_Load);
FormClosing += new FormClosingEventHandler(Form1_Unload);
}
private void Form1_Load(object sender, EventArgs e)
{
SetupLayout();
SetupDataGridView();
PopulateDataGridView();
repoDataGridView.Select(); //focus repoDataGridView
}
private void Form1_Unload(object sender, FormClosingEventArgs e)
{
repoDataSet.WriteXml(Constants.ReposXmlPath);
}
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(1)) //unicode char (decimal) for ctrl+a
{
((TextBox)sender).SelectAll();
e.Handled = true;
}
}
private void NameBox_GotFocus(object sender, EventArgs e)
{
if (nameBox.Text == "Repo Name")
{
nameBox.Text = "";
}
}
private void NameBox_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(nameBox.Text))
{
nameBox.Text = "Repo Name";
}
}
private void urlBox_GotFocus(object sender, EventArgs e)
{
if (urlBox.Text == "Repo URL" || urlBox.Text == "Invalid URL")
{
urlBox.Text = "";
}
}
private void urlBox_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(urlBox.Text) || !ValidateURL(urlBox.Text))
{
urlBox.Text = "Invalid URL";
}
}
private bool ValidateURL(string url)
{
bool result;
try
{
Uri validateUri = new Uri(url);
result = true;
}
catch (UriFormatException)
{
result = false;
}
return result;
}
private void AddNewRow(DataSet dataSet)
{
DataTable table;
table = dataSet.Tables["Repo"];
DataRow newRow = table.NewRow();
newRow["Name"] = nameBox.Text;
newRow["Type"] = typeBox.Text;
newRow["URL"] = urlBox.Text;
if (ValidateURL(urlBox.Text))
{
table.Rows.Add(newRow);
}
}
private void AddNewRowButton_Click(object sender, EventArgs e)
{
AddNewRow(repoDataSet);
}
private void DeleteRowButton_Click(object sender, EventArgs e)
{
if (repoDataGridView.SelectedRows.Count > 0)
{
repoDataGridView.Rows.RemoveAt(repoDataGridView.SelectedRows[0].Index);
}
}
private void ddlButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string path = $@"{ddlDict[button.Name][0]}";
string zipUrl = ddlDict[button.Name][1];
repoBuddy.DirectDownload(path, zipUrl);
button.Text = $"[INSTALLED] {button.Text}";
button.Enabled = false;
}
private void restartRBButton_Click(object sender, EventArgs e)
{
this.Close();
Task.Delay(100).ContinueWith(t => repoBuddy.RestartRebornBuddy());
}
}