This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathFolderNodeCommands.cs
More file actions
432 lines (401 loc) · 16.4 KB
/
FolderNodeCommands.cs
File metadata and controls
432 lines (401 loc) · 16.4 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
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
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Templates;
namespace ICSharpCode.SharpDevelop.Project.Commands
{
public class AddExistingItemsToProject : AbstractMenuCommand
{
public enum ReplaceExistingFile {
Yes = 0,
YesToAll = 1,
No = 2,
Cancel = 3
}
public static ReplaceExistingFile ShowReplaceExistingFileDialog(string caption, string fileName, bool replacingMultiple)
{
if (caption == null)
caption = "${res:ProjectComponent.ContextMenu.AddExistingFiles.ReplaceExistingFile.Title}";
string text = StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles.ReplaceExistingFile}", new StringTagPair("FileName", fileName));
if (replacingMultiple) {
return (ReplaceExistingFile)
MessageService.ShowCustomDialog(caption, text,
0, 3,
"${res:Global.Yes}",
"${res:Global.YesToAll}",
"${res:Global.No}",
"${res:Global.CancelButtonText}");
} else {
return MessageService.AskQuestion(text, caption)
? ReplaceExistingFile.Yes : ReplaceExistingFile.No;
}
}
int GetFileFilterIndex(IProject project, IReadOnlyList<FileFilterDescriptor> fileFilters)
{
if (project != null) {
ProjectBindingDescriptor projectCodon = SD.ProjectService.ProjectBindings.FirstOrDefault(b => b.Language == project.Language);
if (projectCodon != null) {
for (int i = 0; i < fileFilters.Count; ++i) {
for (int j = 0; j < projectCodon.CodeFileExtensions.Length; ++j) {
if (fileFilters[i].ContainsExtension(projectCodon.CodeFileExtensions[j])) {
return i + 1;
}
}
}
}
}
return 0;
}
public static void CopyDirectory(string directoryName, DirectoryNode node, bool includeInProject)
{
directoryName = FileUtility.NormalizePath(directoryName);
string copiedFileName = Path.Combine(node.Directory, Path.GetFileName(directoryName));
LoggingService.Debug("Copy " + directoryName + " to " + copiedFileName);
if (!FileUtility.IsEqualFileName(directoryName, copiedFileName)) {
if (includeInProject && ProjectService.OpenSolution != null) {
// get ProjectItems in source directory
FileService.CopyFile(directoryName, copiedFileName, true, false);
foreach (IProject project in ProjectService.OpenSolution.Projects) {
if (!FileUtility.IsBaseDirectory(project.Directory, directoryName))
continue;
LoggingService.Debug("Searching for child items in " + project.Name);
foreach (ProjectItem item in project.Items) {
FileProjectItem fileItem = item as FileProjectItem;
if (fileItem == null)
continue;
string virtualFullName = Path.Combine(project.Directory, fileItem.VirtualName);
if (FileUtility.IsBaseDirectory(directoryName, virtualFullName)) {
if (item.ItemType == ItemType.Folder && FileUtility.IsEqualFileName(directoryName, virtualFullName)) {
continue;
}
LoggingService.Debug("Found file " + virtualFullName);
FileProjectItem newItem = new FileProjectItem(node.Project, fileItem.ItemType);
if (FileUtility.IsBaseDirectory(directoryName, fileItem.FileName)) {
newItem.FileName = FileName.Create(FileUtility.RenameBaseDirectory(fileItem.FileName, directoryName, copiedFileName));
} else {
newItem.FileName = fileItem.FileName;
}
fileItem.CopyMetadataTo(newItem);
if (fileItem.IsLink) {
string newVirtualFullName = FileUtility.RenameBaseDirectory(virtualFullName, directoryName, copiedFileName);
fileItem.SetEvaluatedMetadata("Link", FileUtility.GetRelativePath(node.Project.Directory, newVirtualFullName));
}
ProjectService.AddProjectItem(node.Project, newItem);
}
}
}
}
DirectoryNode newNode = new DirectoryNode(copiedFileName);
newNode.InsertSorted(node);
if (includeInProject) {
IncludeFileInProject.IncludeDirectoryNode(newNode, false);
}
newNode.Expanding();
} else if (includeInProject) {
foreach (TreeNode childNode in node.Nodes) {
if (childNode is DirectoryNode) {
DirectoryNode directoryNode = (DirectoryNode)childNode;
if (FileUtility.IsEqualFileName(directoryNode.Directory, copiedFileName)) {
IncludeFileInProject.IncludeDirectoryNode(directoryNode, true);
}
}
}
}
}
public static FileProjectItem CopyFile(string fileName, DirectoryNode node, bool includeInProject)
{
string copiedFileName = Path.Combine(node.Directory, Path.GetFileName(fileName));
if (!FileUtility.IsEqualFileName(fileName, copiedFileName)) {
FileService.CopyFile(fileName, copiedFileName, false, true);
}
if (includeInProject) {
FileNode fileNode;
foreach (TreeNode childNode in node.AllNodes) {
if (childNode is FileNode) {
fileNode = (FileNode)childNode;
if (FileUtility.IsEqualFileName(fileNode.FileName, copiedFileName)) {
if (fileNode.FileNodeStatus == FileNodeStatus.Missing) {
fileNode.FileNodeStatus = FileNodeStatus.InProject;
} else if (fileNode.FileNodeStatus == FileNodeStatus.None) {
return IncludeFileInProject.IncludeFileNode(fileNode);
}
return fileNode.ProjectItem as FileProjectItem;
}
}
}
fileNode = new FileNode(copiedFileName);
fileNode.InsertSorted(node);
return IncludeFileInProject.IncludeFileNode(fileNode);
}
return null;
}
public static IEnumerable<string> FindAdditionalFiles(string fileName)
{
List<string> list = new List<string>();
// HACK: find a different way to support .Designer.XYZ
StringParserPropertyContainer.FileCreation["Extension"] = Path.GetExtension(fileName);
string prefix = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName));
foreach (string ext in AddInTree.BuildItems<string>("/SharpDevelop/Workbench/DependentFileExtensions", null, false)) {
if (File.Exists(prefix + ext))
list.Add(prefix + ext);
}
return list;
}
public override void Run()
{
ProjectBrowserPad.Instance.BringToFront();
this.AddExistingItems();
}
protected IEnumerable<FileProjectItem> AddExistingItems()
{
DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;
if (node == null) {
return null;
}
node.Expanding();
node.Expand();
List<FileProjectItem> addedItems = new List<FileProjectItem>();
using (OpenFileDialog fdiag = new OpenFileDialog()) {
fdiag.AddExtension = true;
var fileFilters = ProjectService.GetFileFilters();
fdiag.InitialDirectory = node.Directory;
fdiag.FilterIndex = GetFileFilterIndex(node.Project, fileFilters);
fdiag.Filter = String.Join("|", fileFilters);
fdiag.Multiselect = true;
fdiag.CheckFileExists = true;
fdiag.Title = StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles}");
if (fdiag.ShowDialog(SD.WinForms.MainWin32Window) == DialogResult.OK) {
List<KeyValuePair<string, string>> fileNames = new List<KeyValuePair<string, string>>(fdiag.FileNames.Length);
foreach (string fileName in fdiag.FileNames) {
fileNames.Add(new KeyValuePair<string, string>(fileName, ""));
}
bool addedDependentFiles = false;
foreach (string fileName in fdiag.FileNames) {
foreach (string additionalFile in FindAdditionalFiles(fileName)) {
if (!fileNames.Exists(delegate(KeyValuePair<string, string> pair) {
return FileUtility.IsEqualFileName(pair.Key, additionalFile);
}))
{
addedDependentFiles = true;
fileNames.Add(new KeyValuePair<string, string>(additionalFile, Path.GetFileName(fileName)));
}
}
}
string copiedFileName = Path.Combine(node.Directory, Path.GetFileName(fileNames[0].Key));
if (!FileUtility.IsEqualFileName(fileNames[0].Key, copiedFileName)) {
int res = MessageService.ShowCustomDialog(
fdiag.Title, "${res:ProjectComponent.ContextMenu.AddExistingFiles.Question}",
0, 2,
"${res:ProjectComponent.ContextMenu.AddExistingFiles.Copy}",
"${res:ProjectComponent.ContextMenu.AddExistingFiles.Link}",
"${res:Global.CancelButtonText}");
if (res == 1) {
// Link
foreach (KeyValuePair<string, string> pair in fileNames) {
string fileName = pair.Key;
string relFileName = FileUtility.GetRelativePath(node.Project.Directory, fileName);
FileNode fileNode = new FileNode(fileName, FileNodeStatus.InProject);
FileProjectItem fileProjectItem = new FileProjectItem(node.Project, node.Project.GetDefaultItemType(fileName), relFileName);
fileProjectItem.SetEvaluatedMetadata("Link", Path.Combine(node.RelativePath, Path.GetFileName(fileName)));
fileProjectItem.DependentUpon = pair.Value;
addedItems.Add(fileProjectItem);
fileNode.ProjectItem = fileProjectItem;
fileNode.InsertSorted(node);
ProjectService.AddProjectItem(node.Project, fileProjectItem);
}
node.Project.Save();
if (addedDependentFiles)
node.RecreateSubNodes();
return addedItems.AsReadOnly();
}
if (res == 2) {
// Cancel
return addedItems.AsReadOnly();
}
// only continue for res==0 (Copy)
}
bool replaceAll = false;
foreach (KeyValuePair<string, string> pair in fileNames) {
copiedFileName = Path.Combine(node.Directory, Path.GetFileName(pair.Key));
if (!replaceAll && File.Exists(copiedFileName) && !FileUtility.IsEqualFileName(pair.Key, copiedFileName)) {
ReplaceExistingFile res = ShowReplaceExistingFileDialog(fdiag.Title, Path.GetFileName(pair.Key), true);
if (res == ReplaceExistingFile.YesToAll) {
replaceAll = true;
} else if (res == ReplaceExistingFile.No) {
continue;
} else if (res == ReplaceExistingFile.Cancel) {
break;
}
}
FileProjectItem item = CopyFile(pair.Key, node, true);
if (item != null) {
addedItems.Add(item);
item.DependentUpon = pair.Value;
}
}
node.Project.Save();
if (addedDependentFiles)
node.RecreateSubNodes();
}
}
return addedItems.AsReadOnly();
}
}
public class AddExistingFolderToProject : AbstractMenuCommand
{
public override void Run()
{
ProjectBrowserPad.Instance.BringToFront();
DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;
if (node == null) {
return;
}
node.Expanding();
node.Expand();
using (FolderBrowserDialog dlg = new FolderBrowserDialog()) {
dlg.SelectedPath = node.Directory;
dlg.ShowNewFolderButton = false;
if (dlg.ShowDialog(SD.WinForms.MainWin32Window) == DialogResult.OK) {
string folderName = dlg.SelectedPath;
string copiedFolderName = Path.Combine(node.Directory, Path.GetFileName(folderName));
if (!FileUtility.IsEqualFileName(folderName, copiedFolderName)) {
if (FileUtility.IsBaseDirectory(folderName, node.Directory)) {
MessageService.ShowError("Cannot copy " + folderName + " to " + copiedFolderName);
return;
}
if (Directory.Exists(copiedFolderName)) {
MessageService.ShowError("Cannot copy " + folderName + " to " + copiedFolderName + ": target already exists.");
return;
}
int res = MessageService.ShowCustomDialog(
"${res:ProjectComponent.ContextMenu.ExistingFolder}",
"${res:ProjectComponent.ContextMenu.ExistingFolder.CopyQuestion}",
0, 1,
"${res:ProjectComponent.ContextMenu.AddExistingFiles.Copy}",
"${res:Global.CancelButtonText}");
if (res != 0)
return;
if (!FileService.CopyFile(folderName, copiedFolderName, true, false))
return;
}
// ugly HACK to get IncludeDirectoryNode to work properly
AbstractProjectBrowserTreeNode.ShowAll = true;
try {
node.RecreateSubNodes();
DirectoryNode newNode = node.AllNodes.OfType<DirectoryNode>()
.FirstOrDefault(dir=>FileUtility.IsEqualFileName(copiedFolderName, dir.Directory));
if (newNode != null) {
newNode.Expanding();
IncludeFileInProject.IncludeDirectoryNode(newNode, true);
}
} finally {
AbstractProjectBrowserTreeNode.ShowAll = false;
}
node.RecreateSubNodes();
}
}
}
}
/// <summary>
/// Menu item that display the NewFileDialog dialog and adds it to the solution.
/// </summary>
/// <seealso cref="NewFileDialog"/>
/// <exception cref="NullReferenceException">
/// Thrown if the selected node does not have an ancestor that is a DirectoryNode.
/// </exception>
public class AddNewItemsToProject : AbstractMenuCommand
{
public override void Run()
{
ProjectBrowserPad.Instance.BringToFront();
this.AddNewItems();
}
protected IEnumerable<FileProjectItem> AddNewItems()
{
DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;
if (node == null) {
return null;
}
node.Expand();
node.Expanding();
FileTemplateResult result = SD.UIService.ShowNewFileDialog(node.Project, node.Directory);
if (result != null) {
node.RecreateSubNodes();
ProjectBrowserPad.Instance.ProjectBrowserControl.SelectFile(result.Options.FileName);
return result.NewFiles.Select(node.Project.FindFile).Where(f => f != null).ToArray();
} else {
return null;
}
}
}
public class AddNewFolderToProject : AbstractMenuCommand
{
string GenerateValidDirectoryName(string inDirectory)
{
string directoryName = Path.Combine(inDirectory, ResourceService.GetString("ProjectComponent.NewFolderString"));
if (Directory.Exists(directoryName)) {
int index = 1;
while (Directory.Exists(directoryName + index)) {
index++;
}
return directoryName + index;
}
return directoryName;
}
DirectoryNode CreateNewDirectory(DirectoryNode upper, string directoryName)
{
upper.Expanding();
Directory.CreateDirectory(directoryName);
FileService.FireFileCreated(directoryName, true);
DirectoryNode directoryNode = new DirectoryNode(directoryName, FileNodeStatus.InProject);
directoryNode.InsertSorted(upper);
IncludeFileInProject.IncludeDirectoryNode(directoryNode, false);
return directoryNode;
}
public override void Run()
{
ProjectBrowserPad.Instance.BringToFront();
DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;
if (node == null) {
return;
}
node.Expanding();
string newDirectoryName = GenerateValidDirectoryName(node.Directory);
DirectoryNode newDirectoryNode = CreateNewDirectory(node, newDirectoryName);
ProjectBrowserPad.Instance.StartLabelEdit(newDirectoryNode);
}
}
public class CreateMissingCommand : AbstractMenuCommand
{
public override void Run()
{
TreeNode selectedNode = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedNode;
DirectoryNode node = selectedNode as DirectoryNode;
if (node != null) {
Directory.CreateDirectory(node.Directory);
IncludeFileInProject.IncludeDirectoryNode(node, false);
}
}
}
}