~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Pads.ProjectPad/FolderNodeBuilder.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
namespace MonoDevelop.Ide.Gui.Pads.ProjectPad
52
52
{
53
 
        public abstract class FolderNodeBuilder: TypeNodeBuilder
 
53
        abstract class FolderNodeBuilder: TypeNodeBuilder
54
54
        {
55
55
                public override void GetNodeAttributes (ITreeNavigator treeNavigator, object dataObject, ref NodeAttributes attributes)
56
56
                {
136
136
                }
137
137
        }
138
138
        
139
 
        public abstract class FolderCommandHandler: NodeCommandHandler
 
139
        abstract class FolderCommandHandler: NodeCommandHandler
140
140
        {
 
141
                // CommandHandlers are constantly re-created so it's not possible to cache data inside the instance
 
142
                // Since 'AddExistingFolder' can only be run from the UI thread anyway we can safely just make this static.
 
143
                static FilePath PreviousFolderPath {
 
144
                        get; set;
 
145
                }
 
146
 
141
147
                public abstract string GetFolderPath (object dataObject);
142
148
 
143
149
                public override bool CanDropNode (object dataObject, DragOperation operation)
144
150
                {
145
 
                        string targetPath = GetFolderPath (CurrentNode.DataItem);
 
151
                        string targetDirectory = GetFolderPath (CurrentNode.DataItem);
146
152
                        
147
153
                        if (dataObject is ProjectFile) {
148
154
                                ProjectFile file = (ProjectFile) dataObject;
149
155
                                var srcDir = (file.Project != null && file.IsLink)
150
156
                                        ? file.Project.BaseDirectory.Combine (file.ProjectVirtualPath)
151
157
                                        : file.FilePath.ParentDirectory;
152
 
                                
153
 
                                return (srcDir != targetPath || operation == DragOperation.Copy) && file.DependsOnFile == null;
 
158
 
 
159
                                switch (operation) {
 
160
                                case DragOperation.Move:
 
161
                                        // allow grouped files to be unlinked from their parent
 
162
                                        return srcDir != targetDirectory || file.DependsOnFile != null;
 
163
                                case DragOperation.Copy:
 
164
                                        return true;
 
165
                                default:
 
166
                                        return false;
 
167
                                }
154
168
                        }
155
169
                        else if (dataObject is ProjectFolder) {
156
 
                                return ((ProjectFolder)dataObject).Path != targetPath || operation == DragOperation.Copy;
 
170
                                return ((ProjectFolder)dataObject).Path != targetDirectory || operation == DragOperation.Copy;
157
171
                        }
158
172
                        else if (dataObject is Gtk.SelectionData) {
159
173
                                SelectionData data = (SelectionData) dataObject;
165
179
                
166
180
                public override void OnMultipleNodeDrop (object[] dataObjects, DragOperation operation)
167
181
                {
168
 
                        Set<SolutionEntityItem> toSave = new Set<SolutionEntityItem> ();
 
182
                        var projectsToSave = new HashSet<SolutionEntityItem> ();
 
183
                        var groupedFiles = new HashSet<ProjectFile> ();
 
184
 
 
185
                        foreach (var pf in dataObjects.OfType<ProjectFile> ()) {
 
186
                                foreach (var child in pf.DependentChildren)
 
187
                                        groupedFiles.Add (child);
 
188
                        }
 
189
 
169
190
                        foreach (object dataObject in dataObjects)
170
 
                                DropNode (toSave, dataObject, operation);
171
 
                        IdeApp.ProjectOperations.Save (toSave);
 
191
                                DropNode (projectsToSave, dataObject, groupedFiles, operation);
 
192
 
 
193
                        IdeApp.ProjectOperations.Save (projectsToSave);
172
194
                }
173
195
                
174
 
                void DropNode (Set<SolutionEntityItem> projectsToSave, object dataObject, DragOperation operation)
 
196
                void DropNode (HashSet<SolutionEntityItem> projectsToSave, object dataObject, HashSet<ProjectFile> groupedFiles, DragOperation operation)
175
197
                {
176
 
                        FilePath targetPath = GetFolderPath (CurrentNode.DataItem);
 
198
                        FilePath targetDirectory = GetFolderPath (CurrentNode.DataItem);
177
199
                        FilePath source;
178
200
                        string what;
179
201
                        Project targetProject = (Project) CurrentNode.GetParentDataItem (typeof(Project), true);
180
202
                        Project sourceProject;
181
 
                        System.Collections.Generic.IEnumerable<ProjectFile> groupedChildren = null;
 
203
                        IEnumerable<ProjectFile> groupedChildren = null;
182
204
                        
183
205
                        if (dataObject is ProjectFolder) {
184
206
                                source = ((ProjectFolder) dataObject).Path;
187
209
                        }
188
210
                        else if (dataObject is ProjectFile) {
189
211
                                ProjectFile file = (ProjectFile) dataObject;
 
212
 
 
213
                                // if this ProjectFile is one of the grouped files being pulled in by a parent being copied/moved, ignore it
 
214
                                if (groupedFiles.Contains (file))
 
215
                                        return;
 
216
 
 
217
                                if (file.DependsOnFile != null && operation == DragOperation.Move) {
 
218
                                        // unlink this file from its parent (since its parent is not being moved)
 
219
                                        file.DependsOn = null;
 
220
 
 
221
                                        // if moving a linked file into its containing folder, simply unlink it from its parent
 
222
                                        if (file.FilePath.ParentDirectory == targetDirectory) {
 
223
                                                projectsToSave.Add (targetProject);
 
224
                                                return;
 
225
                                        }
 
226
                                }
 
227
 
190
228
                                sourceProject = file.Project;
191
229
                                if (sourceProject != null && file.IsLink) {
192
230
                                        source = sourceProject.BaseDirectory.Combine (file.ProjectVirtualPath);
201
239
                                if (data.Type != "text/uri-list")
202
240
                                        return;
203
241
                                string sources = System.Text.Encoding.UTF8.GetString (data.Data);
 
242
                                Console.WriteLine ("text/uri-list:\n{0}", sources);
204
243
                                string[] files = sources.Split (new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
205
244
                                for (int n=0; n<files.Length; n++) {
206
245
                                        Uri uri = new Uri (files[n]);
211
250
                                        files[n] = uri.LocalPath;
212
251
                                }
213
252
                                
214
 
                                IdeApp.ProjectOperations.AddFilesToProject (targetProject, files, targetPath);
 
253
                                IdeApp.ProjectOperations.AddFilesToProject (targetProject, files, targetDirectory);
215
254
                                projectsToSave.Add (targetProject);
216
255
                                return;
217
256
                        }
218
257
                        else
219
258
                                return;
220
259
 
221
 
                        targetPath = targetPath.Combine (source.FileName);
 
260
                        var targetPath = targetDirectory.Combine (source.FileName);
222
261
                        // If copying to the same directory, make a copy with a different name
223
262
                        if (targetPath == source)
224
263
                                targetPath = ProjectOperations.GetTargetCopyName (targetPath, dataObject is ProjectFolder);
225
 
                        
 
264
 
 
265
                        var targetChildPaths = groupedChildren != null ? groupedChildren.Select (child => {
 
266
                                var targetChildPath = targetDirectory.Combine (child.FilePath.FileName);
 
267
 
 
268
                                if (targetChildPath == child.FilePath)
 
269
                                        targetChildPath = ProjectOperations.GetTargetCopyName (targetChildPath, false);
 
270
 
 
271
                                return targetChildPath;
 
272
                        }).ToList () : null;
 
273
 
226
274
                        if (dataObject is ProjectFolder) {
227
275
                                string q;
228
276
                                if (operation == DragOperation.Move) {
229
277
                                        if (targetPath.ParentDirectory == targetProject.BaseDirectory)
230
278
                                                q = GettextCatalog.GetString ("Do you really want to move the folder '{0}' to the root folder of project '{1}'?", what, targetProject.Name);
231
279
                                        else
232
 
                                                q = GettextCatalog.GetString ("Do you really want to move the folder '{0}' to the folder '{1}'?", what, targetPath.ParentDirectory.FileName);
 
280
                                                q = GettextCatalog.GetString ("Do you really want to move the folder '{0}' to the folder '{1}'?", what, targetDirectory.FileName);
233
281
                                        if (!MessageService.Confirm (q, AlertButton.Move))
234
282
                                                return;
235
283
                                }
237
285
                                        if (targetPath.ParentDirectory == targetProject.BaseDirectory)
238
286
                                                q = GettextCatalog.GetString ("Do you really want to copy the folder '{0}' to the root folder of project '{1}'?", what, targetProject.Name);
239
287
                                        else
240
 
                                                q = GettextCatalog.GetString ("Do you really want to copy the folder '{0}' to the folder '{1}'?", what, targetPath.ParentDirectory.FileName);
 
288
                                                q = GettextCatalog.GetString ("Do you really want to copy the folder '{0}' to the folder '{1}'?", what, targetDirectory.FileName);
241
289
                                        if (!MessageService.Confirm (q, AlertButton.Copy))
242
290
                                                return;
243
291
                                }
244
292
                        } else if (dataObject is ProjectFile) {
245
 
                                if (File.Exists (targetPath))
246
 
                                        if (!MessageService.Confirm (GettextCatalog.GetString ("The file '{0}' already exists. Do you want to overwrite it?", targetPath.FileName), AlertButton.OverwriteFile))
247
 
                                                return;
 
293
                                foreach (var file in new FilePath[] { targetPath }.Concat (targetChildPaths)) {
 
294
                                        if (File.Exists (file))
 
295
                                                if (!MessageService.Confirm (GettextCatalog.GetString ("The file '{0}' already exists. Do you want to overwrite it?", file.FileName), AlertButton.OverwriteFile))
 
296
                                                        return;
 
297
                                }
248
298
                        }
249
299
                        
250
 
                        ArrayList filesToSave = new ArrayList ();
 
300
                        var filesToSave = new List<Document> ();
251
301
                        foreach (Document doc in IdeApp.Workbench.Documents) {
252
302
                                if (doc.IsDirty && doc.IsFile) {
253
303
                                        if (doc.Name == source || doc.Name.StartsWith (source + Path.DirectorySeparatorChar)) {
284
334
                                AlertButton res = MessageService.AskQuestion (question, AlertButton.Cancel, noSave, AlertButton.Save);
285
335
                                if (res == AlertButton.Cancel)
286
336
                                        return;
287
 
                                else if (res == AlertButton.Save) { 
 
337
                                if (res == AlertButton.Save) {
288
338
                                        try {
289
339
                                                foreach (Document doc in filesToSave) {
290
340
                                                        doc.Save ();
300
350
                                projectsToSave.Add (sourceProject);
301
351
                        if (targetProject != null)
302
352
                                projectsToSave.Add (targetProject);
 
353
 
 
354
                        bool move = operation == DragOperation.Move;
 
355
                        var opText = move ? GettextCatalog.GetString ("Moving files...") : GettextCatalog.GetString ("Copying files...");
303
356
                        
304
 
                        using (IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor (GettextCatalog.GetString("Copying files..."), MonoDevelop.Ide.Gui.Stock.CopyIcon, true))
305
 
                        {
 
357
                        using (var monitor = IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor (opText, Stock.StatusSolutionOperation, true)) {
306
358
                                // If we drag and drop a node in the treeview corresponding to a directory, do not move
307
359
                                // the entire directory. We should only move the files which exist in the project. Otherwise
308
360
                                // we will need a lot of hacks all over the code to prevent us from incorrectly moving version
309
361
                                // control related files such as .svn directories
310
 
                                bool move = operation == DragOperation.Move;
 
362
 
 
363
                                // Note: if we are transferring a ProjectFile, this will copy/move the ProjectFile's DependentChildren as well.
311
364
                                IdeApp.ProjectOperations.TransferFiles (monitor, sourceProject, source, targetProject, targetPath, move, true);
312
365
                        }
313
366
                }
316
369
                public void AddFilesToProject()
317
370
                {
318
371
                        Project project = (Project) CurrentNode.GetParentDataItem (typeof(Project), true);
319
 
                        
 
372
                        var targetRoot = ((FilePath) GetFolderPath (CurrentNode.DataItem)).CanonicalPath;
 
373
 
320
374
                        AddFileDialog fdiag  = new AddFileDialog (GettextCatalog.GetString ("Add files"));
321
 
                        fdiag.CurrentFolder = GetFolderPath (CurrentNode.DataItem);
 
375
                        fdiag.CurrentFolder = !PreviousFolderPath.IsNullOrEmpty ? PreviousFolderPath : targetRoot;
322
376
                        fdiag.SelectMultiple = true;
323
377
                        fdiag.TransientFor = IdeApp.Workbench.RootWindow;
324
378
                        fdiag.BuildActions = project.GetBuildActions ();        
327
381
                        
328
382
                        if (!fdiag.Run ())
329
383
                                return;
330
 
                        
 
384
                        PreviousFolderPath = fdiag.SelectedFiles.Select (f => f.FullPath.ParentDirectory).FirstOrDefault ();
 
385
 
331
386
                        var files = fdiag.SelectedFiles;
332
387
                        overrideAction = fdiag.OverrideAction;
333
388
                        
364
419
                        var targetRoot = ((FilePath) GetFolderPath (CurrentNode.DataItem)).CanonicalPath;
365
420
                        
366
421
                        var ofdlg = new SelectFolderDialog (GettextCatalog.GetString ("Import From Folder")) {
367
 
                                CurrentFolder = targetRoot
 
422
                                CurrentFolder = !PreviousFolderPath.IsNullOrEmpty ? PreviousFolderPath : targetRoot
368
423
                        };
369
424
                        if(!ofdlg.Run ())
370
425
                                return;
371
 
                        
 
426
                        PreviousFolderPath = ofdlg.SelectedFile.CanonicalPath;
 
427
                        if (!PreviousFolderPath.ParentDirectory.IsNullOrEmpty)
 
428
                                PreviousFolderPath = PreviousFolderPath.ParentDirectory;
 
429
 
372
430
                        var srcRoot = ofdlg.SelectedFile.CanonicalPath;
373
431
                        var foundFiles = Directory.GetFiles (srcRoot, "*", SearchOption.AllDirectories);
374
432
                        
384
442
                        if (added)
385
443
                                IdeApp.ProjectOperations.Save (project);
386
444
                }
387
 
                
 
445
 
388
446
                ///<summary>Adds an existing folder to the current folder</summary>
389
447
                [CommandHandler (ProjectCommands.AddExistingFolder)]
390
448
                public void AddExistingFolder ()
393
451
                        var selectedFolder = ((FilePath) GetFolderPath (CurrentNode.DataItem)).CanonicalPath;
394
452
                        
395
453
                        var ofdlg = new SelectFolderDialog (GettextCatalog.GetString ("Add Existing Folder")) {
396
 
                                CurrentFolder = selectedFolder
 
454
                                CurrentFolder = !PreviousFolderPath.IsNullOrEmpty ? PreviousFolderPath : selectedFolder 
397
455
                        };
398
456
                        if(!ofdlg.Run ())
399
457
                                return;
400
 
                        
 
458
 
 
459
                        // We store the parent directory of the folder the user chooses as they will not need to add the same
 
460
                        // directory twice. We can save them navigating up one directory by doing it for them
 
461
                        PreviousFolderPath = ofdlg.SelectedFile.CanonicalPath;
 
462
                        if (!PreviousFolderPath.ParentDirectory.IsNullOrEmpty)
 
463
                                PreviousFolderPath = PreviousFolderPath.ParentDirectory;
 
464
 
401
465
                        var srcRoot = ofdlg.SelectedFile.CanonicalPath;
402
466
                        var targetRoot = selectedFolder.Combine (srcRoot.FileName);
403
467