public SearchView( SearchViewModel viewModel) { DataContext = viewModel; InitializeComponent(); PreviewKeyDown += viewModel.KeyHandler; SearchTextBox.IsVisibleChanged += delegate { DynamoCommands.Search.Execute(null); Keyboard.Focus(this.SearchTextBox); SearchTextBox.InputBindings.AddRange(dynSettings.Bench.InputBindings); }; SearchTextBox.GotKeyboardFocus += delegate { if (SearchTextBox.Text == "Search...") { SearchTextBox.Text = ""; } SearchTextBox.Foreground = Brushes.White; }; SearchTextBox.LostKeyboardFocus += delegate { SearchTextBox.Foreground = Brushes.Gray; }; }
public void CanAddCategoryWithDelimiters() { var model = new SearchViewModel(); var leaf = model.AddCategory("Peter.Boyer"); model.RemoveCategory("Peter.Boyer"); }
public void CanAddCategory() { var model = new SearchViewModel(); var root = model.AddRootCategory("Peter"); var leafCat = new BrowserInternalElement("Boyer", root); root.Items.Add(leafCat); Assert.Contains(leafCat, root.Items ); Assert.Contains( root, model.BrowserRootCategories ); }
public void CanRemoveElementCustomNodeByNameWithSingleCategory() { var model = new SearchViewModel(); model.Add("Peter", "Greens", "A description", System.Guid.NewGuid()); model.SearchAndUpdateResultsSync("Peter"); Assert.AreEqual(1, model.SearchResults.Count); model.Remove("Peter"); model.SearchAndUpdateResultsSync("Peter"); Assert.AreEqual(0, model.SearchResults.Count); }
public void CanRunRemoveCategoryIfCategoryDoesntExist() { var model = new SearchViewModel(); var root = model.AddRootCategory("Peter"); var leaf = new BrowserInternalElement("Boyer", root); root.AddChild(leaf); Assert.Contains(leaf, root.Items); Assert.Contains(root, model.BrowserRootCategories); model.RemoveCategory("Peter.Rabbit"); Assert.True(model.BrowserRootCategories.Contains(root)); Assert.True(root.Items.Contains(leaf)); }
public void CanRemoveCategoryWithDelimiters() { var model = new SearchViewModel(); var root = model.AddRootCategory("Peter"); var leaf = new BrowserInternalElement("Boyer", root); root.AddChild(leaf); Assert.Contains(leaf, root.Items); Assert.Contains(root, model.BrowserRootCategories); model.RemoveCategory("Peter.Boyer"); Assert.True( model.BrowserRootCategories.Contains(root) ); Assert.False( root.Items.Contains(leaf) ); }
/// <summary> /// Class constructor /// </summary> public DynamoController(ExecutionEnvironment env) { dynSettings.Controller = this; this.RunEnabled = true; this.CanRunDynamically = true; Bench = new dynBench(this); dynSettings.Bench = Bench; // custom node loader string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string pluginsPath = Path.Combine(directory, "definitions"); CustomNodeLoader = new CustomNodeLoader(pluginsPath); SearchViewModel = new SearchViewModel(); PackageManagerClient = new PackageManagerClient(this); PackageManagerLoginViewModel = new PackageManagerLoginViewModel(PackageManagerClient); PackageManagerPublishViewModel = new PackageManagerPublishViewModel(PackageManagerClient); FSchemeEnvironment = env; HomeSpace = CurrentSpace = new HomeWorkspace(); Bench.CurrentOffset = new Point(dynBench.CANVAS_OFFSET_X, dynBench.CANVAS_OFFSET_Y); Bench.InitializeComponent(); Bench.Log(String.Format( "Dynamo -- Build {0}.", Assembly.GetExecutingAssembly().GetName().Version)); DynamoLoader.LoadBuiltinTypes(SearchViewModel, this, Bench); DynamoLoader.LoadSamplesMenu(Bench); Bench.settings_curves.IsChecked = true; Bench.settings_curves.IsChecked = false; Bench.LockUI(); Bench.Activated += OnBenchActivated; dynSettings.Workbench = Bench.WorkBench; //run tests if (FScheme.RunTests(Bench.Log)) { if (Bench != null) Bench.Log("All Tests Passed. Core library loaded OK."); } }
/// <summary> /// Class constructor /// </summary> public DynamoController(ExecutionEnvironment env, Type viewModelType, string context) { dynSettings.Controller = this; this.Context = context; //create the view model to which the main window will bind //the DynamoModel is created therein this.DynamoViewModel = (DynamoViewModel)Activator.CreateInstance(viewModelType,new object[]{this}); // custom node loader string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string pluginsPath = Path.Combine(directory, "definitions"); CustomNodeManager = new CustomNodeManager(pluginsPath); SearchViewModel = new SearchViewModel(); PackageManagerClient = new PackageManagerClient(); dynSettings.PackageManagerClient = PackageManagerClient; PublishPackageViewModel = new PublishPackageViewModel(PackageManagerClient); dynSettings.PackageLoader = new PackageLoader(); dynSettings.PackageLoader.DoCachedPackageUninstalls(); dynSettings.PackageLoader.LoadPackages(); FSchemeEnvironment = env; DynamoViewModel.Model.CurrentSpace.X = DynamoView.CANVAS_OFFSET_X; DynamoViewModel.Model.CurrentSpace.Y = DynamoView.CANVAS_OFFSET_Y; dynSettings.Controller.DynamoViewModel.Log(String.Format( "Dynamo -- Build {0}", Assembly.GetExecutingAssembly().GetName().Version)); DynamoLoader.ClearCachedAssemblies(); DynamoLoader.LoadBuiltinTypes(); //run tests if (FScheme.RunTests(dynSettings.Controller.DynamoViewModel.Log)) { dynSettings.Controller.DynamoViewModel.Log("All Tests Passed. Core library loaded OK."); } NodeSubmittedForRendering += new EventHandler(Controller_NodeSubmittedForRendering); NodeRemovedFromRendering += new EventHandler(Controller_NodeRemovedFromRendering); }
/// <summary> /// Load Custom Nodes from the default directory - the "definitions" /// directory where the executing assembly is located.. /// </summary> /// <param name="bench">The logger is needed in order to tell how long it took.</param> public static void LoadCustomNodes(DynamoView bench, CustomNodeLoader customNodeLoader, SearchViewModel searchViewModel) { // custom node loader var sw = new Stopwatch(); sw.Start(); customNodeLoader.UpdateSearchPath(); var nn = customNodeLoader.GetNodeNameCategoryAndGuidList(); // add nodes to search foreach (var pair in nn) { searchViewModel.Add(pair.Item1, pair.Item2, pair.Item3); } sw.Stop(); DynamoCommands.WriteToLogCmd.Execute(string.Format("{0} ellapsed for loading definitions.", sw.Elapsed)); // update search view searchViewModel.SearchAndUpdateResultsSync(searchViewModel.SearchText); }
/// <summary> /// Enumerate the types in an assembly and add them to DynamoController's /// dictionaries and the search view model. Internally catches exceptions and sends the error /// to the console. /// </summary> /// <param name="searchViewModel">The searchViewModel to which the nodes will be added</param> /// <param name="controller">The DynamoController, whose dictionaries will be modified</param> /// <param name="bench">The bench where logging errors will be sent</param> private static void LoadNodesFromAssembly(Assembly assembly, SearchViewModel searchViewModel, DynamoController controller) { try { Type[] loadedTypes = assembly.GetTypes(); foreach (Type t in loadedTypes) { try { //only load types that are in the right namespace, are not abstract //and have the elementname attribute object[] attribs = t.GetCustomAttributes(typeof (NodeNameAttribute), false); if (IsNodeSubType(t) && attribs.Length > 0) { //if we are running in revit (or any context other than NONE) use the DoNotLoadOnPlatforms attribute, //if available, to discern whether we should load this type if (!controller.Context.Equals(Context.NONE)) { object[] platformExclusionAttribs = t.GetCustomAttributes(typeof(DoNotLoadOnPlatformsAttribute), false); if (platformExclusionAttribs.Length > 0) { string[] exclusions = (platformExclusionAttribs[0] as DoNotLoadOnPlatformsAttribute).Values; if (exclusions.Contains(controller.Context)) //if the attribute's values contain the context stored on the controller //then skip loading this type. continue; } } searchViewModel.Add(t); string typeName = (attribs[0] as NodeNameAttribute).Name; var data = new TypeLoadData(assembly, t); if (!controller.BuiltInTypesByNickname.ContainsKey(typeName)) { controller.BuiltInTypesByNickname.Add(typeName, data); } else { dynSettings.Controller.DynamoViewModel.Log("Duplicate type encountered: " + typeName); } if (!controller.BuiltInTypesByName.ContainsKey(t.FullName)) { controller.BuiltInTypesByName.Add(t.FullName, data); } else { dynSettings.Controller.DynamoViewModel.Log("Duplicate type encountered: " + typeName); } } } catch (Exception e) { dynSettings.Controller.DynamoViewModel.Log("Failed to load type from " + assembly.FullName); dynSettings.Controller.DynamoViewModel.Log("The type was " + t.FullName); dynSettings.Controller.DynamoViewModel.Log(e); } } } catch (Exception e) { dynSettings.Controller.DynamoViewModel.Log("Could not load types."); dynSettings.Controller.DynamoViewModel.Log(e); if (e is ReflectionTypeLoadException) { var typeLoadException = e as ReflectionTypeLoadException; Exception[] loaderExceptions = typeLoadException.LoaderExceptions; dynSettings.Controller.DynamoViewModel.Log("Dll Load Exception: " + loaderExceptions[0]); dynSettings.Controller.DynamoViewModel.Log(loaderExceptions[0].ToString()); if (loaderExceptions.Count() > 1) { dynSettings.Controller.DynamoViewModel.Log("Dll Load Exception: " + loaderExceptions[1]); dynSettings.Controller.DynamoViewModel.Log(loaderExceptions[1].ToString()); } } } }
/// <summary> /// Enumerate local library assemblies and add them to DynamoController's /// dictionaries and search. /// </summary> /// <param name="searchViewModel">The searchViewModel to which the nodes will be added</param> /// <param name="controller">The DynamoController, whose dictionaries will be modified</param> internal static void LoadBuiltinTypes(SearchViewModel searchViewModel, DynamoController controller) { Assembly dynamoAssembly = Assembly.GetExecutingAssembly(); string location = Path.GetDirectoryName(dynamoAssembly.Location); #region determine assemblies to load var allLoadedAssembliesByPath = new Dictionary<string, Assembly>(); var allLoadedAssemblies = new Dictionary<string, Assembly>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { allLoadedAssembliesByPath[assembly.Location] = assembly; allLoadedAssemblies[assembly.FullName] = assembly; } string path = Path.Combine(location, "packages"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); IEnumerable<string> allDynamoAssemblyPaths = Directory.GetFiles(location, "*.dll") .Concat(Directory.GetFiles( path, "*.dll", SearchOption.AllDirectories)); var resolver = new ResolveEventHandler(delegate(object sender, ResolveEventArgs args) { Assembly result; allLoadedAssemblies.TryGetValue(args.Name, out result); return result; }); AppDomain.CurrentDomain.AssemblyResolve += resolver; foreach (string assemblyPath in allDynamoAssemblyPaths) { if (allLoadedAssembliesByPath.ContainsKey(assemblyPath)) LoadNodesFromAssembly(allLoadedAssembliesByPath[assemblyPath], searchViewModel, controller); else { try { Assembly assembly = Assembly.LoadFrom(assemblyPath); allLoadedAssemblies[assembly.GetName().Name] = assembly; LoadNodesFromAssembly(assembly, searchViewModel, controller); } catch { } } } AppDomain.CurrentDomain.AssemblyResolve -= resolver; #endregion }
/// <summary> /// Class constructor /// </summary> public DynamoController(ExecutionEnvironment env, bool withUI, Type viewModelType, string context) { dynSettings.Controller = this; this.Context = context; //MVVM: don't construct the main window with a reference to the controller //dynSettings.Bench = new dyndynSettings.Bench(this); //MVVM : create the view model to which the main window will bind //the DynamoModel is created therein //this.DynamoViewModel = new DynamoViewModel(this); this.DynamoViewModel = (DynamoViewModel)Activator.CreateInstance(viewModelType,new object[]{this}); // custom node loader string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string pluginsPath = Path.Combine(directory, "definitions"); CustomNodeLoader = new CustomNodeLoader(pluginsPath); if (withUI) { dynSettings.Bench = new DynamoView(); dynSettings.Bench = dynSettings.Bench; dynSettings.Bench.DataContext = DynamoViewModel; } SearchViewModel = new SearchViewModel(); PackageManagerClient = new PackageManagerClient(this); PackageManagerLoginViewModel = new PackageManagerLoginViewModel(PackageManagerClient); PackageManagerPublishViewModel = new PackageManagerPublishViewModel(PackageManagerClient); FSchemeEnvironment = env; //MVVM : moved to proper view constructor on dyndynSettings.Bench //DynamoViewModel.Model.CurrentSpace.CurrentOffset = new Point(DynamoView.CANVAS_OFFSET_X, DynamoView.CANVAS_OFFSET_Y); DynamoViewModel.Model.CurrentSpace.X = DynamoView.CANVAS_OFFSET_X; DynamoViewModel.Model.CurrentSpace.Y = DynamoView.CANVAS_OFFSET_Y; //dynSettings.Bench.CurrentOffset = new Point(dyndynSettings.Bench.CANVAS_OFFSET_X, dyndynSettings.Bench.CANVAS_OFFSET_Y); //dynSettings.Bench.InitializeComponent(); dynSettings.Controller.DynamoViewModel.Log(String.Format( "Dynamo -- Build {0}.", Assembly.GetExecutingAssembly().GetName().Version)); //MVVM : removed parameter dynSettings.Bench DynamoLoader.LoadBuiltinTypes(SearchViewModel, this);//, dynSettings.Bench); if(dynSettings.Bench != null) DynamoLoader.LoadSamplesMenu(dynSettings.Bench); //dynSettings.Bench.settings_curves.IsChecked = true; //dynSettings.Bench.settings_curves.IsChecked = false; if (dynSettings.Bench != null) { //dynSettings.Bench.LockUI(); //MVVM : callback has been restructured so that it sends a command back to the view model //dynSettings.Bench.Activated += OndynSettings.BenchActivated; //MVVM: we've gone to using a model and a model view of a workspace //do not reference a specific workdynSettings.Bench here. //dynSettings.WorkdynSettings.Bench = dynSettings.Bench.WorkdynSettings.Bench; } //run tests if (FScheme.RunTests(dynSettings.Controller.DynamoViewModel.Log)) { if (dynSettings.Bench != null) this.DynamoViewModel.Log("All Tests Passed. Core library loaded OK."); } }
public void CanTryToRemoveElementFromSearchWithNonexistentName() { var model = new SearchViewModel(); model.Remove("NonExistentName"); model.SearchAndUpdateResultsSync("NonExistentName"); Assert.AreEqual(0, model.SearchResults.Count); }
/// <summary> /// Enumerate the types in an assembly and add them to DynamoController's /// dictionaries and the search view model. Internally catches exceptions and sends the error /// to the console. /// </summary> /// <param name="searchViewModel">The searchViewModel to which the nodes will be added</param> /// <param name="controller">The DynamoController, whose dictionaries will be modified</param> /// <param name="bench">The bench where logging errors will be sent</param> private static void LoadNodesFromAssembly(Assembly assembly, SearchViewModel searchViewModel, DynamoController controller, dynBench bench ) { try { Type[] loadedTypes = assembly.GetTypes(); foreach (Type t in loadedTypes) { //only load types that are in the right namespace, are not abstract //and have the elementname attribute object[] attribs = t.GetCustomAttributes(typeof(NodeNameAttribute), false); if (IsNodeSubType(t) && attribs.Length > 0) { searchViewModel.Add(t); string typeName = (attribs[0] as NodeNameAttribute).Name; var data = new TypeLoadData(assembly, t); controller.builtinTypesByNickname.Add(typeName, data); controller.builtinTypesByTypeName.Add(t.FullName, data); } } } catch (Exception e) { bench.Log("Could not load types."); bench.Log(e); if (e is ReflectionTypeLoadException) { var typeLoadException = e as ReflectionTypeLoadException; Exception[] loaderExceptions = typeLoadException.LoaderExceptions; bench.Log("Dll Load Exception: " + loaderExceptions[0]); bench.Log(loaderExceptions[0].ToString()); if (loaderExceptions.Count() > 1) { bench.Log("Dll Load Exception: " + loaderExceptions[1]); bench.Log(loaderExceptions[1].ToString()); } } } }