EnableContextMenuItems
(
IVertex oClickedVertex
)
{
AssertValid();
// Base name of custom menu items.
const String CustomMenuItemNameBase = "msiCustom";
// Remove any custom menu items that were added in a previous call to
// this method.
ToolStripItemCollection oItems = cmsNodeXLControl.Items;
Int32 iItems = oItems.Count;
for (Int32 i = iItems - 1; i >= 0; i--)
{
ToolStripItem oItem = oItems[i];
if ( oItem.Name.StartsWith(CustomMenuItemNameBase) )
{
oItems.Remove(oItem);
}
}
Boolean bNonEmptyWorkbookRead = this.NonEmptyWorkbookRead;
ICollection<IVertex> oSelectedVertices =
oNodeXLControl.SelectedVertices;
Int32 iSelectedVertices = oSelectedVertices.Count;
// Selecting a vertex's incident edges makes sense only if they are not
// automatically selected when the vertex is clicked.
msiContextSelectIncidentEdges.Visible =
!( new GeneralUserSettings() ).AutoSelect;
// Selectively enable menu items.
Boolean bEnableSelectAllVertices, bEnableDeselectAllVertices,
bEnableSelectAdjacentVertices, bEnableDeselectAdjacentVertices,
bEnableSelectIncidentEdges, bEnableDeselectIncidentEdges,
bEnableEditVertexAttributes, bEnableSelectSubgraphs;
GetVertexCommandEnableFlags(oClickedVertex,
out bEnableSelectAllVertices, out bEnableDeselectAllVertices,
out bEnableSelectAdjacentVertices,
out bEnableDeselectAdjacentVertices,
out bEnableSelectIncidentEdges,
out bEnableDeselectIncidentEdges,
out bEnableEditVertexAttributes,
out bEnableSelectSubgraphs
);
Boolean bEnableSelectAllEdges, bEnableDeselectAllEdges, bNotUsed1,
bNotUsed2, bEnableEditEdgeAttributes;
GetEdgeCommandEnableFlags(null, out bEnableSelectAllEdges,
out bEnableDeselectAllEdges, out bNotUsed1, out bNotUsed2,
out bEnableEditEdgeAttributes);
msiContextSelectAllVertices.Enabled = bEnableSelectAllVertices;
msiContextDeselectAllVertices.Enabled = bEnableDeselectAllVertices;
msiContextSelectAdjacentVertices.Enabled =
bEnableSelectAdjacentVertices;
msiContextDeselectAdjacentVertices.Enabled =
bEnableDeselectAdjacentVertices;
msiContextSelectIncidentEdges.Enabled =
bEnableSelectIncidentEdges;
msiContextDeselectIncidentEdges.Enabled =
bEnableDeselectIncidentEdges;
msiContextEditEdgeAttributes.Enabled = bEnableEditEdgeAttributes;
msiContextEditVertexAttributes.Enabled = bEnableEditVertexAttributes;
msiContextSelectSubgraphs.Enabled = bEnableSelectSubgraphs;
MenuUtil.EnableToolStripMenuItems(
bNonEmptyWorkbookRead && iSelectedVertices > 0,
msiContextForceLayoutSelected
);
MenuUtil.EnableToolStripMenuItems(
bNonEmptyWorkbookRead,
msiContextForceLayout,
msiContextForceLayoutVisible,
msiContextSelectAll,
msiContextDeselectAll,
msiContextInvertSelection
);
MenuUtil.EnableToolStripMenuItems(
bNonEmptyWorkbookRead && iSelectedVertices > 1,
msiContextForceLayoutSelectedWithinBounds
);
msiContextSelectAllEdges.Enabled = bEnableSelectAllEdges;
msiContextDeselectAllEdges.Enabled = bEnableDeselectAllEdges;
MenuUtil.EnableToolStripMenuItems(
msiContextSelectAllVertices.Enabled ||
msiContextSelectAllEdges.Enabled,
msiContextSelectAllVerticesAndEdges
);
MenuUtil.EnableToolStripMenuItems(
msiContextDeselectAllVertices.Enabled ||
msiContextDeselectAllEdges.Enabled,
msiContextDeselectAllVerticesAndEdges
);
// Store the clicked vertex (or null if no vertex was clicked) in the
// menu items' Tag property for use by the menu item's Click handler.
msiContextSelectAdjacentVertices.Tag =
msiContextDeselectAdjacentVertices.Tag =
msiContextSelectIncidentEdges.Tag =
msiContextDeselectIncidentEdges.Tag =
msiContextSelectSubgraphs.Tag =
oClickedVertex;
if (iSelectedVertices == 1)
{
// Look for custom menu item information in the vertex's metadata.
Object oCustomMenuItemInformationAsObject;
IVertex oSelectedVertex = oSelectedVertices.First();
if ( oSelectedVertex.TryGetValue(
ReservedMetadataKeys.CustomContextMenuItems,
typeof( KeyValuePair<String, String>[] ),
out oCustomMenuItemInformationAsObject) )
{
// List of string pairs, one pair for each custom menu item to
// add to the vertex's context menu. The key is the custom
// menu item text and the value is the custom menu item action.
KeyValuePair<String, String> [] aoCustomMenuItemInformation =
( KeyValuePair<String, String> [] )
oCustomMenuItemInformationAsObject;
if (aoCustomMenuItemInformation.Length > 0)
{
// Add a separator before the custom menu items.
ToolStripSeparator oToolStripSeparator =
new ToolStripSeparator();
oToolStripSeparator.Name =
CustomMenuItemNameBase + "Separator";
cmsNodeXLControl.Items.Add(oToolStripSeparator);
}
Int32 i = 0;
foreach (KeyValuePair<String, String> oKeyValuePair in
aoCustomMenuItemInformation)
{
// Add a custom menu item. The tag stores the custom
// action.
String sCustomMenuItemText = oKeyValuePair.Key;
String sCustomMenuItemAction = oKeyValuePair.Value;
ToolStripMenuItem oToolStripMenuItem =
new ToolStripMenuItem(sCustomMenuItemText);
oToolStripMenuItem.Name =
CustomMenuItemNameBase + i.ToString();
oToolStripMenuItem.Tag = sCustomMenuItemAction;
oToolStripMenuItem.Click +=
new EventHandler(this.msiContextCustomMenuItem_Click);
cmsNodeXLControl.Items.Add(oToolStripMenuItem);
i++;
}
}
}
}