private object[] GetStyleMappingResults(StyleModelMapping mapping, Type propertyType, object[] testData, Project project) { if (mapping == null) throw new ArgumentNullException("maping"); if (testData == null) throw new ArgumentNullException("testData"); // Define mapping int valueCnt = 5; int[] values = new int[valueCnt]; values[0] = int.MinValue; values[1] = -1000; values[2] = 0; values[3] = 1000; values[4] = int.MaxValue; IStyle[] styles = new IStyle[valueCnt]; if (IsOfType(propertyType, typeof(ICapStyle))) { styles[0] = project.Design.CapStyles.Arrow; styles[1] = project.Design.CapStyles.Special1; styles[2] = project.Design.CapStyles.None; styles[3] = project.Design.CapStyles.Special2; styles[4] = project.Design.CapStyles.Arrow; } else if (IsOfType(propertyType, typeof(ICharacterStyle))) { styles[0] = project.Design.CharacterStyles.Caption; styles[1] = project.Design.CharacterStyles.Heading1; styles[2] = project.Design.CharacterStyles.Heading2; styles[3] = project.Design.CharacterStyles.Heading3; styles[4] = project.Design.CharacterStyles.Normal; } else if (IsOfType(propertyType, typeof(IColorStyle))) { styles[0] = project.Design.ColorStyles.Black; styles[1] = project.Design.ColorStyles.Red; styles[2] = project.Design.ColorStyles.Yellow; styles[3] = project.Design.ColorStyles.Green; styles[4] = project.Design.ColorStyles.White; } else if (IsOfType(propertyType, typeof(IFillStyle))) { styles[0] = project.Design.FillStyles.Black; styles[1] = project.Design.FillStyles.Red; styles[2] = project.Design.FillStyles.Yellow; styles[3] = project.Design.FillStyles.Green; styles[4] = project.Design.FillStyles.White; } else if (IsOfType(propertyType, typeof(ILineStyle))) { styles[0] = project.Design.LineStyles.Special1; styles[1] = project.Design.LineStyles.Red; styles[2] = project.Design.LineStyles.Yellow; styles[3] = project.Design.LineStyles.Green; styles[4] = project.Design.LineStyles.Special2; } else if (IsOfType(propertyType, typeof(IParagraphStyle))) { styles[0] = project.Design.ParagraphStyles.Label; styles[1] = project.Design.ParagraphStyles.Text; styles[2] = project.Design.ParagraphStyles.Title; styles[3] = project.Design.ParagraphStyles.Text; styles[4] = project.Design.ParagraphStyles.Label; } // Fill mapping mapping.ClearValueRanges(); for (int i = 0; i < valueCnt; ++i) { if (mapping.CanSetInteger) mapping.AddValueRange(values[i], styles[i]); else if (mapping.CanSetFloat) mapping.AddValueRange((float)values[i], styles[i]); } // Set expected results int cnt = testData.Length; object[] result = new object[cnt]; for (int i = 0; i < cnt; ++i) { double value; if (mapping.CanSetFloat) value = Convert.ToSingle(testData[i]); else value = Convert.ToDouble(testData[i]); if (double.IsNaN(value) || double.IsNegativeInfinity(value)) result[i] = null; else if (value < values[1]) result[i] = styles[0]; else if (value >= values[1] && value < values[2]) result[i] = styles[1]; else if (value >= values[2] && value < values[3]) result[i] = styles[2]; else if (value >= values[3] && value < values[4]) result[i] = styles[3]; else if (value >= values[4]) result[i] = styles[4]; else result[i] = null; } return result; }
/// <summary> /// Updates the given ModelMapping with the information from the given row's cells /// </summary> private void UpdateStyleModelMapping(StyleModelMapping styleMapping, int rowIndex) { // First, clear all ranges // ToDo: Optimize this: add new ranges, remove deleted ranges, do not recreate all ranges each time styleMapping.ClearValueRanges(); int rangeColIdx = 0; int styleColIdx = 1; for (int i = 0; i < valueMappingGrid.Rows.Count; ++i) { object rangeValue = valueMappingGrid.Rows[i].Cells[rangeColIdx].Value; object styleName = valueMappingGrid.Rows[i].Cells[styleColIdx].Value; IStyle style = null; // Find style if (valueMappingGrid.Rows[i].Cells[styleColIdx].Value != null) { PropertyInfo shapePropInfo = FindPropertyInfo(shapePropertyInfos, styleMapping.ShapePropertyId); // Get Style value string styleTitle = valueMappingGrid.Rows[i].Cells[styleColIdx].Value.ToString(); foreach (IStyle s in TemplateController.Project.Design.Styles) { Type styleType = s.GetType(); if (!IsOfType(styleType, shapePropInfo.PropertyType)) continue; else if (s.Title.Equals(styleTitle, StringComparison.InvariantCultureIgnoreCase)) { style = s; break; } } } // if all required values were found, create a new mapping range if (rangeValue != null && style != null) { if (styleMapping.Type == StyleModelMapping.MappingType.IntegerStyle) styleMapping.AddValueRange((int)rangeValue, style); else if (styleMapping.Type == StyleModelMapping.MappingType.FloatStyle) styleMapping.AddValueRange((float)rangeValue, style); else throw new NShapeUnsupportedValueException(styleMapping.Type); } } // Set ModelMapping templateController.SetModelMapping(styleMapping); }