Esempio n. 1
0
        SetVertexAttributesForAllGroups
        (
            ListObject oGroupTable
        )
        {
            Debug.Assert(oGroupTable != null);

            Range oVertexColorRange, oVertexShapeRange;

            Object [,] aoVertexColorValues, aoVertexShapeValues;

            if (
                !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                                                               GroupTableColumnNames.VertexShape, out oVertexShapeRange,
                                                               out aoVertexShapeValues)
                ||
                !ExcelTableUtil.TryGetTableColumnDataAndValues(oGroupTable,
                                                               GroupTableColumnNames.VertexColor, out oVertexColorRange,
                                                               out aoVertexColorValues)
                )
            {
                return;
            }

            Int32 iGroups = aoVertexShapeValues.GetUpperBound(0);

            Debug.Assert(aoVertexColorValues.GetUpperBound(0) == iGroups);

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            for (Int32 i = 0; i < iGroups; i++)
            {
                Color       oColor;
                VertexShape eShape;

                GetVertexAttributes(i, iGroups, out oColor, out eShape);

                // Write the color in a format that is understood by
                // ColorConverter2.WorkbookToGraph(), which is what
                // WorksheetReaderBase uses.

                aoVertexColorValues[i + 1, 1] =
                    oColorConverter2.GraphToWorkbook(oColor);

                aoVertexShapeValues[i + 1, 1] =
                    oVertexShapeConverter.GraphToWorkbook(eShape);
            }

            oVertexColorRange.set_Value(Missing.Value, aoVertexColorValues);
            oVertexShapeRange.set_Value(Missing.Value, aoVertexShapeValues);
        }
Esempio n. 2
0
        ThisWorkbook_AttributesEditedInGraph
        (
            Object sender,
            AttributesEditedEventArgs e
        )
        {
            Debug.Assert(e != null);
            AssertValid();

            // The key is the row ID stored in the table's ID column and the value
            // is the one-based row number relative to the worksheet.

            Dictionary <Int32, Int32> oRowIDDictionary;

            if (
                e.EditedVertexAttributes == null
                ||
                !m_oSheets1And2Helper.TableExists
                ||
                !m_oSheets1And2Helper.TryGetAllRowIDs(out oRowIDDictionary)
                )
            {
                return;
            }

            Microsoft.Office.Interop.Excel.ListObject oVertexTable =
                Vertices.InnerObject;

            Globals.ThisWorkbook.ShowWaitCursor = true;

            // Get the columns that might need to be updated.  These columns are
            // not required.

            Microsoft.Office.Interop.Excel.Range oColorColumnData,
                                                 oShapeColumnData, oRadiusColumnData, oAlphaColumnData,
                                                 oVisibilityColumnData, oLabelColumnData, oLabelFillColorColumnData,
                                                 oLabelPositionColumnData, oToolTipColumnData, oLockedColumnData,
                                                 oMarkedColumnData;

            Object [,] aoColorValues          = null;
            Object [,] aoShapeValues          = null;
            Object [,] aoRadiusValues         = null;
            Object [,] aoAlphaValues          = null;
            Object [,] aoVisibilityValues     = null;
            Object [,] aoLabelValues          = null;
            Object [,] aoLabelFillColorValues = null;
            Object [,] aoLabelPositionValues  = null;
            Object [,] aoToolTipValues        = null;
            Object [,] aoLockedValues         = null;
            Object [,] aoMarkedValues         = null;

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.Color, out oColorColumnData,
                                                          out aoColorValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.Shape, out oShapeColumnData,
                                                          out aoShapeValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.Radius, out oRadiusColumnData,
                                                          out aoRadiusValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          CommonTableColumnNames.Alpha, out oAlphaColumnData,
                                                          out aoAlphaValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          CommonTableColumnNames.Visibility, out oVisibilityColumnData,
                                                          out aoVisibilityValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.Label, out oLabelColumnData,
                                                          out aoLabelValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.LabelFillColor,
                                                          out oLabelFillColorColumnData, out aoLabelFillColorValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.LabelPosition, out oLabelPositionColumnData,
                                                          out aoLabelPositionValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.ToolTip, out oToolTipColumnData,
                                                          out aoToolTipValues);

            ExcelTableUtil.TryGetTableColumnDataAndValues(oVertexTable,
                                                          VertexTableColumnNames.Locked, out oLockedColumnData,
                                                          out aoLockedValues);

            if (TryGetMarkedColumnData(out oMarkedColumnData))
            {
                aoMarkedValues = ExcelUtil.GetRangeValues(oMarkedColumnData);
            }

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            VertexVisibilityConverter oVertexVisibilityConverter =
                new VertexVisibilityConverter();

            VertexLabelPositionConverter oVertexLabelPositionConverter =
                new VertexLabelPositionConverter();

            BooleanConverter oBooleanConverter = new BooleanConverter();

            // Loop through the IDs of the vertices whose attributes were edited
            // in the graph.

            EditedVertexAttributes oEditedVertexAttributes =
                e.EditedVertexAttributes;

            foreach (Int32 iID in e.VertexIDs)
            {
                // Look for the row that contains the ID.

                Int32 iRowOneBased;

                if (!oRowIDDictionary.TryGetValue(iID, out iRowOneBased))
                {
                    continue;
                }

                iRowOneBased -= oVertexTable.Range.Row;

                if (oEditedVertexAttributes.Color.HasValue &&
                    aoColorValues != null)
                {
                    aoColorValues[iRowOneBased, 1] =
                        oColorConverter2.GraphToWorkbook(
                            oEditedVertexAttributes.Color.Value);
                }

                if (oEditedVertexAttributes.Shape.HasValue &&
                    aoShapeValues != null)
                {
                    aoShapeValues[iRowOneBased, 1] =
                        oVertexShapeConverter.GraphToWorkbook(
                            oEditedVertexAttributes.Shape.Value);
                }

                if (oEditedVertexAttributes.Radius.HasValue &&
                    aoRadiusValues != null)
                {
                    aoRadiusValues[iRowOneBased, 1] =
                        oEditedVertexAttributes.Radius.Value.ToString();
                }

                if (oEditedVertexAttributes.Alpha.HasValue &&
                    aoAlphaValues != null)
                {
                    aoAlphaValues[iRowOneBased, 1] =
                        oEditedVertexAttributes.Alpha.Value.ToString();
                }

                if (oEditedVertexAttributes.Visibility.HasValue &&
                    aoVisibilityValues != null)
                {
                    aoVisibilityValues[iRowOneBased, 1] =
                        oVertexVisibilityConverter.GraphToWorkbook(
                            oEditedVertexAttributes.Visibility.Value);
                }

                if (!String.IsNullOrEmpty(oEditedVertexAttributes.Label))
                {
                    aoLabelValues[iRowOneBased, 1] = oEditedVertexAttributes.Label;
                }

                if (oEditedVertexAttributes.LabelFillColor.HasValue &&
                    aoLabelFillColorValues != null)
                {
                    aoLabelFillColorValues[iRowOneBased, 1] =
                        oColorConverter2.GraphToWorkbook(
                            oEditedVertexAttributes.LabelFillColor.Value);
                }

                if (oEditedVertexAttributes.LabelPosition.HasValue &&
                    aoLabelPositionValues != null)
                {
                    aoLabelPositionValues[iRowOneBased, 1] =
                        oVertexLabelPositionConverter.GraphToWorkbook(
                            oEditedVertexAttributes.LabelPosition.Value);
                }

                if (!String.IsNullOrEmpty(oEditedVertexAttributes.ToolTip))
                {
                    aoToolTipValues[iRowOneBased, 1] =
                        oEditedVertexAttributes.ToolTip;
                }

                if (oEditedVertexAttributes.Locked.HasValue &&
                    aoLockedValues != null)
                {
                    aoLockedValues[iRowOneBased, 1] =
                        oBooleanConverter.GraphToWorkbook(
                            oEditedVertexAttributes.Locked.Value);
                }

                if (oEditedVertexAttributes.Marked.HasValue &&
                    aoMarkedValues != null)
                {
                    aoMarkedValues[iRowOneBased, 1] =
                        oBooleanConverter.GraphToWorkbook(
                            oEditedVertexAttributes.Marked.Value);
                }
            }

            // Activate this worksheet first, because writing to an inactive
            // worksheet causes problems with the selection in Excel.

            ExcelActiveWorksheetRestorer oExcelActiveWorksheetRestorer =
                m_oSheets1And2Helper.GetExcelActiveWorksheetRestorer();

            ExcelActiveWorksheetState oExcelActiveWorksheetState =
                oExcelActiveWorksheetRestorer.ActivateWorksheet(this.InnerObject);

            try
            {
                m_oSheets1And2Helper.SetColumnDataValues(
                    oColorColumnData, aoColorValues,
                    oShapeColumnData, aoShapeValues,
                    oRadiusColumnData, aoRadiusValues,
                    oAlphaColumnData, aoAlphaValues,
                    oVisibilityColumnData, aoVisibilityValues,
                    oLabelColumnData, aoLabelValues,
                    oLabelFillColorColumnData, aoLabelFillColorValues,
                    oLabelPositionColumnData, aoLabelPositionValues,
                    oToolTipColumnData, aoToolTipValues,
                    oLockedColumnData, aoLockedValues,
                    oMarkedColumnData, aoMarkedValues
                    );
            }
            finally
            {
                oExcelActiveWorksheetRestorer.Restore(oExcelActiveWorksheetState);
            }

            Globals.ThisWorkbook.ShowWaitCursor = false;
        }
        Convert <TGroup>
        (
            ICollection <TGroup> groups,
            GroupToGroupVertices <TGroup> groupToGroupVertices,
            GroupToString <TGroup> groupToGroupName,
            Boolean collapseGroups,
            GroupToString <TGroup> groupToCollapsedGroupAttributes
        )
        {
            Debug.Assert(groups != null);
            Debug.Assert(groupToGroupVertices != null);

            // These columns are needed:
            //
            // * Group name on the group worksheet.
            //
            // * Vertex color on the group worksheet.
            //
            // * Vertex shape on the group worksheet.
            //
            // * Group name on the group-vertex worksheet.
            //
            // * Vertex name on the group-vertex worksheet.
            //
            // * Vertex ID on the group-vertex worksheet.  This gets copied from
            //   the hidden ID column on the Vertex worksheet via an Excel
            //   formula.

            // These columns might be needed:
            //
            // * Collapsed flag on the group worksheet.
            //
            // * Collapsed group attributes on the group worksheet.


            List <GraphMetricValueOrdered> oGroupNamesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexColorsForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexShapesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oCollapsedFlagsForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oCollapsedAttributesForGroupWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oGroupNamesForGroupVertexWorksheet =
                new List <GraphMetricValueOrdered>();

            List <GraphMetricValueOrdered> oVertexNamesForGroupVertexWorksheet =
                new List <GraphMetricValueOrdered>();

            // This column contains a constant value, which is a formula.

            GraphMetricValueOrdered [] aoVertexIDsForGroupVertexWorksheet =
            {
                new GraphMetricValueOrdered(
                    GroupManager.GetExcelFormulaForVertexID())
            };

            Int32 iGroups = groups.Count;
            Int32 iGroup  = 0;

            ColorConverter2 oColorConverter2 = new ColorConverter2();

            VertexShapeConverter oVertexShapeConverter =
                new VertexShapeConverter();

            GraphMetricValueOrdered oTrueGraphMetricValueOrdered =
                new GraphMetricValueOrdered(
                    (new BooleanConverter()).GraphToWorkbook(true));

            foreach (TGroup oGroup in
                     from oGroup in groups
                     orderby groupToGroupVertices(oGroup).Count descending
                     select oGroup)
            {
                String sGroupName;

                if (groupToGroupName == null)
                {
                    sGroupName = 'G' + (iGroup + 1).ToString(
                        CultureInfo.InvariantCulture);
                }
                else
                {
                    sGroupName = groupToGroupName(oGroup);
                }

                Color       oColor;
                VertexShape eShape;

                GroupManager.GetVertexAttributes(iGroup, iGroups, out oColor,
                                                 out eShape);

                GraphMetricValueOrdered oGroupNameGraphMetricValue =
                    new GraphMetricValueOrdered(sGroupName);

                oGroupNamesForGroupWorksheet.Add(oGroupNameGraphMetricValue);

                // Write the color in a format that is understood by
                // ColorConverter2.WorkbookToGraph(), which is what
                // WorksheetReaderBase uses.

                oVertexColorsForGroupWorksheet.Add(
                    new GraphMetricValueOrdered(
                        oColorConverter2.GraphToWorkbook(oColor)));

                oVertexShapesForGroupWorksheet.Add(
                    new GraphMetricValueOrdered(
                        oVertexShapeConverter.GraphToWorkbook(eShape)));

                if (collapseGroups)
                {
                    oCollapsedFlagsForGroupWorksheet.Add(
                        oTrueGraphMetricValueOrdered);
                }

                if (groupToCollapsedGroupAttributes != null)
                {
                    oCollapsedAttributesForGroupWorksheet.Add(
                        new GraphMetricValueOrdered(
                            groupToCollapsedGroupAttributes(oGroup)));
                }

                Int32 iVertices = 0;

                foreach (IVertex oVertex in groupToGroupVertices(oGroup))
                {
                    oGroupNamesForGroupVertexWorksheet.Add(
                        oGroupNameGraphMetricValue);

                    oVertexNamesForGroupVertexWorksheet.Add(
                        new GraphMetricValueOrdered(
                            ExcelTextUtil.ForceCellText(oVertex.Name)));

                    iVertices++;
                }

                iGroup++;
            }

            List <GraphMetricColumn> oGraphMetricColumns =
                new List <GraphMetricColumn>();

            oGraphMetricColumns.AddRange(

                new GraphMetricColumn [] {
                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.Name,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.Required,
                                             oGroupNamesForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.VertexColor,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.VisualProperty,
                                             oVertexColorsForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                             TableNames.Groups,
                                             GroupTableColumnNames.VertexShape,
                                             ExcelTableUtil.AutoColumnWidth, null,
                                             CellStyleNames.VisualProperty,
                                             oVertexShapesForGroupWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.GroupName,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             oGroupNamesForGroupVertexWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.VertexName,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             oVertexNamesForGroupVertexWorksheet.ToArray()
                                             ),

                new GraphMetricColumnOrdered(WorksheetNames.GroupVertices,
                                             TableNames.GroupVertices,
                                             GroupVertexTableColumnNames.VertexID,
                                             ExcelTableUtil.AutoColumnWidth, null, null,
                                             aoVertexIDsForGroupVertexWorksheet
                                             ),
            });

            if (collapseGroups)
            {
                oGraphMetricColumns.Add(
                    new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                                 TableNames.Groups,
                                                 GroupTableColumnNames.Collapsed,
                                                 ExcelTableUtil.AutoColumnWidth, null,
                                                 CellStyleNames.VisualProperty,
                                                 oCollapsedFlagsForGroupWorksheet.ToArray()
                                                 ));
            }

            if (groupToCollapsedGroupAttributes != null)
            {
                oGraphMetricColumns.Add(
                    new GraphMetricColumnOrdered(WorksheetNames.Groups,
                                                 TableNames.Groups,
                                                 GroupTableColumnNames.CollapsedAttributes,
                                                 ExcelTableUtil.AutoColumnWidth, null,
                                                 CellStyleNames.DoNotEdit,
                                                 oCollapsedAttributesForGroupWorksheet.ToArray()
                                                 ));
            }

            return(oGraphMetricColumns.ToArray());
        }