-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadAnalysisCode.cs
More file actions
201 lines (182 loc) · 6.67 KB
/
Copy pathLoadAnalysisCode.cs
File metadata and controls
201 lines (182 loc) · 6.67 KB
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
using System.Drawing;
using System.IO;
using System.Linq;
using StatTag.Controls;
using StatTag.Core;
using StatTag.Core.Models;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using StatTag.Core.Utility;
using StatTag.Models;
namespace StatTag
{
public sealed partial class LoadAnalysisCode : Form
{
public List<CodeFile> Files { get; set; }
public DocumentManager Manager { get; set; }
public LoadAnalysisCode(DocumentManager manager, List<CodeFile> files = null)
{
InitializeComponent();
Manager = manager;
Files = files;
MinimumSize = Size;
UIUtility.SetDialogTitle(this);
}
private void LoadAnalysisCode_Load(object sender, EventArgs e)
{
if (Files != null)
{
foreach (var file in Files)
{
AddItem(file);
}
}
}
private void cmdAdd_Click(object sender, EventArgs e)
{
var fileNames = UIUtility.GetOpenFileNames(Constants.FileFilters.FormatForOpenFileDialog());
if (fileNames != null)
{
var cleanedFiles = fileNames.Where(x => !string.IsNullOrWhiteSpace(x));
foreach (var fileName in cleanedFiles)
{
string package = CodeFile.GuessStatisticalPackage(fileName);
AddItem(new CodeFile { FilePath = fileName, StatisticalPackage = package });
}
}
}
/// <summary>
/// Helper to add a new code file to our managed collections, if it doesn't already exist.
/// </summary>
/// <param name="file">The code file to add to our managed collections</param>
private void AddItem(CodeFile file)
{
// If we already have this code file (determined by the path), we will not add it again.
if (pnlCodeFiles.Controls.OfType<CodeFileEntry>()
.Any(
x =>
x.CodeFile != null &&
x.CodeFile.FilePath.Equals(file.FilePath, StringComparison.CurrentCultureIgnoreCase)))
{
return;
}
var entry = new CodeFileEntry
{
CodeFile = file,
Width = pnlCodeFiles.Width - pnlCodeFiles.Margin.Left - pnlCodeFiles.Margin.Right - 2,
Anchor = AnchorStyles.Left | AnchorStyles.Right
};
entry.CodeFileClick += codeFile_Click;
entry.CodeFileDoubleClick += codeFile_DoubleClick;
pnlCodeFiles.Controls.Add(entry);
}
/// <summary>
/// Helper method to deselect all code files in the list
/// </summary>
private void UnselectAllCodeFiles()
{
foreach (var codeFile in pnlCodeFiles.Controls.OfType<CodeFileEntry>())
{
codeFile.Selected = false;
}
UpdateRemoveButton();
}
/// <summary>
/// Responds to a code file entry being clicked in our panel (list)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void codeFile_Click(object sender, EventArgs e)
{
if (Control.ModifierKeys != Keys.Alt)
{
UnselectAllCodeFiles();
}
(sender as CodeFileEntry).Selected = true;
UpdateRemoveButton();
}
/// <summary>
/// Responds to a code file entry being double-clicked in our panel (list)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void codeFile_DoubleClick(object sender, EventArgs e)
{
var selectedEntry = sender as CodeFileEntry;
if (selectedEntry != null)
{
selectedEntry.Selected = true;
EditFilePath(selectedEntry);
}
}
private void cmdOK_Click(object sender, EventArgs e)
{
var files = new List<CodeFile>();
foreach (var codeFileEntry in pnlCodeFiles.Controls.OfType<CodeFileEntry>())
{
var file = new CodeFile()
{
FilePath = codeFileEntry.CodeFile.FilePath,
StatisticalPackage = CodeFile.GuessStatisticalPackage(codeFileEntry.CodeFile.FilePath)
};
file.LoadTagsFromContent();
files.Add(file);
file.SaveBackup();
}
Files = files;
Manager.SetCodeFileList(Files);
Close();
}
private void cmdRemove_Click(object sender, EventArgs e)
{
var selectedEntries = pnlCodeFiles.Controls.OfType<CodeFileEntry>().Where(x => x.Selected).ToList();
foreach (var entry in selectedEntries)
{
pnlCodeFiles.Controls.Remove(entry);
}
UpdateCodeFileList();
}
/// <summary>
/// Helper method to manage the action of attempting to change a code file's location. It will handle
/// the UI aspects as well as updating the code file data if it is changed.
/// </summary>
/// <param name="entry"></param>
private void EditFilePath(CodeFileEntry entry)
{
string fileName = UIUtility.GetOpenFileName(Constants.FileFilters.FormatForOpenFileDialog());
if (!string.IsNullOrWhiteSpace(fileName))
{
var file = new CodeFile {FilePath = fileName};
entry.CodeFile = file;
}
}
/// <summary>
/// Perform necessary visual updates to the code file list
/// </summary>
private void UpdateCodeFileList()
{
foreach (var item in pnlCodeFiles.Controls.OfType<CodeFileEntry>())
{
item.Index = pnlCodeFiles.Controls.GetChildIndex(item, false);
}
UpdateRemoveButton();
}
private void pnlCodeFiles_ControlAdded(object sender, ControlEventArgs e)
{
UpdateCodeFileList();
}
private void pnlCodeFiles_ControlRemoved(object sender, ControlEventArgs e)
{
UpdateCodeFileList();
}
private void pnlCodeFiles_Click(object sender, EventArgs e)
{
UnselectAllCodeFiles();
}
private void UpdateRemoveButton()
{
cmdRemove.Enabled = pnlCodeFiles.Controls.OfType<CodeFileEntry>().Any(x => x.Selected);
}
}
}