/// <summary>
            /// adds <see cref="PropertyColumn"/>s corresponding to <see cref="PortfolioEntry"/> properties
            /// to table.
            /// </summary>
            private void addPortfolioEntryPropertyColumns()
            {
                // add each property column corresponding to PortfolioEntry
                foreach (eDisplayProperty displayProperty in displayPropertyToPropertyColumn.Keys)
                {
                    PropertyColumn propertyColumn =
                        displayPropertyToPropertyColumn[displayProperty];

                    propertyTable.AddColumn(propertyColumn);
                }
            }
示例#2
0
            /// <summary>
            /// adds <see cref="PropertyColumn"/>s corresponding to <see cref="CoinTicker"/> to table.
            /// </summary>
            private void initCoinTickerPropertyColumns()
            {
                // add PropertyColumns corresponding to CoinTicker to table
                foreach (eDisplayProperty displayProperty in displayPropertyToPropertyColumn.Keys)
                {
                    PropertyColumn propertyColumn =
                        displayPropertyToPropertyColumn[displayProperty];

                    propertyTable.AddColumn(propertyColumn);
                }
            }
示例#3
0
                public override bool Equals(object obj)
                {
                    if (obj == null)
                    {
                        return(false);
                    }

                    PropertyColumn other = obj as PropertyColumn;

                    return(other != null &&
                           base.Equals(other) &&
                           this.property == other.property);
                }
示例#4
0
            // false if remove was unsuccessful / item not found in row list
            /// <summary>
            /// removes <paramref name="propertyColumn"/> from <see cref="PropertyTable"/>'s column list,
            /// if it exists in column list.
            /// returns whether <paramref name="propertyColumn"/> existed in <see cref="PropertyTable"/>'s column list
            /// before being removed.
            /// </summary>
            /// <seealso cref="Table.RemoveColumn(Column)"/>
            /// <param name="propertyColumn"></param>
            /// <returns>
            /// true if <paramref name="propertyColumn"/> existed in <see cref="PropertyTable"/>'s column list
            /// before being removed,
            /// else false
            /// </returns>
            /// <exception cref="OperationRequiresEmptyTableException">
            /// <seealso cref="Table.RemoveColumn(Column)"/>
            /// </exception>
            public bool RemoveColumn(PropertyColumn propertyColumn)
            {
                // remove from column list
                bool removalResult = table.RemoveColumn(propertyColumn);

                if (removalResult)
                {
                    // remove property corresponding to removed column
                    columnProperties.Remove(propertyColumn.Property);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
示例#5
0
        /// <summary>Format the SoilCrop column.</summary>
        /// <param name="column">The column to format.</param>
        private void FormatSoilCropColumn(PropertyColumn column)
        {
            var soilCrop = column.ObjectWithProperty as SoilCrop;

            column.ColumnName = soilCrop.Name.Replace("Soil", "") + " " + column.ColumnName;

            // Colour the crop column.
            var crops       = soilCrop.Parent.Children.Where(child => child is SoilCrop).ToList();
            int cropIndex   = crops.IndexOf(soilCrop);
            int colourIndex = cropIndex % ColourUtilities.Colours.Length;

            column.ForegroundColour = ColourUtilities.Colours[colourIndex];

            // Make the soil crop columns wider to fit the crop name in column title.
            column.Width = 90;
        }
示例#6
0
        public void TestCreateAdapter()
        {
            DataTableAdapterFactory factory = new DataTableAdapterFactory();

            PropertyColumn[] columns = new PropertyColumn[]
            {
                new PropertyColumn("Class1.Field", "Field"),
                new PropertyColumn("Class1.Property", "Property"),
                new PropertyColumn("Time", "Time")
            };

            IDataTableAdapter <TestClass2> adapter = factory.New <TestClass2>(columns);

            List <TestClass2> list = new List <TestClass2>();

            for (int i = 0; i < 10000; i++)
            {
                list.Add(new TestClass2(new TestClass1(4, "4")));
                list.Add(new TestClass2(new TestClass1(40, "40")));
                list.Add(new TestClass2(new TestClass1(12, "12")));
                list.Add(new TestClass2());
            }

            DataTable dt = adapter.Get(list);

            Assert.IsNotNull(dt);
            Assert.AreEqual(list.Count, dt.Rows.Count);
            Assert.AreEqual(4, dt.Columns.Count);

            Assert.AreEqual(list[0].Class1.Property, dt.Rows[0]["Property"]);
            Assert.AreEqual(list[0].Class1.Field, dt.Rows[0]["Field"]);
            Assert.AreEqual(list[0].Time, dt.Rows[0]["Time"]);
            Assert.AreSame(list[0], dt.Rows[0]["DataItem"]);

            IDataTableAdapter <TestClass2> adapter2 = factory.New <TestClass2>(columns);

            Assert.AreSame(adapter, adapter2);

            PropertyColumn[] columns2 = new PropertyColumn[]
            {
                new PropertyColumn("Class1.Field", "Field")
            };

            IDataTableAdapter <TestClass2> adapter3 = factory.New <TestClass2>(columns2);

            Assert.AreNotSame(adapter, adapter3);
        }
示例#7
0
        /// <summary>Find all properties to display in the grid.</summary>
        /// <param name="model">The underlying model we are to use to find the properties.</param>
        private void FindAllProperties(Model model)
        {
            // When user clicks on a SoilCrop, there is no thickness column. In this
            // situation get thickness column from parent model.
            if (model is SoilCrop && propertiesInGrid.Count == 0)
            {
                var thicknessProperty = model.Parent.GetType().GetProperty("Thickness");
                propertiesInGrid.Add(new ThicknessColumn(thicknessProperty, model.Parent));
            }

            foreach (PropertyInfo property in model.GetType().GetProperties())
            {
                var description = ReflectionUtilities.GetAttribute(property, typeof(DescriptionAttribute), false);
                if (property.PropertyType.IsArray && description != null)
                {
                    PropertyColumn column;

                    if (property.Name == "Thickness")
                    {
                        column = new ThicknessColumn(property, model);
                    }
                    else
                    {
                        column = new PropertyColumn(property, model);
                    }

                    if (model is SoilCrop)
                    {
                        FormatSoilCropColumn(column);
                    }

                    propertiesInGrid.Add(column);

                    if (property.Name == "XF")
                    {
                        propertiesInGrid.Add(new PAWCColumn(model, propertiesInGrid));
                    }
                }
            }

            foreach (var soilCrop in model.Children.FindAll(child => child is SoilCrop))
            {
                FindAllProperties(soilCrop);
            }
        }
        public void TestCreateAdapter()
        {
            DataTableAdapterFactory factory = new DataTableAdapterFactory();
            PropertyColumn[] columns = new PropertyColumn[]
                {
                    new PropertyColumn("Class1.Field", "Field"),
                    new PropertyColumn("Class1.Property", "Property"),
                    new PropertyColumn("Time", "Time")
                };

            IDataTableAdapter<TestClass2> adapter = factory.New<TestClass2>(columns);

            List<TestClass2> list = new List<TestClass2>();
            for (int i = 0; i < 10000; i++)
            {
                list.Add(new TestClass2(new TestClass1(4, "4")));
                list.Add(new TestClass2(new TestClass1(40, "40")));
                list.Add(new TestClass2(new TestClass1(12, "12")));
                list.Add(new TestClass2());
            }

            DataTable dt = adapter.Get(list);

            Assert.IsNotNull(dt);
            Assert.AreEqual(list.Count, dt.Rows.Count);
            Assert.AreEqual(4, dt.Columns.Count);

            Assert.AreEqual(list[0].Class1.Property, dt.Rows[0]["Property"]);
            Assert.AreEqual(list[0].Class1.Field, dt.Rows[0]["Field"]);
            Assert.AreEqual(list[0].Time, dt.Rows[0]["Time"]);
            Assert.AreSame(list[0], dt.Rows[0]["DataItem"]);

            IDataTableAdapter<TestClass2> adapter2 = factory.New<TestClass2>(columns);
            Assert.AreSame(adapter, adapter2);

            PropertyColumn[] columns2 = new PropertyColumn[]
                {
                    new PropertyColumn("Class1.Field", "Field")
                };

            IDataTableAdapter<TestClass2> adapter3 = factory.New<TestClass2>(columns2);
            Assert.AreNotSame(adapter, adapter3);
        }
示例#9
0
            /// <summary>
            /// removes each <see cref="PropertyColumn"/> in <paramref name="propertyColumns"/> from
            /// <see cref="PropertyTable"/>'s column list, if it exists in column list.
            /// returns a bool array of length <paramref name="propertyColumns"/>.Count where
            /// the i'th element is true iff it existed in <see cref="PropertyTable"/>'s column list before
            /// being removed.
            /// </summary>
            /// <param name="propertyColumns"></param>
            /// <seealso cref="Table.RemoveColumnRange{T}(IList{T})"/>
            /// <returns>
            /// bool array of length <paramref name="propertyColumns"/>.Count where
            /// the i'th element is true iff it existed in <see cref="PropertyTable"/>'s column list before
            /// being removed.
            /// </returns>
            /// <exception cref="OperationRequiresEmptyTableException">
            /// <seealso cref="Table.RemoveColumnRange{T}(IList{T})"/>
            /// </exception>
            public bool[] RemoveColumnRange(IList <PropertyColumn> propertyColumns)
            {
                // remove from column list
                bool[] removalResult = table.RemoveColumnRange(propertyColumns);

                // for each removed column, remove property corresponding to column
                for (int i = 0; i < removalResult.Length; i++)
                {
                    PropertyColumn currentPropertyColumn = propertyColumns[i];
                    bool           currentColumnRemoved  = removalResult[i];

                    if (currentColumnRemoved)
                    {
                        // remove property corresponding to removed column
                        columnProperties.Remove(currentPropertyColumn.Property);
                    }
                }

                return(removalResult);
            }
示例#10
0
                /// <summary>
                /// parses <paramref name="headers"/>, <paramref name="widths"/>, and <paramref name="properties"/>
                /// as a <see cref="PropertyColumn"/> array of length <paramref name="headers"/>.Count,
                /// where the i'th item has
                /// (<paramref name="headers"/>[i], <paramref name="widths"/>[i], <paramref name="properties"/>[i])
                /// as its values.
                /// </summary>
                /// <param name="headers"></param>
                /// <param name="widths"></param>
                /// <param name="properties"></param>
                /// <returns>
                /// <see cref="PropertyColumn"/> array of length <paramref name="headers"/>.Count,
                /// where the i'th item has
                /// (<paramref name="headers"/>[i], <paramref name="widths"/>[i], <paramref name="properties"/>[i])
                /// as its values
                /// </returns>
                /// <exception cref="Column.WidhtsAndHeadersCountMismatchException">
                /// <seealso cref="Column.ParseArray(IList{string}, IList{int})"/>
                /// </exception>
                /// <exception cref="HeadersAndPropertiesCountMismatchException">
                /// <seealso cref="assertHeadersAndPropertiesCountsMatch(IList{string}, IList{Property})"/>
                /// </exception>
                public static PropertyColumn[] ParseArray(
                    IList <string> headers,
                    IList <int> widths, IList <Property> properties)
                {
                    // assert headers.Count and propertyNames.Count are equal
                    assertHeadersAndPropertiesCountsMatch(headers, properties);

                    // holds result
                    PropertyColumn[] propertyColumns = new PropertyColumn[headers.Count];

                    // parse Column array
                    Table.Column[] columns = Column.ParseArray(headers, widths);

                    // parse PropertyColumn array using columns array and propertyNames array
                    for (int i = 0; i < propertyColumns.Length; i++)
                    {
                        propertyColumns[i] = new PropertyColumn(columns[i], properties[i]);
                    }

                    return(propertyColumns);
                }
        } // end EndProcessing()

        protected override void ApplyViewToInputObject()
        {
            if (!m_calculatedWidths)
            {
                m_view.CalculateWidthsAndAlignments(Host.UI.RawUI.BufferSize.Width, InputObject);
                m_calculatedWidths = true;
            }

            if (!m_headerShown && !HideTableHeaders)
            {
                _WriteHeaders();
                m_headerShown = true;
            }

            // Write row values:
            ColorString row = new ColorString();

            for (int colIdx = 0; colIdx < m_view.Columns.Count; colIdx++)
            {
                if (Stopping)
                {
                    break;
                }

                string val;
                Column c = m_view.Columns[colIdx];
                if (c is PropertyColumn)
                {
                    PropertyColumn pc = (PropertyColumn)c;
                    val = RenderPropertyValue(InputObject, pc.PropertyName, pc.FormatString);
                }
                else
                {
                    ScriptColumn sc = (ScriptColumn)c;
                    val = RenderScriptValue(InputObject, sc.Script);
                    if (null == val)
                    {
                        val = String.Empty;
                    }
                }

                try
                {
                    val = PadAndAlign(val,
                                      c.CalculatedWidth,
                                      c.CalculatedAlignment,
                                      c.TrimLocation);
                }
                catch (Exception e)
                {
                    // Debugging aid to help you figure out what blew up:
                    e.Data["ColumnLabel"] = c.Label;
                    e.Data["val"]         = val;

                    // Sometimes you have a column that's very small (3 or less cells
                    // wide). If you get an error trying to render the value that goes in
                    // it, it will certainly be too wide, but we can't add an ellipsis
                    // when truncating (not enough room). Rather than blow up the
                    // formatting operation, we'll substitute a shorthand to indicate that
                    // an error occurred.
                    if ((e is ArgumentException) && (c.CalculatedWidth <= 3))
                    {
                        if (c.CalculatedWidth == 3)
                        {
                            val = new ColorString(ConsoleColor.Red, "ERR").ToString(DbgProvider.HostSupportsColor);
                        }
                        else if (c.CalculatedWidth == 2)
                        {
                            val = new ColorString(ConsoleColor.Red, "ER").ToString(DbgProvider.HostSupportsColor);
                        }
                        else if (c.CalculatedWidth == 1)
                        {
                            val = new ColorString(ConsoleColor.Red, "X").ToString(DbgProvider.HostSupportsColor);
                        }
                        else
                        {
                            var msg = Util.Sprintf("Calculated width of 0 or less? {0}", c.CalculatedWidth);
                            Util.Fail(msg);
                            throw new Exception(msg, e);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                row.Append(val);

                if (colIdx != (m_view.Columns.Count - 1))
                {
                    row.Append(" ");   // separator
                }
            }
            SafeWriteObject(row);
        } // end ApplyViewToInputObject()
        } // end _CanAddColumn()

        protected override AltTableViewDefinition GenerateView()
        {
            Util.Assert(null != InputObject);

            var columns = new List <Column>();

            if ((null != Property) && (0 != Property.Length))
            {
                IList <string> propNames = ResolvePropertyParameter();
                if (0 == propNames.Count)
                {
                    // TODO: proper error
                    throw new ArgumentException("The specified value for the -Property parameter results in no properties to display.",
                                                "Property");
                }
                columns.AddRange(propNames.Select((pn) => new PropertyColumn(pn, ColumnAlignment.Default, 0)));
            } // end if( Property )
            else
            {
                // Table view is constricted... there is only so much horizontal space, so
                // we'll have to limit how many properties we take. We'll try to prioritize
                // some common ones.
                var alreadyConsidered   = new HashSet <string>();
                int consumedWidth       = 0;
                int numUnspecifiedWidth = 0;
                int estimatedWidth;
                foreach (var propName in ResolvePropertyNames(sm_preferredPropNames, warn: false))
                {
                    alreadyConsidered.Add(propName);
                    try
                    {
                        var proposedColumn = new PropertyColumn(propName,
                                                                ColumnAlignment.Default,
                                                                AltTableViewDefinition.GetWidthForProperty(InputObject,
                                                                                                           propName));
                        if (_CanAddColumn(consumedWidth, numUnspecifiedWidth, proposedColumn, out estimatedWidth))
                        {
                            columns.Add(proposedColumn);
                            consumedWidth += estimatedWidth;
                            if (0 == proposedColumn.Width)
                            {
                                numUnspecifiedWidth++;
                            }
                        }
                    }
                    catch (RuntimeException rte)  // from GetWidthForProperty, which attempt to access the property
                    {
                        SafeWriteWarning(Util.Sprintf("Not considering property {0} for generated view, because it threw: {1}",
                                                      propName,
                                                      Util.GetExceptionMessages(rte)));
                        //LogManager.Trace( "Not considering property {0} for generated view, because it threw: {1}",
                        //                  propName,
                        //                  Util.GetExceptionMessages( rte ) );
                    }
                } // end foreach( preferred property )

                foreach (var pi in InputObject.Properties)
                {
                    if (alreadyConsidered.Contains(pi.Name))
                    {
                        continue;
                    }

                    if (pi.IsGettable)
                    {
                        try
                        {
                            var proposedColumn = new PropertyColumn(pi.Name,
                                                                    ColumnAlignment.Default,
                                                                    AltTableViewDefinition.GetWidthForProperty(InputObject,
                                                                                                               pi.Name));

                            if (_CanAddColumn(consumedWidth, numUnspecifiedWidth, proposedColumn, out estimatedWidth))
                            {
                                columns.Add(proposedColumn);
                                consumedWidth += estimatedWidth;
                                if (0 == proposedColumn.Width)
                                {
                                    numUnspecifiedWidth++;
                                }
                            }
                        }
                        catch (RuntimeException rte)  // from GetWidthForProperty, which attempt to access the property
                        {
                            SafeWriteWarning(Util.Sprintf("Not considering property {0} for generated view, because it threw: {1}",
                                                          pi.Name,
                                                          Util.GetExceptionMessages(rte)));
                            // LogManager.Trace( "Not considering property {0} for generated view, because it threw: {1}",
                            //                   pi.Name,
                            //                   Util.GetExceptionMessages( rte ) );
                        }
                    }
                } // end foreach( all properties )
            }
            return(new AltTableViewDefinition(false, columns));
        } // end GenerateView()
示例#13
0
 /// <summary>
 /// adds <paramref name="propertyColumn"/> to table's column list.
 /// </summary>
 /// <seealso cref="Table.AddColumn(Column)"/>
 /// <param name="propertyColumn"></param>
 /// <exception cref="Table.OperationRequiresEmptyTableException">
 /// <seealso cref="Table.AddColumn(Column)"/>
 /// </exception>
 public void AddColumn(PropertyColumn propertyColumn)
 {
     table.AddColumn(propertyColumn);
     columnProperties.Add(propertyColumn.Property);
 }