// // Copyright 2014 Perforce Software Inc. // using Perforce.Helper; using Perforce.Model; using System.Collections.ObjectModel; using System.Windows.Data; using System.Collections.Generic; using System.Threading.Tasks; using Perforce.View; using System.Windows; using System.Windows.Controls; namespace Perforce.ViewModel { public class ColumnDisplayViewModel : ViewModelBase { private int _column; private SidebarSelector _selector; private string _path; private object _listLock = new object(); private int _lastCount; private string _selected; private ColumnDisplay _view; public ColumnDisplayViewModel(SidebarSelector selector, ColumnDisplay view, int column, string path, string selected, bool loadSynchronous = false) { _selector = selector; _view = view; _column = column; _path = path; _selected = selected; ListingItems = new ObservableCollection(); BindingOperations.EnableCollectionSynchronization(ListingItems, _listLock); if (loadSynchronous) { PopulateDataSync(); } else { PopulateDataAsync(); } } public ObservableCollection ListingItems { get; set; } public int LastCount { get { return _lastCount; } set { _lastCount = value; } } public bool AllowColumnDrop { get { var canDrop = false; // can only drop into the workspace tab if (_selector.SelectorType == SELECTOR_TYPE.WORKSPACE) { if (!string.IsNullOrEmpty(_path)) { var helper = Utility.GetPerforceHelper(); var paths = new List(); paths.Add(_path); var mappings = helper.GetDepotMappings(paths, true); if (mappings.Count == 1) { canDrop = true; } } } return canDrop; } } public string Path { get { return _path; } } public int ColumnNumber { get { return _column; } } public void Refresh(bool sync = false) { if (sync) { PopulateDataSync(); } else { PopulateDataAsync(); } } public void SelectItem(string label) { if (label != null && ListingItems.Count > 0) { foreach (var i in ListingItems) { if (i.LabelText.Equals(label, System.StringComparison.CurrentCultureIgnoreCase)) { i.IsSelected = true; } } } } public ListingItem[] Selected { get { var selected = new List(); foreach (var i in ListingItems) { if (i.IsSelected) { selected.Add(i); } } return selected.ToArray(); } } public ListingItem SelectedItem { get; set; } private void UpdateListingItems() { List selected = new List(); foreach (var i in ListingItems) { if (i.IsSelected) { selected.Add(i.LabelText); } } ListingItems.Clear(); if (_selector.Model != null) { _selector.Model.LabelCount = 0; } IList results = null; switch (_selector.SelectorType) { case SELECTOR_TYPE.WORKSPACE: results = ListingHelper.GetDirectoryListing(_view, _path); break; case SELECTOR_TYPE.SERVER: results = ListingHelper.GetDepotListing(_view, _path); break; case SELECTOR_TYPE.SEARCH: string[] filters = null; var tagFilters = (_selector.Model as SearchSelectorViewModel).TagFilters; if (tagFilters != null) { filters = new string[tagFilters.Count]; tagFilters.CopyTo(filters); } results = ListingHelper.GetSearchResults(_view, filters); break; case SELECTOR_TYPE.FAVORITE: var favModel = (_view.Selector.Model as FavoritesViewModel); var item = favModel.Item; if (item.Selector == SELECTOR_TYPE.WORKSPACE) { results = ListingHelper.GetDirectoryListing(_view, _path); } else { results = ListingHelper.GetDepotListing(_view, _path); } break; case SELECTOR_TYPE.PENDING: results = ListingHelper.GetChangelistFiles(_view); break; case SELECTOR_TYPE.RECENT: results = ListingHelper.GetRecentFiles(_view); break; case SELECTOR_TYPE.TRASH: results = ListingHelper.GetTrashFiles(_view); break; case SELECTOR_TYPE.TAG: break; } if (results != null && results.Count > 0) { foreach (var item in results) { if (selected.Contains(item.LabelText)) { item.IsSelected = true; } ListingItems.Add(item); if (!string.IsNullOrEmpty(_selected) && _selected.Equals(item.LabelText, System.StringComparison.CurrentCultureIgnoreCase)) { item.IsSelected = true; SelectedItem = item; } } _selector.Model.LabelCount = results.Count; } } public void PopulateDataSync() { IsLoading = true; UpdateListingItems(); IsLoading = false; } public void PopulateDataAsync() { IsLoading = true; Task.Factory.StartNew(() => { UpdateListingItems(); IsLoading = false; }); } private bool _loading; public bool IsLoading { get { return _loading; } set { _loading = value; OnPropertyChanged("IsLoading"); } } } }