private void removeConnectable(FilteredConnectablesGroup rootGroup, IConnectable connectable, IEnumerable <ITECObject> parentPath) { if (!filterPredicate(connectable)) { return; } List <FilteredConnectablesGroup> path = rootGroup.GetPath(connectable); path[path.Count - 2].Remove(path.Last()); FilteredConnectablesGroup parentGroup = rootGroup; for (int i = 1; i < path.Count; i++) { if (!containsConnectable(path[i])) { parentGroup.Remove(path[i]); return; } else { parentGroup = path[i]; } } }
private void addConnectable(FilteredConnectablesGroup rootGroup, IConnectable connectable, IEnumerable <ITECObject> parentPath) { if (!filterPredicate(connectable)) { return; } IRelatable rootScope = rootGroup.Scope as IRelatable ?? this.root; List <ITECObject> path = new List <ITECObject>(parentPath); if (rootScope != parentPath.First()) { path = rootScope.GetObjectPath(parentPath.First()); path.Remove(parentPath.First()); path.AddRange(parentPath); } if (path.Count == 0) { logger.Error("New connectable doesn't exist in root object."); return; } FilteredConnectablesGroup lastGroup = rootGroup; int lastIndex = 0; for (int i = path.Count - 1; i > 0; i--) { if (path[i] is ITECScope scope) { FilteredConnectablesGroup group = rootGroup.GetGroup(scope); if (group != null) { lastGroup = group; lastIndex = i; break; } } else { logger.Error("Object in path to connectable isn't ITECScope, cannot build group hierarchy."); return; } } FilteredConnectablesGroup currentGroup = lastGroup; for (int i = lastIndex + 1; i < path.Count; i++) { if (path[i] is ITECScope scope) { currentGroup = currentGroup.Add(scope); } else { logger.Error("Object in path to connectable isn't ITECScope, cannot build group hierarchy."); return; } } }
/// <summary> /// /// </summary> /// <param name="root"></param> /// <param name="watcher"></param> /// <param name="includeFilter">Predicate for "where" clause of direct children of root.</param> public ConnectionsVM(IRelatable root, ChangeWatcher watcher, TECCatalogs catalogs, IEnumerable <TECLocation> locations = null, Func <ITECObject, bool> filterPredicate = null) { if (filterPredicate == null) { filterPredicate = item => true; } this.filterPredicate = filterPredicate; ConnectableFilter.basePredicate = connectable => { if (!connectable.IsConnected() && connectable.AvailableProtocols.Count == 0) { return(false); } if (connectable == SelectedController) { return(false); } return(true); }; this.InterlocksVM = new InterlocksVM(root, watcher, catalogs, filterPredicate); this.root = root; this.Catalogs = catalogs; this.Locations = locations != null ? new ObservableCollection <TECLocation>(locations) : new ObservableCollection <TECLocation>(); if (this.Catalogs.ConduitTypes.Count > 0) { this.DefaultConduitType = this.Catalogs.ConduitTypes[0]; } watcher.Changed += parentChanged; new DirectRelationshipChangedFilter(watcher).DirectRelationshipChanged += parentScopeChanged; this.rootConnectableGroup = new FilteredConnectablesGroup("root", this.ConnectableFilter); this.rootControllerGroup = new FilteredConnectablesGroup("root", this.ControllerFilter); repopulateGroups(null, root, addConnectable); SelectProtocolCommand = new RelayCommand(selectProtocolExecute, selectProtocolCanExecute); CancelProtocolSelectionCommand = new RelayCommand(cancelProtocolSelectionExecute); ConnectionDropHandler = new NetworkConnectionDropTarget(this); this.ControllerFilter.FilterChanged += () => { if (SelectedControllerGroup?.PassesFilter == false) { SelectedControllerGroup = null; } }; this.ConnectableFilter.FilterChanged += () => { if (SelectedConnectableGroup?.PassesFilter == false) { SelectedConnectableGroup = null; } }; DeleteCommand = new RelayCommand <IControllerConnection>(deleteConnectionExecute, canDeleteConnection); }
private static bool containsConnectable(FilteredConnectablesGroup group) { if (group.Scope is IConnectable) { return(true); } foreach (FilteredConnectablesGroup childGroup in group.ChildrenGroups) { if (containsConnectable(childGroup)) { return(true); } } return(false); }