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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
//
// ProjectFileNodeBuilder.cs
//
// Author:
// Lluis Sanchez Gual
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// 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.IO;
using System.Collections.Generic;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Ide.Commands;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core.Collections;
using MonoDevelop.Ide.Gui.Components;
using System.Linq;
using MonoDevelop.Components;
namespace MonoDevelop.Ide.Gui.Pads.ProjectPad
{
class ProjectFileNodeBuilder: TypeNodeBuilder
{
public override Type NodeDataType {
get { return typeof(ProjectFile); }
}
public override Type CommandHandlerType {
get { return typeof(ProjectFileNodeCommandHandler); }
}
public override string GetNodeName (ITreeNavigator thisNode, object dataObject)
{
var file = (ProjectFile) dataObject;
return file.Link.IsNullOrEmpty ? file.FilePath.FileName : file.Link.FileName;
}
public override void GetNodeAttributes (ITreeNavigator parentNode, object dataObject, ref NodeAttributes attributes)
{
var file = (ProjectFile) dataObject;
if ((file.Flags & ProjectItemFlags.Hidden) != 0) {
attributes |= NodeAttributes.Hidden;
return;
}
attributes |= NodeAttributes.AllowRename;
if (!file.Visible && !parentNode.Options ["ShowAllFiles"])
attributes |= NodeAttributes.Hidden;
}
public override void BuildNode (ITreeBuilder treeBuilder, object dataObject, NodeInfo nodeInfo)
{
ProjectFile file = (ProjectFile) dataObject;
nodeInfo.Label = GLib.Markup.EscapeText (file.Link.IsNullOrEmpty ? file.FilePath.FileName : file.Link.FileName);
if (!File.Exists (file.FilePath)) {
nodeInfo.Label = "<span foreground='red'>" + nodeInfo.Label + "</span>";
}
nodeInfo.Icon = DesktopService.GetIconForFile (file.FilePath, Gtk.IconSize.Menu);
if (file.IsLink && nodeInfo.Icon != null) {
var overlay = ImageService.GetIcon ("md-link-overlay").WithSize (Xwt.IconSize.Small);
var cached = Context.GetComposedIcon (nodeInfo.Icon, overlay);
if (cached != null)
nodeInfo.Icon = cached;
else {
var ib = new Xwt.Drawing.ImageBuilder (nodeInfo.Icon.Width, nodeInfo.Icon.Height);
ib.Context.DrawImage (nodeInfo.Icon, 0, 0);
ib.Context.DrawImage (overlay, 0, 0);
var res = ib.ToVectorImage ();
ib.Dispose ();
Context.CacheComposedIcon (nodeInfo.Icon, overlay, res);
nodeInfo.Icon = res;
}
}
}
public override object GetParentObject (object dataObject)
{
var file = (ProjectFile) dataObject;
var dir = !file.IsLink ? file.FilePath.ParentDirectory : file.Project.BaseDirectory.Combine (file.ProjectVirtualPath).ParentDirectory;
if (!string.IsNullOrEmpty (file.DependsOn)) {
ProjectFile groupUnder = file.Project.Files.GetFile (file.FilePath.ParentDirectory.Combine (file.DependsOn));
if (groupUnder != null)
return groupUnder;
}
if (dir == file.Project.BaseDirectory)
return file.Project;
return new ProjectFolder (dir, file.Project, null);
}
public override int CompareObjects (ITreeNavigator thisNode, ITreeNavigator otherNode)
{
if (otherNode.DataItem is ProjectFolder)
return 1;
if (!(thisNode.DataItem is ProjectFile))
return DefaultSort;
if (!(otherNode.DataItem is ProjectFile))
return DefaultSort;
string name1 = thisNode.NodeName;
string name2 = otherNode.NodeName;
//Compare filenames without extension
string path1 = Path.GetFileNameWithoutExtension (name1);
string path2 = Path.GetFileNameWithoutExtension (name2);
int cmp = string.Compare (path1, path2, StringComparison.CurrentCultureIgnoreCase);
if (cmp != 0)
return cmp;
//Compare extensions
string ext1 = Path.GetExtension (name1);
string ext2 = Path.GetExtension (name2);
return string.Compare (ext1, ext2, StringComparison.CurrentCultureIgnoreCase);
}
public override bool HasChildNodes (ITreeBuilder builder, object dataObject)
{
ProjectFile file = (ProjectFile) dataObject;
return file.HasChildren;
}
public override void BuildChildNodes (ITreeBuilder treeBuilder, object dataObject)
{
base.BuildChildNodes (treeBuilder, dataObject);
ProjectFile file = (ProjectFile) dataObject;
if (file.HasChildren)
foreach (ProjectFile pf in file.DependentChildren)
treeBuilder.AddChild (pf);
}
}
class ProjectFileNodeCommandHandler: NodeCommandHandler
{
public override void OnRenameStarting (ref int selectionStart, ref int selectionLength)
{
string name = CurrentNode.NodeName;
selectionStart = 0;
selectionLength = Path.GetFileNameWithoutExtension(name).Length;
}
public override void RenameItem (string newName)
{
ProjectFile newProjectFile = null;
var file = (ProjectFile) CurrentNode.DataItem;
FilePath newPath, newLink = FilePath.Null;
if (file.IsLink) {
var oldLink = file.ProjectVirtualPath;
newLink = oldLink.ParentDirectory.Combine (newName);
newPath = file.Project.BaseDirectory.Combine (newLink);
} else {
newPath = file.FilePath.ParentDirectory.Combine (newName);
}
try {
if (file.Project != null)
newProjectFile = file.Project.Files.GetFileWithVirtualPath (newPath.ToRelative (file.Project.BaseDirectory));
if (!FileService.IsValidPath (newPath)) {
MessageService.ShowWarning (GettextCatalog.GetString ("The name you have chosen contains illegal characters. Please choose a different name."));
} else if ((newProjectFile != null && newProjectFile != file) || File.Exists (file.FilePath.ParentDirectory.Combine (newName))) {
// If there is already a file under the newPath which is *different*, then throw an exception
MessageService.ShowWarning (GettextCatalog.GetString ("File or directory name is already in use. Please choose a different one."));
} else {
FileService.RenameFile (file.FilePath, newName);
if (file.Project != null)
IdeApp.ProjectOperations.Save (file.Project);
}
} catch (ArgumentException) { // new file name with wildcard (*, ?) characters in it
MessageService.ShowWarning (GettextCatalog.GetString ("The name you have chosen contains illegal characters. Please choose a different name."));
} catch (IOException ex) {
MessageService.ShowError (GettextCatalog.GetString ("There was an error renaming the file."), ex);
}
}
public override void ActivateItem ()
{
ProjectFile file = (ProjectFile) CurrentNode.DataItem;
IdeApp.Workbench.OpenDocument (file.FilePath, file.Project);
}
public override void ActivateMultipleItems ()
{
ProjectFile file;
for (int i = 0; i < CurrentNodes.Length; i++) {
// Only bring the last file to the front
file = (ProjectFile) CurrentNodes [i].DataItem;
IdeApp.Workbench.OpenDocument (file.FilePath, file.Project, i == CurrentNodes.Length - 1);
}
}
public override DragOperation CanDragNode ()
{
return DragOperation.Copy | DragOperation.Move;
}
public override bool CanDropNode (object dataObject, DragOperation operation)
{
var target = (ProjectFile) CurrentNode.DataItem;
var pf = dataObject as ProjectFile;
return pf != null && pf != target && !pf.HasChildren && target.DependsOn == null;
}
void Drop (ProjectFile pf, DragOperation operation, HashSet<SolutionEntityItem> projectsToSave)
{
var target = (ProjectFile) CurrentNode.DataItem;
var targetDirectory = target.FilePath.ParentDirectory;
// file dependencies only work if they are in the same physical folder
if (pf.FilePath.ParentDirectory != targetDirectory) {
var targetPath = targetDirectory.Combine (pf.FilePath.FileName);
// if copying to the same directory, make a copy with a different name
if (targetPath == pf.FilePath)
targetPath = ProjectOperations.GetTargetCopyName (targetPath, false);
if (File.Exists (targetPath))
if (!MessageService.Confirm (GettextCatalog.GetString ("The file '{0}' already exists. Do you want to overwrite it?", targetPath.FileName), AlertButton.OverwriteFile))
return;
// unlink the project file from its current parent
pf.DependsOn = null;
projectsToSave.Add (pf.Project);
bool move = operation == DragOperation.Move;
var opText = move ? GettextCatalog.GetString ("Moving file...") : GettextCatalog.GetString ("Copying file...");
using (var monitor = IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor (opText, Stock.StatusWorking, true))
IdeApp.ProjectOperations.TransferFiles (monitor, pf.Project, pf.FilePath, target.Project, targetPath, move, true);
pf = target.Project.Files.GetFile (targetPath);
}
// the dropped project file now depends on the file it was just dropped onto
pf.DependsOn = target.FilePath.FileName;
projectsToSave.Add (pf.Project);
}
public override void OnMultipleNodeDrop (object[] dataObjects, DragOperation operation)
{
var projectsToSave = new HashSet<SolutionEntityItem> ();
foreach (var dataObject in dataObjects)
Drop ((ProjectFile) dataObject, operation, projectsToSave);
IdeApp.ProjectOperations.Save (projectsToSave);
}
public override bool CanDeleteItem ()
{
return true;
}
[CommandHandler (EditCommands.Delete)]
[AllowMultiSelection]
public override void DeleteMultipleItems ()
{
var projects = new Set<SolutionEntityItem> ();
var files = new List<ProjectFile> ();
bool hasChildren = false;
foreach (var node in CurrentNodes) {
var pf = (ProjectFile) node.DataItem;
projects.Add (pf.Project);
if (pf.HasChildren)
hasChildren = true;
files.Add (pf);
}
var removeFromProject = new AlertButton (GettextCatalog.GetString ("_Remove from Project"), Gtk.Stock.Remove);
string question;
string secondaryText = null;
bool filesExist = CheckAnyFileExists (files);
if (filesExist) {
secondaryText = GettextCatalog.GetString ("The Delete option permanently removes the file from your hard disk. " +
"Click Remove from Project if you only want to remove it from your current solution.");
}
if (hasChildren) {
if (files.Count == 1)
question = GettextCatalog.GetString ("Are you sure you want to remove the file {0} and " +
"its code-behind children from project {1}?",
Path.GetFileName (files[0].Name), files[0].Project.Name);
else
question = GettextCatalog.GetString ("Are you sure you want to remove the selected files and " +
"their code-behind children from the project?");
} else {
if (files.Count == 1)
question = GettextCatalog.GetString ("Are you sure you want to remove file {0} from project {1}?",
Path.GetFileName (files[0].Name), files[0].Project.Name);
else
question = GettextCatalog.GetString ("Are you sure you want to remove the selected files from the project?");
}
var result = MessageService.AskQuestion (question, secondaryText, GetDeleteConfirmationButtons (filesExist, removeFromProject));
if (result != removeFromProject && result != AlertButton.Delete)
return;
foreach (var file in files) {
var project = file.Project;
var inFolder = project.Files.GetFilesInVirtualPath (file.ProjectVirtualPath.ParentDirectory).ToList ();
if (inFolder.Count == 1 && inFolder [0] == file && project.Files.GetFileWithVirtualPath (file.ProjectVirtualPath.ParentDirectory) == null) {
// This is the last project file in the folder. Make sure we keep
// a reference to the folder, so it is not deleted from the tree.
var folderFile = new ProjectFile (project.BaseDirectory.Combine (file.ProjectVirtualPath.ParentDirectory));
folderFile.Subtype = Subtype.Directory;
project.Files.Add (folderFile);
}
if (file.HasChildren) {
foreach (var child in file.DependentChildren.ToArray()) {
project.Files.Remove (child);
if (result == AlertButton.Delete)
FileService.DeleteFile (child.Name);
}
}
project.Files.Remove (file);
if (result == AlertButton.Delete && !file.IsLink)
FileService.DeleteFile (file.Name);
}
IdeApp.ProjectOperations.Save (projects);
}
static bool CheckAnyFileExists (IEnumerable<ProjectFile> files)
{
foreach (ProjectFile file in files) {
if (!file.IsLink && File.Exists (file.Name))
return true;
if (file.HasChildren) {
foreach (var child in file.DependentChildren.ToArray()) {
if (File.Exists (child.Name))
return true;
}
}
}
return false;
}
static AlertButton[] GetDeleteConfirmationButtons (bool includeDelete, AlertButton removeFromProject)
{
if (includeDelete)
return new [] { AlertButton.Delete, AlertButton.Cancel, removeFromProject };
return new [] { AlertButton.Cancel, removeFromProject };
}
[CommandUpdateHandler (EditCommands.Delete)]
public void UpdateRemoveItem (CommandInfo info)
{
//don't allow removing children from parents. The parent can be removed and will remove the whole group.
info.Enabled = CanDeleteMultipleItems ();
info.Text = GettextCatalog.GetString ("Remove");
}
[CommandHandler (ViewCommands.OpenWithList)]
public void OnOpenWith (object ob)
{
ProjectFile finfo = (ProjectFile) CurrentNode.DataItem;
((FileViewer)ob).OpenFile (finfo.Name);
}
[CommandUpdateHandler (ViewCommands.OpenWithList)]
public void OnOpenWithUpdate (CommandArrayInfo info)
{
var pf = (ProjectFile) CurrentNode.DataItem;
PopulateOpenWithViewers (info, pf.Project, pf.FilePath);
}
internal static void PopulateOpenWithViewers (CommandArrayInfo info, Project project, string filePath)
{
var viewers = DisplayBindingService.GetFileViewers (filePath, project).ToList ();
//show the default viewer first
var def = viewers.FirstOrDefault (v => v.CanUseAsDefault) ?? viewers.FirstOrDefault (v => v.IsExternal);
if (def != null) {
CommandInfo ci = info.Add (def.Title, def);
ci.Description = GettextCatalog.GetString ("Open with '{0}'", def.Title);
if (viewers.Count > 1)
info.AddSeparator ();
}
//then the builtins, followed by externals
FileViewer prev = null;
foreach (FileViewer fv in viewers) {
if (def != null && fv.Equals (def))
continue;
if (prev != null && fv.IsExternal != prev.IsExternal)
info.AddSeparator ();
CommandInfo ci = info.Add (fv.Title, fv);
ci.Description = GettextCatalog.GetString ("Open with '{0}'", fv.Title);
prev = fv;
}
}
[CommandHandler (FileCommands.SetBuildAction)]
[AllowMultiSelection]
public void OnSetBuildAction (object ob)
{
Set<SolutionEntityItem> projects = new Set<SolutionEntityItem> ();
string action = (string)ob;
foreach (ITreeNavigator node in CurrentNodes) {
ProjectFile file = (ProjectFile) node.DataItem;
file.BuildAction = action;
projects.Add (file.Project);
}
IdeApp.ProjectOperations.Save (projects);
}
[CommandUpdateHandler (FileCommands.SetBuildAction)]
public void OnSetBuildActionUpdate (CommandArrayInfo info)
{
var toggledActions = new Set<string> ();
Project proj = null;
foreach (var node in CurrentNodes) {
var finfo = (ProjectFile) node.DataItem;
//disallow multi-slect on more than one project, since available build actions may differ
if (proj == null && finfo.Project != null) {
proj = finfo.Project;
} else if (proj == null || proj != finfo.Project) {
info.Clear ();
return;
}
toggledActions.Add (finfo.BuildAction);
}
if (proj == null)
return;
foreach (string action in proj.GetBuildActions ()) {
if (action == "--") {
info.AddSeparator ();
} else {
CommandInfo ci = info.Add (action, action);
ci.Checked = toggledActions.Contains (action);
if (ci.Checked)
ci.CheckedInconsistent = toggledActions.Count > 1;
}
}
}
[CommandHandler (FileCommands.ShowProperties)]
[AllowMultiSelection]
public void OnShowProperties ()
{
IdeApp.Workbench.Pads.PropertyPad.BringToFront (true);
}
//NOTE: This command is slightly odd, as it operates on a tri-state value,
//when only being a dual-state control. However, it's straightforward enough.
// Enabled == (PreserveNewest | Always)
// Disabled == None
// Disabling == !None -> None
// Enabling == None -> PreserveNewest
//So there is no way to use.
[CommandHandler (FileCommands.CopyToOutputDirectory)]
[AllowMultiSelection]
public void OnCopyToOutputDirectory ()
{
//if all of the selection is already checked, then toggle checks them off
//else it turns them on. hence we need to find if they're all checked,
bool allChecked = true;
foreach (ITreeNavigator node in CurrentNodes) {
ProjectFile file = (ProjectFile) node.DataItem;
if (file.CopyToOutputDirectory == FileCopyMode.None) {
allChecked = false;
break;
}
}
Set<SolutionEntityItem> projects = new Set<SolutionEntityItem> ();
foreach (ITreeNavigator node in CurrentNodes) {
ProjectFile file = (ProjectFile) node.DataItem;
projects.Add (file.Project);
if (allChecked) {
file.CopyToOutputDirectory = FileCopyMode.None;
} else {
file.CopyToOutputDirectory = FileCopyMode.PreserveNewest;
}
}
IdeApp.ProjectOperations.Save (projects);
}
[CommandUpdateHandler (FileCommands.CopyToOutputDirectory)]
public void OnCopyToOutputDirectoryUpdate (CommandInfo info)
{
foreach (ITreeNavigator node in CurrentNodes) {
ProjectFile file = (ProjectFile) node.DataItem;
if (file.CopyToOutputDirectory != FileCopyMode.None) {
info.Checked = true;
} else if (info.Checked) {
info.CheckedInconsistent = true;
}
}
}
}
}
|