/// <summary>
        /// Document loaded.
        /// </summary>
        public override void OnDocumentLoaded()
        {
            base.OnDocumentLoaded();


            // clear current error list
            this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

            List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
            if (this.SelectedModelContextViewModel.ModelContext.SerializationResult.Count() > 0)
            {
                // add serialization result items

                foreach (SerializationMessage serializationMessage in this.SelectedModelContextViewModel.ModelContext.SerializationResult)
                {
                    SerializationErrorListItemViewModel item = new SerializationErrorListItemViewModel(this.ViewModelStore, serializationMessage);
                    items.Add(item);
                }
            }

            if (this.SelectedModelContextViewModel.ModelContext.ValidationResult.Count > 0)
            {
                foreach (IValidationMessage message in this.SelectedModelContextViewModel.ModelContext.ValidationResult)
                {
                    if (message is ModelValidationMessage)
                    {
                        ModelErrorListItemViewModel item = new ModelErrorListItemViewModel(this.ViewModelStore, message as ModelValidationMessage);
                        items.Add(item);
                    }
                    else if (message is ValidationMessage)
                    {
                        StringErrorListItemViewModel item = new StringErrorListItemViewModel(this.ViewModelStore, message.MessageId, ModelErrorListItemViewModel.ConvertCategory(message.Type), message.Description);
                        items.Add(item);
                    }
                }
            }

            // notify of change
            if (items.Count > 0)
                this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
        }
        /// <summary>
        /// Validate command executed.
        /// </summary>
        protected override void OnValidateCommandExecuted()
        {
            if (this.SelectedItemViewModel == null)
                return;

            using (new ToolFramework.Modeling.Visualization.ViewModel.UI.WaitCursor())
            {
                // clear error list
                this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                List<IValidationMessage> messages = this.ModelData.CurrentModelContext.Validate(this.SelectedItemViewModel.Element);
                List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
                foreach (IValidationMessage message in messages)
                {
                    if (message is ModelValidationMessage)
                    {
                        ModelErrorListItemViewModel item = new ModelErrorListItemViewModel(this.ViewModelStore, message as ModelValidationMessage);
                        items.Add(item);
                    }
                    else if (message is ValidationMessage)
                    {
                        StringErrorListItemViewModel item = new StringErrorListItemViewModel(this.ViewModelStore, message.MessageId, ModelErrorListItemViewModel.ConvertCategory(message.Type), message.Description);
                        items.Add(item);
                    }
                }

                // notify of change
                this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
            }
        }
        /// <summary>
        /// Assigns the property value to the property of every source element.
        /// </summary>
        /// <param name="value">Property value to be assigned.</param>
        public override void SetPropertyValue(object value)
        {
            // empty string values are treated as null...
            Html html = value as Html;
            if (html != null)
                if (html.HtmlData.Trim() == string.Empty)
                    html = null;

            using (Transaction transaction = this.Store.TransactionManager.BeginTransaction("Update property value - " + this.PropertyName))
            {
                PropertyGridEditorViewModel.SetPropertyValues(this.Elements, this.PropertyName, value);
                transaction.Commit();
            }

            // handle errors
            if (html != null)
                if (html.ValidationResult.Count() > 0)
                {
                    // clear current error list
                    this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                    // add serialization result items
                    List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
                    foreach (IValidationMessage validationmessage in html.ValidationResult)
                    {
                        StringErrorListItemViewModel item = new StringErrorListItemViewModel(this.ViewModelStore, validationmessage.MessageId, ModelErrorListItemViewModel.ConvertCategory(validationmessage.Type), validationmessage.Description);
                        items.Add(item);
                    }

                    // notify of change
                    this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
                }
        }
        /// <summary>
        /// Open model.
        /// </summary>
        /// <param name="fileName">File to open.</param>
        public virtual void OpenModel(string fileName)
        {
            if (this.SelectedModelContextViewModel == null)
                return;
            if (this.SelectedModelContextViewModel.ModelContext == null)
                return;
            
            if (CloseModelCommandCanExecute())
                if (!CloseModelCommandExecute())
                    return;

            if (this.Ribbon != null)
                this.Ribbon.IsBackstageOpen = false;

            if (!System.IO.File.Exists(fileName))
            {
                IMessageBoxService messageBox = this.GlobalServiceProvider.Resolve<IMessageBoxService>();
                messageBox.ShowError("File does not exists: " + fileName);
                return;
            }

            // LOAD model in background thread
            // in main UI thread, load avalondock
            // --> wait on ui thread for model load to finish...
            /*if (!this.isInitialyInitialized)
            {
                #region MT-Load
                PleaseWaitHelper.Show();

                loadingModelEvent.Reset();

                this.ViewModelStore.InLoad = true;
                this.Reset();

                System.ComponentModel.BackgroundWorker loadModel = new System.ComponentModel.BackgroundWorker();
                loadModel.DoWork += new System.ComponentModel.DoWorkEventHandler(loadModel_DoWork);
                loadModel.RunWorkerAsync(fileName);

                this.Initialize();
                this.loadingModelEvent.WaitOne();

                // add to mru list
                this.MRUFilesViewModel.AddMRUEntry(fileName);

                // clear current error list
                this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
                if (this.SelectedModelContextViewModel.ModelContext.SerializationResult.Count() > 0)
                {
                    // add serialization result items

                    foreach (SerializationMessage serializationMessage in this.SelectedModelContextViewModel.ModelContext.SerializationResult)
                    {
                        SerializationErrorListItemViewModel item = new SerializationErrorListItemViewModel(this.ViewModelStore, serializationMessage);
                        items.Add(item);
                    }
                }

                if (this.SelectedModelContextViewModel.ModelContext.ValidationResult.Count > 0)
                {
                    foreach (IValidationMessage message in this.SelectedModelContextViewModel.ModelContext.ValidationResult)
                    {
                        if (message is ModelValidationMessage)
                        {
                            ModelErrorListItemViewModel item = new ModelErrorListItemViewModel(this.ViewModelStore, message as ModelValidationMessage);
                            items.Add(item);
                        }
                        else if (message is ValidationMessage)
                        {
                            StringErrorListItemViewModel item = new StringErrorListItemViewModel(this.ViewModelStore, message.MessageId, ModelErrorListItemViewModel.ConvertCategory(message.Type), message.Description);
                            items.Add(item);
                        }
                    }
                }

                this.ViewModelStore.InLoad = false;

                // Open views based on the configuration in the layout manager
                if (this.LayoutManager != null)
                    if (this.LayoutManager.ConfigurationManager != null)
                        foreach (string paneName in this.LayoutManager.CurrentConfiguration.Configurations.Keys)
                        {
                            DockingPaneConfiguration paneConfig = this.LayoutManager.CurrentConfiguration.Configurations[paneName];
                            if (paneConfig.IsRestorable && paneConfig.RestoreInformation != null && paneConfig.DockingPaneType != null)
                            {
                                if (!this.LayoutManager.HasWindow(paneConfig.PaneName))
                                {
                                    // create new modal window
                                    BaseDiagramSurfaceViewModel vm = this.ViewModelStore.TopMostStore.Factory.CreateRestorableViewModel(paneConfig.DockingPaneType);
                                    if (vm != null && vm is IRestorableDockableViewModel)
                                    {
                                        if ((vm as IRestorableDockableViewModel).Restore(paneConfig.RestoreInformation))
                                        {
                                            OpenViewModelEventArgs args = new OpenViewModelEventArgs(vm);
                                            args.DockingPaneStyle = PDE.ToolFramework.Modeling.Visualization.ViewModel.DockingPaneStyle.DockedInDocumentPane;
                                            this.EventManager.GetEvent<OpenViewModelEvent>().Publish(args);
                                        }
                                        else
                                        {
                                            vm.Dispose();
                                        }
                                    }
                                }
                            }


                            // notify of change
                            if (items.Count > 0)
                                this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
                        }

                PleaseWaitHelper.Hide();
                #endregion
            }
            else
            {*/
                #region Normal Load
            using (new ToolFramework.Modeling.Visualization.ViewModel.UI.WaitCursor())
            {
                this.ViewModelStore.InLoad = true;
                
                this.Reset();
                this.SelectedModelContextViewModel.ModelContext.Load(fileName, false);

                // add to mru list
                this.MRUFilesViewModel.AddMRUEntry(fileName);

                // clear current error list
                this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();

                if (this.SelectedModelContextViewModel.ModelContext.SerializationResult.Count() > 0)
                {
                    // add serialization result items

                    foreach (SerializationMessage serializationMessage in this.SelectedModelContextViewModel.ModelContext.SerializationResult)
                    {
                        SerializationErrorListItemViewModel item = new SerializationErrorListItemViewModel(this.ViewModelStore, serializationMessage);
                        items.Add(item);
                    }
                }

                if (this.SelectedModelContextViewModel.ModelContext.ValidationResult.Count > 0)
                {
                    foreach (IValidationMessage message in this.SelectedModelContextViewModel.ModelContext.ValidationResult)
                    {
                        if (message is ModelValidationMessage)
                        {
                            ModelErrorListItemViewModel item = new ModelErrorListItemViewModel(this.ViewModelStore, message as ModelValidationMessage);
                            items.Add(item);
                        }
                        else if (message is ValidationMessage)
                        {
                            StringErrorListItemViewModel item = new StringErrorListItemViewModel(this.ViewModelStore, message.MessageId, ModelErrorListItemViewModel.ConvertCategory(message.Type), message.Description);
                            items.Add(item);
                        }
                    }
                }

                this.ViewModelStore.InLoad = false;

                // Open views based on the configuration in the layout manager
                    if (this.LayoutManager != null)
                        if (this.LayoutManager.ConfigurationManager != null)
                foreach (string paneName in this.LayoutManager.CurrentConfiguration.Configurations.Keys)
                {
                    DockingPaneConfiguration paneConfig = this.LayoutManager.CurrentConfiguration.Configurations[paneName];
                    if (paneConfig.IsRestorable && paneConfig.RestoreInformation != null && paneConfig.DockingPaneType != null)
                    {
                        if (!this.LayoutManager.HasWindow(paneConfig.PaneName))
                        {
                            // create new modal window
                            BaseDiagramSurfaceViewModel vm = this.ViewModelStore.TopMostStore.Factory.CreateRestorableViewModel(paneConfig.DockingPaneType);
                            if (vm != null && vm is IRestorableDockableViewModel)
                            {
                                if ((vm as IRestorableDockableViewModel).Restore(paneConfig.RestoreInformation))
                                {
                                    OpenViewModelEventArgs args = new OpenViewModelEventArgs(vm);
                                    args.DockingPaneStyle = PDE.ToolFramework.Modeling.Visualization.ViewModel.DockingPaneStyle.DockedInDocumentPane;
                                    this.EventManager.GetEvent<OpenViewModelEvent>().Publish(args);
                                }
                                else
                                {
                                    vm.Dispose();
                                }
                            }
                        }
                    }
                }

                // notify of change
                if (items.Count > 0)
                    this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
            }
                #endregion
            //}

        }