Chronos Plugins 5.9.0
This documentation covers the plugin interfaces definitions and an example implementation.
Loading...
Searching...
No Matches
DateColumn.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.ComponentModel.Design;
5using System.Drawing.Design;
6using System.Globalization;
7using System.Linq;
10using MockPlugin.Properties;
11
13{
20 // ReSharper disable once UnusedType.Global
22 {
23 // Do NOT localize this
24 public string InternalName => "MockPlugin_Date";
25 public string VisibleName => LocalizeMockPlugin.DateColumn_VisibleName_Date;
26 public IEnumerable<object> ComboboxItems => null;
27 public Type ValueType => typeof(MyDate);
28
32 public IEnumerable<IColumnMenu> ColumnHeaderMenu
33 {
34 get
35 {
36 yield return new ColumnHeaderMenu(() => LocalizeMockPlugin.DateColumn_ColumnHeaderMenu_Fill_down, cells => IncreaseBy(cells, dt => dt));
37 yield return new ColumnHeaderMenu(() => LocalizeMockPlugin.DateColumn_ColumnHeaderMenu_Daily, cells => IncreaseBy(cells, dt => dt.Add(TimeSpan.FromDays(1))));
38 yield return new ColumnHeaderMenu(() => LocalizeMockPlugin.DateColumn_ColumnHeaderMenu_Weekly, cells => IncreaseBy(cells,dt => dt.Add(TimeSpan.FromDays(7))));
39 yield return new ColumnHeaderMenu(() => LocalizeMockPlugin.DateColumn_ColumnHeaderMenu_Monthly, cells => IncreaseBy(cells, dt => dt.AddMonths(1)));
40 }
41 }
42
43 private void IncreaseBy(IReadOnlyList<ICellAccessor> cells, Func<DateTime,DateTime> adder)
44 {
45 if (cells.Count >= 2)
46 {
47 var currVal = (MyDate) cells[0].Value;
48 foreach (var someCell in cells.Skip(1))
49 {
50 currVal = new MyDate(adder(currVal.DateTime));
51 someCell.Value = currVal;
52 }
53 }
54 }
55 }
56
60 [Editor(typeof(MyDateEditor),typeof(UITypeEditor))]
61 [TypeConverter(typeof(MyDateConverter))]
62 public class MyDate
63 {
64 public MyDate(DateTime dt)
65 {
66 DateTime = dt;
67 }
68
69 public DateTime DateTime { get; }
70 public override string ToString()
71 {
72 return DateTime.ToString(CultureInfo.InvariantCulture);
73 }
74 }
75
79 public class MyDateConverter : TypeConverter
80 {
81 private readonly TypeConverter mConv = TypeDescriptor.GetConverter(typeof(DateTime));
82 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
83 {
84 if (value is MyDate mdt)
85 {
86 return mConv.ConvertTo(context, CultureInfo.InvariantCulture, mdt.DateTime, destinationType);
87 }
88 return mConv.ConvertTo(context, CultureInfo.InvariantCulture, value, destinationType);
89 }
90
91 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
92 {
93 // TypeConverter throws a NotSupportedException if the type is already correct, don't call ConvertFrom then!
94 if (value is DateTime dt)
95 {
96 return new MyDate(dt);
97 }
98 // ReSharper disable once PossibleNullReferenceException
99 return new MyDate((DateTime) mConv.ConvertFrom(context, CultureInfo.InvariantCulture, value));
100 }
101
102 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
103 {
104 return mConv.CanConvertFrom(context, sourceType);
105 }
106
107 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
108 {
109 return mConv.CanConvertTo(context, destinationType);
110 }
111 }
112
116 public class MyDateEditor : DateTimeEditor
117 {
118 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
119 {
120 if (value is MyDate mdt)
121 {
122 // ReSharper disable once PossibleNullReferenceException
123 return new MyDate((DateTime)base.EditValue(context, provider, mdt.DateTime));
124 }
125 return base.EditValue(context, provider, value);
126 }
127 }
128}
Classes and interfaces that are meant for plugins. The classes and interfaces below this namespace ar...
Interfaces for custom sample list column type definitions. These interfaces allow you to define new c...
Definition ColumnMenu.cs:4
The classes in this namespace demonstrate how to define your own sample list column types.
Implement this interface on a class that defines a custom column that can be used just like a builtin...
Definition ColumnType.cs:18
Just a general purpose column header menu.
Example column type that allows to pick a date and time from a graphical editor.
Definition DateColumn.cs:22
string InternalName
Nametag that will be used internally for storing a column of this type in the method....
Definition DateColumn.cs:24
void IncreaseBy(IReadOnlyList< ICellAccessor > cells, Func< DateTime, DateTime > adder)
Definition DateColumn.cs:43
string VisibleName
Column type name that will be presented to the user in the method editor in the "Cell Type" column....
Definition DateColumn.cs:25
IEnumerable< object > ComboboxItems
List of possible values that can be presented to the user in a drop-down list.
Definition DateColumn.cs:26
IEnumerable< IColumnMenu > ColumnHeaderMenu
Provide some column header menus for increasing the date in typically used steps.
Definition DateColumn.cs:33
Type ValueType
Used for getting information which UITypeEditor, TypeConverter etc to use for your column.
Definition DateColumn.cs:27
Simple wrapper around DateTime that makes sure to always use the invariant culture.
Definition DateColumn.cs:63
Just like the default converter, but always use the invariant culture and wraps/unwraps the "MyDate".
Definition DateColumn.cs:80
override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Definition DateColumn.cs:82
override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Definition DateColumn.cs:91
Just unwrap and edit the inner DateTime.
override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)