Esempio n. 1
0
        ReadVertexTable
        (
            ListObject oVertexTable,
            ReadWorkbookContext oReadWorkbookContext,
            IGraph oGraph,
            out Boolean bLayoutOrderSet,
            out Boolean bToolTipSet
        )
        {
            Debug.Assert(oVertexTable != null);
            Debug.Assert(oReadWorkbookContext != null);
            Debug.Assert(oGraph != null);
            AssertValid();

            bLayoutOrderSet = bToolTipSet = false;

            if (GetTableColumnIndex(oVertexTable,
                                    VertexTableColumnNames.VertexName, false) == NoSuchColumn)
            {
                // Nothing can be done without vertex names.

                return;
            }

            Boolean bReadAllEdgeAndVertexColumns =
                oReadWorkbookContext.ReadAllEdgeAndVertexColumns;

            if (oReadWorkbookContext.FillIDColumns)
            {
                FillIDColumn(oVertexTable);
            }

            // Get the names of all the column pairs that are used to add custom
            // menu items to the vertex context menu in the graph.

            TableColumnAdder oTableColumnAdder = new TableColumnAdder();

            ICollection <KeyValuePair <String, String> > aoCustomMenuItemPairNames =
                oTableColumnAdder.GetColumnPairNames(oVertexTable,
                                                     VertexTableColumnNames.CustomMenuItemTextBase,
                                                     VertexTableColumnNames.CustomMenuItemActionBase);

            IVertexCollection oVertices = oGraph.Vertices;

            Dictionary <String, IVertex> oVertexNameDictionary =
                oReadWorkbookContext.VertexNameDictionary;

            Dictionary <Int32, IIdentityProvider> oEdgeRowIDDictionary =
                oReadWorkbookContext.EdgeRowIDDictionary;

            BooleanConverter oBooleanConverter =
                oReadWorkbookContext.BooleanConverter;

            VertexVisibilityConverter oVertexVisibilityConverter =
                new VertexVisibilityConverter();

            VertexLabelPositionConverter oVertexLabelPositionConverter =
                new VertexLabelPositionConverter();

            ExcelTableReader oExcelTableReader =
                new ExcelTableReader(oVertexTable);

            HashSet <String> oColumnNamesToExclude = new HashSet <String>(
                new String[] {
                VertexTableColumnNames.VertexName
            });

            foreach (ExcelTableReader.ExcelTableRow oRow in
                     oExcelTableReader.GetRows())
            {
                // Get the name of the vertex.

                String sVertexName;

                if (!oRow.TryGetNonEmptyStringFromCell(
                        VertexTableColumnNames.VertexName, out sVertexName))
                {
                    continue;
                }

                // If the vertex was added to the graph as part of an edge,
                // retrieve the vertex.

                IVertex oVertex;

                if (!oVertexNameDictionary.TryGetValue(sVertexName, out oVertex))
                {
                    oVertex = null;
                }

                // Assume a default visibility.

                Visibility eVisibility = Visibility.ShowIfInAnEdge;
                String     sVisibility;

                if (oRow.TryGetNonEmptyStringFromCell(
                        CommonTableColumnNames.Visibility, out sVisibility))
                {
                    if (!oVertexVisibilityConverter.TryWorkbookToGraph(
                            sVisibility, out eVisibility))
                    {
                        OnInvalidVisibility(oRow);
                    }
                }

                switch (eVisibility)
                {
                case Visibility.ShowIfInAnEdge:

                    // If the vertex is part of an edge, show it using the
                    // specified vertex attributes.  Otherwise, skip the vertex
                    // row.

                    if (oVertex == null)
                    {
                        continue;
                    }

                    break;

                case Visibility.Skip:

                    // Skip the vertex row and any edge rows that include the
                    // vertex.  Do not read them into the graph.

                    if (oVertex != null)
                    {
                        // Remove the vertex and its incident edges from the
                        // graph and dictionaries.

                        foreach (IEdge oIncidentEdge in oVertex.IncidentEdges)
                        {
                            if (oIncidentEdge.Tag is Int32)
                            {
                                oEdgeRowIDDictionary.Remove(
                                    (Int32)oIncidentEdge.Tag);
                            }
                        }

                        oVertexNameDictionary.Remove(sVertexName);

                        oVertices.Remove(oVertex);

                        // (The vertex doesn't get added to
                        // ReadWorkbookContext.VertexIDDictionary until after
                        // this switch statement, so it doesn't need to be
                        // removed from that dictionary.)
                    }

                    continue;

                case Visibility.Hide:

                    // If the vertex is part of an edge, hide it and its
                    // incident edges.  Otherwise, skip the vertex row.

                    if (oVertex == null)
                    {
                        continue;
                    }

                    // Hide the vertex and its incident edges.

                    oVertex.SetValue(ReservedMetadataKeys.Visibility,
                                     VisibilityKeyValue.Hidden);

                    foreach (IEdge oIncidentEdge in oVertex.IncidentEdges)
                    {
                        oIncidentEdge.SetValue(ReservedMetadataKeys.Visibility,
                                               VisibilityKeyValue.Hidden);
                    }

                    break;

                case Visibility.Show:

                    // Show the vertex using the specified attributes
                    // regardless of whether it is part of an edge.

                    if (oVertex == null)
                    {
                        oVertex = CreateVertex(sVertexName, oVertices,
                                               oVertexNameDictionary);
                    }

                    break;

                default:

                    Debug.Assert(false);
                    break;
                }

                Debug.Assert(oVertex != null);

                // If there is an ID column, add the vertex to the vertex ID
                // dictionary and set the vertex's Tag to the ID.

                AddToRowIDDictionary(oRow, oVertex,
                                     oReadWorkbookContext.VertexRowIDDictionary);

                if (bReadAllEdgeAndVertexColumns)
                {
                    // All columns except the vertex name should be read and stored
                    // as metadata on the vertex.

                    ReadAllColumns(oExcelTableReader, oRow, oVertex,
                                   oColumnNamesToExclude);

                    continue;
                }

                // Layout order.

                if (ReadLayoutOrder(oRow, oVertex))
                {
                    bLayoutOrderSet = true;
                }

                // Location and Locked.

                if (!oReadWorkbookContext.IgnoreVertexLocations)
                {
                    Boolean bLocationSpecified = false;

                    bLocationSpecified = ReadLocation(oRow,
                                                      oReadWorkbookContext.VertexLocationConverter, oVertex);

                    ReadLocked(oRow, oBooleanConverter, bLocationSpecified,
                               oVertex);
                }

                // Polar coordinates.

                ReadPolarCoordinates(oRow, oVertex);

                // Marked.

                ReadMarked(oRow, oBooleanConverter, oVertex);

                // Custom menu items.

                if (aoCustomMenuItemPairNames.Count > 0)
                {
                    ReadCustomMenuItems(oRow, aoCustomMenuItemPairNames, oVertex);
                }

                // Alpha.

                ReadAlpha(oRow, oVertex);

                // Tooltip.

                if (ReadCellAndSetMetadata(oRow, VertexTableColumnNames.ToolTip,
                                           oVertex, ReservedMetadataKeys.VertexToolTip))
                {
                    bToolTipSet = true;
                }

                // Label.

                if (oReadWorkbookContext.ReadVertexLabels)
                {
                    ReadCellAndSetMetadata(oRow, VertexTableColumnNames.Label,
                                           oVertex, ReservedMetadataKeys.PerVertexLabel);
                }

                // Label fill color.

                ReadColor(oRow, VertexTableColumnNames.LabelFillColor, oVertex,
                          ReservedMetadataKeys.PerVertexLabelFillColor,
                          oReadWorkbookContext.ColorConverter2);

                // Label position.

                ReadLabelPosition(oRow, oVertexLabelPositionConverter, oVertex);

                // Radius.

                Nullable <Single> oRadiusWorkbook = new Nullable <Single>();

                oRadiusWorkbook = ReadRadius(oRow,
                                             oReadWorkbookContext.VertexRadiusConverter, oVertex);

                // Shape.

                VertexShape eVertexShape;

                if (!ReadShape(oRow, oVertex, out eVertexShape))
                {
                    eVertexShape = oReadWorkbookContext.DefaultVertexShape;
                }

                // Label font size.

                if (eVertexShape == VertexShape.Label && oRadiusWorkbook.HasValue)
                {
                    // The vertex radius is used to specify font size when the
                    // shape is Label.

                    oVertex.SetValue(ReservedMetadataKeys.PerVertexLabelFontSize,
                                     oReadWorkbookContext.VertexRadiusConverter.
                                     WorkbookToLabelFontSize(oRadiusWorkbook.Value));
                }

                // Image URI.

                if (eVertexShape == VertexShape.Image &&
                    oReadWorkbookContext.ReadVertexImages)
                {
                    ReadImageUri(oRow, oVertex,
                                 oReadWorkbookContext.VertexRadiusConverter,

                                 oRadiusWorkbook.HasValue ? oRadiusWorkbook :
                                 oReadWorkbookContext.DefaultVertexImageSize
                                 );
                }

                // Color

                ReadColor(oRow, VertexTableColumnNames.Color, oVertex,
                          ReservedMetadataKeys.PerColor,
                          oReadWorkbookContext.ColorConverter2);
            }

            if (bReadAllEdgeAndVertexColumns)
            {
                // Store the vertex column names on the graph.

                oGraph.SetValue(ReservedMetadataKeys.AllVertexMetadataKeys,
                                FilterColumnNames(oExcelTableReader, oColumnNamesToExclude));
            }
        }
        //*************************************************************************
        //  Method: ReadVertexTable()
        //
        /// <summary>
        /// Reads the vertex table and adds the contents to a graph.
        /// </summary>
        ///
        /// <param name="oVertexTable">
        /// Table that contains the vertex data.
        /// </param>
        ///
        /// <param name="oReadWorkbookContext">
        /// Provides access to objects needed for converting an Excel workbook to a
        /// NodeXL graph.
        /// </param>
        ///
        /// <param name="oGraph">
        /// Graph to add vertices to.
        /// </param>
        ///
        /// <param name="bLayoutOrderSet">
        /// Gets set to true if a vertex layout order was specified for at least
        /// one vertex, or false otherwise.
        /// </param>
        ///
        /// <param name="bToolTipSet">
        /// Gets set to true if a tooltip was specified for at least one vertex, or
        /// false otherwise.
        /// </param>
        //*************************************************************************
        protected void ReadVertexTable(
            ListObject oVertexTable,
            ReadWorkbookContext oReadWorkbookContext,
            IGraph oGraph,
            out Boolean bLayoutOrderSet,
            out Boolean bToolTipSet
            )
        {
            Debug.Assert(oVertexTable != null);
            Debug.Assert(oReadWorkbookContext != null);
            Debug.Assert(oGraph != null);
            AssertValid();

            bLayoutOrderSet = bToolTipSet = false;

            if (GetTableColumnIndex(oVertexTable,
            VertexTableColumnNames.VertexName, false) == NoSuchColumn)
            {
            // Nothing can be done without vertex names.

            return;
            }

            Boolean bReadAllEdgeAndVertexColumns =
            oReadWorkbookContext.ReadAllEdgeAndVertexColumns;

            if (oReadWorkbookContext.FillIDColumns)
            {
            FillIDColumn(oVertexTable);
            }

            // Get the names of all the column pairs that are used to add custom
            // menu items to the vertex context menu in the graph.

            TableColumnAdder oTableColumnAdder = new TableColumnAdder();

            ICollection< KeyValuePair<String, String> > aoCustomMenuItemPairNames =
            oTableColumnAdder.GetColumnPairNames(oVertexTable,
                VertexTableColumnNames.CustomMenuItemTextBase,
                VertexTableColumnNames.CustomMenuItemActionBase);

            IVertexCollection oVertices = oGraph.Vertices;

            Dictionary<String, IVertex> oVertexNameDictionary =
            oReadWorkbookContext.VertexNameDictionary;

            Dictionary<Int32, IIdentityProvider> oEdgeRowIDDictionary =
            oReadWorkbookContext.EdgeRowIDDictionary;

            BooleanConverter oBooleanConverter =
            oReadWorkbookContext.BooleanConverter;

            VertexVisibilityConverter oVertexVisibilityConverter =
            new VertexVisibilityConverter();

            VertexLabelPositionConverter oVertexLabelPositionConverter =
            new VertexLabelPositionConverter();

            ExcelTableReader oExcelTableReader =
            new ExcelTableReader(oVertexTable);

            HashSet<String> oColumnNamesToExclude = new HashSet<String>(
            new String[] {
                VertexTableColumnNames.VertexName
                } );

            foreach ( ExcelTableReader.ExcelTableRow oRow in
            oExcelTableReader.GetRows() )
            {
            // Get the name of the vertex.

            String sVertexName;

            if ( !oRow.TryGetNonEmptyStringFromCell(
                VertexTableColumnNames.VertexName, out sVertexName) )
            {
                continue;
            }

            // If the vertex was added to the graph as part of an edge,
            // retrieve the vertex.

            IVertex oVertex;

            if ( !oVertexNameDictionary.TryGetValue(sVertexName, out oVertex) )
            {
                oVertex = null;
            }

            // Assume a default visibility.

            Visibility eVisibility = Visibility.ShowIfInAnEdge;
            String sVisibility;

            if ( oRow.TryGetNonEmptyStringFromCell(
                    CommonTableColumnNames.Visibility, out sVisibility) )
            {
                if ( !oVertexVisibilityConverter.TryWorkbookToGraph(
                    sVisibility, out eVisibility) )
                {
                    OnInvalidVisibility(oRow);
                }
            }

            switch (eVisibility)
            {
                case Visibility.ShowIfInAnEdge:

                    // If the vertex is part of an edge, show it using the
                    // specified vertex attributes.  Otherwise, skip the vertex
                    // row.

                    if (oVertex == null)
                    {
                        continue;
                    }

                    break;

                case Visibility.Skip:

                    // Skip the vertex row and any edge rows that include the
                    // vertex.  Do not read them into the graph.

                    if (oVertex != null)
                    {
                        // Remove the vertex and its incident edges from the
                        // graph and dictionaries.

                        foreach (IEdge oIncidentEdge in oVertex.IncidentEdges)
                        {
                            if (oIncidentEdge.Tag is Int32)
                            {
                                oEdgeRowIDDictionary.Remove(
                                    (Int32)oIncidentEdge.Tag);
                            }
                        }

                        oVertexNameDictionary.Remove(sVertexName);

                        oVertices.Remove(oVertex);

                        // (The vertex doesn't get added to
                        // ReadWorkbookContext.VertexIDDictionary until after
                        // this switch statement, so it doesn't need to be
                        // removed from that dictionary.)
                    }

                    continue;

                case Visibility.Hide:

                    // If the vertex is part of an edge, hide it and its
                    // incident edges.  Otherwise, skip the vertex row.

                    if (oVertex == null)
                    {
                        continue;
                    }

                    // Hide the vertex and its incident edges.

                    oVertex.SetValue(ReservedMetadataKeys.Visibility,
                        VisibilityKeyValue.Hidden);

                    foreach (IEdge oIncidentEdge in oVertex.IncidentEdges)
                    {
                        oIncidentEdge.SetValue(ReservedMetadataKeys.Visibility,
                            VisibilityKeyValue.Hidden);
                    }

                    break;

                case Visibility.Show:

                    // Show the vertex using the specified attributes
                    // regardless of whether it is part of an edge.

                    if (oVertex == null)
                    {
                        oVertex = CreateVertex(sVertexName, oVertices,
                            oVertexNameDictionary);
                    }

                    break;

                default:

                    Debug.Assert(false);
                    break;
            }

            Debug.Assert(oVertex != null);

            // If there is an ID column, add the vertex to the vertex ID
            // dictionary and set the vertex's Tag to the ID.

            AddToRowIDDictionary(oRow, oVertex,
                oReadWorkbookContext.VertexRowIDDictionary);

            if (bReadAllEdgeAndVertexColumns)
            {
                // All columns except the vertex name should be read and stored
                // as metadata on the vertex.

                ReadAllColumns( oExcelTableReader, oRow, oVertex,
                    oColumnNamesToExclude);

                continue;
            }

            // Layout order.

            if ( ReadLayoutOrder(oRow, oVertex) )
            {
                bLayoutOrderSet = true;
            }

            // Location and Locked.

            if (!oReadWorkbookContext.IgnoreVertexLocations)
            {
                Boolean bLocationSpecified = false;

                bLocationSpecified = ReadLocation(oRow,
                    oReadWorkbookContext.VertexLocationConverter, oVertex);

                ReadLocked(oRow, oBooleanConverter, bLocationSpecified,
                    oVertex);
            }

            // Polar coordinates.

            ReadPolarCoordinates(oRow, oVertex);

            // Marked.

            ReadMarked(oRow, oBooleanConverter, oVertex);

            // Custom menu items.

            if (aoCustomMenuItemPairNames.Count > 0)
            {
                ReadCustomMenuItems(oRow, aoCustomMenuItemPairNames, oVertex);
            }

            // Alpha.

            ReadAlpha(oRow, oVertex);

            // Tooltip.

            if ( ReadCellAndSetMetadata(oRow, VertexTableColumnNames.ToolTip,
                oVertex, ReservedMetadataKeys.VertexToolTip) )
            {
                bToolTipSet = true;
            }

            // Label.

            if (oReadWorkbookContext.ReadVertexLabels)
            {
                ReadCellAndSetMetadata(oRow, VertexTableColumnNames.Label,
                    oVertex, ReservedMetadataKeys.PerVertexLabel);
            }

            // Label fill color.

            ReadColor(oRow, VertexTableColumnNames.LabelFillColor, oVertex,
                ReservedMetadataKeys.PerVertexLabelFillColor,
                oReadWorkbookContext.ColorConverter2);

            // Label position.

            ReadLabelPosition(oRow, oVertexLabelPositionConverter, oVertex);

            // Radius.

            Nullable<Single> oRadiusWorkbook = new Nullable<Single>();

            oRadiusWorkbook = ReadRadius(oRow,
                oReadWorkbookContext.VertexRadiusConverter, oVertex);

            // Shape.

            VertexShape eVertexShape;

            if ( !ReadShape(oRow, oVertex, out eVertexShape) )
            {
                eVertexShape = oReadWorkbookContext.DefaultVertexShape;
            }

            // Label font size.

            if (eVertexShape == VertexShape.Label && oRadiusWorkbook.HasValue)
            {
                // The vertex radius is used to specify font size when the
                // shape is Label.

                oVertex.SetValue( ReservedMetadataKeys.PerVertexLabelFontSize,
                    oReadWorkbookContext.VertexRadiusConverter.
                        WorkbookToLabelFontSize(oRadiusWorkbook.Value) );
            }

            // Image URI.

            if (eVertexShape == VertexShape.Image &&
                oReadWorkbookContext.ReadVertexImages)
            {
                ReadImageUri(oRow, oVertex,
                    oReadWorkbookContext.VertexRadiusConverter,

                    oRadiusWorkbook.HasValue ? oRadiusWorkbook :
                        oReadWorkbookContext.DefaultVertexImageSize
                    );
            }

            // Color

            ReadColor(oRow, VertexTableColumnNames.Color, oVertex,
                ReservedMetadataKeys.PerColor,
                oReadWorkbookContext.ColorConverter2);
            }

            if (bReadAllEdgeAndVertexColumns)
            {
            // Store the vertex column names on the graph.

            oGraph.SetValue( ReservedMetadataKeys.AllVertexMetadataKeys,
                FilterColumnNames(oExcelTableReader, oColumnNamesToExclude) );
            }
        }
        //*************************************************************************
        //  Method: btnCustomizeVertexMenu_Click()
        //
        /// <summary>
        /// Handles the Click event on the btnCustomizeVertexMenu button.
        /// </summary>
        ///
        /// <param name="sender">
        /// Standard event argument.
        /// </param>
        ///
        /// <param name="e">
        /// Standard event argument.
        /// </param>
        //*************************************************************************
        private void btnCustomizeVertexMenu_Click(
            object sender,
            EventArgs e
            )
        {
            AssertValid();

            const String Message =
            "Use this to add custom menu items to the menu that appears when"
            + " you right-click a vertex in the NodeXL graph."
            + "\r\n\r\n"
            + "Clicking \"Yes\" below will add a pair of columns to the"
            + " Vertices worksheet -- one for menu item text and another for"
            + " the action to take when the menu item is selected."
            + "\r\n\r\n"
            + "For example, if you add the column pair and enter \"Send Mail"
            + " To\" for a vertex's menu item text and \"mailto:[email protected]\""
            + " for the action, then right-clicking the vertex in the NodeXL"
            + " graph and selecting \"Send Mail To\" from the right-click menu"
            + " will open a new email message addressed to [email protected]."
            + "\r\n\r\n"
            + "If you want to open a Web page when the menu item is selected,"
            + " enter an URL for the action."
            + "\r\n\r\n"
            + "If you want to add more than one custom menu item to a vertex's"
            + " right-click menu, run this again to add another pair of"
            + " columns."
            + "\r\n\r\n"
            + "Do you want to add a pair of columns to the Vertices worksheet?"
            ;

            if (MessageBox.Show(Message, this.ApplicationName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Information) !=
                DialogResult.Yes)
            {
            return;
            }

            // Create and use the object that adds the columns to the vertex
            // table.

            TableColumnAdder oTableColumnAdder = new TableColumnAdder();

            this.UseWaitCursor = true;

            try
            {
            oTableColumnAdder.AddColumnPair(m_oWorkbook,
                WorksheetNames.Vertices, TableNames.Vertices,
                VertexTableColumnNames.CustomMenuItemTextBase,
                VertexTableColumnWidths.CustomMenuItemText,
                VertexTableColumnNames.CustomMenuItemActionBase,
                VertexTableColumnWidths.CustomMenuItemAction
                );

            this.UseWaitCursor = false;
            }
            catch (Exception oException)
            {
            this.UseWaitCursor = false;

            ErrorUtil.OnException(oException);
            }
        }
Esempio n. 4
0
        btnCustomizeVertexMenu_Click
        (
            object sender,
            EventArgs e
        )
        {
            AssertValid();

            const String Message =
                "Use this to add custom menu items to the menu that appears when"
                + " you right-click a vertex in the NodeXL graph."
                + "\r\n\r\n"
                + "Clicking \"Yes\" below will add a pair of columns to the"
                + " Vertices worksheet -- one for menu item text and another for"
                + " the action to take when the menu item is selected."
                + "\r\n\r\n"
                + "For example, if you add the column pair and enter \"Send Mail"
                + " To\" for a vertex's menu item text and \"mailto:[email protected]\""
                + " for the action, then right-clicking the vertex in the NodeXL"
                + " graph and selecting \"Send Mail To\" from the right-click menu"
                + " will open a new email message addressed to [email protected]."
                + "\r\n\r\n"
                + "If you want to open a Web page when the menu item is selected,"
                + " enter an URL for the action."
                + "\r\n\r\n"
                + "If you want to add more than one custom menu item to a vertex's"
                + " right-click menu, run this again to add another pair of"
                + " columns."
                + "\r\n\r\n"
                + "Do you want to add a pair of columns to the Vertices worksheet?"
            ;

            if (MessageBox.Show(Message, this.ApplicationName,
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information) !=
                DialogResult.Yes)
            {
                return;
            }

            // Create and use the object that adds the columns to the vertex
            // table.

            TableColumnAdder oTableColumnAdder = new TableColumnAdder();

            this.UseWaitCursor = true;

            try
            {
                oTableColumnAdder.AddColumnPair(m_oWorkbook,
                                                WorksheetNames.Vertices, TableNames.Vertices,
                                                VertexTableColumnNames.CustomMenuItemTextBase,
                                                VertexTableColumnWidths.CustomMenuItemText,
                                                VertexTableColumnNames.CustomMenuItemActionBase,
                                                VertexTableColumnWidths.CustomMenuItemAction
                                                );

                this.UseWaitCursor = false;
            }
            catch (Exception oException)
            {
                this.UseWaitCursor = false;

                ErrorUtil.OnException(oException);
            }
        }