Exemplo n.º 1
0
        public void GenerateChildren(ViewModelContext context, IViewModelNode viewModelNode, ref bool handled)
        {
            if ((viewModelNode.NodeValue as string) == "Root")
            {
                viewModelNode.Children.Add(new ViewModelNode("SearchResults", new EnumerableViewModelContent<ViewModelReference>(() =>
                    searchResults != null && searchResults.IsCompleted ? searchResults.Result.Select(searchResult => new ViewModelReference(KeyValuePair.Create(UrlType.SearchResult, searchResult), true))
                                                                       : new ViewModelReference[] { })));
                viewModelNode.Children.Add(new ViewModelNode("SearchFilter",
                    new LambdaViewModelContent<string>(
                        () => searchFilter,
                        (newFilter) =>
                        {
                            searchFilter = newFilter;
                            StartSearch();
                        })));
                viewModelNode.Children.Add(new ViewModelNode("Packages",
                    new EnumerableViewModelContent<ViewModelReference>(() => engineContext.PackageManager.Packages.Select(package => new ViewModelReference(package, true)))));

                fileTracker = new FileTracker();
                fileTracker.Setup("/global_data");
                fileTracker.Setup("/global_data2");
                viewModelNode.Children.Add(new ViewModelNode("FileTracker", new RootViewModelContent(fileTracker)).GenerateChildren(context));
            }
            if (viewModelNode.Type == typeof(FileTracker))
            {
                viewModelNode.Content.SerializeFlags = ViewModelContentSerializeFlags.None;
                viewModelNode.Children.Add(new ViewModelNode("RootFolder", KeyValuePair.Create(UrlType.FileTracker, "/")).GenerateChildren(context));
                handled = true;
            }
            if (viewModelNode.NodeValue is Package)
            {
                viewModelNode.Children.Add(new ViewModelNode("Name", new PropertyInfoViewModelContent(new ParentNodeValueViewModelContent(), typeof(Package).GetProperty("Name"))));
                handled = true;
            }

            if (viewModelNode.NodeValue is Tuple<UrlType, string>)
            {
                var nodeValue = (Tuple<UrlType, string>)viewModelNode.NodeValue;
                var url = nodeValue.Item2;

                if (nodeValue.Item1 == UrlType.SearchResult)
                {
                    // Load thumbnail (not cached yet)
                    if (url.EndsWith(".png") || url.EndsWith(".jpg"))
                    {
                        //var textureData = engineContext.ContentManager.LoadAsync<Image>(url);
                        var thumbnail = new ViewModelNode("Thumbnail",
                            new AsyncViewModelContent<Image>(new NullViewModelContent(), operand => engineContext.AssetManager.Load<Image>(url)));
                        viewModelNode.Children.Add(thumbnail);
                        /*textureData.ContinueWith(task =>
                            {
                                thumbnail.Value = task.Result;
                                thumbnail.Content.Flags |= ViewModelFlags.Static;
                            });*/
                    }
                    /*else
                    {
                        throw new NotImplementedException();
                    }*/

                    viewModelNode.Content = new RootViewModelContent(url);
                    viewModelNode.Content.SerializeFlags = ViewModelContentSerializeFlags.Serialize;
                }
                else if (nodeValue.Item1 == UrlType.FileTracker)
                {
                    viewModelNode.Content = new RootViewModelContent(url);
                    viewModelNode.Content.SerializeFlags = ViewModelContentSerializeFlags.Serialize;

                    viewModelNode.Children.Add(new ViewModelNode("SetAsSearchFilter", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
                        {
                            searchRoot = url;
                            StartSearch();
                        }))));

                    if (url.EndsWith("/"))
                    {
                        viewModelNode.Children.Add(new ViewModelNode("Folders", new EnumerableViewModelContent<ViewModelReference>(() =>
                                fileTracker.Files
                                    .Where(file => file.StartsWith(url))
                                    .GroupBy(file =>
                                        {
                                            var separatorIndex = file.IndexOf('/', url.Length + 1);
                                            return file.Substring(url.Length, separatorIndex != -1 ? separatorIndex - url.Length + 1 : file.Length - url.Length);
                                        })
                                    .Where(x => x.Key.EndsWith("/") || x.Key.EndsWith(".dat") || x.Key.EndsWith(".xksl"))
                                    .Select(x => new ViewModelReference(KeyValuePair.Create(UrlType.FileTracker, url + x.Key), this))
                            )));
                    }
                }

                handled = true;
            }
        }