/// <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>
        /// 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
            //}

        }
        /// <summary>
        /// Save command executed.
        /// </summary>
        protected virtual void SaveModelCommandExecuted()
        {
            if (this.SelectedModelContextViewModel == null)
                return;
            if (this.SelectedModelContextViewModel.ModelContext == null)
                return;

            using (new ToolFramework.Modeling.Visualization.ViewModel.UI.WaitCursor())
            {
                this.SelectedModelContextViewModel.ModelContext.Save((this.SelectedModelContextViewModel.ModelContext.RootElement as IParentModelElement).DomainFilePath);
                //this.SelectedModelContextViewModel.ModelContext.Save(this.ViewModelStore.GetDomainModelServices(this.SelectedModelContextViewModel.ModelContext.RootElement).ElementParentProvider.GetDomainModelFilePath(this.SelectedModelContextViewModel.ModelContext.RootElement));

                if (this.SelectedModelContextViewModel.ModelContext.SerializationResult.Count() > 0)
                {
                    // clear current error list
                    this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                    // add serialization result items
                    List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
                    foreach (SerializationMessage serializationMessage in this.SelectedModelContextViewModel.ModelContext.SerializationResult)
                    {
                        SerializationErrorListItemViewModel item = new SerializationErrorListItemViewModel(this.ViewModelStore, serializationMessage);
                        items.Add(item);
                    }

                    // notify of change
                    this.EventManager.GetEvent<ErrorListAddItems>().Publish(items);
                }
            }
        }
        /// <summary>
        /// New command executed.
        /// </summary>
        protected virtual void NewModelCommandExecuted()
        {
            if (this.SelectedModelContextViewModel == null)
                return;
            if (this.SelectedModelContextViewModel.ModelContext == null)
                return;

            if (CloseModelCommandCanExecute())
                if (!CloseModelCommandExecute())
                    return;

            ISaveFileService saveFileService = this.GlobalServiceProvider.Resolve<ISaveFileService>();
            saveFileService.Filter = this.SelectedModelContextViewModel.EditorTitle + " files|*.xml|All files|*.*";
            if (saveFileService.ShowDialog(null) == true)
            {
                System.IO.StreamWriter writer = System.IO.File.CreateText(saveFileService.FileName);
                writer.Close();

                using (new ToolFramework.Modeling.Visualization.ViewModel.UI.WaitCursor())
                {
                    this.Reset();
                    this.SelectedModelContextViewModel.ModelContext.Load(saveFileService.FileName, false);

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

                    if (this.SelectedModelContextViewModel.ModelContext.SerializationResult.Count() > 0)
                    {
                        // clear current error list
                        this.EventManager.GetEvent<ErrorListClearItems>().Publish(this);

                        // add serialization result items
                        List<BaseErrorListItemViewModel> items = new List<BaseErrorListItemViewModel>();
                        foreach (SerializationMessage serializationMessage in this.SelectedModelContextViewModel.ModelContext.SerializationResult)
                        {
                            SerializationErrorListItemViewModel item = new SerializationErrorListItemViewModel(this.ViewModelStore, serializationMessage);
                            items.Add(item);
                        }

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

            if (!this.isInitialyInitialized)
            {
                PleaseWaitHelper.Show();
                this.Initialize();
                PleaseWaitHelper.Hide();
            }

            this.CloseCommand.RaiseCanExecuteChanged();
            this.SaveAsModelCommand.RaiseCanExecuteChanged();
            this.SaveModelCommand.RaiseCanExecuteChanged();
        }