This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
/
FileDropHelper.cs
139 lines (120 loc) · 4.03 KB
/
FileDropHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling;
using Sawczyn.EFDesigner.EFModel.Extensions;
namespace Sawczyn.EFDesigner.EFModel
{
public static class FileDropHelper
{
public const int BAD_ARGUMENT_COUNT = 1;
public const int CANNOT_LOAD_ASSEMBLY = 2;
public const int CANNOT_WRITE_OUTPUTFILE = 3;
public const int CANNOT_CREATE_DBCONTEXT = 4;
public const int CANNOT_FIND_APPROPRIATE_CONSTRUCTOR = 5;
public const int AMBIGUOUS_REQUEST = 6;
public static void HandleDrop(Store store, string filename)
{
if (store == null || filename == null)
return;
try
{
StatusDisplay.Show($"Reading {filename}");
AssemblyProcessor assemblyProcessor = new AssemblyProcessor(store);
TextFileProcessor textFileProcessor = new TextFileProcessor(store);
textFileProcessor.LoadCache(filename);
Process(store, filename, assemblyProcessor, textFileProcessor);
}
catch (Exception e)
{
ErrorDisplay.Show(e.Message);
}
finally
{
StatusDisplay.Show(string.Empty);
}
}
public static void HandleMultiDrop(Store store, IEnumerable<string> filenames)
{
List<string> filenameList = filenames?.ToList();
if (store == null || filenameList == null)
return;
try
{
StatusDisplay.Show($"Processing {filenameList.Count} files");
AssemblyProcessor assemblyProcessor = new AssemblyProcessor(store);
TextFileProcessor textFileProcessor = new TextFileProcessor(store);
try
{
// may not work. Might not be a text file
textFileProcessor.LoadCache(filenameList);
}
catch
{
// if not, no big deal. Either it's not a text file, or we'll just process suboptimally
}
foreach (string filename in filenameList)
Process(store, filename, assemblyProcessor, textFileProcessor);
}
catch (Exception e)
{
ErrorDisplay.Show(e.Message);
}
finally
{
StatusDisplay.Show(string.Empty);
}
}
private static bool IsAssembly(string filename)
{
try
{
AssemblyName.GetAssemblyName(filename);
}
catch (BadImageFormatException)
{
return false;
}
return true;
}
private static void Process(Store store, string filename, AssemblyProcessor assemblyProcessor, TextFileProcessor textFileProcessor)
{
try
{
Cursor.Current = Cursors.WaitCursor;
ModelRoot.BatchUpdating = true;
if (IsAssembly(filename))
{
using (Transaction tx = store.TransactionManager.BeginTransaction("Process dropped assembly"))
{
if (assemblyProcessor.Process(filename))
{
StatusDisplay.Show("Creating diagram elements. This might take a while...");
tx.Commit();
ModelDisplay.LayoutDiagram(store.ModelRoot().GetActiveDiagram() as EFModelDiagram);
}
}
}
else
{
using (Transaction tx = store.TransactionManager.BeginTransaction("Process dropped class"))
{
if (textFileProcessor.Process(filename))
{
StatusDisplay.Show("Creating diagram elements. This might take a while...");
tx.Commit();
}
}
}
}
finally
{
Cursor.Current = Cursors.Default;
ModelRoot.BatchUpdating = false;
StatusDisplay.Show("");
}
}
}
}