-
Notifications
You must be signed in to change notification settings - Fork 137
/
MainForm.cs
479 lines (398 loc) · 17.3 KB
/
MainForm.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Windows.Forms;
using SolarWinds.InformationService.Contract2;
using SolarWinds.InformationService.InformationServiceClient;
using SwqlStudio.Properties;
using SwqlStudio.Subscriptions;
using SwqlStudio.Utils;
namespace SwqlStudio
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = false)]
internal partial class MainForm : Form, IApplicationService
{
private static readonly SolarWinds.Logging.Log log = new SolarWinds.Logging.Log();
private ServerList serverList;
private readonly BindingList<ConnectionInfo> connectionsDataSource = new BindingList<ConnectionInfo>();
private ConnectionsManager connectionsManager;
public PropertyBag QueryParameters
{
get { return this.filesDock.QueryParameters; }
set { this.filesDock.QueryParameters = value; }
}
public SubscriptionManager SubscriptionManager { get; } = new SubscriptionManager();
public ConnectionInfo SelectedConnection
{
get { return this.connectionsCombobox.SelectedItem as ConnectionInfo; }
set { this.connectionsCombobox.SelectedItem = value; }
}
public MainForm()
{
InitializeComponent();
InitializeDockPanel();
SetEntityGroupingMode((EntityGroupingMode)Enum.Parse(typeof(EntityGroupingMode), Settings.Default.EntityGroupingMode));
startTimer.Enabled = true;
SubscriptionManager = new SubscriptionManager();
AssignConnectionsDataSource();
}
private void InitializeDockPanel()
{
this.filesDock.SetObjectExplorerImageList(this.ObjectExplorerImageList);
this.serverList = new ServerList();
this.serverList.ConnectionAdded += ServerListOnConnectionAdded;
this.serverList.ConnectionRemoved += ServerListOnConnectionRemoved;
this.connectionsManager = new ConnectionsManager(this, this.serverList);
var tabsFactory = new TabsFactory(this.filesDock, this, this.serverList, this.connectionsManager);
this.filesDock.SetAplicationService(tabsFactory);
this.filesDock.ActiveContentChanged += FilesDock_ActiveContentChanged;
}
private void AssignConnectionsDataSource()
{
var connectionsDropDown = this.connectionsCombobox.ComboBox;
connectionsDropDown.DisplayMember = "Title";
connectionsDropDown.DataSource = this.connectionsDataSource;
}
private void FilesDock_ActiveContentChanged(object sender, EventArgs e)
{
this.RefreshSelectedConnections();
IConnectionTab activeConnectionTab = this.filesDock.ActiveConnectionTab;
if (activeConnectionTab != null)
{
this.SelectedConnection = activeConnectionTab.ConnectionInfo;
}
}
public void RefreshSelectedConnections()
{
IConnectionTab activeConnectionTab = this.filesDock.ActiveConnectionTab;
this.connectionsCombobox.Enabled = activeConnectionTab == null || activeConnectionTab.AllowsChangeConnection;
}
private void ServerListOnConnectionAdded(object sender, ConnectionsEventArgs e)
{
ConnectionInfo addedConnection = e.Connection;
this.connectionsDataSource.Add(addedConnection);
this.serverList.TryGetProvider(addedConnection, out IMetadataProvider provider);
this.filesDock.AddServer(provider, addedConnection);
this.SelectedConnection = addedConnection;
if (this.connectionsDataSource.Count == 1)
this.filesDock.ReplaceConnection(null, addedConnection);
}
private void ServerListOnConnectionRemoved(object sender, ConnectionsEventArgs e)
{
this.connectionsDataSource.Remove(e.Connection);
if(connectionsDataSource.Any())
this.SelectedConnection = connectionsDataSource.First();
this.filesDock.CloseServer(e.Connection, this.SelectedConnection);
}
private void startTimer_Tick(object sender, EventArgs e)
{
startTimer.Enabled = false;
this.filesDock.AddNewQueryTab();
}
#region Code related to File menu
private void menuFileNew_Click(object sender, EventArgs e)
{
try
{
ConnectionInfo.DoWithExceptionTranslation(() => connectionsManager.CreateConnection());
}
catch (Exception ex)
{
log.Error("Failed to connect", ex);
var msg = $"Unable to connect to Information Service.\n{ex.Message}";
MessageBox.Show(msg, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void menuFileOpen_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
this.filesDock.OpenFiles(openFileDialog.FileNames);
}
private void menuFileClose_Click(object sender, EventArgs e)
{
this.filesDock.CloseActiveContent();
}
private void menuFileSave_Click(object sender, EventArgs e)
{
var editor = this.filesDock.ActiveQueryTab;
if (editor != null)
DoSave(editor);
}
private bool DoSave(QueryTab editor)
{
if (string.IsNullOrEmpty(editor.FileName))
return DoSaveAs(editor);
return SaveEditor(editor, editor.FileName);
}
private void menuFileSaveAs_Click(object sender, EventArgs e)
{
var editor = this.filesDock.ActiveQueryTab;
if (editor != null)
DoSaveAs(editor);
}
private bool DoSaveAs(QueryTab editor)
{
saveFileDialog.FileName = editor.FileName;
if (saveFileDialog.ShowDialog() != DialogResult.OK)
return false;
return SaveEditor(editor, saveFileDialog.FileName);
}
private bool SaveEditor(QueryTab editor, string fileName)
{
try
{
File.WriteAllText(fileName, editor.QueryText);
editor.FileName = fileName;
editor.MarkSaved();
// The syntax highlighting strategy doesn't change
// automatically, so do it manually.
//editor.Document.HighlightingStrategy =
// HighlightingStrategyFactory.CreateHighlightingStrategyForFile(editor.FileName);
return true;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, ex.GetType().Name);
return false;
}
}
private void menuNotificationListenerActive_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
if (SubscriptionManager.IsListening())
{
SubscriptionManager.CloseListeningService();
menuNotificationListenerActive.Checked = false;
}
else
{
Action x = () => menuNotificationListenerActive.Checked = true;
SubscriptionManager.StartListening(() => this.BeginInvoke(x));
}
}
finally
{
Cursor.Current = Cursors.Default;
}
}
private void menuFileExit_Click(object sender, EventArgs e)
{
Close();
}
#endregion
#region Code related to Edit menu
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
this.filesDock.ActiveQueryTab?.Undo();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
this.filesDock.ActiveQueryTab?.Redo();
}
private void menuEditCut_Click(object sender, EventArgs e)
{
this.filesDock.ActiveQueryTab?.Cut();
}
private void menuEditCopy_Click(object sender, EventArgs e)
{
this.filesDock.ActiveQueryTab?.CopySelectionToClipboard();
}
private void menuEditPaste_Click(object sender, EventArgs e)
{
this.filesDock.ActiveQueryTab?.Paste();
}
#endregion
#region Other stuff
private void TextEditor_FormClosing(object sender, FormClosingEventArgs e)
{
// Ask user to save changes
foreach (var editor in this.filesDock.QueryTabs)
{
if (editor.Modified && Settings.Default.PromptToSaveOnClose)
{
var r = MessageBox.Show(this, $"Save changes to {editor.FileName ?? "new file"}?",
"Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (r == DialogResult.Cancel)
e.Cancel = true;
else if (r == DialogResult.Yes)
if (!DoSave(editor))
e.Cancel = true;
}
if (editor.Tag is ConnectionInfo info)
{
info.Dispose();
}
}
}
/// <summary>We handle DragEnter and DragDrop so users can drop files on the editor.</summary>
private void TextEditorForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void TextEditorForm_DragDrop(object sender, DragEventArgs e)
{
string[] list = e.Data.GetData(DataFormats.FileDrop) as string[];
if (list != null)
this.filesDock.OpenFiles(list);
}
#endregion
private void menuQueryExecute_Click(object sender, EventArgs e)
{
if (this.filesDock.ActiveQueryTab != null)
this.filesDock.ActiveQueryTab.RunQuery();
}
private void parametersToolStripMenuItem_Click(object sender, EventArgs e)
{
this.filesDock.ShowParametersToolbox();
}
private void enumEntitiesToolStripMenuItem_Click(object sender, EventArgs e)
{
ConnectionInfo connection = this.SelectedConnection;
if (connection == null)
return; // should we try to connect?
string errorMsg;
try
{
EntityClassGraph entityClassGraph = new EntityClassGraph(connection.Connection);
EntityClassGraphForm form = new EntityClassGraphForm(entityClassGraph);
form.Show(this);
return;
}
catch (FaultException<InfoServiceFaultContract> ex)
{
log.Error("Failed to connect", ex);
errorMsg = ex.Detail.Message;
}
catch (Exception ex)
{
errorMsg = ex.Message;
}
if (errorMsg != null)
{
errorMsg = string.Format("Unable to connect to generate entity graph. {0}", errorMsg);
MessageBox.Show(this, errorMsg, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void playbackToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (this.filesDock.ActiveQueryTab != null)
this.filesDock.ActiveQueryTab.RunPlayback();
}
private void aboutSWQLStudioToolStripMenuItem_Click(object sender, EventArgs e)
{
new About().ShowDialog(this);
}
private void byNamespaceToolStripMenuItem_Click(object sender, EventArgs e)
{
SetEntityGroupingMode(EntityGroupingMode.ByNamespace);
}
private void noGroupingToolStripMenuItem_Click(object sender, EventArgs e)
{
SetEntityGroupingMode(EntityGroupingMode.Flat);
}
private void SetEntityGroupingMode(EntityGroupingMode mode)
{
byNamespaceToolStripMenuItem.Checked = mode == EntityGroupingMode.ByNamespace;
noGroupingToolStripMenuItem.Checked = mode == EntityGroupingMode.Flat;
byBaseTypeToolStripMenuItem.Checked = mode == EntityGroupingMode.ByBaseType;
byHierarchyToolStripMenuItem.Checked = mode == EntityGroupingMode.ByHierarchy;
Settings.Default.EntityGroupingMode = mode.ToString();
Settings.Default.Save();
this.filesDock.SetEntityGroupingMode(mode);
}
private void byBaseTypeToolStripMenuItem_Click(object sender, EventArgs e)
{
SetEntityGroupingMode(EntityGroupingMode.ByBaseType);
}
private void byHierarchyToolStripMenuItem_Click(object sender, EventArgs e)
{
SetEntityGroupingMode(EntityGroupingMode.ByHierarchy);
}
private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
menuNotificationListenerActive.Visible =
separatorAboveNotificationListenerActive.Visible =
!Settings.Default.UseActiveSubscriber;
menuNotificationListenerActive.CheckState = SubscriptionManager.IsListening()
? CheckState.Checked
: CheckState.Unchecked;
}
private void menuFileTabPage_Click(object sender, EventArgs e)
{
this.filesDock.CreateTabFromPrevious();
}
private void enableAutocompleteToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.AutocompleteEnabled = enableAutocompleteToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void preferencesToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
enableAutocompleteToolStripMenuItem.Checked = Settings.Default.AutocompleteEnabled;
promptToSaveOnCloseToolStripMenuItem.Checked = Settings.Default.PromptToSaveOnClose;
}
private void searchInTreeHotKeyToolStripMenuItem_Click(object sender, EventArgs e)
{
this.filesDock.FocusSearch();
}
private void discoverQueryParametersToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
this.filesDock.AllowSetParameters(this.discoverQueryParametersMenuItem.Checked);
}
private void connectionsCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
IConnectionTab activeConnectionTab = this.filesDock.ActiveConnectionTab;
if (activeConnectionTab != null && this.SelectedConnection != null)
{
activeConnectionTab.ConnectionInfo = this.SelectedConnection;
}
}
private void disconnectToolButton_Click(object sender, EventArgs e)
{
this.SelectedConnection?.Close();
}
private void refreshToolButton_Click(object sender, EventArgs e)
{
var connection = this.SelectedConnection;
if (connection != null)
this.filesDock.RefreshServer(connection);
}
private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
copyQueryAsToolStripMenuItem.Enabled = filesDock.ActiveQueryTab != null;
}
private void curlCmdToolStripMenuItem_Click(object sender, EventArgs e)
{
CopyQueryAs(CommandLineGenerator.GetQueryForCurlCmd);
}
private void curlBashToolStripMenuItem_Click(object sender, EventArgs e)
{
CopyQueryAs(CommandLineGenerator.GetQueryForCurlBash);
}
private void getSwisDataPowerShellToolStripMenuItem_Click(object sender, EventArgs e)
{
CopyQueryAs(CommandLineGenerator.GetQueryForPowerShellGetSwisData);
}
private void CopyQueryAs(Func<string, ConnectionInfo, string> formatter)
{
var connection = filesDock.ActiveConnectionTab?.ConnectionInfo;
if (connection == null)
return;
var query = filesDock.ActiveQueryTab.QueryText;
string command = formatter(query, connection);
Clipboard.SetText(command);
}
private void promptToSaveOnCloseToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings.Default.PromptToSaveOnClose = !Settings.Default.PromptToSaveOnClose;
Settings.Default.Save();
}
}
}