aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Explorer/ExplorerControl.cs
blob: 9561072d01cf0c4ef0b51c3430a0f1d20b80add2 (plain)
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.Commands;
using Tango.Touch.Controls;

namespace Tango.Explorer
{
    public class ExplorerControl : Control
    {
        private bool _changing_current_path;
        private TouchListBox _listBox;

        public String CurrentPath
        {
            get { return (String)GetValue(CurrentPathProperty); }
            set { SetValue(CurrentPathProperty, value); }
        }
        public static readonly DependencyProperty CurrentPathProperty =
            DependencyProperty.Register("CurrentPath", typeof(String), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnCurrentPathChanged()));

        public ExplorerFolderItem CurrentFolder
        {
            get { return (ExplorerFolderItem)GetValue(CurrentFolderProperty); }
            set { SetValue(CurrentFolderProperty, value); }
        }
        public static readonly DependencyProperty CurrentFolderProperty =
            DependencyProperty.Register("CurrentFolder", typeof(ExplorerFolderItem), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnCurrentFolderChanged()));

        public ExplorerItem SelectedItem
        {
            get { return (ExplorerItem)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(ExplorerItem), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnSelectedItemChanged()));

        public IEnumerable<ExplorerItem> SelectedItems
        {
            get { return (IEnumerable<ExplorerItem>)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems", typeof(IEnumerable<ExplorerItem>), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnSelectedItemsChanged()));

        public RelayCommand BackCommand
        {
            get { return (RelayCommand)GetValue(BackCommandProperty); }
            set { SetValue(BackCommandProperty, value); }
        }
        public static readonly DependencyProperty BackCommandProperty =
            DependencyProperty.Register("BackCommand", typeof(RelayCommand), typeof(ExplorerControl), new PropertyMetadata(null));

        public RelayCommand<ExplorerFileItem> FileSelectedCommand
        {
            get { return (RelayCommand<ExplorerFileItem>)GetValue(FileSelectedCommandProperty); }
            set { SetValue(FileSelectedCommandProperty, value); }
        }
        public static readonly DependencyProperty FileSelectedCommandProperty =
            DependencyProperty.Register("FileSelectedCommand", typeof(RelayCommand<ExplorerFileItem>), typeof(ExplorerControl), new PropertyMetadata(null));

        public String Filter
        {
            get { return (String)GetValue(FilterProperty); }
            set { SetValue(FilterProperty, value); }
        }
        public static readonly DependencyProperty FilterProperty =
            DependencyProperty.Register("Filter", typeof(String), typeof(ExplorerControl), new PropertyMetadata(null, (d, e) => (d as ExplorerControl).OnCurrentPathChanged()));

        public bool EnableFileSelection
        {
            get { return (bool)GetValue(EnableFileSelectionProperty); }
            set { SetValue(EnableFileSelectionProperty, value); }
        }
        public static readonly DependencyProperty EnableFileSelectionProperty =
            DependencyProperty.Register("EnableFileSelection", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(true));

        public bool EnableMultiSelect
        {
            get { return (bool)GetValue(EnableMultiSelectProperty); }
            set { SetValue(EnableMultiSelectProperty, value); }
        }
        public static readonly DependencyProperty EnableMultiSelectProperty =
            DependencyProperty.Register("EnableMultiSelect", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(false));

        public bool IsMultiSelecting
        {
            get { return (bool)GetValue(IsMultiSelectingProperty); }
            set { SetValue(IsMultiSelectingProperty, value); }
        }
        public static readonly DependencyProperty IsMultiSelectingProperty =
            DependencyProperty.Register("IsMultiSelecting", typeof(bool), typeof(ExplorerControl), new PropertyMetadata(false));

        static ExplorerControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ExplorerControl), new FrameworkPropertyMetadata(typeof(ExplorerControl)));
        }

        public ExplorerControl()
        {
            BackCommand = new RelayCommand(NavigateBack);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _listBox = GetTemplateChild("PART_ListBox") as TouchListBox;
        }

        private void OnCurrentPathChanged()
        {
            if (_changing_current_path) return;

            if (_listBox != null)
            {
                _listBox.IsMultiSelecting = false;
            }

            _changing_current_path = true;

            if (CurrentPath == null)
            {
                CurrentFolder = null;
            }
            else if (Directory.Exists(CurrentPath))
            {
                CurrentFolder = ExplorerFolderItem.LoadFromPath(CurrentPath, Filter);
            }

            _changing_current_path = false;
        }

        private void OnCurrentFolderChanged()
        {
            if (_changing_current_path) return;

            _changing_current_path = true;

            CurrentPath = CurrentFolder.Path;

            _changing_current_path = false;
        }

        private void OnSelectedItemChanged()
        {
            if (SelectedItem != null && _listBox != null)
            {
                if (SelectedItem is ExplorerFolderItem)
                {
                    var folder = SelectedItem as ExplorerFolderItem;
                    folder = ExplorerFolderItem.LoadFromPath(folder.Path, Filter);
                    CurrentFolder = folder;
                    SelectedItem = null;
                }
                else if (SelectedItem is ExplorerFileItem && EnableFileSelection && !_listBox.IsMultiSelecting)
                {
                    FileSelectedCommand?.Execute(SelectedItem);
                }
            }
        }

        private void OnSelectedItemsChanged()
        {
            if (SelectedItems != null)
            {
                if (SelectedItems is INotifyCollectionChanged)
                {
                    (SelectedItems as INotifyCollectionChanged).CollectionChanged -= ExplorerControl_CollectionChanged;
                    (SelectedItems as INotifyCollectionChanged).CollectionChanged += ExplorerControl_CollectionChanged;
                }

                PreventMultiExtensionSelection();
            }
        }

        private void ExplorerControl_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            PreventMultiExtensionSelection();
        }

        private void PreventMultiExtensionSelection()
        {
            if (_listBox != null && SelectedItems != null)
            {
                if (_listBox.IsMultiSelecting && _listBox.SelectedItems != null && _listBox.SelectedItems.Count > 1)
                {
                    var firstItem = _listBox.SelectedItems.OfType<ExplorerFileItem>().FirstOrDefault();

                    if (firstItem != null)
                    {
                        foreach (var item in _listBox.SelectedItems.OfType<ExplorerFileItem>().ToList())
                        {
                            if (item.Extension.ToLower() != firstItem.Extension.ToLower())
                            {
                                var listBoxItem = _listBox.GetItems().FirstOrDefault(x => x.DataContext == item);

                                if (listBoxItem != null)
                                {
                                    listBoxItem.IsSelected = false;
                                    _listBox.SelectedItems.Remove(item);
                                }
                            }
                        }
                    }
                }
            }
        }

        public void NavigateBack()
        {
            if (CurrentFolder != null)
            {
                var parentPath = CurrentFolder.GetParentPath();

                if (parentPath != null)
                {
                    CurrentFolder = ExplorerFolderItem.LoadFromPath(parentPath, Filter);
                }
            }
        }
    }
}