-
Notifications
You must be signed in to change notification settings - Fork 137
/
CrudTab.cs
169 lines (146 loc) · 5.14 KB
/
CrudTab.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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SolarWinds.InformationService.Contract2;
using SwqlStudio.Metadata;
namespace SwqlStudio
{
public partial class CrudTab : UserControl, IConnectionTab
{
private const int ControlsMargin = 3;
private readonly CrudOperation _operation;
private readonly Dictionary<Property, TextBox> _textboxes = new Dictionary<Property, TextBox>();
private TextBox _uri;
private Entity _entity;
public CrudTab(CrudOperation operation)
{
_operation = operation;
InitializeComponent();
}
public Entity Entity
{
get => _entity;
set
{
_entity = value;
CreateSubComponents();
}
}
/// <inheritdoc />
public IApplicationService ApplicationService { get; set; }
/// <inheritdoc />
public ConnectionInfo ConnectionInfo { get; set; }
public event EventHandler CloseItself;
private void CreateSubComponents()
{
_textboxes.Clear();
container.Controls.Clear();
container.SuspendLayout();
try
{
var y = 0;
bool createProperties = true;
switch (_operation)
{
case CrudOperation.Create:
break;
case CrudOperation.Delete:
createProperties = false;
goto case CrudOperation.Update;
case CrudOperation.Update:
_uri = CreateLabelAndTextbox("SwisUri", ref y);
y += ControlsMargin * 3;
break;
default:
throw new ArgumentOutOfRangeException();
}
// for delete, uri is enough
if (createProperties)
{
foreach (var property in _entity.Properties)
{
var textBox = CreateLabelAndTextbox(property.Name, ref y);
_textboxes[property] = textBox;
}
}
var commit = new Button();
container.Controls.Add(commit);
commit.Text = _operation.ToString();
commit.Top = y;
commit.AutoSize = true;
commit.Click += OnCommit;
}
finally
{
container.ResumeLayout();
}
}
private void OnCommit(object sender, EventArgs e)
{
var propertyBag = CreatePropertyBag();
try
{
var message = "";
switch (_operation)
{
case CrudOperation.Create:
var rv = ConnectionInfo.Proxy.Create(_entity.FullName, propertyBag);
message = $"Created: {rv}";
break;
case CrudOperation.Update:
ConnectionInfo.Proxy.Update(_uri.Text, propertyBag);
message = "OK";
break;
case CrudOperation.Delete:
ConnectionInfo.Proxy.Delete(_uri.Text);
message = "OK";
break;
default:
throw new ArgumentOutOfRangeException();
}
MessageBox.Show(message, "SwqlStudio", MessageBoxButtons.OK, MessageBoxIcon.Information);
OnCloseItself();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to {_operation}: {ex.Message}",
"SwqlStudio",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private PropertyBag CreatePropertyBag()
{
var rv = new PropertyBag();
foreach (var property in _textboxes)
{
if (!string.IsNullOrEmpty(property.Value.Text))
{
rv[property.Key.Name] = property.Value.Text;
}
}
return rv;
}
private TextBox CreateLabelAndTextbox(string name, ref int y)
{
var label = new Label();
container.Controls.Add(label);
label.Text = name;
label.AutoSize = true;
label.Top = y;
y += label.Height + ControlsMargin;
var textBox = new TextBox();
container.Controls.Add(textBox);
textBox.Top = y;
textBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
textBox.Width = container.Width - ControlsMargin * 2;
textBox.Left = ControlsMargin;
y += textBox.Height + ControlsMargin;
return textBox;
}
protected virtual void OnCloseItself()
{
CloseItself?.Invoke(this, EventArgs.Empty);
}
}
}