/// <summary> /// Creates a stack panel for the given row element. Row elements can contain only /// group elements and label elements. /// </summary> /// <param name="row"></param> /// <returns></returns> private NStackPanel CreateRow(NXmlElement row, NColor categoryColor) { NStackPanel stack = new NStackPanel(); stack.Direction = ENHVDirection.LeftToRight; stack.FillMode = ENStackFillMode.Equal; // FIX: make this a setting stack.HorizontalSpacing = GroupHorizontalSpacing; stack.Padding = new NMargins(GroupHorizontalSpacing, 0); for (int i = 0, count = row.ChildrenCount; i < count; i++) { NXmlElement child = row.GetChildAt(i) as NXmlElement; if (child == null) { continue; } switch (child.Name) { case "group": stack.Add(CreateGroup(child, categoryColor)); break; case "label": stack.Add(CreateLabel(child)); break; default: throw new Exception("Unsuppoted row child element"); } } return(stack); }
private static bool TryGetFactor(NXmlElement element, string attribute, out double result) { string str = NStringHelpers.SafeTrim(element.GetAttributeValue(attribute)); if (str == null || str.Length == 0) { result = 0; return(false); } bool isPercent = str[str.Length - 1] == '%'; if (isPercent) { str = str.Substring(0, str.Length - 1); } if (Double.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out result) == false) { return(false); } if (isPercent) { result = result / 100; } return(true); }
private static NTreeViewItem GetDefaultExampleItem(NTreeViewItem item, int maxDepth, int depth) { NXmlElement xmlElement = item.Tag as NXmlElement; if (xmlElement != null && xmlElement.GetAttributeValue("default") == "true") { return(item); } if (depth < maxDepth) { depth++; NTreeViewItemCollection items = item.Items; for (int i = 0, count = items.Count; i < count; i++) { NTreeViewItem defaultItem = GetDefaultExampleItem(items[i], maxDepth, depth); if (defaultItem != null) { return(defaultItem); } } } return(null); }
/// <summary> /// Creates a group box for the given group element or a directly a table panel if the /// given group element does not have a name. Group elements can contain only tile /// elements, label elements and other group elements. /// </summary> /// <param name="group"></param> /// <param name="borderColor"></param> /// <returns></returns> private NWidget CreateGroup(NXmlElement group, NColor borderColor) { // Create a table panel NTableFlowPanel tablePanel = CreateTablePanel(group, borderColor); // Get the group title string groupTitle = group.GetAttributeValue("name"); if (String.IsNullOrEmpty(groupTitle)) { return(tablePanel); } // Create a group box NLabel headerLabel = new NLabel(groupTitle); NGroupBox groupBox = new NGroupBox(headerLabel); groupBox.Header.HorizontalPlacement = ENHorizontalPlacement.Center; groupBox.Padding = new NMargins(ItemVerticalSpacing); groupBox.Border = NBorder.CreateFilledBorder(borderColor); // Create the table panel with the examples tiles groupBox.Content = CreateTablePanel(group, borderColor); return(groupBox); }
private void AddItemsFor(NTreeViewItemCollection items, NXmlElement element) { int childCount = element.ChildrenCount; for (int i = 0; i < childCount; i++) { NXmlElement child = element.GetChildAt(i) as NXmlElement; if (child == null) { continue; } NTreeViewItem item = CreateTreeViewItem(child); if (item != null) { items.Add(item); if (item.Tag is NXmlElement == false) { // This is a folder item, so add items for its children, too AddItemsFor(item.Items, child); } } else { AddItemsFor(items, child); } } }
private static NTreeViewItem CreateTreeViewItem(NXmlNode node) { // Create a tree view item for the current XML node NTreeViewItem item; switch (node.NodeType) { case ENXmlNodeType.CDATA: case ENXmlNodeType.Comment: case ENXmlNodeType.Document: item = new NTreeViewItem(node.Name); break; case ENXmlNodeType.Declaration: case ENXmlNodeType.Element: string text = node.Name; NXmlElement element = (NXmlElement)node; INIterator <NKeyValuePair <string, string> > attributesIter = element.GetAttributesIterator(); if (attributesIter != null) { // Append the element attributes StringBuilder sb = new StringBuilder(text); while (attributesIter.MoveNext()) { sb.Append(" "); sb.Append(attributesIter.Current.Key); sb.Append("=\""); sb.Append(attributesIter.Current.Value); sb.Append("\""); } text = sb.ToString(); } item = new NTreeViewItem(text); break; case ENXmlNodeType.Text: item = new NTreeViewItem("Text: \"" + ((NXmlTextNode)node).Text + "\""); break; default: throw new Exception("New ENXmlNodeType?"); } // Traverse the node's children and create a child item for each of them INIterator <NXmlNode> iter = node.GetChildNodesIterator(); if (iter != null) { while (iter.MoveNext()) { NTreeViewItem childItem = CreateTreeViewItem(iter.Current); item.Items.Add(childItem); } } // Return the created tree view item return(item); }
public void InitForElement(NXmlElement element, bool loadDefaultExample) { // Populate the examples tree view if (m_TreeView.Items.Count > 0) { m_TreeView.SelectedItem = null; m_TreeView.Items.Clear(); } int childCount = element.ChildrenCount; if (childCount > 0) { // This is a tile or an example folder AddItemsFor(m_TreeView.Items, element); // Expand all items m_TreeView.ExpandAll(true); } else { // This is a single example m_TreeView.Items.Add(CreateTreeViewItem(element)); } if (loadDefaultExample) { // Find and select the default example m_TreeView.SelectedItem = GetDefaultOrFirstExampleItem(); } else { CreateBreadcrumb(element); } }
/// <summary> /// Parses the background colors of the status labels defined in the examples XML file. /// </summary> /// <param name="styleElement"></param> private void ParseStatusColors(NXmlElement styleElement) { m_StatusColorMap = new NMap <string, NColor>(); if (styleElement == null) { return; } for (int i = 0, count = styleElement.ChildrenCount; i < count; i++) { NXmlElement child = styleElement.GetChildAt(i) as NXmlElement; if (child == null || child.Name != "status") { continue; } // Get the status name string name = child.GetAttributeValue("name"); if (name == null) { continue; } // Parse the status color string colorStr = child.GetAttributeValue("color"); NColor color; if (NColor.TryParse(colorStr, out color) == false) { continue; } // Add the name/color pair to the status color map m_StatusColorMap.Set(name, color); } }
private static NXmlNode SerializeTreeViewItem(NTreeViewItem item) { NElementInfo elementInfo = (NElementInfo)item.Tag; if (elementInfo == null) { string text = ((NLabel)item.Header.Content).Text; return(new NXmlTextNode(ENXmlNodeType.Text, text)); } // Create an XML element for the current tree view item NXmlElement element = new NXmlElement(elementInfo.Name); if (elementInfo.Attributes.Count > 0) { // Set the element's attributes INIterator <NKeyValuePair <string, string> > iter = elementInfo.Attributes.GetIterator(); while (iter.MoveNext()) { element.SetAttribute(iter.Current.Key, iter.Current.Value); } } // Loop through the item's children for (int i = 0, childCount = item.Items.Count; i < childCount; i++) { element.AddChild(SerializeTreeViewItem(item.Items[i])); } return(element); }
/// <summary> /// Creates the product groups stack panel. /// </summary> /// <param name="root"></param> /// <returns></returns> private NStackPanel CreateProductGroupsStack(NXmlElement root) { NMap <string, NStackPanel> stackMap = new NMap <string, NStackPanel>(); // Create the main stack NStackPanel mainStack = new NStackPanel(); mainStack.Direction = ENHVDirection.LeftToRight; mainStack.Margins = new NMargins(0, LaneSpacing * 2); // Create a stack panel for each license groups and add it to the main stack int count = root.ChildrenCount; for (int i = 0; i < count; i++) { NXmlElement categoryElement = root.GetChildAt(i) as NXmlElement; if (categoryElement == null) { continue; } string license = categoryElement.GetAttributeValue("license"); NStackPanel licenseGroupStack; if (!stackMap.TryGet(license, out licenseGroupStack)) { // A stack panel for the license group not found, so create one licenseGroupStack = CreateProductGroupStack(); stackMap.Add(license, licenseGroupStack); // Create a stack for the current group and its name NStackPanel stack = new NStackPanel(); stack.Direction = ENHVDirection.TopToBottom; // 1. Add the license group stack stack.Add(licenseGroupStack); // 2. Add the bracket NColor color = NColor.Parse(categoryElement.GetAttributeValue("color")); NWidget bracket = CreateLicenseGroupBracket(color); stack.Add(bracket); // 3. Add the label NLabel label = new NLabel(license); label.HorizontalPlacement = ENHorizontalPlacement.Center; label.TextFill = new NColorFill(color); label.Font = new NFont(NFontDescriptor.DefaultSansFamilyName, InfoFontSize); stack.Add(label); mainStack.Add(stack); } // Create an image box for the current category NImageBox imageBox = CreateImageBox(categoryElement); licenseGroupStack.Add(imageBox); } return(mainStack); }
/// <summary> /// Navigates to the given element. /// </summary> /// <param name="element"></param> public void NavigateToExample(NXmlElement element) { if (this.Content != m_ExampleHost) { this.Content = m_ExampleHost; m_ExampleHost.InitForElement(element, true); } }
private void OnCopyLinkToClipboardClick(NEventArgs arg) { NDataObject dataObject = new NDataObject(); NXmlElement element = (NXmlElement)arg.CurrentTargetNode.Tag; dataObject.SetData(NDataFormat.TextFormat, m_ExamplesPath + "?example=" + element.GetAttributeValue("type")); NClipboard.SetDataObject(dataObject); }
private static bool IsPartOfBreadcrumb(NXmlElement element) { if (element.Name == "group" && element.ChildrenCount == 1) { return(false); } return(true); }
/// <summary> /// Initializes the panel and creates its content. /// </summary> public void Initialize(NXmlElement root) { Clear(); Add(CreateHeaderLabel()); Add(CreateProductGroupsStack(root)); Add(CreateFooterLabel()); Add(CreatePlatformsStack()); }
private NTreeViewItem CreateTreeViewItem(NXmlElement element) { if (IsSingleExampleTile(element)) { // This is a tile with a single example, so create only the example tree view item return(CreateTreeViewItem((NXmlElement)element.GetChildAt(0))); } NImage icon; string name = element.GetAttributeValue("name"); if (String.IsNullOrEmpty(name)) { return(null); } switch (element.Name) { case "tile": case "group": case "folder": icon = NResources.Image__16x16_Folders_png; break; case "example": icon = NResources.Image__16x16_Contacts_png; break; default: return(null); } NExampleTile tile = new NExampleTile(icon, name); tile.Status = element.GetAttributeValue("status"); tile.Box2.VerticalPlacement = ENVerticalPlacement.Center; tile.Spacing = NDesign.HorizontalSpacing; NTreeViewItem item = new NTreeViewItem(tile); if (element.Name == "example") { // This is an example element item.Tag = element; if (NApplication.Platform == ENPlatform.Silverlight) { // Handle the right click event in Silverlight to show a context menu // for copying a link to the example item.MouseDown += OnTreeViewItemMouseDown; } } return(item); }
private void OnBreadcrumbButtonClick(NEventArgs arg) { // Close the old example CloseExample(); // Load the new example NButton button = (NButton)arg.CurrentTargetNode; NXmlElement element = (NXmlElement)button.Tag; InitForElement(element, false); }
/// <summary> /// Creates a table panel, which is the content of a group. /// </summary> /// <param name="owner"></param> /// <param name="borderColor"></param> /// <returns></returns> private NTableFlowPanel CreateTablePanel(NXmlElement owner, NColor borderColor) { int maxRows; if (TryGetInt(owner, "maxRows", out maxRows) == false) { maxRows = defaultMaxRows; } // Create a table flow panel for the items NTableFlowPanel table = new NTableFlowPanel(); table.UniformWidths = ENUniformSize.Max; table.UniformHeights = ENUniformSize.Max; table.ColFillMode = ENStackFillMode.Equal; table.Direction = ENHVDirection.TopToBottom; table.HorizontalSpacing = ItemHorizontalSpacing; table.VerticalSpacing = ItemVerticalSpacing; table.MaxOrdinal = maxRows; // Create the items and add them to the table panel string categoryNamespace = GetNamespace(owner); int childCount = owner.ChildrenCount; for (int i = 0; i < childCount; i++) { NXmlElement child = owner.GetChildAt(i) as NXmlElement; if (child == null) { continue; } switch (child.Name) { case "tile": table.Add(CreateTile(child, categoryNamespace)); break; case "group": table.Add(CreateGroup(child, borderColor)); break; case "label": table.Add(CreateLabel(child)); break; default: throw new Exception("New examples XML tag?"); } } return(table); }
internal static string GetNamespace(NXmlElement element) { string result; do { result = element.GetAttributeValue("namespace"); element = element.Parent as NXmlElement; }while ((result == null || result.Length == 0) && element != null); return(result); }
private static bool TryGetInt(NXmlElement element, string attribute, out int result) { string str = element.GetAttributeValue(attribute); if (str == null || str.Length == 0) { result = 0; return(false); } return(Int32.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out result)); }
public void LoadFromStream(Stream stream) { // Load an xml document from the stream NXmlDocument xmlDocument = NXmlDocument.LoadFromStream(stream); // Process it if (xmlDocument == null || xmlDocument.ChildrenCount != 1) { return; } m_ExamplesMap = new NStringMap <NWidget>(false); // Get the root element (i.e. the <document> element) NXmlElement rootElement = (NXmlElement)xmlDocument.GetChildAt(0); // Process the head NXmlElement titleElement = (NXmlElement)rootElement.GetFirstChild("title"); m_HeaderLabel.Text = ((NXmlTextNode)titleElement.GetChildAt(0)).Text; NXmlElement statusColorsElement = (NXmlElement)rootElement.GetFirstChild("statusColors"); ParseStatusColors(statusColorsElement); // Process the categories NXmlElement categoriesElement = (NXmlElement)rootElement.GetFirstChild("categories"); ((NWelcomePanel)m_PagePanel[0]).Initialize(categoriesElement); for (int i = 0, count = categoriesElement.ChildrenCount; i < count; i++) { NXmlElement child = categoriesElement.GetChildAt(i) as NXmlElement; if (child == null) { continue; } if (child.Name != "category") { throw new Exception("The body element can contain only category elements"); } // Create a widget and add it to the categories panel NWidget category = CreateCategory(child); m_PagePanel.Add(category); } // Init the search box m_SearchBox.InitAutoComplete(m_ExamplesMap, new NExampleFactory()); m_SearchBox.ListBoxItemSelected += OnSearchBoxListBoxItemSelected; }
private static int GetDistanceFromRoot(NXmlElement element) { int distanceFromRoot = 0; NXmlNode node = element.Parent; while (node != null) { node = node.Parent; distanceFromRoot++; } return(distanceFromRoot - 1); }
private NImageBox CreateImageBox(NXmlElement categoryElement) { string emfName = categoryElement.GetAttributeValue("namespace") + ".emf"; // Get the metafile byte[] metaImage = EmfDecompressor.GetMetaImage(emfName); // Create an image box for it NComponentImageBox imageBox = new NComponentImageBox(); imageBox.Image = new NImage(new NBytesImageSource(metaImage)); imageBox.Status = categoryElement.GetAttributeValue("status"); imageBox.Tag = categoryElement; return(imageBox); }
private void OnTreeViewSelectedPathChanged(NValueChangeEventArgs arg) { // Close the old example CloseExample(); // Load the new example NTreeViewItem selectedItem = ((NTreeView)arg.TargetNode).SelectedItem; if (selectedItem != null) { NXmlElement element = selectedItem.Tag as NXmlElement; if (element != null) { LoadExample(element); } } }
/// <summary> /// Navigates to the given example. This is used by the Silverlight examples when /// there's an example specified in the query string, for example: /// "SilverlightTestPage.html?example=UI.NTooBarExample". /// </summary> /// <param name="exampleType"></param> public void NavigateToExample(string exampleType) { NXmlDocument document; using (Stream stream = NResources.Instance.GetResourceStream("RSTR_Examples_xml")) { document = NXmlDocument.LoadFromStream(stream); } // Find the XML element with the given example type: NXmlElement element = GetExampleElement(document, exampleType); if (element != null) { NavigateToExample(element); } }
private void OnExampleCategoryMouseUp(NMouseButtonEventArgs arg) { NXmlElement element = (NXmlElement)arg.TargetNode.Tag; // Find the category correpsonding to the XML element of the clicked header int count = m_PagePanel.Count; for (int i = 0; i < count; i++) { if (m_PagePanel[i].Tag == element) { m_PagePanel.VisibleIndex = i; return; } } // No category selected, so make the Welcome Screen visible m_PagePanel.VisibleIndex = 0; }
private void CreateBreadcrumb(NXmlElement element) { NList <NXmlElement> path = GetBreadcrumbPath(element); for (int i = 0, count = path.Count; i < count; i++) { NXmlElement curElement = path[i]; if (IsSingleExampleTile(curElement)) { continue; } string name = curElement.GetAttributeValue("name"); if (String.IsNullOrEmpty(name)) { continue; } if (m_Toolbar.Items[m_Toolbar.Items.Count - 1] is NButton) { NLabel label = new NLabel(" > "); label.TextAlignment = ENContentAlignment.MiddleCenter; label.VerticalPlacement = ENVerticalPlacement.Fit; m_Toolbar.Items.Add(label); } if (i != (count - 1)) { NButton button = new NButton(name); button.Content.VerticalPlacement = ENVerticalPlacement.Center; button.Click += OnBreadcrumbButtonClick; button.Tag = path[i]; m_Toolbar.Items.Add(button); } else { NLabel label = new NLabel(name); label.VerticalPlacement = ENVerticalPlacement.Center; m_Toolbar.Items.Add(label); } } }
/// <summary> /// Creates a widget for the given category element. /// Category elements can contain only row elements. /// </summary> /// <param name="category"></param> /// <returns></returns> private NWidget CreateCategory(NXmlElement category) { string categoryTitle = category.GetAttributeValue("name"); NColor color = NColor.ParseHex(category.GetAttributeValue("color")); NColor lightColor = color.Lighten(0.6f); // Create the header label NExampleCategoryHeader categoryHeader = new NExampleCategoryHeader(categoryTitle); categoryHeader.BackgroundFill = new NColorFill(color); categoryHeader.Status = category.GetAttributeValue("status"); // Create a stack panel for the category widgets NStackPanel stack = new NStackPanel(); stack.VerticalSpacing = ItemVerticalSpacing; stack.BackgroundFill = new NColorFill(lightColor); stack.Tag = category; // Add the header label stack.Add(categoryHeader); // Loop through the rows for (int i = 0, count = category.ChildrenCount; i < count; i++) { NXmlElement row = category.GetChildAt(i) as NXmlElement; if (row == null) { continue; } if (row.Name != "row") { throw new Exception("Category elements should contain only rows"); } NStackPanel rowStack = CreateRow(row, color); stack.Add(rowStack); } return(stack); }
private static NXmlElement GetExampleElement(NXmlNode node, string type) { NXmlElement element = node as NXmlElement; if (element != null && element.Name == "example" && element.GetAttributeValue("type") == type) { return(element); } for (int i = 0, count = node.ChildrenCount; i < count; i++) { element = GetExampleElement(node.GetChildAt(i), type); if (element != null) { return(element); } } return(null); }
private void LoadExample(NXmlElement element) { string groupNamespace = NExamplesHomePage.GetNamespace(element); string name = element.GetAttributeValue("name"); string type = groupNamespace + "." + element.GetAttributeValue("type"); try { type = "Nevron.Nov.Examples." + type; Type exampleType = Type.GetType(type); if (exampleType != null) { NDomType domType = NDomType.FromType(exampleType); NDebug.Assert(domType != null, "The example type:" + type + " is not a valid type"); // Create the example DateTime start = DateTime.Now; NExampleBase example = domType.CreateInstance() as NExampleBase; example.Title = name; example.Initialize(); m_Splitter.Pane2.Content = example; string stats = "Example created in: " + (DateTime.Now - start).TotalSeconds + " seconds, "; // Evaluate the example start = DateTime.Now; OwnerDocument.Evaluate(); stats += " evaluated in: " + (DateTime.Now - start).TotalSeconds + " seconds"; m_StatusLabel.Text = stats; } // Set the breadcrumb CreateBreadcrumb(element); } catch (Exception ex) { NTrace.WriteException("Failed to load example", ex); m_Splitter.Pane2.Content = new NErrorPanel("Failed to load example. Exception was: " + ex.Message); } }
/// <summary> /// Gets a list of elements that represent the breadcrumb (path) to the given element /// from the root. /// </summary> /// <param name="element"></param> /// <returns></returns> private static NList <NXmlElement> GetBreadcrumbPath(NXmlElement element) { NList <NXmlElement> list = new NList <NXmlElement>(); list.Add(element); element = (NXmlElement)element.Parent; while (element.Name != "document") { // A tile with only 1 child or a tile single child of a group should not be added to the breadcrumb if (IsPartOfBreadcrumb(element)) { list.Add(element); } element = (NXmlElement)element.Parent; } list.Reverse(); return(list); }