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 |
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using Ayende.NHibernateQueryAnalyzer.Core.Model;
using Ayende.NHibernateQueryAnalyzer.UserInterface.Controls;
using Ayende.NHibernateQueryAnalyzer.UserInterface.Interfaces;
using Ayende.NHibernateQueryAnalyzer.UserInterface.Presenters;
using log4net;
using WeifenLuo.WinFormsUI;
namespace Ayende.NHibernateQueryAnalyzer.UserInterface
{
public partial class MainForm : Form, IMainView
{
public const string DefaultNamePrefix = "New Project #";
private readonly ILog logger = LogManager.GetLogger(typeof(MainForm));
private readonly IMainPresenter presenter;
private Wait wait;
public IProjectView CurrentProjectView { get; set; }
public MainForm(IProjectsRepository repository)
: this()
{
presenter = new MainPresenter(this, repository);
}
private MainForm()
{
InitializeComponent();
ux_DockingZone.DocumentStyle = DocumentStyles.DockingWindow;
}
public Project CurrentProject
{
get { return presenter.CurrentProject; }
set
{
presenter.CurrentProject = value;
UpdateProjectName();
}
}
public void UpdateProjectName()
{
Text = presenter.CurrentProject != null
? presenter.CurrentProject.Name + " - NHibernate Query Analyzer"
: "NHibernate Query Analyzer";
}
#region Event Handlers
private void ux_MenuFileExit_Click(object sender, EventArgs e)
{
Close();
}
private void ux_MenuFileNewProject_Click(object sender, EventArgs e)
{
presenter.DisplayNewProject();
}
private void ux_MenuFileOpenProject_Click(object sender, EventArgs e)
{
presenter.OpenProject();
}
private void ux_MenuFileSave_Click(object sender, EventArgs e)
{
try
{
presenter.SaveDocument();
}
catch (Exception ex)
{
MessageBox.Show("Can't save document because: " + ex.Message, "Document was not saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ux_MenuQuery_Popup(object sender, EventArgs e)
{
QueryForm qf = ux_DockingZone.ActiveDocument as QueryForm;
bool queryWindowIsActive = (qf != null) && (qf.CanExecuteQuery);
bool projectIsBuilt = ProjectIsBuilt();
ux_MenuQueryExecute.Enabled = queryWindowIsActive;
ux_MenuQueryOpen.Enabled = projectIsBuilt;
}
private bool ProjectIsBuilt()
{
foreach (DockContent content in ux_DockingZone.Documents)
{
if (content is IProjectView)
{
return ((IProjectView)content).ProjectPresenter.Project.IsProjectBuilt;
}
}
return false;
}
public IMainPresenter MainPresenter
{
get { return presenter; }
}
public Query SelectProjectQuery(Project prj)
{
using (OpenQueryForm oqf = new OpenQueryForm(prj))
{
if (oqf.ShowDialog(this) == DialogResult.OK) { return oqf.SelectedQuery; }
return null;
}
}
private void ux_MenuQueryExecute_Click(object sender, EventArgs e)
{
try
{
presenter.ExecuteActiveQuery();
}
catch (Exception ex)
{
MessageBox.Show("Can't execute the currect query because: " + ex.Message, "Query error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ux_MenuFileNewQuery_Click(object sender, EventArgs e)
{
presenter.AddNewQuery();
}
private void ux_MenuQueryOpen_Click(object sender, EventArgs e)
{
presenter.OpenQuery();
}
private void ux_MenuWindowNext_Click(object sender, EventArgs e)
{
if (ux_DockingZone.Documents.Length <= 0) return;
int index = Array.IndexOf(ux_DockingZone.Documents, ux_DockingZone.ActiveDocument);
if (index + 1 < ux_DockingZone.Documents.Length)
{ ux_DockingZone.Documents[index + 1].DockHandler.Activate(); }
else
{ ux_DockingZone.Documents[0].DockHandler.Activate(); }
}
private void ux_MenuWindowPrevious_Click(object sender, EventArgs e)
{
if (ux_DockingZone.Documents.Length <= 0) return;
int index = Array.IndexOf(ux_DockingZone.Documents, ux_DockingZone.ActiveDocument);
if (index - 1 >= 0)
{ ux_DockingZone.Documents[index - 1].DockHandler.Activate(); }
else
{ ux_DockingZone.Documents[ux_DockingZone.Documents.Length - 1].DockHandler.Activate(); }
}
private void ux_MenuFileSaveAs_Click(object sender, EventArgs e)
{
try
{
presenter.SaveDocumentAs();
}
catch (Exception ex)
{
MessageBox.Show("Can't save document because: " + ex.Message, "Document was not saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ux_MenuFileNew_Popup(object sender, EventArgs e)
{
ux_MenuFileNewQuery.Enabled = (CurrentProject != null) && CurrentProject.IsProjectBuilt;
}
private void ux_MenuWindowClose_Click(object sender, EventArgs e)
{
presenter.CloseCurrentDocument();
}
private void ux_MenuFile_Popup(object sender, EventArgs e)
{
ux_MenuFileSaveAs.Enabled = ux_DockingZone.ActiveDocument != null;
ux_MenuFileSave.Enabled = ux_DockingZone.ActiveDocument != null;
}
private void MainForm_Closing(object sender, CancelEventArgs e)
{
e.Cancel = !presenter.CloseProject();
}
private void ux_MenuFileNewMapping_Click(object sender, EventArgs e)
{
presenter.CreateNewHbmDocument();
}
private void ux_MenuFileNewConfiguration_Click(object sender, EventArgs e)
{
presenter.CreateNewCfgDocument();
}
private void ux_MenuFileOpenMapping_Click(object sender, EventArgs e)
{
try
{
if (ux_OpenMappingDialog.ShowDialog(this) != DialogResult.Cancel)
{
presenter.OpenHbmDocument(ux_OpenMappingDialog.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Can't open document because: " + ex.Message, "Problem openning document", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ux_MenuFileOpenConfiguration_Click(object sender, EventArgs e)
{
try
{
if (ux_OpenConfigDialog.ShowDialog(this) != DialogResult.Cancel)
{
presenter.OpenCfgDocument(ux_OpenConfigDialog.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Can't open document because: " + ex.Message, "Problem openning document", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ux_MenuHelpOnline_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo("http://www.assembla.com/wiki/show/NHibernateQueryAnalyzer");
Process.Start(psi);
}
private void ux_MenuHelpReportBug_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo("mailto:NHibernateQueryAnalyzer@gmail.com");
Process.Start(psi);
}
private void ux_MenuHelpAbout_Click(object sender, EventArgs e)
{
using (About about = new About())
{ about.ShowDialog(this); }
}
#endregion
#region IMainView Implementation
public event EventHandler TitleChanged;
public void Display(IView view)
{
NQADocument d = view as NQADocument;
if (d != null)
{ d.Show(ux_DockingZone); }
}
public SaveFileDialog SaveMappingDialog
{
get { return ux_SaveMappingDialog; }
}
public SaveFileDialog SaveConfigDialog
{
get { return ux_SaveConfigDialog; }
}
public IView ActiveDocument
{
get { return ux_DockingZone.ActiveDocument as IView; }
}
public IEnumerable Documents
{
get { return ux_DockingZone.Documents; }
}
public IView[] ShowUnsavedDialog(IView[] unsavedView)
{
using (UnsavedFilesForm uff = new UnsavedFilesForm())
{
uff.Views = unsavedView;
if (uff.ShowDialog(this) == DialogResult.Cancel)
{
return null;
}
return uff.SelectedView;
}
}
/// <summary>
/// Starts the wait message by the UI. The view needs to check every <c>checkInterval</c>
/// that the work was completed (using <c>HasFinishedWork()</c> method).
/// The work should finish in shouldWaitFor, but there is no gurantee about it.
/// <c>EndWait</c> is called to end the wait.
/// </summary>
/// <param name="waitMessage">The Wait message.</param>
/// <param name="checkInterval">Check interval.</param>
/// <param name="shouldWaitFor">Should wait for.</param>
public void StartWait(string waitMessage, int checkInterval, int shouldWaitFor)
{
wait = new Wait(waitMessage, checkInterval, shouldWaitFor);
wait.ShowDialog(this);
}
public void EndWait(string endMessage)
{
wait.Close();
wait.Dispose();
wait = null;
ux_StatusBarMessages.Text = endMessage;
}
public void AddException(Exception ex)
{
ShowError(ex.ToString());
}
public void ShowError(string error)
{
MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public void ExecuteInUIThread(Delegate d, params object[] parameters)
{
if (logger.IsDebugEnabled)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Invoke Required: {0}, Executing method: {1} on {2}: ", InvokeRequired, d.Method.Name, d.Method.DeclaringType.FullName);
foreach (object parameter in parameters)
{
sb.Append(parameter != null ? parameter.ToString() : "null");
sb.Append(", ");
}
logger.Debug(sb.ToString());
}
if (InvokeRequired)
{
Invoke(d, parameters);
}
else
{
d.DynamicInvoke(parameters);
}
}
public bool AskYesNo(string question, string title)
{
return MessageBox.Show(this, question, title, MessageBoxButtons.YesNo) == DialogResult.Yes;
}
public string Ask(string question, string answer)
{
using (Input input = new Input())
{
return input.Ask(question, answer, this);
}
}
public bool HasChanges
{
get { return true; }
set { }
}
public bool Save()
{
return false;
}
public string Title
{
get { return Text; }
set
{
if (string.IsNullOrEmpty(value))
{ Text = "NHibernate Query Analyzer"; }
else
{ Text = value + " - NHibernate Query Analyzer"; }
if (TitleChanged != null) TitleChanged(this, EventArgs.Empty);
}
}
public void Close(bool askToSave)
{
Close();
}
public bool SaveAs()
{
return true;
}
public Project SelectExistingProject()
{
using (OpenProjectForm opf = new OpenProjectForm(presenter))
{
if (opf.ShowDialog(this) == DialogResult.OK)
{
return opf.SelectedProject;
}
return null;
}
}
#endregion
}
} |