public static OutlineItem LoadFromClipboard() { if (Clipboard.ContainsText()) { var clipboardText = Clipboard.GetText(); try { var xmlElement = XElement.Parse(clipboardText); var item = OutlineItem.CreateFromXmlElement(xmlElement); return(item); } catch (Exception) { return(null); } } return(null); /* * var clipboardAdapter = new OutlineItemCollectionClipboardAdapter(); * var itemCollection = clipboardAdapter.GetItemCollection(); * * if (itemCollection != null && itemCollection.Count > 0) * { * return itemCollection[0]; * } * * return null;*/ }
public static void ReplaceWith(this OutlineItem itemToReplace, OutlineItem item) { var parent = itemToReplace.ParentItem; var index = itemToReplace.IndexInParent(); parent.Items.RemoveAt(index); parent.Items.Insert(index, item); }
public static int IndexInParent(this OutlineItem item) { if (item.ParentItem != null) { return(item.ParentItem.Items.IndexOf(item)); } return(-1); }
public OutlineItem Clone() { //Temporary solution. Todo: Implement copy constructor. var thisItemAsXmlElement = ToXmlElement(); var clone = OutlineItem.CreateFromXmlElement(thisItemAsXmlElement); return(clone); }
public static OutlineItem PreviousItem(this OutlineItem item) { var prevSibling = item.PreviousSibling(); if (prevSibling != null) { return(prevSibling); } return(item.ParentItem); }
public static bool IsFirstChildOfParent(this OutlineItem item) { if (item.IndexInParent() == 0) { return(true); } if (item.IndexInParent() == -1) { throw new InvalidOperationException(); } return(false); }
public static bool IsLastChildOfParent(this OutlineItem item) { var indexInParent = item.IndexInParent(); if (indexInParent != -1) { if (item.ParentItem.Items.Count == indexInParent + 1) { return(true); } } throw new InvalidOperationException(); }
public static OutlineDocument CreateFromXmlElement(XElement xmlElement) { if (xmlElement == null) { throw new ArgumentNullException(nameof(xmlElement)); } if (xmlElement.Name != "SimpleOutlineDocument") { throw new DecodingException(); } if (xmlElement.Attributes().Count() > 0) { throw new DecodingException(); } if (xmlElement.Elements().SingleOrDefault(e => e.Name != "Outline") != null) { throw new DecodingException(); } if (xmlElement.Elements().SingleOrDefault(e => e.Name == "Outline") == null) { throw new DecodingException(); } var xmlOutlineElement = xmlElement.Element("Outline"); if (xmlOutlineElement.Attributes().Count() > 0) { throw new DecodingException(); } if (xmlOutlineElement.Elements("Item").Count() != 1) { throw new DecodingException(); } var thisOutlineDocument = new OutlineDocument(); var rootItemXmlElement = xmlOutlineElement.Element("Item"); var rootOutlineItem = OutlineItem.CreateFromXmlElement(rootItemXmlElement); thisOutlineDocument.Items[0] = rootOutlineItem; return(thisOutlineDocument); }
public static OutlineItem PreviousSibling(this OutlineItem item) { var indexInParent = item.IndexInParent(); if (indexInParent == -1) { return(null); } if (!item.IsFirstChildOfParent()) { return(item.ParentItem.Items[indexInParent - 1]); } return(null); }
public static OutlineItem NextSibling(this OutlineItem item) { var indexInParent = item.IndexInParent(); if (indexInParent == -1) { return(null); } if (!item.IsLastChildOfParent()) { return(item.ParentItem.Items[indexInParent + 1]); } return(null); }
public static OutlineItem CreateFromXmlElement(XElement xmlElement) { if (xmlElement == null) { throw new ArgumentNullException(nameof(xmlElement)); } if (xmlElement.Name != "Item") { throw new DecodingException(); } if (xmlElement.Attributes().SingleOrDefault(attr => attr.Name == "Name") == null) { throw new DecodingException(); } if (xmlElement.Attributes().SingleOrDefault(attr => attr.Name != "Name") != null) { throw new DecodingException(); } if (xmlElement.Elements().SingleOrDefault(elem => elem.Name != "Item") != null) { throw new DecodingException(); } var thisOutlineItemName = xmlElement.Attribute("Name").Value.ToString(); var thisOutlineItem = new OutlineItem(thisOutlineItemName); foreach (XElement childOutlineItemXmlElement in xmlElement.Elements("Item")) { var childOutlineItem = OutlineItem.CreateFromXmlElement(childOutlineItemXmlElement); thisOutlineItem.Items.Add(childOutlineItem); } return(thisOutlineItem); }
public static OutlineItem NextItem(this OutlineItem item) { if (item.Items.Count != 0) { return(item.Items[0]); } var nextSibling = item.NextSibling(); if (nextSibling != null) { return(nextSibling); } var parentOfItem = item.ParentItem; if (parentOfItem == null) { return(null); } return(parentOfItem.NextSibling()); }