aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Notifications/ItemBase.cs
blob: 41bfb8e4d5c9e4fc7745c6505a7b613ce6320eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Commands;

namespace Tango.PPC.Common.Notifications
{
    /// <summary>
    /// Represents a base notification item class.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="System.IDisposable" />
    public abstract class ItemBase : ExtendedObject, IDisposable
    {
        /// <summary>
        /// Occurs when the item has been closed.
        /// </summary>
        public event EventHandler Closed;

        /// <summary>
        /// Occurs when the item has been pressed.
        /// </summary>
        public event EventHandler Pressed;

        /// <summary>
        /// Gets or sets the remove action.
        /// </summary>
        internal Action RemoveAction { get; set; }

        /// <summary>
        /// Gets or sets the view type.
        /// </summary>
        public abstract Type ViewType { get; }

        /// <summary>
        /// Gets or sets the close command.
        /// </summary>
        public RelayCommand CloseCommand { get; set; }

        /// <summary>
        /// Gets or sets the pressed command.
        /// </summary
        public RelayCommand PressedCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemBase"/> class.
        /// </summary>
        public ItemBase()
        {
            CloseCommand = new RelayCommand(Close);
            PressedCommand = new RelayCommand(OnPreesed);
        }

        /// <summary>
        /// Called when the item has been pressed.
        /// </summary>
        protected virtual void OnPreesed()
        {
            Pressed?.Invoke(this, new EventArgs());
        }

        /// <summary>
        /// Called after the close command has been raised.
        /// </summary>
        public virtual void Close()
        {
            RemoveAction?.Invoke();
            Closed?.Invoke(this, new EventArgs());
        }

        /// <summary>
        /// Disposes the item.
        /// </summary>
        public void Dispose()
        {
            Close();
        }
    }
}
>/// </summary> public static class ObjectExtensions { private static bool _jsonSettingsSet; /// <summary> /// Performs shallow cloning of the object excluding generic properties.. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">The object.</param> /// <returns></returns> public static T ShallowClone<T>(this T obj) { var cloned = Activator.CreateInstance<T>(); foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) { if (!prop.PropertyType.IsGenericTypeAndNotNullable()) { prop.SetValue(cloned, prop.GetValue(obj)); } } return cloned; } /// <summary> /// Performs a shallow mapping to the specified object excluding generic properties. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">The source.</param> /// <param name="destination">The destination object.</param> public static void ShallowCopyTo<T>(this T source, T destination) { foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) { if (!prop.PropertyType.IsGenericTypeAndNotNullable()) { prop.SetValue(destination, prop.GetValue(source)); } } } /// <summary> /// Maps the object primitive properties values to the destination object. /// </summary> /// <param name="source">The source object.</param> /// <param name="destination">The destination object.</param> /// <param name="flags">The mapping flags.</param> /// <param name="condition">Optional condition.</param> public static void MapPropertiesTo(this object source, object destination, MappingFlags flags = MappingFlags.NoReferenceTypes, Func<PropertyInfo, bool> condition = null) { foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var propType = prop.PropertyType; var value = prop.GetValue(source); if (condition != null) { if (!condition(prop)) continue; } if (!propType.IsValueType && flags.HasFlag(MappingFlags.ValueTypesOnly)) { continue; } if (propType == typeof(String) && flags.HasFlag(MappingFlags.NoStrings)) { continue; } if (propType.IsClass && propType != typeof(String) && flags.HasFlag(MappingFlags.NoReferenceTypes)) { continue; } if (flags.HasFlag(MappingFlags.NoNullStrings) && propType == typeof(String) && String.IsNullOrEmpty(value as String)) { continue; } var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); if (desProp != null && desProp.PropertyType == prop.PropertyType && desProp.SetMethod != null) { desProp.SetValue(destination, value); } } } /// <summary> /// Maps the object primitive properties values to the destination object. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> public static void MapPrimitivesTo(this object source, object destination) { source.MapPropertiesTo(destination, MappingFlags.ValueTypesOnly); } /// <summary> /// Serializes the specified object to indented json string. /// </summary> /// <param name="obj">The object.</param> /// <returns></returns> public static String ToJsonString(this Object obj) { if (obj == null) return "null"; if (!_jsonSettingsSet) { JsonConvert.DefaultSettings = (() => { var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); settings.ContractResolver = new ProtobufContractResolver(); return settings; }); _jsonSettingsSet = true; } return JsonConvert.SerializeObject(obj, Formatting.Indented); } /// <summary> /// Serializes the specified object to indented json string. /// </summary> /// <param name="obj">The object.</param> /// <returns></returns> public static String ToJsonString(this Object obj, params String[] ignoreProperties) { var settings = new JsonSerializerSettings() { ContractResolver = new DynamicContractResolver(ignoreProperties) }; settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); } /// <summary> /// Serializes the specified object to indented json html string. /// </summary> /// <param name="obj">The object.</param> /// <returns></returns> public static String ToHtmlJsonString(this Object obj, params String[] ignoreProperties) { var settings = new JsonSerializerSettings() { ContractResolver = new HtmlContractResolver(ignoreProperties) }; settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); } /// <summary> /// Gets the property value by the specified path (e.g DateTime.Date.Days). /// </summary> /// <param name="obj">The object.</param> /// <param name="propertyPath">The property path.</param> /// <returns></returns> public static Object GetPropertyValueByPath(this object obj, String propertyPath) { if (propertyPath != null) { if (propertyPath.Contains('.')) { string[] Split = propertyPath.Split('.'); string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); return GetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty); } else { var prop = obj.GetType().GetProperty(propertyPath); if (prop != null) { return prop.GetValue(obj, null); } else { return obj.ToString(); } } } else { return obj.ToString(); } } /// <summary> /// Sets the property value by the specified path (e.g DateTime.Date.Days). /// </summary> /// <param name="obj">The object.</param> /// <param name="propertyPath">The property path.</param> /// <returns></returns> public static void SetPropertyValueByPath(this object obj, String propertyPath, object value) { if (propertyPath != null) { if (propertyPath.Contains('.')) { string[] Split = propertyPath.Split('.'); string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); SetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty, value); } else { obj.GetType().GetProperty(propertyPath).SetValue(obj, value); } } } } }