/// <summary> /// Gets or sets the item at the given zero-based index. /// </summary> /// <param name="index">The zero-based index of the item.</param> /// <returns>The object retrieved or the object that is being set to the specified index.</returns> public Object this[Int32 index] { get { return(this.GetItemAt(index)); } set { // Throw an exception when using the indirect collection. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // This insures that the index is within the array bounds. if (index < 0 || index >= this.items.Count) { throw new ArgumentOutOfRangeException("index"); } // Set the item at the given index to the given value. this.items[index] = value; } }
/// <summary> /// Gets a Visual child of this Panel at the specified index position. /// </summary> /// <param name="index">The index position of the Visual child.</param> /// <returns>A Visual child of the parent Panel element.</returns> protected override Visual GetVisualChild(int index) { if (index < 0 || index >= this.VisualChildrenCount) { throw new ArgumentOutOfRangeException(ExceptionMessage.Format(ExceptionMessages.VisualArgumentOutOfRange, index)); } return(this.uiElementCollection[index]); }
/// <summary> /// Removes the item at the specified position from the collection. /// </summary> /// <param name="index">The position of the item to remove.</param> void IEditableCollectionView.RemoveAt(Int32 index) { IEditableCollectionView iEditableCollectionView = this.currentView as IEditableCollectionView; if (iEditableCollectionView == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.MemberNotAllowedForView, "RemoveAt")); } iEditableCollectionView.RemoveAt(index); }
/// <summary> /// Begins an edit transaction of the specified item. /// </summary> /// <param name="item">The item to edit.</param> void IEditableCollectionView.EditItem(Object item) { IEditableCollectionView iEditableCollectionView = this.currentView as IEditableCollectionView; if (iEditableCollectionView == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.MemberNotAllowedForView, "EditItem")); } iEditableCollectionView.EditItem(item); }
/// <summary> /// Ends the add transaction and saves the pending new item. /// </summary> void IEditableCollectionView.CommitNew() { IEditableCollectionView iEditableCollectionView = this.currentView as IEditableCollectionView; if (iEditableCollectionView == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.MemberNotAllowedForView, "CommitNew")); } iEditableCollectionView.CommitNew(); }
/// <summary> /// Adds a new item to the collection. /// </summary> /// <returns></returns> Object IEditableCollectionView.AddNew() { IEditableCollectionView iEditableCollectionView = this.currentView as IEditableCollectionView; if (iEditableCollectionView == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.MemberNotAllowedForView, "AddNew")); } return(iEditableCollectionView.AddNew()); }
/// <summary> /// Removes the item at the specified index of the collection or view. /// </summary> /// <param name="index">The zero-based index of the item to remove.</param> public void RemoveAt(Int32 index) { // Throw an exception when using the indirect collection. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // Remove the item from the inner collection and update the view. this.items.RemoveAt(index); }
/// <summary> /// Removes the specified item reference from the collection or view. /// </summary> /// <param name="value">The object to remove.</param> public void Remove(Object value) { // Throw an exception when using the indirect collection. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // Remove the item from the inner collection and refresh the view. this.items.Remove(value); }
/// <summary> /// Inserts an element into the collection at the specified index. /// </summary> /// <param name="index">The zero-based index at which to insert the item.</param> /// <param name="value">The item to insert.</param> public void Insert(Int32 index, Object value) { // Throw an exception when using the indirect collection. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // Insert the item into the direct collection. this.items.Insert(index, value); this.Refresh(); }
/// <summary> /// Adds an item to the IList. /// </summary> /// <param name="value">The object to add to the IList.</param> /// <returns>The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection.</returns> public Int32 Add(Object value) { // Throw an exception when using the indirect collection. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // This will add the item to the direct collection managed by this wrapper. this.items.Add(value); return(this.items.Count - 1); }
/// <summary> /// Clears the collection and releases the references on all items currently in the collection. /// </summary> public void Clear() { // This operation is illegal in the indirect mode. if (this.itemsSource != null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.ItemsSourceInUse)); } // As long as the above conditions are met it's alright to clear out the base collection. if (this.items != null) { this.items.Clear(); } }
/// <summary> /// Copies the elements of the collection to an array, starting at a particular array index. /// </summary> /// <param name="array">The destination array to copy to.</param> /// <param name="index">The zero-based index in the destination array.</param> public void CopyTo(Array array, Int32 index) { // Don't allow an empty array to be copied. if (array == null) { throw new ArgumentNullException("array"); } // Only 1 dimensional (vector) arrays can be copied. if (array.Rank > 1) { throw new ArgumentException(ExceptionMessage.Format(ExceptionMessages.BadTargetArray)); } // Insure that the start index is valid. if (index < 0) { throw new ArgumentOutOfRangeException("index"); } // The operation can't be done while waiting for a refresh. // An indexable collection can be copied directly. An IEnumerable collection requires a little more work to construct and use an iterator. ICollection iCollection = this.currentView as ICollection; if (iCollection != null) { iCollection.CopyTo(array, index); } else { // This will enumerate through the collection when it's based on IEnumerable and copy elements into the destination array. IEnumerable iEnumerable = this.currentView as IEnumerable; IEnumerator enumerator = iEnumerable.GetEnumerator(); IList iList = array as IList; while (enumerator.MoveNext()) { // There's no way to perform this check outside of the enumerator as we don't know how many items are in the IEnumerable collection. if (index >= array.Length) { throw new ArgumentException(ExceptionMessage.Format(ExceptionMessages.CopyToNotEnoughSpace)); } // This will copy the next element out of the current collection's view and into the given array. iList[index++] = enumerator.Current; } } }
/// <summary> /// Gets the identifiers for the template URI. /// </summary> private void GetIdentifiersForUri() { // This will collect the unique names of all the replacement parameters in the template URI. this.uriIdentifiers.Clear(); MatchCollection matches = conversionRegex.Matches(this.uri.OriginalString); foreach (Match match in matches) { String item = match.Groups["parameter"].Value; if (this.uriIdentifiers.Contains(item)) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.UriMappingUriTemplateCannotRepeatIdentifier, "Uri")); } this.uriIdentifiers.Add(item); } }
/// <summary> /// Check that the URI meets the conditions required for mapping. /// </summary> private void Initialize() { // Check that a mapping URI has been provided for this mapping. if (this.mappedUri == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.PropertyCannotBeNull, "MappedUri")); } // Check that a URI has been provided for this mapping. if (this.uri == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.PropertyCannotBeNull, "Uri")); } // The object can be uninitialized by seeting either of it's public properties. This will re-initialize it just in time for the mapping. if (!this.initialized) { String regexBuilder = this.uri.OriginalString.Replace("\\", "\\\\"); this.uriRegex = new Regex("^" + conversionRegex.Replace(regexBuilder, "(?<$1>.*)") + "$"); this.GetIdentifiersForUri(); this.GetIdentifiersForMappedUri(); this.initialized = true; } }
/// <summary> /// Handle all unhandled exceptions. /// </summary> /// <param name="sender">The object that generated the event.</param> /// <param name="dispatcherUnhandledExceptionEventArgs">Information about the exception event.</param> void OnDispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs) { // A certain class of errors should kill the application, others will just mortally wound it. This is used to euthanize the application when the right // conditions arise. Boolean isFatal = false; // This string will collect the error message that is eventually displayed to the user after the exceptions are examined in order of importance to the // user. The more generic exceptions are examined first and then, if more specific information is available, it will overwrite the generic message. String errorMessage = null; // This handler will try to extract the most specific error message and work its way up to the least specific. As a general rule, if a specific error // message can be made more friendly for the user, the effort is made to provide a less technical description and a more action-oriented message. if (dispatcherUnhandledExceptionEventArgs.Exception.InnerException != null) { // The inner exception has the real reason for this exception. Exception innerException = dispatcherUnhandledExceptionEventArgs.Exception.InnerException; // This will determine if the invalid period for a license has expired and provide detailed instructions about how to correct the error // including the product name and manufacturer. InvalidLicenseException invalidlicenseException = innerException as InvalidLicenseException; if (invalidlicenseException != null) { IProductId iProductId = invalidlicenseException.Instance as IProductId; errorMessage = ExceptionMessage.Format( Teraque.ExplorerChromeExample.Properties.Resources.InvalidLicense, App.productNames[iProductId.ProductId]); isFatal = true; } // This will determine if the evaluation period for a license has expired and provide detailed instructions about how to correct the error // including the product name and manufacturer. EvaluationLicenseException evaluationlicenseException = innerException as EvaluationLicenseException; if (evaluationlicenseException != null) { IProductId iProductId = evaluationlicenseException.Instance as IProductId; errorMessage = ExceptionMessage.Format( Teraque.ExplorerChromeExample.Properties.Resources.EvaluationLicense, App.productNames[iProductId.ProductId]); isFatal = true; } // This will determine if the a valid license has expired and provide detailed instructions about how to correct the error including the product // name and manufacturer. ExpiredLicenseException expiredlicenseException = innerException as ExpiredLicenseException; if (expiredlicenseException != null) { IProductId iProductId = expiredlicenseException.Instance as IProductId; errorMessage = ExceptionMessage.Format( Teraque.ExplorerChromeExample.Properties.Resources.ExpiredLicense, App.productNames[iProductId.ProductId]); isFatal = true; } // If a message wasn't extracted for a specific purpose above then use the general message associated with the inner exception as the text that the // user will see. if (errorMessage == null) { errorMessage = dispatcherUnhandledExceptionEventArgs.Exception.InnerException.Message; } } // The handling of this event works from the most specific error message to the least specific. If no error message has been extracted by this point // the message associated with the exception that generated this event is used as the most general message. if (errorMessage == null) { errorMessage = dispatcherUnhandledExceptionEventArgs.Exception.Message; } // Display the message associated with the exception to the user. MessageBox.Show(errorMessage, Teraque.ExplorerChromeExample.Properties.Resources.Title, MessageBoxButton.OK, MessageBoxImage.Error); // If the error was fatal then shut down the application. if (isFatal) { Application.Current.Shutdown(); } // The exceptions will not kill the program. dispatcherUnhandledExceptionEventArgs.Handled = true; }