/// <summary> /// Find a FeatureLayer in the map which uses the /// specified table, and return it. /// </summary> /// <param name="map">The Map object to search</param> /// <param name="sourceTable"> /// A table which may or may not be displayed in a FeatureLayer</param> /// <param name="currentFeatureLayer"> /// If null is passed, we will return the first FeatureLayer we find /// that uses the specified table; if currentFeatureLayer is not /// null, we will search for the next FeatureLayer that uses /// the same table. /// </param> /// <param name="wrapAround">true if you want a "find next" search /// to wrap around to the beginning of the layer tree, if necessary, /// to find the next layer; false if you do not want the search to wrap. /// </param> /// <returns>A FeatureLayer object (which may be identical to /// the currentFeatureLayer param, e.g. if the currentFeatureLayer /// param represents the only FeatureLayer in the map); or null if /// the map does not contain any FeatureLayer based on the specified table. /// </returns> private FeatureLayer LocateNextFeatureLayer( Map map, Table sourceTable, FeatureLayer currentFeatureLayer, bool wrapAround) { FilterByLayerType featureLayerFilter = new FilterByLayerType( LayerType.Normal, LayerType.Grid, LayerType.Raster, LayerType.Wms); MapLayerEnumerator mapLayerEnum = map.Layers.GetMapLayerEnumerator( featureLayerFilter, MapLayerEnumeratorOptions.Recurse); FeatureLayer matchingFeatureLayer = null; FeatureLayer firstMatchingFeatureLayer = null; bool bPassedCurrentFeatureLayer = false; foreach (FeatureLayer featLyr in mapLayerEnum) { if (featLyr.Table == sourceTable) { // This FeatureLayer uses the correct table if (firstMatchingFeatureLayer == null) { // We found our first match, so make a note of it, // even though it may not be ideal (i.e. it may not // be the "next" FeatureLayer that was requested). firstMatchingFeatureLayer = featLyr; } // Now determine whether this match is an ideal match. // We may have been passed a FeatureLayer, and asked // to find the "next" FeatureLayer. So we may need // to skip over the current FeatureLayer if it's // the same as the FeatureLayer that was passed in. if (currentFeatureLayer != null && !bPassedCurrentFeatureLayer) { // We WERE asked to find the "next" FeatureLayer, // which means that our first task is to find // the FeatureLayer that the user right-clicked. // But according to the flag, we have not yet // looped past the currently-selected FeatureLayer. // So we will continue the loop instead of // assigning matchingFeatureLayer. if (featLyr == currentFeatureLayer) { bPassedCurrentFeatureLayer = true; } continue; } matchingFeatureLayer = featLyr; break; } } if (matchingFeatureLayer == null) { // We did not find an ideal match (i.e. we may have been // asked to find the Next FeatureLayer, and there may not // have been a Next FeatureLayer). // At this point, since we did not find a perfect match, // we will consider a less-than-perfect match. if (wrapAround) { // wrapAround is true, meaning that when we search for // the next layer, we should wrap around to the top of // the layer list, if necessary matchingFeatureLayer = firstMatchingFeatureLayer; } else { // wrapAround is false, meaning that when we cannot find // a next layer, we should return the layer that was // originally specified, which will tell the caller, // "there is no Next layer." matchingFeatureLayer = currentFeatureLayer; } } return(matchingFeatureLayer); }
/// <summary> /// Find a LabelSource in the map which uses the /// specified table, and return it, or return null /// if no suitable LabelSource exists. /// </summary> /// <param name="map">The Map object to search</param> /// <param name="sourceTable"> /// A table which may or may not be labeled currently</param> /// <param name="currentLabelSource"> /// If null is passed, we will return the first LabelSource we find /// that uses the specified table; if currentLabelSource is not /// null, we will search for the next LabelSource that uses /// the same table. /// </param> /// <param name="wrapAround">true if you want a "find next" search /// to wrap around to the beginning of the layer tree, if necessary, /// to find the next LabelSource; false if you do not the search to wrap. /// </param> /// <returns>A LabelSource object (which may be identical to /// the currentLabelSource param, e.g. if the currentLabelSource /// param represents the only LabelSource in the map); or null if /// the map does not contain any LabelSource based on the specified table. /// </returns> private LabelSource LocateNextLabelSource( Map map, Table sourceTable, LabelSource currentLabelSource, bool wrapAround) { // Get a collection of all LabelLayers in the map FilterByLayerType labelLayerFilter = new FilterByLayerType(LayerType.Label); MapLayerEnumerator mapLayerEnum = map.Layers.GetMapLayerEnumerator( labelLayerFilter, MapLayerEnumeratorOptions.Recurse); _firstLabelLayer = null; LabelSource matchingLabelSource = null; LabelSource firstMatchingLabelSource = null; bool bPassedCurrentLabelSource = false; // Given the set of all LabelLayers in the layer tree, // search each LabelLayer to try to find a LabelSource // child node that is based on the specified table // (the sourceTable param). // OR: If a non-null currentLabelSource param // was passed in, then it represents an existing // LabelSource, and our job is to find the NEXT // LabelSource that uses the same table. foreach (LabelLayer ll in mapLayerEnum) { if (_firstLabelLayer == null) { // Make a note of the first LabelLayer we find; // later, if we decide to create a new LabelSource, // it will go into this first LabelLayer. _firstLabelLayer = ll; } // Look through this LabelLayer's collection of LabelSources foreach (LabelSource lblSource in ll.Sources) { if (lblSource.Table == sourceTable) { // This LabelSource uses the correct table if (firstMatchingLabelSource == null) { // We found our first match, so make a note of it, // even though it may not be ideal (i.e. it may not // be the "next" LabelSource that was requested). firstMatchingLabelSource = lblSource; } // Now determine whether this match is an ideal match. // We may have been passed a LabelSource, and asked // to find the "next" LabelSource. So we may need // to skip over the current LabelSource if it's // the same as the LabelSource that was passed in. if (currentLabelSource != null && !bPassedCurrentLabelSource) { // We WERE asked to find the "next" LabelSource, // which means that our first task is to find // the LabelSource that the user right-clicked. // But according to the flag, we have not yet // looped past the currently-selected LabelSource. // So we will continue the loop instead of // assigning matchingLabelSource. if (lblSource == currentLabelSource) { // We were asked to find the next LabelSource, // and this LabelSource is the same one that // was passed in. In this case, just set the // flag, so that the next match will be used. bPassedCurrentLabelSource = true; } continue; } // We found a LabelSource that is a perfect match. matchingLabelSource = lblSource; break; } } // This ends the "for each LabelSource in this LabelLayer" loop if (matchingLabelSource != null) { // We found an ideal match, so we can skip searching // the other LabelLayers. Break the outer foreach loop: break; } } // This ends the "for each LabelLayer" loop if (matchingLabelSource == null) { // We end up here if we did not find an ideal match; for // example, if we were asked to find the "next" node, and // we did not find a next matching node, but we did find a // previous matching node, we end up here. // At this point, since we did not find a perfect match, // we will consider a less-than-perfect match. if (wrapAround) { // Wrapping is On, meaning that when we search for // the next LabelSource, we should wrap around to the top of // the layer list, if necessary matchingLabelSource = firstMatchingLabelSource; } else { // Wrap is Off, meaning: if we did not find a Next LabelSource, // return the LabelSource that was originally specified, // which will tell the caller, "there IS no Next LabelSource." matchingLabelSource = currentLabelSource; } } return(matchingLabelSource); }