forked from lstratman/EasyConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookmarksWindow.cs
1836 lines (1552 loc) · 77.3 KB
/
BookmarksWindow.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using EasyConnect.Protocols;
using EasyTabs;
using System.Windows.Forms.VisualStyles;
using EasyConnect.Properties;
using System.Configuration;
using System.Threading.Tasks;
using EasyConnect.Common;
namespace EasyConnect
{
/// <summary>
/// Bookmarks window that displays a split pane with a tree view containing bookmarks folders on the left and a list of bookmarks and immediate child
/// folders of the currently selected folder on the right.
/// </summary>
public partial class BookmarksWindow : Form
{
/// <summary>
/// Main application instance that this window is associated with, which is used to call back into application functionality.
/// </summary>
protected MainForm _applicationForm = null;
/// <summary>
/// Dictionary containing the indexes in <see cref="_listViewImageList"/> of the icons for each connection protocol.
/// </summary>
protected Dictionary<Type, int> _connectionTypeIcons = new Dictionary<Type, int>();
/// <summary>
/// When right clicking on a tree node in <see cref="FoldersTreeView"/> or a list item in <see cref="_bookmarksListView"/>, this contains the item that
/// was clicked on.
/// </summary>
protected object _contextMenuItem = null;
/// <summary>
/// If the user has copied bookmarks/folders by using Ctrl+C or the context menu, this contains the list of items selected at that time; it can contain
/// bookmarks, folders, or both.
/// </summary>
protected List<object> _copiedItems = new List<object>();
/// <summary>
/// If the user has cut bookmarks/folders by using Ctrl+X or the context menu, this contains the list of items selected at that time; it can contain
/// bookmarks, folders, or both.
/// </summary>
protected List<object> _cutItems = new List<object>();
/// <summary>
/// Flag that can be set during reorganization of the bookmarks tree view or list view that prevents the items from being automatically sorted and
/// causing excessive redraws; used during batch insert/removal operations.
/// </summary>
protected bool _deferSort = false;
/// <summary>
/// Flag set indicating that the user is dragging items from the right-hand list view.
/// </summary>
protected bool _draggingFromListView = false;
/// <summary>
/// Flag set indicating that the user is dragging items from the left-hand tree view.
/// </summary>
protected bool _draggingFromTree = false;
/// <summary>
/// Lookup that provides the <see cref="BookmarksFolder"/> instance for each <see cref="TreeNode"/> in <see cref="_bookmarksFoldersTreeView"/>.
/// </summary>
protected Dictionary<TreeNode, BookmarksFolder> _folderTreeNodes = new Dictionary<TreeNode, BookmarksFolder>();
/// <summary>
/// Lookup that provides the <see cref="IConnection"/> instance for each <see cref="ListViewItem"/> in <see cref="_bookmarksListView"/> that's a
/// bookmark as opposed to a folder.
/// </summary>
protected Dictionary<ListViewItem, IConnection> _listViewConnections = new Dictionary<ListViewItem, IConnection>();
/// <summary>
/// If the user is dragging to a location in <see cref="_bookmarksListView"/>, this is the target that they are dropping on.
/// </summary>
protected ListViewItem _listViewDropTarget = null;
/// <summary>
/// Lookup that provides the <see cref="BookmarksFolder"/> instance for each <see cref="ListViewItem"/> in <see cref="_bookmarksListView"/> that's a
/// folder as opposed to a bookmark.
/// </summary>
protected Dictionary<ListViewItem, BookmarksFolder> _listViewFolders = new Dictionary<ListViewItem, BookmarksFolder>();
/// <summary>
/// Flag indicating whether <see cref="_contextMenuItem"/> should be set automatically in <see cref="_folderContextMenu_Opening"/> to the currently
/// open folder in <see cref="FoldersTreeView"/>.
/// </summary>
protected bool _setContextMenuItem = true;
/// <summary>
/// Flag set to indicate that we should display the options for a <see cref="IConnection"/> instance in <see cref="_bookmarksListView"/> when they
/// finish editing the item's label; this is done when a new bookmark is created as we first ask the user to name the bookmark and, when that's
/// complete, allow the user to edit the options for the connection.
/// </summary>
protected bool _showOptionsAfterItemLabelEdit = false;
/// <summary>
/// If the user is dragging to a location in <see cref="_bookmarksFoldersTreeView"/>, this is the target that they are dropping on.
/// </summary>
protected TreeNode _treeViewDropTarget = null;
protected ListViewItem _itemEditingNotes = null;
protected bool _listViewNotesDoubleClickStarted = false;
protected Timer _listViewNotesDoubleClickTimer = new Timer();
protected TextBox _notesTextBox = null;
protected HtmlPanel _urlPanel = null;
/// <summary>
/// Constructor; deserializes the bookmarks folder structure, adds the various folder nodes to <see cref="_bookmarksFoldersTreeView"/>, and gets the
/// icons for each protocol.
/// </summary>
/// <param name="applicationForm">Main application instance.</param>
public BookmarksWindow(MainForm applicationForm)
{
InitializeComponent();
_toolsMenu.Renderer = new EasyConnectToolStripRender();
_listViewNotesDoubleClickTimer.Tick += _listViewNotesDoubleClickTimer_Tick;
_listViewNotesDoubleClickTimer.Interval = SystemInformation.DoubleClickTime;
_applicationForm = applicationForm;
_bookmarksFoldersTreeView.Sorted = true;
_bookmarksListView.ListViewItemSorter = new BookmarksListViewComparer();
// Set the handler methods for changing the bookmarks or child folders; these are responsible for updating the tree view and list view UI when
// items are added or removed from the bookmarks or child folders collections
Bookmarks.Instance.RootFolder.Bookmarks.CollectionModified += Bookmarks_CollectionModified;
Bookmarks.Instance.RootFolder.ChildFolders.CollectionModified += ChildFolders_CollectionModified;
_folderTreeNodes[_bookmarksFoldersTreeView.Nodes[0]] = Bookmarks.Instance.RootFolder;
// Call Bookmarks_CollectionModified and ChildFolders_CollectionModified recursively through the folder structure to "simulate" bookmarks and
// folders being added to the collection so that the initial UI state for the tree view can be created
InitializeTreeView(Bookmarks.Instance.RootFolder);
_bookmarksFoldersTreeView.Nodes[0].Expand();
foreach (IProtocol protocol in ConnectionFactory.GetProtocols())
{
// Get the icon for each protocol type and add an entry for it to the "Add bookmark" menu item
Icon icon = new Icon(protocol.ProtocolIcon, 16, 16);
_listViewImageList.Images.Add(icon);
_connectionTypeIcons[protocol.ConnectionType] = _listViewImageList.Images.Count - 1;
IProtocol currentProtocol = protocol;
ToolStripMenuItem protocolMenuItem = new ToolStripMenuItem(
protocol.ProtocolTitle, null, (sender, args) => _addBookmarkMenuItem_Click(currentProtocol));
_addBookmarkMenuItem.DropDownItems.Add(protocolMenuItem);
}
_bookmarkContextMenu.Renderer = new EasyConnectToolStripRender();
_folderContextMenu.Renderer = new EasyConnectToolStripRender();
_iconPictureBox.Image = new Icon(Icon, 16, 16).ToBitmap();
_urlPanel = new HtmlPanel
{
AutoScroll = false,
Width = _urlPanelContainer.Width,
Height = _urlPanelContainer.Height,
Left = 0,
Top = 1,
Font = urlTextBox.Font,
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top
};
_urlPanelContainer.Controls.Add(_urlPanel);
_urlPanel.Text = String.Format(
@"<span style=""background-color: #FFFFFF; font-family: {2}; font-size: {1}pt; height: {0}px; color: #9999BF"">easyconnect://<font color=""black"">bookmarks</font></span>",
_urlPanel.Height, urlTextBox.Font.SizeInPoints, urlTextBox.Font.FontFamily.GetName(0));
#if APPX
_updatesMenuItem.Visible = false;
_toolsMenuSeparator2.Visible = false;
#else
_updatesMenuItem.Visible = ConfigurationManager.AppSettings["checkForUpdates"] != "false";
_toolsMenuSeparator2.Visible = ConfigurationManager.AppSettings["checkForUpdates"] != "false";
#endif
}
private void _listViewNotesDoubleClickTimer_Tick(object sender, EventArgs e)
{
_listViewNotesDoubleClickTimer.Stop();
_listViewNotesDoubleClickStarted = false;
}
/// <summary>
/// Main application instance that this window is associated with, which is used to call back into application functionality.
/// </summary>
protected MainForm ParentTabs
{
get
{
return (MainForm) Parent;
}
}
/// <summary>
/// Lookup that provides the <see cref="BookmarksFolder"/> instance for each <see cref="TreeNode"/> in <see cref="_bookmarksFoldersTreeView"/>.
/// </summary>
public Dictionary<TreeNode, BookmarksFolder> TreeNodeFolders
{
get
{
return _folderTreeNodes;
}
}
/// <summary>
/// The tree view containing the folder structure for the bookmarks.
/// </summary>
public TreeView FoldersTreeView
{
get
{
return _bookmarksFoldersTreeView;
}
}
/// <summary>
/// Recursive method to initialize the UI for <see cref="_bookmarksFoldersTreeView"/> by adding each <see cref="BookmarksFolder"/> to it and populate
/// <see cref="_folderTreeNodes"/>.
/// </summary>
/// <param name="currentFolder">Current folder being processed.</param>
protected void InitializeTreeView(BookmarksFolder currentFolder)
{
// Simulate adding all of the bookmarks in the folder to the bookmarks collection
if (currentFolder.Bookmarks != null && currentFolder.Bookmarks.Count > 0)
{
currentFolder.Bookmarks.ForEach(b => b.ParentFolder = currentFolder);
Bookmarks_CollectionModified(
currentFolder.Bookmarks, new ListModificationEventArgs(ListModification.RangeAdded, 0, currentFolder.Bookmarks.Count));
}
// Simulate adding each child folder to the folders collection
if (currentFolder.ChildFolders != null && currentFolder.ChildFolders.Count > 0)
{
currentFolder.ChildFolders.ForEach(f => f.ParentFolder = currentFolder);
ChildFolders_CollectionModified(
currentFolder.ChildFolders, new ListModificationEventArgs(ListModification.RangeAdded, 0, currentFolder.ChildFolders.Count));
// Call this recursively for each child folder
foreach (BookmarksFolder childFolder in currentFolder.ChildFolders)
InitializeTreeView(childFolder);
}
}
/// <summary>
/// Processor for the use of shortcut keys, which are currently Ctrl+C for copying bookmarks and folders, Ctrl+X for cutting them, Ctrl+V for
/// pasting them into a destination folder, and Delete for deleting bookmarks and folders.
/// </summary>
/// <param name="msg">Message received by the window's callback method.</param>
/// <param name="keyData">Keys that the user has pressed.</param>
/// <returns>True if we processed the command, base.<see cref="ProcessCmdKey"/> otherwise.</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.C))
{
// If the user has the tree view focused currently, then copy the currently selected folder in the tree
if (_bookmarksFoldersTreeView.Focused && _bookmarksFoldersTreeView.SelectedNode != null)
{
_contextMenuItem = _folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode];
_copyFolderMenuItem_Click(null, null);
return true;
}
// Otherwise, copy all of the selected items in the list view
else if (_bookmarksListView.Focused && _bookmarksListView.SelectedItems.Count > 0)
{
copyToolStripMenuItem_Click(null, null);
return true;
}
}
else if (keyData == (Keys.Control | Keys.V))
{
// Paste whatever is in the list of cut or copied objects into the currently selected folder in the tree
if (_bookmarksFoldersTreeView.SelectedNode != null)
{
_contextMenuItem = _folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode];
_pasteFolderMenuItem_Click(null, null);
return true;
}
}
else if (keyData == (Keys.Control | Keys.X))
{
// If the user has the tree view focused currently, then cut the currently selected folder in the tree
if (_bookmarksFoldersTreeView.Focused && _bookmarksFoldersTreeView.SelectedNode != null)
{
_contextMenuItem = _folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode];
_cutFolderMenuItem_Click(null, null);
return true;
}
// Otherwise, cut all of the selected items in the list view
else if (_bookmarksListView.Focused && _bookmarksListView.SelectedItems.Count > 0)
{
_cutBookmarkMenuItem_Click(null, null);
return true;
}
}
if (keyData == Keys.Delete)
{
// If the user has the tree view focused currently, then delete the currently selected folder in the tree
if (_bookmarksFoldersTreeView.Focused && _bookmarksFoldersTreeView.SelectedNode != null)
{
_contextMenuItem = _folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode];
_deleteFolderMenuItem_Click(null, null);
return true;
}
// Otherwise, delete all of the selected items in the list view
else if (_bookmarksListView.Focused && _bookmarksListView.SelectedItems.Count > 0)
{
deleteToolStripMenuItem1_Click(null, null);
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// When a child folder is added to/removed from <see cref="BookmarksFolder.ChildFolders"/>, we add that folder to the tree view, update
/// <see cref="IConnection.ParentFolder"/> for each connection in the folder, and add the folder to <see cref="_folderTreeNodes"/> if necessary.
/// </summary>
/// <param name="sender"><see cref="BookmarksFolder.ChildFolders"/> instance that's being modified.</param>
/// <param name="e">Range specifier indicating which items were added to/removed from the collection.</param>
private void ChildFolders_CollectionModified(object sender, ListModificationEventArgs e)
{
ListWithEvents<BookmarksFolder> childFolders = sender as ListWithEvents<BookmarksFolder>;
bool sortListView = false;
// If items are being added to the list of child folders
if (e.Modification == ListModification.ItemModified || e.Modification == ListModification.ItemAdded || e.Modification == ListModification.RangeAdded)
{
for (int i = e.StartIndex; i < e.StartIndex + e.Count; i++)
{
// Get the tree nodes in the tree view for the parent (which will always exist) and the child (which will exist if the folder is not
// newly-created and is instead being moved from another folder)
BookmarksFolder currentFolder = childFolders[i];
TreeNode parentTreeNode = _folderTreeNodes.Single(kvp => kvp.Value == currentFolder.ParentFolder).Key;
TreeNode folderTreeNode = _folderTreeNodes.SingleOrDefault(kvp => kvp.Value == currentFolder).Key;
// If we don't have a tree node for this child (it's newly created)
if (folderTreeNode == null)
{
// Create a new TreeNode for this child and add it to the parent
folderTreeNode = new TreeNode(currentFolder.Name);
parentTreeNode.Nodes.Add(folderTreeNode);
// Add this tree node to the lookup
_folderTreeNodes[folderTreeNode] = currentFolder;
// Assign the child folders/bookmarks collection modification event handlers
currentFolder.ChildFolders.CollectionModified += ChildFolders_CollectionModified;
currentFolder.Bookmarks.CollectionModified += Bookmarks_CollectionModified;
// Call this method recursively for the child folders under this child folder; this is necessary when we're first setting up the UI
// from InitializeTreeView to ensure that all the child folders are added to the tree view and all the list modification event handlers
// are assigned properly
ChildFolders_CollectionModified(
currentFolder.ChildFolders, new ListModificationEventArgs(ListModification.RangeAdded, 0, currentFolder.ChildFolders.Count));
}
// If it's just being moved from another location, simply update its parent
else
{
folderTreeNode.Parent.Nodes.Remove(folderTreeNode);
parentTreeNode.Nodes.Add(folderTreeNode);
}
// If this node in the tree view is currently focused, add the child folder to the list view and set the flag indicating that we should
// sort the list view
if (_bookmarksFoldersTreeView.SelectedNode == parentTreeNode)
{
ListViewItem newItem = new ListViewItem(currentFolder.Name, 0);
newItem.SubItems.Add("");
newItem.SubItems.Add(currentFolder.Notes);
_bookmarksListView.Items.Add(newItem);
_listViewFolders[newItem] = currentFolder;
sortListView = true;
}
}
}
// Otherwise, items are being deleted from the list of child folders
else
{
KeyValuePair<TreeNode, BookmarksFolder> containerFolder = _folderTreeNodes.SingleOrDefault(kvp => kvp.Value.ChildFolders == childFolders);
if (containerFolder.Key != null)
{
// Responding to delete requests is a little confusing since the items have already been removed from the collection, so we look at each
// child tree node in the parent tree view folder and see which ones correspond to folders that are no longer in the BookmarksFolder's
// ChildFolders collection
for (int i = 0; i < containerFolder.Key.Nodes.Count; i++)
{
TreeNode treeNode = containerFolder.Key.Nodes[i];
// We can't find this folder corresponding to this child tree node in the parent's ChildFolders collection so we know it was deleted
if (!containerFolder.Value.ChildFolders.Contains(_folderTreeNodes[treeNode]))
{
KeyValuePair<ListViewItem, BookmarksFolder> listViewItem =
_listViewFolders.SingleOrDefault(kvp => kvp.Value == _folderTreeNodes[treeNode]);
// If the folder being removed from the collection is in the list view, remove it from there and set the sort flag
if (listViewItem.Key != null)
{
_bookmarksListView.Items.Remove(listViewItem.Key);
_listViewFolders.Remove(listViewItem.Key);
sortListView = true;
}
// Remove this node and all of its children from the tree view
RemoveAllFolders(treeNode);
containerFolder.Key.Nodes.Remove(treeNode);
i--;
}
}
}
}
if (IsHandleCreated && !_deferSort)
{
// Sort the tree view and, if necessary, the list view
_bookmarksFoldersTreeView.BeginInvoke(new Action(SortTreeView));
if (sortListView)
_bookmarksListView.BeginInvoke(new Action(_bookmarksListView.Sort));
}
}
/// <summary>
/// When a bookmark is added to/removed from <see cref="BookmarksFolder.Bookmarks"/>, we update <see cref="_bookmarksListView"/> if the parent folder
/// is currently selected.
/// </summary>
/// <param name="sender"><see cref="BookmarksFolder.Bookmarks"/> collection that's being modified.</param>
/// <param name="e">Range specifier indicating which items were added to/removed from the collection..</param>
private void Bookmarks_CollectionModified(object sender, ListModificationEventArgs e)
{
ListWithEvents<IConnection> bookmarks = sender as ListWithEvents<IConnection>;
bool sortListView = false;
// Bookmarks are being added to the collection
if (e.Modification == ListModification.ItemModified || e.Modification == ListModification.ItemAdded || e.Modification == ListModification.RangeAdded)
{
for (int i = e.StartIndex; i < e.StartIndex + e.Count; i++)
{
IConnection currentBookmark = bookmarks[i];
TreeNode parentTreeNode = _folderTreeNodes.Single(kvp => kvp.Value == currentBookmark.ParentFolder).Key;
// If the parent folder is currently selected in the tree view, update the list view to add the items and set the flag to sort the list
// view
if (_bookmarksFoldersTreeView.SelectedNode == parentTreeNode)
{
ListViewItem newItem = new ListViewItem(currentBookmark.DisplayName, _connectionTypeIcons[currentBookmark.GetType()]);
newItem.SubItems.Add(currentBookmark.Host);
newItem.SubItems.Add(currentBookmark.Notes);
_listViewConnections[newItem] = currentBookmark;
_bookmarksListView.Items.Add(newItem);
sortListView = true;
}
}
}
// Otherwise, bookmarks are being removed from the collection
else
{
KeyValuePair<TreeNode, BookmarksFolder> containerFolder = _folderTreeNodes.SingleOrDefault(kvp => kvp.Value.Bookmarks == bookmarks);
if (containerFolder.Key != null && containerFolder.Key == _bookmarksFoldersTreeView.SelectedNode)
{
// Responding to delete requests is a little confusing since the items have already been removed from the collection, so we look at each
// list view item in the list view and see which ones correspond to bookmarks that are no longer in the BookmarksFolder's Bookmarks
// collection
for (int i = 0; i < _bookmarksListView.Items.Count; i++)
{
ListViewItem bookmark = _bookmarksListView.Items[i];
if (bookmark.ImageIndex == 0)
continue;
// If the list view doesn't contain this bookmark, it's been deleted, so remove it from the list view and set the sort flag
if (!containerFolder.Value.Bookmarks.Contains(_listViewConnections[bookmark]))
{
_listViewConnections.Remove(bookmark);
_bookmarksListView.Items.Remove(bookmark);
sortListView = true;
i--;
}
}
}
}
// Sort the list view if necessary
if (IsHandleCreated && sortListView && !_deferSort)
_bookmarksListView.BeginInvoke(new Action(_bookmarksListView.Sort));
}
/// <summary>
/// Imports bookmarks previously saved via a call to <see cref="Bookmarks.Export"/> and overwrites any existing bookmarks data.
/// </summary>
/// <param name="path">Path of the file that we're loading from.</param>
public async Task Import(string path)
{
//ISSUE: Display shows old and new Bookmark items
//ISSUE: Dialog shows truncated suggested file name
if (await Bookmarks.Instance.Import(path))
{
// Set the handler methods for changing the bookmarks or child folders; these are responsible for updating the tree view and list view
// UI when items are added or removed from the bookmarks or child folders collections
Bookmarks.Instance.RootFolder.Bookmarks.CollectionModified += Bookmarks_CollectionModified;
Bookmarks.Instance.RootFolder.ChildFolders.CollectionModified += ChildFolders_CollectionModified;
_folderTreeNodes[_bookmarksFoldersTreeView.Nodes[0]] = Bookmarks.Instance.RootFolder;
// Call Bookmarks_CollectionModified and ChildFolders_CollectionModified recursively through the folder structure to "simulate"
// bookmarks and folders being added to the collection so that the initial UI state for the tree view can be created
InitializeTreeView(Bookmarks.Instance.RootFolder);
_bookmarksFoldersTreeView.Nodes[0].Expand();
}
}
/// <summary>
/// Handler method that is called after a node in <see cref="_bookmarksFoldersTreeView"/> is selected. It initializes the display of
/// <see cref="_bookmarksListView"/> with the bookmarks and immediate child folders in the selected tree view node.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_bookmarksFoldersTreeView"/> in this case.</param>
/// <param name="e">Selection arguments associated with the event.</param>
private void _bookmarksTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
// Clear out the list view
_bookmarksListView.Items.Clear();
_listViewConnections.Clear();
_listViewFolders.Clear();
if (!_folderTreeNodes.ContainsKey(e.Node))
return;
BookmarksFolder folder = _folderTreeNodes[e.Node];
// Add each child folder to the list view
foreach (BookmarksFolder childFolder in folder.ChildFolders)
{
ListViewItem item = new ListViewItem(
new string[]
{
childFolder.Name,
"",
childFolder.Notes
}, 0);
_listViewFolders[item] = childFolder;
_bookmarksListView.Items.Add(item);
}
// Add each bookmark to the list view
foreach (IConnection bookmark in folder.Bookmarks)
{
ListViewItem item = new ListViewItem(bookmark.DisplayName, _connectionTypeIcons[bookmark.GetType()]);
item.SubItems.Add(bookmark.Host);
item.SubItems.Add(bookmark.Notes);
_listViewConnections[item] = bookmark;
_bookmarksListView.Items.Add(item);
}
}
/// <summary>
/// Handler method that is called when the user double-clicks on an item in <see cref="_bookmarksListView"/>. Opens the folder in the list view if
/// the clicked item was a folder, otherwise opens the bookmark connection.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_bookmarksListView"/> in this case.</param>
/// <param name="e">Arguments associated with the click event.</param>
private async void _bookmarksListView_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (_bookmarksListView.SelectedItems.Count > 0)
{
// If the double-clicked item was a bookmark, open its connection in a new tab
if (_listViewConnections.ContainsKey(_bookmarksListView.SelectedItems[0]))
await _applicationForm.Connect(_listViewConnections[_bookmarksListView.SelectedItems[0]], true);
// Otherwise, open the folder
else
{
BookmarksFolder folder = _listViewFolders[_bookmarksListView.SelectedItems[0]];
TreeNode node = _folderTreeNodes.First(p => p.Value == folder).Key;
_bookmarksFoldersTreeView.SelectedNode = node;
}
}
}
/// <summary>
/// Handler method that is called when a user clicks in <see cref="_bookmarksListView"/>. If it's a right click and the user has actually clicked on
/// an item, we display the context menu.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_bookmarksListView"/> in this case.</param>
/// <param name="e">Arguments associated with the click event.</param>
private void _bookmarksListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ListViewHitTestInfo hitTestInfo = _bookmarksListView.HitTest(e.Location);
if (hitTestInfo.Item != null)
{
if (_listViewConnections.ContainsKey(hitTestInfo.Item))
{
_contextMenuItem = _listViewConnections[hitTestInfo.Item];
_bookmarksListView.ContextMenuStrip = null;
_bookmarkContextMenu.Show(Cursor.Position);
}
else
{
_contextMenuItem = _listViewFolders[hitTestInfo.Item];
_setContextMenuItem = false;
_folderContextMenu.Show(Cursor.Position);
}
}
}
}
/// <summary>
/// Handler method that's called when the user clicks the "Open bookmark in new tab" menu item.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void _openBookmarkNewTabMenuItem_Click(object sender, EventArgs e)
{
await OpenSelectedBookmarks();
}
/// <summary>
/// Handler method that's called when the user clicks the "Edit" menu item in the context menu that appears when the user right-clicks on a bookmark
/// in the list view. Opens the options window for the connection's protocol type.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _editBookmarkMenuItem_Click(object sender, EventArgs e)
{
ListViewItem selectedItem = _bookmarksListView.SelectedItems[0];
TitleBarTab optionsTab = new TitleBarTab(ParentTabs)
{
Content = new OptionsWindow(ParentTabs)
{
OptionsForms = new List<Form>
{
ConnectionFactory.CreateOptionsForm(_listViewConnections[selectedItem])
},
Text = "Options for " + _listViewConnections[selectedItem].DisplayName
}
};
// When the options form is closed, update the second column in the list view with the updated host for the connection
optionsTab.Content.FormClosed += (o, args) => selectedItem.SubItems[1].Text = _listViewConnections[selectedItem].Host;
ParentTabs.Tabs.Add(optionsTab);
ParentTabs.ResizeTabContents(optionsTab);
ParentTabs.SelectedTab = optionsTab;
}
/// <summary>
/// Handler method that's called when the user clicks the "Delete" menu item in the context menu that appears when the user right-clicks on an item
/// in the list view. Removes the items from the list view, the lookups collection, and their parent folders and then re-sorts the list view and tree
/// view.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
{
// Defer the sorting until after we're finished to eliminate unnecessary redraws
_deferSort = true;
foreach (ListViewItem selectedItem in _bookmarksListView.SelectedItems)
{
if (_listViewConnections.ContainsKey(selectedItem))
{
_folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode].Bookmarks.Remove(_listViewConnections[selectedItem]);
_listViewConnections.Remove(selectedItem);
}
else
{
_folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode].ChildFolders.Remove(_listViewFolders[selectedItem]);
_listViewFolders.Remove(selectedItem);
}
_bookmarksListView.Items.Remove(selectedItem);
}
// Sort the tree view and the list view and then save the bookmarks
_deferSort = false;
_bookmarksFoldersTreeView.BeginInvoke(new Action(SortTreeView));
_bookmarksListView.BeginInvoke(new Action(_bookmarksListView.Sort));
await Bookmarks.Instance.Save();
}
/// <summary>
/// Handler method that's called when the user clicks the "Open all bookmarks" menu item in the context menu that appears when the user right-clicks on
/// a node in the tree view. Opens all descendant bookmarks in the folder and its children in separate tabs.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void _folderOpenAllMenuItem_Click(object sender, EventArgs e)
{
await OpenAllBookmarks(_contextMenuItem as BookmarksFolder);
}
/// <summary>
/// Recursive method that opens all the descendant bookmarks in <paramref name="folder"/> in separate tabs.
/// </summary>
/// <param name="folder">Current folder for which we are opening bookmarks.</param>
private async Task OpenAllBookmarks(BookmarksFolder folder)
{
foreach (IConnection connection in folder.Bookmarks)
await _applicationForm.Connect(connection);
foreach (BookmarksFolder childFolder in folder.ChildFolders)
await OpenAllBookmarks(childFolder);
}
/// <summary>
/// Opens all bookmarks currently selected in <see cref="_bookmarksListView"/>.
/// </summary>
private async Task OpenSelectedBookmarks()
{
foreach (ListViewItem item in _bookmarksListView.SelectedItems)
await _applicationForm.Connect(_listViewConnections[item]);
}
/// <summary>
/// Handler method that's called when the user clicks in <see cref="_bookmarksFoldersTreeView"/>. If it's a right click and the user actually clicks
/// on a node, we open the context menu for that folder.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_bookmarksFoldersTreeView"/> in this case.</param>
/// <param name="e">Arguments associated with the mouse click event.</param>
private void _bookmarksTreeView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeViewHitTestInfo hitTestInfo = _bookmarksFoldersTreeView.HitTest(e.Location);
if (hitTestInfo.Node != null)
{
_bookmarksFoldersTreeView.SelectedNode = hitTestInfo.Node;
_contextMenuItem = _folderTreeNodes[hitTestInfo.Node];
_setContextMenuItem = false;
_folderContextMenu.Show(Cursor.Position);
}
}
}
/// <summary>
/// Handler method that's called when the user clicks a menu item under the "Add bookmark..." menu item. We create a new item in
/// <see cref="_bookmarksListView"/> with the proper icon for the protocol and open its label for editing.
/// </summary>
/// <param name="type">Protocol that the bookmark is being created for.</param>
private async void _addBookmarkMenuItem_Click(IProtocol type)
{
// Create a new connection instance by cloning the protocol's default
IConnection connection = (IConnection) (await ConnectionFactory.GetDefaults(type.GetType())).Clone();
connection.Name = "New Connection";
// Add the bookmark to the current folder
if (_folderTreeNodes[FoldersTreeView.SelectedNode] != (_contextMenuItem as BookmarksFolder))
{
FoldersTreeView.SelectedNode = _folderTreeNodes.Single(n => n.Value == (_contextMenuItem as BookmarksFolder)).Key;
FoldersTreeView.SelectedNode.Expand();
}
_deferSort = true;
(_contextMenuItem as BookmarksFolder).Bookmarks.Add(connection);
_deferSort = false;
// Set the flag so that once the user is finished renaming the connection, we open the options for it
_showOptionsAfterItemLabelEdit = true;
ListViewItem newListItem = _listViewConnections.First(pair => pair.Value == connection).Key;
_bookmarksListView.SelectedIndices.Clear();
SortListView();
await Bookmarks.Instance.Save();
// Start the edit process for the new list item's name
newListItem.Selected = true;
newListItem.BeginEdit();
}
/// <summary>
/// Handler method that's called when the user clicks the "Add folder..." menu item in the context menu that appears when the user right-clicks in the
/// tree view. Adds a new node to the tree view and the parent <see cref="BookmarksFolder"/> instance.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event</param>
private async void _addFolderMenuItem_Click(object sender, EventArgs e)
{
// Create a new BookmarksFolder, add it to the tree view, and add it to the parent folder instance
BookmarksFolder newFolder = new BookmarksFolder
{
Name = "New folder"
};
_deferSort = true;
(_contextMenuItem as BookmarksFolder).ChildFolders.Add(newFolder);
_deferSort = false;
TreeNode newNode = _folderTreeNodes.SingleOrDefault(kvp => kvp.Value == newFolder).Key;
_bookmarksFoldersTreeView.SelectedNode.Expand();
_bookmarksFoldersTreeView.SelectedNode = newNode;
SortTreeView();
await Bookmarks.Instance.Save();
newNode.BeginEdit();
}
/// <summary>
/// Handler method that's called when the user finishes renaming a folder in the tree view. We set the <see cref="BookmarksFolder.Name"/> property
/// of the corresponding <see cref="BookmarksFolder"/> to the new value.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void _bookmarksTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.CancelEdit || String.IsNullOrEmpty(e.Label))
return;
_folderTreeNodes[_bookmarksFoldersTreeView.SelectedNode].Name = e.Label;
await Bookmarks.Instance.Save();
_bookmarksFoldersTreeView.BeginInvoke(new Action(SortTreeView));
}
/// <summary>
/// Handler method that's called when the user clicks the "Rename..." menu item in the context menu that appears when the user right-clicks on an node
/// in the tree view. Starts the label edit process on the tree node.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _renameFolderMenuItem_Click(object sender, EventArgs e)
{
if (_bookmarksFoldersTreeView.Focused)
_bookmarksFoldersTreeView.SelectedNode.BeginEdit();
else
_listViewFolders.Single(kvp => kvp.Value == _contextMenuItem as BookmarksFolder).Key.BeginEdit();
}
/// <summary>
/// Handler method that's called when the user clicks the "Delete" menu item in the context menu that appears when the user right-clicks on an node
/// in the tree view.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void _deleteFolderMenuItem_Click(object sender, EventArgs e)
{
_deferSort = true;
BookmarksFolder folder = (BookmarksFolder) _contextMenuItem;
folder.ParentFolder.ChildFolders.Remove(folder);
_bookmarksListView.Items.Clear();
_bookmarksFoldersTreeView.BeginInvoke(new Action(_bookmarksFoldersTreeView.Sort));
_deferSort = false;
await Bookmarks.Instance.Save();
}
/// <summary>
/// Recursive method that's called to remove a folder and all its given descendants from the <see cref="_folderTreeNodes"/> lookup. Called from
/// <see cref="ChildFolders_CollectionModified"/>.
/// </summary>
/// <param name="currentNode">Tree node for the folder that we are to remove.</param>
private void RemoveAllFolders(TreeNode currentNode)
{
foreach (TreeNode childNode in currentNode.Nodes)
RemoveAllFolders(childNode);
_folderTreeNodes.Remove(currentNode);
}
/// <summary>
/// Handler method that's called after the user finishes renaming an item in <see cref="_bookmarksListView"/>. Sets the <see cref="IConnection.Name"/>
/// or <see cref="BookmarksFolder.Name"/> property and, if the user is setting the name for a newly-created bookmark, opens the option window for that
/// bookmark.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_bookmarksListView"/> in this case.</param>
/// <param name="e">Item being edited and its new label.</param>
private async void _bookmarksListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
if (e.CancelEdit || String.IsNullOrEmpty(e.Label))
return;
if (_listViewConnections.ContainsKey(_bookmarksListView.Items[e.Item]))
{
IConnection connection = _listViewConnections[_bookmarksListView.Items[e.Item]];
connection.Name = e.Label;
// If this is a new connection (no Host property set) and the item label doesn't contain spaces, we default the host name for the connection to
// the label name
if (String.IsNullOrEmpty(connection.Host) && !e.Label.Contains(" "))
{
connection.Host = e.Label;
_bookmarksListView.Items[e.Item].SubItems[1].Text = e.Label;
}
}
else
_listViewFolders[_bookmarksListView.Items[e.Item]].Name = e.Label;
await Bookmarks.Instance.Save();
if (_showOptionsAfterItemLabelEdit)
{
// Open the options window for the new bookmark if its name was set for the first time
ListViewItem selectedItem = _bookmarksListView.SelectedItems[0];
TitleBarTab optionsTab = new TitleBarTab(ParentTabs)
{
Content = new OptionsWindow(ParentTabs)
{
OptionsForms = new List<Form>
{
ConnectionFactory.CreateOptionsForm(_listViewConnections[selectedItem])
}
}
};
// When the options window is closed, update the value in the host column to what was supplied by the user
optionsTab.Content.FormClosed += (o, args) => selectedItem.SubItems[1].Text = _listViewConnections[selectedItem].Host;
ParentTabs.Tabs.Add(optionsTab);
ParentTabs.ResizeTabContents(optionsTab);
ParentTabs.SelectedTab = optionsTab;
_showOptionsAfterItemLabelEdit = false;
}
_bookmarksListView.BeginInvoke(new Action(_bookmarksListView.Sort));
}
/// <summary>
/// Handler method that's called when the user clicks the "Rename..." menu item in the context menu that appears when the user right-clicks on an item
/// in <see cref="_bookmarksListView"/>; calls <see cref="ListViewItem.BeginEdit"/> on the selected item.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
_bookmarksListView.SelectedItems[0].BeginEdit();
}
/// <summary>
/// Handler method that's called when the "Open bookmark in new window..." menu item in the context menu that appears when the user right-clicks on a
/// bookmark item in <see cref="_bookmarksListView"/>.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _openBookmarkNewWindowMenuItem_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm(
new List<IConnection>
{
_listViewConnections[_bookmarksListView.SelectedItems[0]]
});