-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddContextForm.cs
More file actions
90 lines (76 loc) · 3 KB
/
Copy pathAddContextForm.cs
File metadata and controls
90 lines (76 loc) · 3 KB
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
using System;
using System.Collections.Generic;
using Eto.Drawing;
using Eto.Forms;
namespace devtime
{
public class AddContextForm : Dialog<bool>
{
public string? ContextName { get; private set; }
private readonly TextBox _textBox;
public AddContextForm()
{
Title = "Add project";
Icon = IconLoader.LoadIcon("add.png");
Resizable = false;
_textBox = new TextBox { MaxLength = 255, Width = 150 };
var okButton = new Button { Text = "OK", Width = 70, Height = 23 };
okButton.Click += (sender, e) => OkClick();
var cancelButton = new Button { Text = "Cancel", Width = 70, Height = 23 };
cancelButton.Click += (sender, e) => Close(false);
DefaultButton = okButton;
AbortButton = cancelButton;
var buttonLayout = new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 5,
Items = { okButton, cancelButton }
};
Content = new StackLayout
{
Padding = new Padding(10, 5),
Spacing = 5,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
Items =
{
new Label { Text = "Project name:" },
_textBox,
new StackLayout { HorizontalContentAlignment = HorizontalAlignment.Right, Items = { buttonLayout } }
}
};
}
private void OkClick()
{
string text = _textBox.Text.Trim();
if (string.IsNullOrWhiteSpace(text))
{
MessageBox.Show(this, "The project name cannot be empty.", "devtime error", MessageBoxButtons.OK, MessageBoxType.Error);
return;
}
if (text.Length > 255 || !IsValidProjectName(text))
{
MessageBox.Show(this, "Invalid project name (must only contain a-z, A-Z, 0-9 and underscores, with the first character not being a digit, and max 255 characters).\nPlease try another name.", "devtime error", MessageBoxButtons.OK, MessageBoxType.Error);
return;
}
List<string> contexts = DB.ListTables();
if (contexts.Contains(text))
{
MessageBox.Show(this, $"A project named '{text}' already exists.\nPlease try another name.", "devtime error", MessageBoxButtons.OK, MessageBoxType.Error);
return;
}
ContextName = text;
Close(true);
}
private bool IsValidProjectName(string str)
{
if (string.IsNullOrEmpty(str)) return false;
for (int i = 0; i < str.Length; ++i)
{
char c = str[i];
if (i == 0 && char.IsDigit(c)) return false;
if (!char.IsLetterOrDigit(c) && c != '_') return false;
}
return true;
}
}
}