#region License // Copyright (c) 2010, Jasper Yeh. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of ClearCanvas Inc. nor the names of its contributors // may be used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY // OF SUCH DAMAGE. #endregion using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using ClearCanvas.Common; using ClearCanvas.Common.Utilities; using ClearCanvas.Desktop; namespace Nullstack.ClearCanvasEx.DevTools.History { [ExtensionPoint] public sealed class CommandHistoryComponentViewExtensionPoint : ExtensionPoint {} [AssociateView(typeof (CommandHistoryComponentViewExtensionPoint))] public class CommandHistoryComponent : ApplicationComponent { private static readonly FieldInfo _rfHistoryField = typeof(CommandHistory).GetField("_history", BindingFlags.NonPublic | BindingFlags.Instance); private event EventHandler _currentCommandChanged; private readonly BindingList _commandList; private IWorkspace _targetWorkspace; private int _currentCommand; public CommandHistoryComponent() { _commandList = new BindingList(); _commandList.AllowEdit = false; _commandList.AllowNew = false; _commandList.AllowRemove = false; } public override void Stop() { this.TargetWorkspace = null; base.Stop(); } public event EventHandler CurrentCommandChanged { add { _currentCommandChanged += value; } remove { _currentCommandChanged -= value; } } public int CurrentCommand { get { return _currentCommand; } } public IBindingList CommandList { get { return _commandList; } } public IWorkspace TargetWorkspace { get { return _targetWorkspace; } set { if (_targetWorkspace != value) { if (_targetWorkspace != null) { _targetWorkspace.Closed -= OnTargetWorkspaceClosed; if (_targetWorkspace.CommandHistory != null) _targetWorkspace.CommandHistory.CurrentCommandChanged -= OnCommandHistoryChanged; } _targetWorkspace = value; Refresh(); if (_targetWorkspace != null) { if (_targetWorkspace.CommandHistory != null) _targetWorkspace.CommandHistory.CurrentCommandChanged += OnCommandHistoryChanged; _targetWorkspace.Closed += OnTargetWorkspaceClosed; } } } } public void Refresh() { _commandList.RaiseListChangedEvents = false; _commandList.Clear(); _currentCommand = -1; try { if (_targetWorkspace != null) { CommandHistory ch = _targetWorkspace.CommandHistory; if (ch != null) { List history = _rfHistoryField.GetValue(ch) as List; if (history != null) { foreach (UndoableCommand command in history) _commandList.Add(CommandInfo.CreateCommandInfo(command)); } _currentCommand = ch.CurrentCommandIndex; //if (_currentCommand >= 0 && _currentCommand < _commandList.Count) // _commandList[_currentCommand].IsCurrent = true; } } } finally { EventsHelper.Fire(_currentCommandChanged, this, new EventArgs()); _commandList.RaiseListChangedEvents = true; _commandList.ResetBindings(); } } private void OnCommandHistoryChanged(object sender, EventArgs e) { Refresh(); } private void OnTargetWorkspaceClosed(object sender, ClosedEventArgs e) { this.TargetWorkspace = null; } } }