//************************************************************************* // Method: ReadGroupVertexTable() // /// <summary> /// Reads the group vertex table. /// </summary> /// /// <param name="oGroupVertexTable"> /// The group vertex table. /// </param> /// /// <param name="oReadWorkbookContext"> /// Provides access to objects needed for converting an Excel workbook to a /// NodeXL graph. /// </param> /// /// <param name="oGroupNameDictionary"> /// The key is the group name and the value is the GroupInformation object /// for the group. /// </param> /// /// <param name="oGraph"> /// Graph to add group data to. /// </param> //************************************************************************* protected void ReadGroupVertexTable( ListObject oGroupVertexTable, ReadWorkbookContext oReadWorkbookContext, Dictionary<String, GroupInformation> oGroupNameDictionary, IGraph oGraph ) { Debug.Assert(oGroupVertexTable != null); Debug.Assert(oReadWorkbookContext != null); Debug.Assert(oGroupNameDictionary != null); Debug.Assert(oGraph != null); AssertValid(); Dictionary<String, IVertex> oVertexNameDictionary = oReadWorkbookContext.VertexNameDictionary; ExcelTableReader oExcelTableReader = new ExcelTableReader(oGroupVertexTable); foreach ( ExcelTableReader.ExcelTableRow oRow in oExcelTableReader.GetRows() ) { // Get the group vertex information from the row. String sGroupName, sVertexName; if ( !oRow.TryGetNonEmptyStringFromCell( GroupVertexTableColumnNames.GroupName, out sGroupName) || !oRow.TryGetNonEmptyStringFromCell( GroupVertexTableColumnNames.VertexName, out sVertexName) ) { continue; } // Get the group information for the vertex and store the group // information in the vertex. GroupInformation oGroupInformation; IVertex oVertex; if ( !oGroupNameDictionary.TryGetValue(sGroupName, out oGroupInformation) || !oVertexNameDictionary.TryGetValue(sVertexName, out oVertex) ) { continue; } oVertex.SetValue(ReservedMetadataKeys.PerColor, oGroupInformation.VertexColor); oVertex.SetValue(ReservedMetadataKeys.PerVertexShape, oGroupInformation.VertexShape); if (oReadWorkbookContext.SaveGroupVertices) { Debug.Assert(oGroupInformation.Vertices != null); oGroupInformation.Vertices.Add(oVertex); } } }
//************************************************************************* // Method: ReadGroupTable() // /// <summary> /// Reads the group table. /// </summary> /// /// <param name="oGroupTable"> /// The group table. /// </param> /// /// <param name="oReadWorkbookContext"> /// Provides access to objects needed for converting an Excel workbook to a /// NodeXL graph. /// </param> /// /// <returns> /// A dictionary. The key is the group name and the value is a /// GroupInformation object for the group. /// </returns> //************************************************************************* protected Dictionary<String, GroupInformation> ReadGroupTable( ListObject oGroupTable, ReadWorkbookContext oReadWorkbookContext ) { Debug.Assert(oGroupTable != null); Debug.Assert(oReadWorkbookContext != null); AssertValid(); if (oReadWorkbookContext.FillIDColumns) { FillIDColumn(oGroupTable); } Dictionary<String, GroupInformation> oGroupNameDictionary = new Dictionary<String, GroupInformation>(); ColorConverter2 oColorConverter2 = oReadWorkbookContext.ColorConverter2; BooleanConverter oBooleanConverter = oReadWorkbookContext.BooleanConverter; ExcelTableReader oExcelTableReader = new ExcelTableReader(oGroupTable); foreach ( ExcelTableReader.ExcelTableRow oRow in oExcelTableReader.GetRows() ) { // Get the group information. String sGroupName; Color oVertexColor; VertexShape eVertexShape; if ( !oRow.TryGetNonEmptyStringFromCell(GroupTableColumnNames.Name, out sGroupName) || !TryGetColor(oRow, GroupTableColumnNames.VertexColor, oColorConverter2, out oVertexColor) || !TryGetVertexShape(oRow, GroupTableColumnNames.VertexShape, out eVertexShape) ) { continue; } Boolean bCollapsed = false; Boolean bCollapsedCellValue; if ( TryGetBoolean(oRow, GroupTableColumnNames.Collapsed, oBooleanConverter, out bCollapsedCellValue) && bCollapsedCellValue ) { bCollapsed = true; } Int32 iRowIDAsInt32; Nullable<Int32> iRowID = null; if ( oRow.TryGetInt32FromCell(CommonTableColumnNames.ID, out iRowIDAsInt32) ) { iRowID = iRowIDAsInt32; } GroupInformation oGroupInformation = new GroupInformation( sGroupName, iRowID, oVertexColor, eVertexShape, bCollapsed); if (oReadWorkbookContext.SaveGroupVertices) { // ReadGroupVertexTable() will save the group's vertices in // this LinkedList. oGroupInformation.Vertices = new LinkedList<IVertex>(); } try { oGroupNameDictionary.Add(sGroupName, oGroupInformation); } catch (ArgumentException) { Range oInvalidCell = oRow.GetRangeForCell( GroupTableColumnNames.Name); OnWorkbookFormatError( String.Format( "The cell {0} contains a duplicate group name. There" + " can't be two rows with the same group name." , ExcelUtil.GetRangeAddress(oInvalidCell) ), oInvalidCell ); } } return (oGroupNameDictionary); }
//************************************************************************* // Method: ReadClusterTable() // /// <summary> /// Reads the cluster table. /// </summary> /// /// <param name="oClusterTable"> /// The cluster table. /// </param> /// /// <param name="oReadWorkbookContext"> /// Provides access to objects needed for converting an Excel workbook to a /// NodeXL graph. /// </param> /// /// <returns> /// A dictionary. The key is the cluster name and the value is a /// ClusterInformation object for the cluster. /// </returns> //************************************************************************* protected Dictionary<String, ClusterInformation> ReadClusterTable( ListObject oClusterTable, ReadWorkbookContext oReadWorkbookContext ) { Debug.Assert(oClusterTable != null); Debug.Assert(oReadWorkbookContext != null); AssertValid(); Dictionary<String, ClusterInformation> oClusterNameDictionary = new Dictionary<String, ClusterInformation>(); ColorConverter2 oColorConverter2 = oReadWorkbookContext.ColorConverter2; ExcelTableReader oExcelTableReader = new ExcelTableReader(oClusterTable); foreach ( ExcelTableReader.ExcelTableRow oRow in oExcelTableReader.GetRows() ) { // Get the cluster information. String sClusterName; Color oVertexColor; VertexShape eVertexShape; if ( !oRow.TryGetNonEmptyStringFromCell( ClusterTableColumnNames.Name, out sClusterName) || !TryGetColor(oRow, ClusterTableColumnNames.VertexColor, oColorConverter2, out oVertexColor) || !TryGetVertexShape(oRow, ClusterTableColumnNames.VertexShape, out eVertexShape) ) { continue; } // Add the cluster information to the dictionary. ClusterInformation oClusterInformation = new ClusterInformation(); oClusterInformation.VertexColor = oVertexColor; oClusterInformation.VertexShape = eVertexShape; try { oClusterNameDictionary.Add( sClusterName, oClusterInformation); } catch (ArgumentException) { Range oInvalidCell = oRow.GetRangeForCell( ClusterTableColumnNames.Name); OnWorkbookFormatError( String.Format( "The cell {0} contains a duplicate cluster name. There" + " can't be two rows with the same cluster name." , ExcelUtil.GetRangeAddress(oInvalidCell) ), oInvalidCell ); } } return (oClusterNameDictionary); }
//************************************************************************* // Method: ReadClusterVertexTable() // /// <summary> /// Reads the cluster vertex table. /// </summary> /// /// <param name="oClusterVertexTable"> /// The cluster vertex table. /// </param> /// /// <param name="oVertexNameDictionary"> /// The key is the vertex name from the edge or vertex worksheet and the /// value is the IVertex object. /// </param> /// /// <param name="oClusterNameDictionary"> /// The key is the cluster name and the value is the ClusterInformation /// object for the cluster. /// </param> /// /// <param name="oGraph"> /// Graph to add cluster data to. /// </param> /// /// <returns> /// A dictionary. The key is the cluster name and the value is the /// ClusterInformation object for the cluster. /// </returns> //************************************************************************* protected void ReadClusterVertexTable( ListObject oClusterVertexTable, Dictionary<String, ClusterInformation> oClusterNameDictionary, Dictionary<String, IVertex> oVertexNameDictionary, IGraph oGraph ) { Debug.Assert(oClusterVertexTable != null); Debug.Assert(oClusterNameDictionary != null); Debug.Assert(oVertexNameDictionary != null); Debug.Assert(oGraph != null); AssertValid(); ExcelTableReader oExcelTableReader = new ExcelTableReader(oClusterVertexTable); foreach ( ExcelTableReader.ExcelTableRow oRow in oExcelTableReader.GetRows() ) { // Get the cluster vertex information from the row. String sClusterName, sVertexName; if ( !oRow.TryGetNonEmptyStringFromCell( ClusterVertexTableColumnNames.ClusterName, out sClusterName) || !oRow.TryGetNonEmptyStringFromCell( ClusterVertexTableColumnNames.VertexName, out sVertexName) ) { continue; } // Get the cluster information for the vertex and store the cluster // information in the vertex. ClusterInformation oClusterInformation; IVertex oVertex; if ( !oClusterNameDictionary.TryGetValue(sClusterName, out oClusterInformation) || !oVertexNameDictionary.TryGetValue(sVertexName, out oVertex) ) { continue; } oVertex.SetValue(ReservedMetadataKeys.PerColor, oClusterInformation.VertexColor); oVertex.SetValue(ReservedMetadataKeys.PerVertexShape, oClusterInformation.VertexShape); } }
//************************************************************************* // 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) ); } }