-
Notifications
You must be signed in to change notification settings - Fork 1
/
FolderTreeView.cs
261 lines (227 loc) · 8.59 KB
/
FolderTreeView.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
// Filename: FolderTreeView.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
public class FolderTreeView : Form
{
private TreeView folderTreeView;
private Button checkAllButton;
private Button uncheckAllButton;
private Button startButton;
private string rootFolder;
private FolderSelection folderSelection;
public FolderTreeView(string rootFolder)
{
this.rootFolder = rootFolder;
folderSelection = SelectionManager.GetFolderSelection(rootFolder);
folderTreeView = new TreeView
{
CheckBoxes = true // Enable checkboxes
};
folderTreeView.AfterCheck += FolderTreeView_AfterCheck; // Event handler for AfterCheck
checkAllButton = new Button { Text = "Check All" };
uncheckAllButton = new Button { Text = "Uncheck All" };
startButton = new Button { Text = "Start Operation" };
checkAllButton.Click += CheckAllButton_Click;
uncheckAllButton.Click += UncheckAllButton_Click;
startButton.Click += StartButton_Click;
LoadFolders(rootFolder);
Controls.Add(folderTreeView);
Controls.Add(checkAllButton);
Controls.Add(uncheckAllButton);
Controls.Add(startButton);
// Layout
folderTreeView.Dock = DockStyle.Fill;
checkAllButton.Dock = DockStyle.Bottom;
uncheckAllButton.Dock = DockStyle.Bottom;
startButton.Dock = DockStyle.Bottom;
this.SizeChanged += FolderTreeView_SizeChanged;
this.Height = 600; // Adjust the initial height
CheckForNewFolders();
}
private void LoadFolders(string path)
{
var rootNode = new TreeNode(path) { Tag = path };
folderTreeView.Nodes.Add(rootNode);
LoadSubFolders(rootNode);
rootNode.Expand(); // Expand the selected folder
}
private void LoadSubFolders(TreeNode node)
{
var path = (string)node.Tag;
foreach (var directory in Directory.GetDirectories(path))
{
var relativePath = Path.GetRelativePath(rootFolder, directory);
var subNode = new TreeNode(Path.GetFileName(directory))
{
Tag = directory,
Checked = folderSelection.SelectedFolders.Any(f => f.FolderPath == relativePath && f.Included)
};
node.Nodes.Add(subNode);
LoadSubFolders(subNode);
}
foreach (var file in Directory.GetFiles(path))
{
var relativePath = Path.GetRelativePath(rootFolder, file);
var fileNode = new TreeNode(Path.GetFileName(file))
{
Tag = file,
Checked = folderSelection.SelectedFolders.Any(f => f.FolderPath == relativePath && f.Included)
};
node.Nodes.Add(fileNode);
}
}
private void FolderTreeView_AfterCheck(object? sender, TreeViewEventArgs e)
{
if (e.Node == null) return; // Check for null
// Prevent recursive calls
folderTreeView.AfterCheck -= FolderTreeView_AfterCheck;
CheckAllSubNodes(e.Node, e.Node.Checked);
// Re-enable the event handler
folderTreeView.AfterCheck += FolderTreeView_AfterCheck;
}
private void CheckAllSubNodes(TreeNode treeNode, bool nodeChecked)
{
if (treeNode == null) return; // Check for null
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
CheckAllSubNodes(node, nodeChecked);
}
}
private void CheckAllButton_Click(object? sender, EventArgs e)
{
SetNodeCheckState(folderTreeView.Nodes, true);
}
private void UncheckAllButton_Click(object? sender, EventArgs e)
{
SetNodeCheckState(folderTreeView.Nodes, false);
}
private void StartButton_Click(object? sender, EventArgs e)
{
SaveFolderSelections();
StartOperationAsync();
}
private async void StartOperationAsync()
{
var saveFileDialog = new SaveFileDialog
{
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",
DefaultExt = "txt",
InitialDirectory = SelectionManager.LoadSelections().Folders
.FirstOrDefault(f => f.SourceFolder == rootFolder)?.OutputFile // Use the saved output file path
};
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
folderSelection.OutputFile = saveFileDialog.FileName; // Update the output file path
SelectionManager.UpdateFolderSelection(folderSelection);
var includedFiles = GetIncludedFiles();
using (var progressForm = new ProgressForm())
{
progressForm.Show();
int totalFiles = includedFiles.Count();
int processedFiles = 0;
await Task.Run(() =>
{
foreach (var file in includedFiles)
{
// Process the file (pseudo-code)
// ...
processedFiles++;
int progress = (int)((double)processedFiles / totalFiles * 100);
progressForm.Invoke(new Action(() => progressForm.SetProgress(progress)));
}
FileAggregator.AggregateFiles(rootFolder, includedFiles, saveFileDialog.FileName);
});
progressForm.Close();
}
MessageBox.Show("Operation completed!");
this.Close();
// Ask the user if they want to open the file
var result = MessageBox.Show("Do you want to open the newly created file?", "Open File", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
try
{
var processInfo = new System.Diagnostics.ProcessStartInfo(saveFileDialog.FileName)
{
UseShellExecute = true
};
System.Diagnostics.Process.Start(processInfo);
}
catch (Exception ex)
{
MessageBox.Show($"Error opening file: {ex.Message}");
}
}
}
}
public IEnumerable<string> GetIncludedFiles()
{
var includedFiles = new List<string>();
GetIncludedFilesRecursive(folderTreeView.Nodes, includedFiles);
return includedFiles;
}
private void GetIncludedFilesRecursive(TreeNodeCollection nodes, List<string> includedFiles)
{
foreach (TreeNode node in nodes)
{
if (node.Checked && File.Exists((string)node.Tag))
{
includedFiles.Add((string)node.Tag);
}
GetIncludedFilesRecursive(node.Nodes, includedFiles);
}
}
private void CheckForNewFolders()
{
CheckForNewFoldersRecursive(folderTreeView.Nodes);
}
private void CheckForNewFoldersRecursive(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
var relativePath = Path.GetRelativePath(rootFolder, (string)node.Tag);
if (!folderSelection.SelectedFolders.Any(f => f.FolderPath == relativePath))
{
node.ForeColor = Color.Green; // Indicate new folders
node.Expand(); // Expand to show new folders
}
CheckForNewFoldersRecursive(node.Nodes);
}
}
private void SaveFolderSelections()
{
folderSelection.SelectedFolders.Clear();
SaveFolderSelectionsRecursive(folderTreeView.Nodes);
SelectionManager.UpdateFolderSelection(folderSelection);
}
private void SaveFolderSelectionsRecursive(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
var relativePath = Path.GetRelativePath(rootFolder, (string)node.Tag);
folderSelection.SelectedFolders.Add(new FolderStatus
{
FolderPath = relativePath,
Included = node.Checked
});
SaveFolderSelectionsRecursive(node.Nodes);
}
}
private void SetNodeCheckState(TreeNodeCollection nodes, bool checkState)
{
foreach (TreeNode node in nodes)
{
node.Checked = checkState;
SetNodeCheckState(node.Nodes, checkState);
}
}
private void FolderTreeView_SizeChanged(object? sender, EventArgs e)
{
folderTreeView.Height = this.ClientSize.Height - checkAllButton.Height - uncheckAllButton.Height - startButton.Height - 20;
}
}