-
Notifications
You must be signed in to change notification settings - Fork 20
/
ClassView.cs
276 lines (245 loc) · 10.4 KB
/
ClassView.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using GitForce.Properties;
namespace GitForce
{
/// <summary>
/// This class contains support functions and data definitions
/// for the various views within forms.
/// </summary>
public static class ClassView
{
/// <summary>
/// Enumeration of icons for files at different stage
/// </summary>
public enum Img
{
FileUnmodified,
FileModified,
FileAdded,
FileDeleted,
FileRenamed,
FileCopied,
FileUnmerged,
FileUntracked,
FolderClosed,
FolderOpened,
DatabaseClosed,
DatabaseOpened,
ChangelistRoot,
Changelist,
FolderClosedModified,
FolderOpenedModified,
}
/// <summary>
/// Describes a mapping from image number into the resource icon
/// </summary>
private static readonly Dictionary<Img, Icon> Res = new Dictionary<Img, Icon> {
{ Img.FileUnmodified, Resources.TreeIconU },
{ Img.FileModified, Resources.TreeIconM },
{ Img.FileAdded, Resources.TreeIconA },
{ Img.FileDeleted, Resources.TreeIconD },
{ Img.FileRenamed, Resources.TreeIconR },
{ Img.FileCopied, Resources.TreeIconC },
{ Img.FileUnmerged, Resources.TreeIconX },
{ Img.FileUntracked, Resources.TreeIconQ },
{ Img.FolderClosed, Resources.TreeIconFC },
{ Img.FolderOpened, Resources.TreeIconFO },
{ Img.DatabaseClosed, Resources.TreeIconDB },
{ Img.DatabaseOpened, Resources.TreeIconDB },
{ Img.ChangelistRoot, Resources.Change0 },
{ Img.Changelist, Resources.Change1 },
{ Img.FolderClosedModified, Resources.TreeIconFCM },
{ Img.FolderOpenedModified, Resources.TreeIconFOM },
};
/// <summary>
/// Describes a mapping from a git file status code to the image associated with it
/// </summary>
private static readonly Dictionary<char, Img> Staticons = new Dictionary<char, Img> {
{ ' ', Img.FileUnmodified },
{ 'M', Img.FileModified },
{ 'A', Img.FileAdded },
{ 'D', Img.FileDeleted },
{ 'R', Img.FileRenamed },
{ 'C', Img.FileCopied },
{ 'U', Img.FileUnmerged },
{ '?', Img.FileUntracked },
};
/// <summary>
/// Returns the image list to be used with a tree view showing the files
/// </summary>
public static ImageList GetImageList()
{
ImageList il = new ImageList();
il.ImageSize = new Size(32, 16);
il.ColorDepth = ColorDepth.Depth32Bit;
foreach (var kvp in Res)
il.Images.Add(kvp.Value);
return il;
}
/// <summary>
/// Traverse tree nodes and assign an icon corresponding to each file status.
/// Since each file has 2 status codes, X and Y, for index with relation to the
/// git cache, and for current file with relation to the index, isIndex
/// specifies which code will be used.
/// </summary>
public static void ViewAssignIcon(ClassStatus status, TreeNode rootNode, bool isIndex)
{
TreeNodeCollection nodes = rootNode.Nodes;
foreach (TreeNode tn in nodes)
{
if (tn.Tag is ClassCommit)
tn.ImageIndex = (int)Img.Changelist;
else
{
string name = isIndex ? tn.Text : tn.Tag.ToString();
if (status.IsMarked(name) || tn.Nodes.Count == 0)
{
char icon = isIndex ? status.Xcode(name) : status.Ycode(name);
if (Staticons.ContainsKey(icon))
tn.ImageIndex = (int)Staticons[icon];
else
tn.ImageIndex = 0;
}
else {
tn.ImageIndex = status.HasModifiedDescendants (name)
? (int)Img.FolderClosedModified
: (int)Img.FolderClosed;
}
}
ViewAssignIcon(status, tn, isIndex);
}
}
public static void ViewUpdateToolTips (TreeNode rootNode) {
TreeNodeCollection nodes = rootNode.Nodes;
foreach (TreeNode tn in nodes)
{
switch ((Img)tn.ImageIndex) {
case Img.FileUnmodified: /* no tooltip for unmodified files */ break;
case Img.FileModified: tn.ToolTipText = "Modified"; break;
case Img.FileAdded: tn.ToolTipText = "Added"; break;
case Img.FileDeleted: tn.ToolTipText = "Deleted"; break;
case Img.FileRenamed: tn.ToolTipText = "Renamed"; break;
case Img.FileCopied: tn.ToolTipText = "Copied"; break;
case Img.FileUnmerged: tn.ToolTipText = "Unmerged"; break;
case Img.FileUntracked: tn.ToolTipText = "Untracked"; break;
case Img.FolderClosed:
case Img.FolderOpened: /* no tooltip for unmodified folders */ break;
case Img.DatabaseClosed:
case Img.DatabaseOpened: tn.ToolTipText = "Repository"; break;
case Img.ChangelistRoot:
case Img.Changelist: tn.ToolTipText = "Changelist"; break;
case Img.FolderClosedModified:
case Img.FolderOpenedModified:
tn.ToolTipText = "Folder contains changes";
break;
default:
// tn.ToolTipText = "Unknown...";
break;
}
ViewUpdateToolTips (tn);
}
}
/// <summary>
/// Build a tree view given the list of files
/// </summary>
/// <param name="tnRoot">Root node to base the tree on</param>
/// <param name="list">List of files in git format</param>
/// <param name="sortBy">File sorting order</param>
public static void BuildTree(TreeNode tnRoot, List<string> list, GitDirectoryInfo.SortBy sortBy)
{
BuildTreeRecurse(tnRoot, list, sortBy);
}
/// <summary>
/// Recursive function to traverse the virtual directory of
/// a flat git response files and build a visual tree component.
/// </summary>
/// <param name="tnRoot">Root node to base the tree on</param>
/// <param name="list">List of files in git format</param>
/// <param name="sortBy">File sorting order</param>
private static void BuildTreeRecurse(TreeNode tnRoot, List<string> list, GitDirectoryInfo.SortBy sortBy)
{
string rootPath = tnRoot.Tag.ToString();
GitDirectoryInfo dir = new GitDirectoryInfo(rootPath, list);
GitDirectoryInfo[] dirs = dir.GetDirectories();
foreach (GitDirectoryInfo d in dirs)
{
TreeNode tn = new TreeNode(d.Name);
tn.Tag = d.FullName + Path.DirectorySeparatorChar;
tnRoot.Nodes.Add(tn);
BuildTreeRecurse(tn, d.List, sortBy);
}
GitFileInfo[] files = dir.GetFiles(sortBy);
foreach (GitFileInfo file in files)
{
TreeNode tn = new TreeNode(file.Name);
tn.Tag = file.FullName;
tnRoot.Nodes.Add(tn);
}
}
/// <summary>
/// Builds a flat list of tree nodes based on a given list of files
/// </summary>
/// <param name="tnRoot">Root node to build the list on</param>
/// <param name="list">List of relative path file names</param>
public static void BuildFileList(TreeNode tnRoot, List<string> list, GitDirectoryInfo.SortBy sortBy)
{
// Sort the list according to the sorting rule
if (sortBy == GitDirectoryInfo.SortBy.Name)
list.Sort();
else
{
var vSort = from s in list
orderby Path.GetExtension(s), s ascending
select s;
list = vSort.ToList();
}
// Build a list view);
foreach (string s in list)
{
TreeNode tn = new TreeNode(s);
tn.Tag = s;
tnRoot.Nodes.Add(tn);
}
}
/// <summary>
/// Build a tree view of commit nodes using hints from repo commit groups.
/// Commit nodes have Tags containing absolute paths to files,
/// while the relative paths (with respect to repo root) are simply in the Text.
/// </summary>
public static TreeNode BuildCommitsView(ClassRepo repo, List<string> list)
{
repo.Commits.Rebuild(list);
// Step 1: Build the default list with all files under this status class
TreeNode root = new TreeNode("Pending Changelists");
root.Tag = "root"; // Tag of the root node
foreach (var c in repo.Commits.Bundle)
{
var commitNode = new TreeNode(GetCommitNodeText(c)) { Tag = c };
if (c.IsCollapsed) commitNode.Collapse();
foreach (var f in c.Files)
{
var tn = new TreeNode(f) { Tag = f };
commitNode.Nodes.Add(tn);
}
root.Nodes.Add(commitNode);
}
root.ExpandAll();
return root;
}
public static string GetCommitNodeText(ClassCommit commit)
{
if (commit == null) return string.Empty;
var rerult = commit.DescriptionTitle;
if (commit.Files.Count > 0)
{
rerult += " (" + commit.Files.Count + ")";
}
return rerult;
}
}
}