1 | using System.Windows.Forms; |
---|
2 | using System.ComponentModel; |
---|
3 | using System.Collections; |
---|
4 | using Com.Piwigo.Uploader.DTO; |
---|
5 | |
---|
6 | namespace Com.Piwigo.Uploader |
---|
7 | { |
---|
8 | public partial class DataTreeView : TreeView |
---|
9 | { |
---|
10 | private object _DataSource; |
---|
11 | private CurrencyManager _CurrManager; |
---|
12 | |
---|
13 | [Category("Data")] |
---|
14 | [RefreshProperties(RefreshProperties.Repaint)] |
---|
15 | [TypeConverter("System.Windows.Forms.Design.DataSourceConverter")] |
---|
16 | public object DataSource |
---|
17 | { |
---|
18 | get |
---|
19 | { |
---|
20 | return _DataSource; |
---|
21 | } |
---|
22 | set |
---|
23 | { |
---|
24 | if (value == null) |
---|
25 | { |
---|
26 | if (_CurrManager != null) |
---|
27 | { |
---|
28 | _CurrManager.ListChanged -= new ListChangedEventHandler(_CurrManager_ListChanged); |
---|
29 | _CurrManager = null; |
---|
30 | } |
---|
31 | } |
---|
32 | else |
---|
33 | { |
---|
34 | if (value is IList) |
---|
35 | { |
---|
36 | _DataSource = value; |
---|
37 | if (this.BindingContext == null) |
---|
38 | { |
---|
39 | this.BindingContext = new System.Windows.Forms.BindingContext(); |
---|
40 | } |
---|
41 | _CurrManager = this.BindingContext[value] as CurrencyManager; |
---|
42 | _CurrManager.ListChanged += new ListChangedEventHandler(_CurrManager_ListChanged); |
---|
43 | } |
---|
44 | else if (value is IListSource) |
---|
45 | { |
---|
46 | IListSource myListSource = value as IListSource; |
---|
47 | if (myListSource.ContainsListCollection) |
---|
48 | { |
---|
49 | throw new System.Exception("Invalid DataSource"); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | _DataSource = value; |
---|
54 | if (this.BindingContext == null) |
---|
55 | { |
---|
56 | this.BindingContext = new System.Windows.Forms.BindingContext(); |
---|
57 | } |
---|
58 | _CurrManager = this.BindingContext[value] as CurrencyManager; |
---|
59 | _CurrManager.ListChanged += new ListChangedEventHandler(_CurrManager_ListChanged); |
---|
60 | } |
---|
61 | } |
---|
62 | else |
---|
63 | { |
---|
64 | throw new System.Exception("Invalid DataSource"); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | public DataTreeView() |
---|
72 | { |
---|
73 | InitializeComponent(); |
---|
74 | } |
---|
75 | |
---|
76 | void _CurrManager_ListChanged(object sender, ListChangedEventArgs e) |
---|
77 | { |
---|
78 | switch (e.ListChangedType) |
---|
79 | { |
---|
80 | case ListChangedType.ItemAdded: |
---|
81 | case ListChangedType.ItemChanged: |
---|
82 | case ListChangedType.ItemDeleted: |
---|
83 | case ListChangedType.ItemMoved: |
---|
84 | case ListChangedType.Reset: |
---|
85 | BuildAlbumTree(_CurrManager.List as LocalAlbumList); |
---|
86 | break; |
---|
87 | case ListChangedType.PropertyDescriptorAdded: |
---|
88 | case ListChangedType.PropertyDescriptorChanged: |
---|
89 | case ListChangedType.PropertyDescriptorDeleted: |
---|
90 | default: |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | private static TreeNode buildAlbumToNode(LocalAlbum aAlbum) |
---|
96 | { |
---|
97 | System.Windows.Forms.TreeNode aNode = new System.Windows.Forms.TreeNode(aAlbum.Name, 1, 1); |
---|
98 | aNode.ToolTipText = aAlbum.LocalDir; |
---|
99 | aNode.Tag = aAlbum; |
---|
100 | |
---|
101 | foreach (LocalImage Img in aAlbum.LocalImages) |
---|
102 | { |
---|
103 | System.Windows.Forms.TreeNode imgNode = new System.Windows.Forms.TreeNode(Img.Name, 2, 2); |
---|
104 | imgNode.ToolTipText = Img.LocalFile; |
---|
105 | imgNode.Tag = Img; |
---|
106 | aNode.Nodes.Add(imgNode); |
---|
107 | } |
---|
108 | |
---|
109 | foreach (LocalAlbum Alb in aAlbum.SubAlbums) |
---|
110 | { |
---|
111 | aNode.Nodes.Add(buildAlbumToNode(Alb)); |
---|
112 | } |
---|
113 | |
---|
114 | return aNode; |
---|
115 | } |
---|
116 | |
---|
117 | private void BuildAlbumTree(LocalAlbumList lstAlbum) |
---|
118 | { |
---|
119 | if (this.Nodes.Count > 0) |
---|
120 | { |
---|
121 | this.Nodes.Clear(); |
---|
122 | } |
---|
123 | |
---|
124 | TreeNode root = new TreeNode("Albums", 0, 0); |
---|
125 | this.Nodes.Add(root); |
---|
126 | if (lstAlbum != null) |
---|
127 | { |
---|
128 | foreach (LocalAlbum Alb in lstAlbum) |
---|
129 | { |
---|
130 | root.Nodes.Add(buildAlbumToNode(Alb)); |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|