/// <summary> /// Makes it so that if there are any pre-existing listeners to the SelectFeatuers /// event when creating a clone of this object, those listeners are removed. /// They should be added correctly when the cloned item is added to the collection, /// after being cloned. /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { FeatureCategory cat = copy as FeatureCategory; if(cat != null && cat.SelectFeatures != null) { foreach (var handler in cat.SelectFeatures.GetInvocationList()) { cat.SelectFeatures -= (EventHandler<ExpressionEventArgs>)handler; } } if(cat != null) { cat.CreateContextMenuItems(); } base.OnCopy(copy); }
/// <summary> /// This helps the copy process by preparing the internal variables AFTER memberwiseclone has created this /// /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { // Setting the image property has a built in check to dispose the older reference. // After a MemberwiseClone, however, this older reference is still in use in the original PictureSymbol. // We must, therefore, set the private variables to null before doing the cloning. PictureSymbol duplicate = copy as PictureSymbol; if (duplicate != null) { duplicate._image = null; duplicate._original = null; } base.OnCopy(copy); if(duplicate != null) { duplicate._image = _image.Copy(); duplicate._original = _original.Copy(); } }
/// <summary> /// Occurs during a copy operation and handles removing surplus event handlers /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { // Remove event handlers from the copy (since Memberwise clone also clones handlers.) FeatureLayer flCopy = copy as FeatureLayer; if (flCopy == null) return; if(flCopy.ViewAttributes != null) { foreach (var handler in flCopy.ViewAttributes.GetInvocationList()) { flCopy.ViewAttributes -= (HandledEventHandler) handler; } } if(flCopy.LabelSetup != null) { foreach (var handler in flCopy.LabelSetup.GetInvocationList()) { flCopy.LabelSetup -= (HandledEventHandler) handler; } } if(flCopy.SnapShotTaken != null) { foreach (var handler in flCopy.SnapShotTaken.GetInvocationList()) { flCopy.SnapShotTaken -= (EventHandler<SnapShotEventArgs>) handler; } } if(flCopy.SchemeApplied != null) { foreach (var handler in flCopy.SchemeApplied.GetInvocationList()) { flCopy.SchemeApplied -= (EventHandler) handler; } } base.OnCopy(copy); }
/// <summary> /// Special handling of not copying the parent during a copy operation /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { FeatureScheme scheme = copy as FeatureScheme; if (scheme != null && scheme.SelectFeatures != null) { foreach (var handler in scheme.SelectFeatures.GetInvocationList()) { scheme.SelectFeatures -= (EventHandler<ExpressionEventArgs>)handler; } } // Disconnecting the parent prevents the immediate application of copied scheme categories to the original layer. SuspendEvents(); base.OnCopy(copy); ResumeEvents(); }
/// <summary> /// This occurs during the Copy method and is overridable by sub-classes /// </summary> /// <param name="copy">The duplicate descriptor</param> protected virtual void OnCopy(Descriptor copy) { // This checks any property on copy, and if it is cloneable, it // creates a clone instead Type copyType = copy.GetType(); PropertyInfo[] copyProperties = DistinctNames(copyType.GetProperties(BindingFlags.Public | BindingFlags.Instance)); PropertyInfo[] myProperties = DistinctNames(GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)); foreach (PropertyInfo p in copyProperties) { if (p.CanWrite == false) continue; if(myProperties.Contains(p.Name) == false) continue; PropertyInfo myProperty = myProperties.GetFirst(p.Name); object myValue = myProperty.GetValue(this, null); if (myProperty.GetCustomAttributes(typeof(ShallowCopy), true).Length > 0) { // This property is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) continue; p.SetValue(copy, cloneable.Clone(), null); } FieldInfo[] copyFields = copyType.GetFields(BindingFlags.Public | BindingFlags.Instance); FieldInfo[] myFields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo f in copyFields) { if (myFields.Contains(f.Name) == false) continue; FieldInfo myField = myFields.GetFirst(f.Name); object myValue = myField.GetValue(copy); if (myField.GetCustomAttributes(typeof(ShallowCopy), true).Length > 0) { // This field is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) continue; f.SetValue(copy, cloneable.Clone()); } }
/// <summary> /// Temporarilly handle list duplication here. This may be replaced by more general /// duplication extension methods for IList. /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { base.OnCopy(copy); TextSymbolGroup tsg = copy as TextSymbolGroup; List<int> selectedLabels = new List<int>(); foreach (int value in _selectedLabels) { selectedLabels.Add(value); } tsg.SelectedLabels = selectedLabels; List<int> unselectedLabels = new List<int>(); foreach (int value in _regularLabels) { unselectedLabels.Add(value); } tsg.RegularLabels = unselectedLabels; }
/// <summary> /// special treatment for event handlers during a copy event /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { // Remove event handlers from the copy. (They will be set again when adding to a new map.) Layer copyLayer = copy as Layer; if (copyLayer == null) return; if(copyLayer.LayerSelected != null) { foreach (var handler in copyLayer.LayerSelected.GetInvocationList()) { copyLayer.LayerSelected -= (EventHandler<LayerSelectedEventArgs>) handler; } } if(copyLayer.ZoomToLayer != null) { foreach (var handler in copyLayer.ZoomToLayer.GetInvocationList()) { copyLayer.ZoomToLayer -= (EventHandler<EnvelopeArgs>) handler; } } if(copyLayer.ShowProperties != null) { foreach (var handler in copyLayer.ShowProperties.GetInvocationList()) { copyLayer.ShowProperties -= (HandledEventHandler) handler; } } if (copyLayer.FinishedLoading != null) { foreach (var handler in copyLayer.FinishedLoading.GetInvocationList()) { copyLayer.FinishedLoading -= (EventHandler) handler; } } if (copyLayer.SelectionChanged != null) { foreach (var handler in copyLayer.SelectionChanged.GetInvocationList()) { copyLayer.SelectionChanged -= (EventHandler)handler; } } base.OnCopy(copy); }
/// <summary> /// Handles updating event handlers during a copy process /// </summary> /// <param name="copy"></param> protected override void OnCopy(Descriptor copy) { LegendItem myCopy = copy as LegendItem; if(myCopy != null && myCopy.ItemChanged != null) { foreach (Delegate handler in myCopy.ItemChanged.GetInvocationList()) { myCopy.ItemChanged -= (EventHandler)handler; } } if(myCopy != null && myCopy.RemoveItem != null) { foreach (Delegate handler in myCopy.RemoveItem.GetInvocationList()) { myCopy.RemoveItem -= (EventHandler)handler; } } base.OnCopy(copy); }