private IHierarchyObject PreviousSibling(IHierarchyObject item) { if (item == null) { return(null); } var parent = item.GetParent(); if (parent == null) { return(null); } IHierarchyObject previous = null; foreach (var child in parent.GetAllChildren()) { if (child.Equals(item)) { return(previous); } previous = child; } Debug.Assert(false); return(null); }
private IHierarchyObject NextSibling(IHierarchyObject item) { if (item == null) { return(null); } var parent = item.GetParent(); if (parent == null) { return(null); } bool found = false; foreach (var child in parent.GetAllChildren()) { if (found) { return(child); } if (child.Equals(item)) { found = true; } } Debug.Assert(found); return(null); }
/// <summary> /// Transforme un sous-noeud en noeud /// </summary> /// <param name="model">le noeud à édenté</param> public void OutdentNode(IHierarchyObject item) { if (item.GetParent() != null) { IHierarchyObject parent = item.GetParent().GetParent(); if (parent != null) { item.GetParent().ForgetChild(item); parent.AddChild(item); if (Change != null) { Change(); } SelectItem(this, item); } } }
/// <summary> /// Transforme un noeud en sous-noeud /// </summary> /// <param name="model">le noeud à édenté</param> public void IndentNode(IHierarchyObject item) { if (item.GetParent() != null) { int position = item.GetPosition(); IHierarchyObject child = item.GetParent().GetChildByPosition(position - 1); if (child != null) { item.GetParent().ForgetChild(item); child.AddChild(item); if (Change != null) { Change(); } SelectItem(this, item); } } }
/// <summary> /// Déplace un noeud vers le haut ou vers le bas /// </summary> /// <param name="item"></param> /// <param name="sens">Le sens du déplacement. 0 => UP, 1=> DOWN</param> public void MoveNode(IHierarchyObject item, int sens = 0) { if (item.GetParent() != null) { int position = item.GetPosition() + (sens == 0 ? -1 : 1); IHierarchyObject child = item.GetParent().GetChildByPosition(position); if (child != null) { child.SetPosition(item.GetPosition()); item.GetParent().UpdateChild(child); item.SetPosition(position); item.GetParent().UpdateChild(item); if (Change != null) { Change(); } SelectItem(this, item); } } }
/// <summary> /// Supprime un noeud et ses fils. /// </summary> /// <param name="model">Le noeud à supprimer</param> public void DeleteNode(IHierarchyObject item) { if (item != null && item.GetParent() != null) { MessageBoxResult result = Util.MessageDisplayer.DisplayYesNoQuestion("Delete Item", "Do you want to delete node : " + item.ToString() + "?"); if (result == MessageBoxResult.Yes) { int index = item.GetPosition(); item.GetParent().RemoveChild(item); item.GetParent().UpdateParents(); if (Change != null) { Change(); } if (this.Root.GetItems().Count > 1) { selectNodeAfterDelete(item, index); } } } }
/// <summary> /// Returns the item (or null) directly following <paramref name="item"/> in /// "previous sibling, parent" order. /// </summary> public IHierarchyObject GetPreviousItem(IHierarchyObject item) { if (item == null) { return(null); } // a // b // c // d // e // // Return "b" is we are on "c" // Return "d" if we are on "e" // Return "e" if we are on "a" // Return "a" if we are on "b" // var result = PreviousSibling(item); if (result == null) { var parent = item.GetParent(); if (parent != null) { return(parent); } // "a" case: get the very last child at the bottom of the tree while (true) { Debug.Assert(item != null); var last = LastChild(item); if (last == null) { return(item); } item = last; } } while (true) { Debug.Assert(result != null); var last = LastChild(result); if (last == null) { return(result); } result = last; } }
/// <summary> /// Select the visual TreeViewItem corresponding to the view model <paramref /// name="hierarchyObject"/>. /// /// Note: This method is synchronous (i.e. does not post any message to the /// UI thread queue) and works only with a virtualizing stack panel. /// </summary> public static TreeViewItem SelectTreeViewItem(TreeView treeView, IHierarchyObject hierarchyObject) { // Build child->parent->ancestor(s) sequence as a stack so we can go // top-down in the tree during next phase. var objectStack = new Stack <IHierarchyObject>(); while (hierarchyObject != null) { objectStack.Push(hierarchyObject); hierarchyObject = hierarchyObject.GetParent(); } // Call "BringObjectToView" on each hierarchy object in the stack, keeping // track of the parent "ItemsControl" ("TreeView" or "TreeViewItem") to // expand and dive into. ItemsControl parentItemsControl = treeView; IHierarchyObject parentObject = null; while (objectStack.Count > 0) { var childObject = objectStack.Pop(); if (childObject.IsVisual) { var childItemsControl = BringObjectToView(parentItemsControl, parentObject, childObject); if (childItemsControl == null) { Logger.LogWarning("Tree view item corresponding to hierarchy object was not found."); parentItemsControl = null; break; } parentItemsControl = childItemsControl; } parentObject = childObject; } // If the desired tree view item has been, select it var desiredTreeViewItem = parentItemsControl as TreeViewItem; if (desiredTreeViewItem != null) { SetSelectedItem(treeView, desiredTreeViewItem); } return(desiredTreeViewItem); }
/// <summary> /// Returns the item (or null) directly following <paramref name="item"/> in /// "first child, next sibling" order. /// </summary> public IHierarchyObject GetNextItem(IHierarchyObject item) { if (item == null) { return(null); } // First child if any var result = FirstChild(item); if (result != null) { return(result); } // a // b // c // d // e // // Return "b" is we are on "a" // Return "d" if we are on "c" // Return "e" if we are on "d" // Return "a" if we are on "e" // while (true) { Debug.Assert(item != null); result = NextSibling(item); if (result != null) { return(result); } var parent = item.GetParent(); if (parent == null) { return(item); } item = parent; } }
public void selectNodeAfterDelete(IHierarchyObject itemSupprime, int i) { IHierarchyObject itemParent = itemSupprime.GetParent(); IHierarchyObject itemASelectionne; if (itemParent.GetItems().Count > 0) { if (i > 0) { itemASelectionne = itemParent.GetItems()[i - 1] as IHierarchyObject; } else { itemASelectionne = itemParent.GetItems()[0] as IHierarchyObject; } } else { itemASelectionne = itemParent; } SelectItem(this, itemASelectionne); }
/// <summary> /// Cette méthode permet de désactiver un menuItem dans le cas /// où l'opération associée à ce menuItem n'est pas possible pour /// le noeud courant. /// </summary> /// <param name="e"></param> protected override void OnContextMenuOpening(ContextMenuEventArgs e) { IHierarchyObject selectedItem = (IHierarchyObject)this.SelectedItem; this.NewMenuItem.IsEnabled = this.Root != null; this.EditMenuItem.IsEnabled = this.Root != null && selectedItem != null && CurrentParent != null; this.CutMenuItem.IsEnabled = this.Root != null && selectedItem != null && CurrentParent != null; this.CopyMenuItem.IsEnabled = this.Root != null && selectedItem != null && CurrentParent != null; this.PasteMenuItem.IsEnabled = this.Root != null && selectedItem != null && !Util.ClipbordUtil.IsClipBoardEmpty(); this.DeleteMenuItem.IsEnabled = this.Root != null && selectedItem != null && CurrentParent != null; this.MoveUpMenuItem.IsEnabled = this.Root != null && selectedItem != null && selectedItem.GetPosition() > 0 && CurrentParent != null; this.MoveDownMenuItem.IsEnabled = this.Root != null && selectedItem != null && selectedItem.GetPosition() < selectedItem.GetParent().GetItems().Count - 1 && CurrentParent != null; this.IndentMenuItem.IsEnabled = this.Root != null && selectedItem != null && selectedItem.GetPosition() > 0 && CurrentParent != null; this.OutdentMenuItem.IsEnabled = this.Root != null && selectedItem != null && selectedItem.GetParent() != null && selectedItem.GetParent() != Root && CurrentParent != null; }