-
Notifications
You must be signed in to change notification settings - Fork 53
/
MainForm.cs
1814 lines (1612 loc) · 72.7 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// XSDDiagram - A XML Schema Definition file viewer
// Copyright (C) 2006-2016 Regis COSNIER
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Security.Principal;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
// To generate the XMLSchema.cs file:
// > xsd.exe XMLSchema.xsd /classes /l:cs /n:XMLSchema /order
using XSDDiagram.Rendering;
namespace XSDDiagram
{
public partial class MainForm : Form
{
private DiagramPrinter _diagramPrinter;
private DiagramGdiRenderer _diagramGdiRenderer;
private Rectangle _renderingClipRectangle = new Rectangle();
private Diagram diagram = new Diagram();
private Schema schema = new Schema();
private Dictionary<string, TabPage> hashtableTabPageByFilename = new Dictionary<string, TabPage>();
private string originalTitle = "";
private DiagramItem contextualMenuPointedElement = null;
//private string currentLoadedSchemaFilename = "";
private TextBox textBoxAnnotation;
private WebBrowser webBrowserDocumentation;
private bool webBrowserSupported = true;
private string backupUsername = "", backupPassword = "";
private MRUManager mruManager;
private static MainForm mainForm = null;
public static MainForm Form { get { return mainForm; } }
public MainForm()
{
mainForm = this;
InitializeComponent();
bool isElevated = false;
WindowsIdentity identity = null;
try
{
identity = WindowsIdentity.GetCurrent();
if (identity != null)
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (principal != null)
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
catch (UnauthorizedAccessException)
{
}
catch (Exception)
{
}
finally
{
if (identity != null)
identity.Dispose();
}
this.toolsToolStripMenuItem.Visible = isElevated && !Options.IsRunningOnMono;
this.diagram.ShowDocumentation = this.toolStripButtonShowDocumentation.Checked = Options.ShowDocumentation | Settings.Default.ShowDocumentation;
this.diagram.AlwaysShowOccurence = Settings.Default.AlwaysShowOccurence;
this.diagram.ShowType = Settings.Default.ShowType;
this.diagram.CompactLayoutDensity = Settings.Default.CompactLayoutDensity;
this.originalTitle = Text;
this.toolStripComboBoxSchemaElement.Sorted = true;
this.toolStripComboBoxSchemaElement.Items.Add("");
this.diagram.RequestAnyElement += new Diagram.RequestAnyElementEventHandler(diagram_RequestAnyElement);
this.panelDiagram.VirtualSize = new Size(0, 0);
this.panelDiagram.DiagramControl.ContextMenuStrip = this.contextMenuStripDiagram;
this.panelDiagram.DiagramControl.MouseWheel += new MouseEventHandler(DiagramControl_MouseWheel);
this.panelDiagram.DiagramControl.MouseClick += new MouseEventHandler(DiagramControl_MouseClick);
this.panelDiagram.DiagramControl.MouseHover += new EventHandler(DiagramControl_MouseHover);
this.panelDiagram.DiagramControl.MouseMove += new MouseEventHandler(DiagramControl_MouseMove);
//this.panelDiagram.DiagramControl.KeyDown += DiagramControl_KeyDown;
this.panelDiagram.DiagramControl.KeyDown += new KeyEventHandler(DiagramControl_KeyDown);
this.panelDiagram.DiagramControl.Paint += new PaintEventHandler(DiagramControl_Paint);
this.schema.RequestCredential += schema_RequestCredential;
this.backupUsername = Options.Username;
this.backupPassword = Options.Password;
if (Options.IsRunningOnMono)
{
try
{
new WebBrowser().Navigate("about:blank");
}
catch
{
webBrowserSupported = false;
}
}
UpdateActionsState();
}
bool schema_RequestCredential(string url, string realm, int attemptCount, out string username, out string password)
{
string label = "The file '" + url + "' requires a username and password.";
LoginPromptForm dlg = new LoginPromptForm(label);
dlg.Username = backupUsername;
dlg.Password = backupPassword;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
backupUsername = username = dlg.Username;
backupPassword = password = dlg.Password;
return true;
}
username = password = "";
return false;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (Options.IsRunningOnMono)
{
// Prevent exception with Linux on Mono
object[] toolStripMenuItems = new object[] { this.fileToolStripMenuItem, this.fileToolStripMenuItem, this.openToolStripMenuItem, this.openToolStripMenuItem, this.openURLToolStripMenuItem, this.openURLToolStripMenuItem, this.saveDiagramToolStripMenuItem, this.saveDiagramToolStripMenuItem, this.validateXMLFileToolStripMenuItem, this.validateXMLFileToolStripMenuItem, this.recentFilesToolStripMenuItem, this.recentFilesToolStripMenuItem, this.closeToolStripMenuItem, this.closeToolStripMenuItem, this.toolStripMenuItem2, this.pageToolStripMenuItem, this.pageToolStripMenuItem, this.printPreviewToolStripMenuItem, this.printPreviewToolStripMenuItem, this.printToolStripMenuItem, this.printToolStripMenuItem, this.toolStripMenuItem1, this.exitToolStripMenuItem, this.exitToolStripMenuItem, this.toolsToolStripMenuItem, this.toolsToolStripMenuItem, this.windowsExplorerRegistrationToolStripMenuItem, this.windowsExplorerRegistrationToolStripMenuItem, this.registerToolStripMenuItem, this.registerToolStripMenuItem, this.unregisterToolStripMenuItem, this.unregisterToolStripMenuItem, this.windowToolStripMenuItem, this.windowToolStripMenuItem, this.nextTabToolStripMenuItem, this.nextTabToolStripMenuItem, this.previousTabToolStripMenuItem, this.previousTabToolStripMenuItem, this.helpToolStripMenuItem, this.helpToolStripMenuItem, this.aboutToolStripMenuItem, this.aboutToolStripMenuItem, this.toolStripMenuItemAttributesCopyLine, this.toolStripMenuItemAttributesCopyLine, this.toolStripMenuItemAttributesCopyList, this.toolStripMenuItemAttributesCopyList, this.toolStripMenuItemEnumerateCopyLine, this.toolStripMenuItemEnumerateCopyLine, this.toolStripMenuItemEnumerateCopyList, this.toolStripMenuItemEnumerateCopyList, this.addToDiagrammToolStripMenuItem, this.addToDiagrammToolStripMenuItem, this.toolStripMenuItem4, this.toolStripMenuItemElementsCopyLine, this.toolStripMenuItemElementsCopyLine, this.toolStripMenuItemElementsCopyList, this.toolStripMenuItemElementsCopyList, this.gotoXSDFileToolStripMenuItem, this.gotoXSDFileToolStripMenuItem, this.expandToolStripMenuItem, this.expandToolStripMenuItem, this.removeFromDiagramToolStripMenuItem, this.removeFromDiagramToolStripMenuItem, this.toolStripMenuItem3, this.addAllToolStripMenuItem, this.addAllToolStripMenuItem, this.removeAllToolStripMenuItem, this.removeAllToolStripMenuItem, this.expandOneLevelToolStripMenuItem, this.expandOneLevelToolStripMenuItem };
foreach (var toolStripMenuItem in toolStripMenuItems)
GC.SuppressFinalize(toolStripMenuItem);
}
if (disposing)
{
if (components != null)
{
components.Dispose();
components = null;
}
if (_diagramPrinter != null)
{
_diagramPrinter.Dispose();
_diagramPrinter = null;
}
}
base.Dispose(disposing);
}
private void MainForm_Load(object sender, EventArgs e)
{
this.mruManager = new MRUManager(this.recentFilesToolStripMenuItem, "xsddiagram", this.recentFilesToolStripMenuSubItemFile_Click, this.recentFilesToolStripMenuSubItemClearAll_Click);
this.toolStripComboBoxZoom.SelectedIndex = Settings.Default.Zoom; // 8;
this.toolStripComboBoxAlignement.SelectedIndex = Settings.Default.Alignement; // 1;
this.toolStripButtonTogglePanel.Checked = Settings.Default.DisplayPanel;
this.splitContainerMain.Panel2Collapsed = !this.toolStripButtonTogglePanel.Checked;
if (!string.IsNullOrEmpty(Options.InputFile))
{
LoadSchema(Options.InputFile);
foreach (var rootElement in Options.RootElements)
{
string elementName = rootElement;
string elementNamespace = null;
if (!string.IsNullOrEmpty(elementName))
{
var pos = rootElement.IndexOf("@");
if (pos != -1)
{
elementName = rootElement.Substring(0, pos);
elementNamespace = rootElement.Substring(pos + 1);
}
}
foreach (var element in schema.Elements)
{
if ((elementNamespace != null && elementNamespace == element.NameSpace && element.Name == elementName) ||
(elementNamespace == null && element.Name == elementName))
{
diagram.Add(element.Tag, element.NameSpace);
}
}
}
for (int i = 0; i < Options.ExpandLevel; i++)
{
diagram.ExpandOneLevel();
}
UpdateDiagram();
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "xsd files (*.xsd)|*.xsd|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
LoadSchema(openFileDialog.FileName);
}
private void openURLToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenURLForm openURLForm = new OpenURLForm("");
if (openURLForm.ShowDialog() == DialogResult.OK)
LoadSchema(openURLForm.URL);
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
CleanupUserInterface(true);
}
private void recentFilesToolStripMenuSubItemFile_Click(object sender, EventArgs evt)
{
string filenameOrURL = (sender as ToolStripItem).Text;
LoadSchema(filenameOrURL);
//this.mruManager.RemoveRecentFile(filenameOrURL);
}
private void recentFilesToolStripMenuSubItemClearAll_Click(object sender, EventArgs evt)
{
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("UniformResourceLocator"))
{
string url = e.Data.GetData(DataFormats.Text, true) as string;
if (!string.IsNullOrEmpty(url))
LoadSchema(url.Trim());
}
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if(files != null && files.Length > 0)
LoadSchema(files[0]);
}
}
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)
|| e.Data.GetDataPresent("UniformResourceLocator")
)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void saveDiagramToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "SVG files (*.svg)|*.svg" + (Options.IsRunningOnMono ? "" : "|EMF files (*.emf)|*.emf") + "|PNG files (*.png)|*.png|JPG files (*.jpg)|*.jpg|TXT files (*.txt)|*.txt|CSV files (*.csv)|*.csv|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string outputFilename = saveFileDialog.FileName;
try
{
DiagramExporter exporter = new DiagramExporter(diagram);
Graphics g1 = this.panelDiagram.DiagramControl.CreateGraphics();
exporter.Export(outputFilename, g1, new DiagramAlertHandler(SaveAlert), new Dictionary<string, object>()
{
{ "TextOutputFields", Options.TextOutputFields }
//For future parameters, {}
});
g1.Dispose();
}
catch (System.ArgumentException ex)
{
MessageBox.Show("You have reach the system limit.\r\nPlease remove some element from the diagram to make it smaller.");
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
catch (System.Runtime.InteropServices.ExternalException ex)
{
MessageBox.Show("You have reach the system limit.\r\nPlease remove some element from the diagram to make it smaller.");
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
}
}
bool SaveAlert(string title, string message)
{
return MessageBox.Show(this, message, title, MessageBoxButtons.YesNo) == DialogResult.Yes;
}
SettingsForm settingsForm;
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (settingsForm == null) {
settingsForm = new SettingsForm();
settingsForm.FormClosed += SettingsForm_FormClosed;
settingsForm.Show(this);
}
settingsForm.Focus();
}
private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
{
settingsForm.FormClosed -= SettingsForm_FormClosed;
settingsForm = null;
}
internal void ChangeSetting(string settingName)
{
switch (settingName)
{
case "AlwaysShowOccurence":
this.diagram.AlwaysShowOccurence = Settings.Default.AlwaysShowOccurence;
UpdateDiagram();
break;
case "ShowType":
this.diagram.ShowType = Settings.Default.ShowType;
UpdateDiagram();
break;
case "CompactLayoutDensity":
this.diagram.CompactLayoutDensity = Settings.Default.CompactLayoutDensity;
UpdateDiagram();
break;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Save();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutForm().ShowDialog(this);
}
private void toolStripComboBoxSchemaElement_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.toolStripComboBoxSchemaElement.SelectedItem != null)
{
XSDObject xsdObject = this.toolStripComboBoxSchemaElement.SelectedItem as XSDObject;
if (xsdObject != null)
SelectSchemaElement(xsdObject);
}
}
private void toolStripButtonAddToDiagram_Click(object sender, EventArgs e)
{
if (this.toolStripComboBoxSchemaElement.SelectedItem != null)
{
XSDObject xsdObject = this.toolStripComboBoxSchemaElement.SelectedItem as XSDObject;
if (xsdObject != null)
{
DiagramItem diagramItem = this.diagram.Add(xsdObject.Tag, xsdObject.NameSpace);
if(diagramItem != null)
SelectDiagramElement(diagramItem, true);
else
UpdateDiagram();
}
}
}
private void toolStripButtonAddAllToDiagram_Click(object sender, EventArgs e)
{
DiagramItem firstDiagramItem = null;
foreach (XSDObject xsdObject in this.schema.ElementsByName.Values)
if (xsdObject != null)
{
DiagramItem diagramItem = this.diagram.Add(xsdObject.Tag, xsdObject.NameSpace);
if (firstDiagramItem == null && diagramItem != null)
firstDiagramItem = diagramItem;
}
if(firstDiagramItem != null)
SelectDiagramElement(firstDiagramItem, true);
else
UpdateDiagram();
}
void DiagramControl_Paint(object sender, PaintEventArgs e)
{
if (_diagramGdiRenderer == null)
_diagramGdiRenderer = new DiagramGdiRenderer(e.Graphics);
else if (e.Graphics != _diagramGdiRenderer.Graphics)
_diagramGdiRenderer.Graphics = e.Graphics;
if (_diagramGdiRenderer != null)
{
Point virtualPoint = this.panelDiagram.VirtualPoint;
e.Graphics.TranslateTransform(-(float)virtualPoint.X, -(float)virtualPoint.Y);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
_renderingClipRectangle.Location = virtualPoint;
_renderingClipRectangle.Size = this.panelDiagram.DiagramControl.ClientRectangle.Size;
_diagramGdiRenderer.Render(diagram, _renderingClipRectangle);
}
}
private void UpdateDiagram()
{
if (this.diagram.RootElements.Count != 0)
{
Graphics g = this.panelDiagram.DiagramControl.CreateGraphics();
this.diagram.Layout(g);
g.Dispose();
Size bbSize = this.diagram.BoundingBox.Size + this.diagram.Padding + this.diagram.Padding;
this.panelDiagram.VirtualSize = new Size((int)(bbSize.Width * this.diagram.Scale), (int)(bbSize.Height * this.diagram.Scale));
}
else
this.panelDiagram.VirtualSize = new Size(0, 0);
}
private void UpdateTitle(string filename)
{
if (filename.Length > 0)
Text = this.originalTitle + " - " + filename;
else
Text = this.originalTitle;
}
private void LoadSchema(string schemaFilename)
{
Cursor = Cursors.WaitCursor;
this.mruManager.AddRecentFile(schemaFilename);
CleanupUserInterface(false);
UpdateTitle(schemaFilename);
schema.LoadSchema(schemaFilename);
UpdateActionsState();
foreach (XSDObject xsdObject in schema.Elements)
{
this.listViewElements.Items.Add(new ListViewItem(new string[] { xsdObject.Name, xsdObject.Type, xsdObject.NameSpace })).Tag = xsdObject;
this.toolStripComboBoxSchemaElement.Items.Add(xsdObject);
}
Cursor = Cursors.Default;
if (this.schema.LoadError.Count > 0)
{
ErrorReportForm errorReportForm = new ErrorReportForm();
errorReportForm.Errors = this.schema.LoadError;
errorReportForm.ShowDialog(this);
}
this.diagram.ElementsByName = this.schema.ElementsByName;
if (this.schema.FirstElement != null)
this.toolStripComboBoxSchemaElement.SelectedItem = this.schema.FirstElement;
else
this.toolStripComboBoxSchemaElement.SelectedIndex = 0;
tabControlView_Selected(null, null);
this.tabControlView.SuspendLayout();
foreach (string filename in this.schema.XsdFilenames)
{
string fullPath = filename;
Control browser = null;
if (webBrowserSupported)
browser = new WebBrowser();
else
browser = new System.Windows.Forms.TextBox() { Multiline = true, ReadOnly = true, ScrollBars = ScrollBars.Both };
browser.Dock = DockStyle.Fill;
browser.TabIndex = 0;
try
{
new Uri(filename);
}
catch
{
fullPath = Path.GetFullPath(filename);
}
TabPage tabPage = new TabPage(Path.GetFileNameWithoutExtension(filename));
tabPage.Tag = fullPath;
tabPage.ToolTipText = fullPath;
tabPage.Controls.Add(browser);
tabPage.UseVisualStyleBackColor = true;
this.tabControlView.TabPages.Add(tabPage);
this.hashtableTabPageByFilename[filename] = tabPage;
}
this.tabControlView.ResumeLayout();
//currentLoadedSchemaFilename = schemaFilename;
}
private void UpdateActionsState()
{
bool isSchemaLoaded = schema.IsLoaded();
toolStripButtonSaveDiagram.Enabled = isSchemaLoaded;
toolStripButtonPrint.Enabled = isSchemaLoaded;
toolStripButtonAddToDiagram.Enabled = isSchemaLoaded;
toolStripButtonAddAllToDiagram.Enabled = isSchemaLoaded;
toolStripButtonRemoveAllFromDiagram.Enabled = isSchemaLoaded;
toolStripButtonExpandOneLevel.Enabled = isSchemaLoaded;
closeToolStripMenuItem.Enabled = isSchemaLoaded;
saveDiagramToolStripMenuItem.Enabled = isSchemaLoaded;
validateXMLFileToolStripMenuItem.Enabled = isSchemaLoaded;
printPreviewToolStripMenuItem.Enabled = isSchemaLoaded;
printToolStripMenuItem.Enabled = isSchemaLoaded;
}
private void CleanupUserInterface(bool fullCleanup)
{
this.diagram.Clear();
this.panelDiagram.VirtualSize = new Size(0, 0);
this.panelDiagram.VirtualPoint = new Point(0, 0);
this.panelDiagram.Clear();
this.hashtableTabPageByFilename.Clear();
this.listViewElements.Items.Clear();
this.listViewAttributes.Items.Clear();
this.toolStripComboBoxSchemaElement.SelectedItem = "";
this.toolStripComboBoxSchemaElement.Items.Clear();
this.toolStripComboBoxSchemaElement.Items.Add("");
this.propertyGridSchemaObject.SelectedObject = null;
this.textBoxElementPath.Text = "";
while (this.tabControlView.TabCount > 1)
this.tabControlView.TabPages.RemoveAt(1);
ShowDocumentation(null);
if (fullCleanup)
{
UpdateTitle("");
schema.Cleanup();
UpdateActionsState();
}
}
void DiagramControl_MouseClick(object sender, MouseEventArgs e)
{
Point location = e.Location;
location.Offset(this.panelDiagram.VirtualPoint);
DiagramItem resultElement;
DiagramHitTestRegion resultRegion;
this.diagram.HitTest(location, out resultElement, out resultRegion);
if (resultRegion != DiagramHitTestRegion.None)
{
if (resultRegion == DiagramHitTestRegion.ChildExpandButton)
{
if (resultElement.HasChildElements)
{
if (resultElement.ChildElements.Count == 0)
{
this.diagram.ExpandChildren(resultElement);
resultElement.ShowChildElements = true;
}
else
{
this.diagram.ClearSearch();
resultElement.ShowChildElements ^= true;
}
//UpdateDiagram();
//this.panelDiagram.ScrollTo(this.diagram.ScalePoint(resultElement.Location), true);
SelectDiagramElement(resultElement, true);
}
}
else if (resultRegion == DiagramHitTestRegion.Element)
{
if ((ModifierKeys & (Keys.Control | Keys.Shift)) == (Keys.Control | Keys.Shift)) // For Debug
{
this.toolStripComboBoxSchemaElement.SelectedItem = "";
this.propertyGridSchemaObject.SelectedObject = resultElement;
}
else
SelectDiagramElement(resultElement);
}
else
SelectDiagramElement(null);
}
}
private void SelectDiagramElement(DiagramItem element)
{
SelectDiagramElement(element, false);
}
private void SelectDiagramElement(DiagramItem element, bool scrollToElement)
{
this.textBoxElementPath.Text = "";
if (element == null)
{
this.toolStripComboBoxSchemaElement.SelectedItem = "";
this.propertyGridSchemaObject.SelectedObject = null;
this.listViewAttributes.Items.Clear();
}
else
{
XSDObject xsdObject;
if (this.schema.ElementsByName.TryGetValue(element.FullName, out xsdObject) && xsdObject != null)
this.toolStripComboBoxSchemaElement.SelectedItem = xsdObject;
else
this.toolStripComboBoxSchemaElement.SelectedItem = null;
SelectSchemaElement(element);
string path = '/' + element.Name;
DiagramItem parentElement = element.Parent;
while (parentElement != null)
{
if (parentElement.ItemType == DiagramItemType.element && !string.IsNullOrEmpty(parentElement.Name))
path = '/' + parentElement.Name + path;
parentElement = parentElement.Parent;
}
this.textBoxElementPath.Text = path;
}
this.diagram.SelectElement(element);
UpdateDiagram();
if (scrollToElement)
this.panelDiagram.ScrollTo(this.diagram.ScalePoint(element.Location), true);
}
private void SelectSchemaElement(XSDObject xsdObject)
{
SelectSchemaElement(xsdObject.Tag, xsdObject.NameSpace);
}
private void SelectSchemaElement(DiagramItem diagramBase)
{
SelectSchemaElement(diagramBase.TabSchema, diagramBase.NameSpace);
}
private void SelectSchemaElement(XMLSchema.openAttrs openAttrs, string nameSpace)
{
this.propertyGridSchemaObject.SelectedObject = openAttrs;
ShowDocumentation(null);
XMLSchema.annotated annotated = openAttrs as XMLSchema.annotated;
if (annotated != null)
{
// Element documentation
if (annotated.annotation != null)
ShowDocumentation(annotated.annotation);
// Show the enumeration
ShowEnumerate(annotated);
// Attributes enumeration
List<XSDAttribute> listAttributes = DiagramHelpers.GetAnnotatedAttributes(this.schema, annotated, nameSpace);
//This part i modify
this.listViewAttributes.Items.Clear();
listAttributes.Reverse();
foreach (XSDAttribute attribute in listAttributes)
{
string s = "";
//dgis fix github issue 2 (attribute.Tag == null ???)
if (attribute.Tag != null && attribute.Tag.simpleType != null && attribute.Tag.simpleType.Item is XMLSchema.restriction)
{
XMLSchema.restriction r = attribute.Tag.simpleType.Item as XMLSchema.restriction;
if (r.Items != null)
{
for (int i = 0; i < r.Items.Length; i++)
{
s += r.ItemsElementName[i].ToString() + "(" + r.Items[i].id + " " + r.Items[i].value + ");";
}
}
}
this.listViewAttributes.Items.Add(new ListViewItem(new string[] { attribute.Name, attribute.Type, attribute.Use, attribute.DefaultValue, s })).Tag = attribute;
}
//Adrian--
}
}
private void ShowEnumerate(XMLSchema.attribute attribute)
{
this.listViewEnumerate.Items.Clear();
if (attribute != null)
{
if (attribute.type != null)
{
//XSDObject xsdObject = this.schema.ElementsByName[DiagramHelpers.QualifiedNameToFullName("type", attribute.type)] as XSDObject;
//if (xsdObject != null)
XSDObject xsdObject;
if (this.schema.ElementsByName.TryGetValue(DiagramHelpers.QualifiedNameToFullName("type", attribute.type), out xsdObject) && xsdObject != null)
{
XMLSchema.annotated annotatedElement = xsdObject.Tag as XMLSchema.annotated;
if (annotatedElement is XMLSchema.simpleType)
ShowEnumerate(annotatedElement as XMLSchema.simpleType);
}
}
else if (attribute.simpleType != null)
{
ShowEnumerate(attribute.simpleType);
}
}
}
private void ShowEnumerate(XMLSchema.annotated annotated)
{
this.listViewEnumerate.Items.Clear();
if (annotated != null)
{
XMLSchema.element element = annotated as XMLSchema.element;
if (element != null && element.type != null)
{
//XSDObject xsdObject = this.schema.ElementsByName[DiagramHelpers.QualifiedNameToFullName("type", element.type)] as XSDObject;
//if (xsdObject != null)
XSDObject xsdObject;
if (this.schema.ElementsByName.TryGetValue(DiagramHelpers.QualifiedNameToFullName("type", element.type), out xsdObject) && xsdObject != null)
{
XMLSchema.annotated annotatedElement = xsdObject.Tag as XMLSchema.annotated;
if (annotatedElement is XMLSchema.simpleType)
ShowEnumerate(annotatedElement as XMLSchema.simpleType);
}
}
}
}
private void ShowEnumerate(XMLSchema.simpleType simpleType)
{
if (simpleType != null)
{
if (simpleType.Item != null)
{
XMLSchema.restriction restriction = simpleType.Item as XMLSchema.restriction;
if (restriction != null && restriction.ItemsElementName != null)
{
for (int i = 0; i < restriction.ItemsElementName.Length; i++)
{
if (restriction.ItemsElementName[i] == XMLSchema.ItemsChoiceType.enumeration)
{
XMLSchema.facet facet = restriction.Items[i] as XMLSchema.facet;
if (facet != null)
this.listViewEnumerate.Items.Add(facet.value).Tag = facet;
}
}
if (this.listViewEnumerate.Items.Count != 0)
this.listViewEnumerate.Columns[0].Width = -1;
}
}
}
}
private void ShowDocumentation(XMLSchema.annotation annotation)
{
if (this.textBoxAnnotation == null)
{
//
// webBrowserDocumentation
//
if(webBrowserSupported)
{
this.webBrowserDocumentation = new System.Windows.Forms.WebBrowser();
this.webBrowserDocumentation.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowserDocumentation.Location = new System.Drawing.Point(0, 0);
this.webBrowserDocumentation.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowserDocumentation.Name = "webBrowserDocumentation";
this.webBrowserDocumentation.Size = new System.Drawing.Size(214, 117);
this.webBrowserDocumentation.TabIndex = 1;
this.splitContainerDiagramElement.Panel2.Controls.Add(this.webBrowserDocumentation);
}
else
this.webBrowserDocumentation = null;
//
// textBoxAnnotation
//
this.textBoxAnnotation = new System.Windows.Forms.TextBox();
this.textBoxAnnotation.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxAnnotation.Location = new System.Drawing.Point(0, 0);
this.textBoxAnnotation.Multiline = true;
this.textBoxAnnotation.Name = "textBoxAnnotation";
this.textBoxAnnotation.ReadOnly = true;
this.textBoxAnnotation.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxAnnotation.Size = new System.Drawing.Size(214, 117);
this.textBoxAnnotation.TabIndex = 0;
this.splitContainerDiagramElement.Panel2.Controls.Add(this.textBoxAnnotation);
}
if (annotation == null)
{
this.textBoxAnnotation.Text = "";
this.textBoxAnnotation.Visible = true;
if (this.webBrowserDocumentation != null)
this.webBrowserDocumentation.Visible = false;
return;
}
bool isWebDocumentation = false;
Uri uriResult;
foreach (object o in annotation.Items)
{
if (o is XMLSchema.documentation)
{
XMLSchema.documentation documentation = o as XMLSchema.documentation;
if (documentation.Any != null && documentation.Any.Length > 0 && documentation.Any[0].Value != null)
{
}
else if (documentation.source != null && Uri.TryCreate(documentation.source, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
{
if (this.webBrowserDocumentation != null)
{
isWebDocumentation = true;
this.textBoxAnnotation.Visible = false;
this.webBrowserDocumentation.Visible = true;
this.webBrowserDocumentation.Navigate(documentation.source);
}
}
break;
}
}
if(!isWebDocumentation)
{
this.textBoxAnnotation.Text = DiagramHelpers.GetAnnotationText(annotation);
this.textBoxAnnotation.Visible = true;
if (this.webBrowserDocumentation != null)
this.webBrowserDocumentation.Visible = false;
}
}
private void listViewAttributes_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listViewAttributes.SelectedItems.Count > 0)
{
XSDAttribute xsdAttribute = this.listViewAttributes.SelectedItems[0].Tag as XSDAttribute;
XMLSchema.attribute attribute = xsdAttribute.Tag;
if (attribute != null && attribute.annotation != null)
ShowDocumentation(attribute.annotation);
else
ShowDocumentation(null);
ShowEnumerate(attribute);
}
}
private void listViewEnumerate_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listViewEnumerate.SelectedItems.Count > 0)
{
XMLSchema.facet facet = this.listViewEnumerate.SelectedItems[0].Tag as XMLSchema.facet;
if (facet != null && facet.annotation != null)
ShowDocumentation(facet.annotation);
else
ShowDocumentation(null);
}
}
private void toolStripComboBoxZoom_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string zoomString = this.toolStripComboBoxZoom.SelectedItem as string;
zoomString = zoomString.Replace("%", "");
float zoom = (float)int.Parse(zoomString) / 100.0f;
if (zoom >= 0.10 && zoom <= 10)
{
//Point virtualCenter = this.panelDiagram.VirtualPoint;
//virtualCenter.Offset(this.panelDiagram.DiagramControl.Width / 2, this.panelDiagram.DiagramControl.Height / 2);
//Size oldSize = this.panelDiagram.VirtualSize;
//this.diagram.Scale = zoom;
//UpdateDiagram();
//Size newSize = this.panelDiagram.VirtualSize;
//virtualCenter.X = (int)((float)newSize.Width / (float)oldSize.Width * (float)virtualCenter.X);
//virtualCenter.Y = (int)((float)newSize.Height / (float)oldSize.Height * (float)virtualCenter.Y);
//if (virtualCenter.X > this.diagram.BoundingBox.Right)
// virtualCenter.X = this.diagram.BoundingBox.Right;
//if (virtualCenter.Y > this.diagram.BoundingBox.Bottom)
// virtualCenter.Y = this.diagram.BoundingBox.Bottom;
//this.panelDiagram.ScrollTo(virtualCenter, true);
Point virtualCenter = this.panelDiagram.VirtualPoint;
virtualCenter.Offset(this.panelDiagram.DiagramControl.Width / 2, this.panelDiagram.DiagramControl.Height / 2);
Size oldSize = this.panelDiagram.VirtualSize;
this.diagram.Scale = zoom;
UpdateDiagram();
Size newSize = this.panelDiagram.VirtualSize;
Point newVirtualCenter = new Point();
newVirtualCenter.X = (int)((float)newSize.Width / (float)oldSize.Width * (float)virtualCenter.X);
newVirtualCenter.Y = (int)((float)newSize.Height / (float)oldSize.Height * (float)virtualCenter.Y);
if (newVirtualCenter.X > this.diagram.BoundingBox.Right)
newVirtualCenter.X = this.diagram.BoundingBox.Right;
if (newVirtualCenter.Y > this.diagram.BoundingBox.Bottom)
newVirtualCenter.Y = this.diagram.BoundingBox.Bottom;
this.panelDiagram.ScrollTo(newVirtualCenter, true);
}
}
catch { }
Settings.Default.Zoom = this.toolStripComboBoxZoom.SelectedIndex;
}
private void toolStripComboBoxZoom_TextChanged(object sender, EventArgs e)
{
//try
//{
// string zoomString = this.toolStripComboBoxZoom.SelectedItem as string;
// zoomString = zoomString.Replace("%", "");
// float zoom = (float)int.Parse(zoomString) / 100.0f;
// if (zoom >= 0.10 && zoom <= 10)
// {
// this.diagram.Scale = zoom;
// UpdateDiagram();
// }
//}
//catch { }
}
void DiagramControl_MouseWheel(object sender, MouseEventArgs e)
{
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Delta > 0)
{
if (this.toolStripComboBoxZoom.SelectedIndex < this.toolStripComboBoxZoom.Items.Count - 1)
this.toolStripComboBoxZoom.SelectedIndex++;
}
else
{
if (this.toolStripComboBoxZoom.SelectedIndex > 0)
this.toolStripComboBoxZoom.SelectedIndex--;
}
}
}
private void pageToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (_diagramPrinter == null)
{
_diagramPrinter = new DiagramPrinter();
}
_diagramPrinter.Diagram = diagram;
_diagramPrinter.PageSetup();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (_diagramPrinter == null)
{
_diagramPrinter = new DiagramPrinter();
}
_diagramPrinter.Diagram = diagram;