using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using Tango.BL.Enumerations; using Tango.Core; using Tango.Core.DI; using Tango.FSE.BL; using Tango.FSE.Common.Authentication; using Tango.FSE.Common.Connection; using Tango.FSE.Common.Diagnostics; using Tango.FSE.Common.Notifications; using Tango.PMR.Diagnostics; namespace Tango.FSE.Diagnostics.Project { public abstract class DiagnosticsWidget : ExtendedObject, IDisposable { [JsonIgnore] [TangoInject(TangoInjectMode.WhenAvailable)] protected IAuthenticationProvider AuthenticationProvider { get; set; } [JsonIgnore] [TangoInject(TangoInjectMode.WhenAvailable)] protected IMachineProvider MachineProvider { get; set; } [JsonIgnore] [TangoInject(TangoInjectMode.WhenAvailable)] protected INotificationProvider NotificationProvider { get; set; } private FSEServicesContainer _services; [JsonIgnore] [TangoInject(TangoInjectMode.WhenAvailable)] public FSEServicesContainer Services { get { return _services; } set { _services = value; OnServicesAvailable(); } } public String ID { get; set; } private int _column; public int Column { get { return _column; } set { _column = value; RaisePropertyChangedAuto(); } } private int _row; public int Row { get { return _row; } set { _row = value; RaisePropertyChangedAuto(); } } private int _columnSpan; public int ColumnSpan { get { return _columnSpan; } set { _columnSpan = value; RaisePropertyChangedAuto(); } } private int _rowSpan; public int RowSpan { get { return _rowSpan; } set { _rowSpan = value; RaisePropertyChangedAuto(); } } private double _width; public double Width { get { return _width; } set { _width = value; RaisePropertyChangedAuto(); } } private double _height; public double Height { get { return _height; } set { _height = value; RaisePropertyChangedAuto(); } } private bool _displayComponentName; public bool DisplayComponentName { get { return _displayComponentName; } set { _displayComponentName = value; RaisePropertyChangedAuto(); } } private Dock _componentNameAlignment; public Dock ComponentNameAlignment { get { return _componentNameAlignment; } set { _componentNameAlignment = value; RaisePropertyChangedAuto(); } } private String _customComponentName; public String CustomComponentName { get { return _customComponentName; } set { _customComponentName = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(DisplayName)); } } private bool _isVisible; [JsonIgnore] public bool IsVisible { get { return _isVisible; } set { if (_isVisible != value) { _isVisible = value; OnVisibleChanged(_isVisible); RaisePropertyChangedAuto(); } } } private bool _isSelected; [JsonIgnore] public bool IsSelected { get { return _isSelected; } set { _isSelected = value; RaisePropertyChangedAuto(); } } [JsonIgnore] public abstract String DisplayName { get; } [JsonIgnore] public bool SupportsComponentSelection { get { return this is ISupportsComponentSelection; } } [JsonIgnore] public bool IsCurrentUserEditor { get { return AuthenticationProvider.CurrentUser.HasPermission(Permissions.FSE_EditDiagnosticsProject); } } [JsonIgnore] public virtual bool HasSettings { get { return (this is DiagnosticsConfigurableWidget) || (SupportsComponentSelection && (this as ISupportsComponentSelection).EnableComponentSelection); } } private bool _editMode; [JsonIgnore] public bool EditMode { get { return _editMode; } set { _editMode = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasSettings)); } } [JsonIgnore] public bool IsDisposed { get; protected set; } public DiagnosticsWidget() { ID = Guid.NewGuid().ToString(); Width = 100; Height = 100; ComponentNameAlignment = Dock.Top; TangoIOC.Default.Inject(this); } protected virtual void OnServicesAvailable() { } public virtual void OnDiagnosticsData(DiagnosticsPackage package) { //Do Nothing. } protected virtual void OnVisibleChanged(bool isVisible) { //Called when the widget visibility changed. } public abstract FrameworkElement GetView(); public virtual Task Init() { return Task.FromResult(true); } protected async void InitAsync() { await Init(); } public DiagnosticsWidget Clone() { var json = JsonConvert.SerializeObject(this, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); var cloned = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }) as DiagnosticsWidget; cloned.ID = Guid.NewGuid().ToString(); return cloned; } public void RaiseHasSettings() { RaisePropertyChanged(nameof(HasSettings)); } public virtual void Dispose() { IsDisposed = true; } } }