/// <summary>
        /// Unloads the current editor.
        /// </summary>
        private void UnloadCurrentEditor()
        {
            if (_currentEditor != null)
            {
                //unload the current editor.
                _currentEditor.ContentEdited -= new EventHandler(_editor_ContentEdited);

                EnableableSmartContentEditor contentContext = _currentEditor as EnableableSmartContentEditor;
                if (contentContext != null)
                    contentContext.UnloadEditor();

                _currentEditor = null;
                //TODO: does this clear cause the current editor to be disposed?  We don't want it to be.
                Controls.Clear();
                _sidebarContext.UpdateStatusBar(String.Empty);
            }
        }
        public override void UpdateView(object htmlSelection, bool force)
        {
            if (htmlSelection == null) //true when the a non-smartcontent element is selected
            {
                //reset the selected smart content (fixes bug 492456)
                _selectedElement = null;
                _selectedSmartContentId = null;
                UnloadCurrentEditor();
                return;
            }

            Debug.Assert(htmlSelection is SmartContentSelection || (htmlSelection is IHtmlEditorSelection && InlineEditField.IsEditField(htmlSelection)));

            IHTMLElement selectedElement = null;
            SmartContentSelection smartContentSelection = htmlSelection as SmartContentSelection;
            if (smartContentSelection != null && smartContentSelection.ContentState == SmartContentState.Enabled)
            {
                selectedElement = smartContentSelection.HTMLElement;
            }
            else if (htmlSelection is IHtmlEditorSelection)
            {
                selectedElement = ContentSourceManager.GetContainingSmartContent(
                    ((IHtmlEditorSelection)(htmlSelection)).SelectedMarkupRange.ParentElement());
            }

            _currentSelection = htmlSelection;
            if (selectedElement != null)
            {
                //if the selected element id is still the same, then the sidebar is currently
                //in synch with the smart content.
                //Note: the element id will change each time an edit is made to the smart content
                if (!force && _selectedElement != null && _selectedSmartContentId != null && selectedElement.id == _selectedSmartContentId)
                    return;
                else
                {
                    _selectedElement = selectedElement;
                    _selectedSmartContentId = selectedElement.id;

                    if (_currentEditor != null)
                    {
                        UnloadCurrentEditor();
                    }
                }

                ContentSourceManager.ParseContainingElementId(_selectedElement.id, out _contentSourceId, out _contentItemId);

                SmartContentEditor editor = (SmartContentEditor)_contentSourceControls[_contentSourceId];
                ContentSourceInfo contentSource = _contentSourceContext.FindContentSource(_contentSourceId);
                if (contentSource != null && contentSource.Instance is SmartContentSource)
                {
                    _contentSource = (SmartContentSource)contentSource.Instance;

                    if (_editableSmartContent != null)
                        _editableSmartContent.Dispose();
                    _editableSmartContent = new EditableSmartContent(_contentSourceContext, _contentSource, _selectedElement);

                    if (editor == null)
                    {
                        editor = _contentSource.CreateEditor(this);

                        if (editor is IActiveSmartContentEditor)
                            ((IActiveSmartContentEditor)editor).ForceContentEdited += new EventHandler(ContentSourceSidebarControl_ForceContentEdited);

                        //apply the current scale the new control
                        if (editor != null)
                        {
                            editor.Scale(new SizeF(scale.Width, scale.Height));
                        }
                        _contentSourceControls[_contentSourceId] = editor;
                    }

                    if (editor != null)
                    {
                        editor.ContentEdited += new EventHandler(_editor_ContentEdited);
                        editor.SelectedContent = _editableSmartContent;

                        EnableableSmartContentEditor enableableSmartContentEditor = editor as EnableableSmartContentEditor;
                        if (enableableSmartContentEditor != null)
                            enableableSmartContentEditor.ContentEnabled = true;

                        if (editor != _currentEditor)
                        {
                            if (enableableSmartContentEditor != null)
                                Controls.Clear();
                            else
                            {
                                //load the new editor
                                editor.Dock = DockStyle.Fill;
                                editor.Width = Width - DockPadding.Left - DockPadding.Right;
                                this.Controls.Add(editor);
                            }

                            // set the title caption
                            // Text = String.Format( CultureInfo.CurrentCulture, Res.Get(StringId.PluginSidebarTitle), contentSource.InsertableContentSourceSidebarText ) ;
                        }

                    }
                }
                _currentEditor = editor;
            }
            else
                _currentEditor = null;
        }
 public InternalSmartContentContext(IInternalSmartContentContextSource contextSource, string contentSourceId)
 {
     _contextSource = contextSource;
     _smartContentEditor = contextSource.GetSmartContentEditor(contentSourceId);
 }