298 lines
12 KiB
C#
298 lines
12 KiB
C#
using Aurora.Api.Devices;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Controls;
|
|
|
|
namespace AuroraUIComponent.Util
|
|
{
|
|
|
|
/// <summary>
|
|
/// int 범위 체크 Validation (Simple)
|
|
/// </summary>
|
|
public class IntRangeValidationRule : ValidationRule
|
|
{
|
|
public int MinValue { get; set; } = int.MinValue;
|
|
public int MaxValue { get; set; } = int.MaxValue;
|
|
|
|
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
|
{
|
|
if (value == null || string.IsNullOrWhiteSpace(value.ToString())) {
|
|
//return new ValidationResult(false, "Input cannot be empty.");
|
|
return ValidationResult.ValidResult;
|
|
}
|
|
|
|
string input = value.ToString();
|
|
|
|
//string pattern = @"^\d+\s*~\s*\d+$"; // [int] ~ [int]
|
|
//if (Regex.IsMatch(input, pattern)) {
|
|
// return ValidationResult.ValidResult;
|
|
//}
|
|
|
|
//pattern = @"^(\d+)(,\d+)*$"; // comma-separated list
|
|
//if (Regex.IsMatch(input, pattern)) {
|
|
// return ValidationResult.ValidResult;
|
|
//}
|
|
|
|
string pattern = @"^(\d+~\d+|\d+)(,(\d+~\d+|\d+))*$"; // comma-separated list & [int] ~ [int] 조합
|
|
if (Regex.IsMatch(input, pattern)) {
|
|
|
|
var splitList = input.Split(',');
|
|
|
|
foreach (var splitToken in splitList) {
|
|
if (splitToken.Contains('~')) { // [int] ~ [int]
|
|
var splitList2 = splitToken.Split('~');
|
|
if (splitList2.Count() < 2) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
if (!int.TryParse(splitList2[0], out int value0)) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
if (!int.TryParse(splitList2[1], out int value1)) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
|
|
if (MinValue > value0 || MaxValue < value0) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
|
|
if (MinValue > value1 || MaxValue < value1) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
} else { // single [int]
|
|
if (!int.TryParse(splitToken, out int value0)) {
|
|
return new ValidationResult(false, $"Over Integer Range");
|
|
}
|
|
|
|
if (MinValue > value0 || MaxValue < value0) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
}
|
|
}
|
|
|
|
return ValidationResult.ValidResult;
|
|
}
|
|
|
|
return new ValidationResult(false, $"Not Supported Format [{value}]");
|
|
}
|
|
|
|
public List<int> Parse(string value)
|
|
{
|
|
string pattern = @"^(\d+~\d+|\d+)(,(\d+~\d+|\d+))*$"; // comma-separated list & [int] ~ [int] 조합
|
|
|
|
if (Regex.IsMatch(value, pattern)) {
|
|
var splitList = value.Split(',');
|
|
List<int> result = new List<int>();
|
|
|
|
foreach (var splitToken in splitList) {
|
|
if (splitToken.Contains('~')) { // [int] ~ [int]
|
|
var splitList2 = splitToken.Split('~');
|
|
if (splitList2.Count() < 2) {
|
|
continue;
|
|
}
|
|
if (!int.TryParse(splitList2[0], out int value0)) {
|
|
continue;
|
|
}
|
|
if (!int.TryParse(splitList2[1], out int value1)) {
|
|
continue;
|
|
}
|
|
|
|
// 사용자 입력은 1~1776 으로 입력받고, parsing할 때에는 -1 해서 0~1775로 저장
|
|
for (int i = Math.Min(value0, value1); i <= Math.Max(value0, value1); i++) {
|
|
if (!result.Contains(i) && MinValue <= i && MaxValue >= i) {
|
|
result.Add(i);
|
|
}
|
|
}
|
|
} else { // single [int]
|
|
if (int.TryParse(splitToken, out int value0)) {
|
|
if (!result.Contains(value0) && MinValue <= value0 && MaxValue >= value0) {
|
|
result.Add(value0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Sort();
|
|
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void SetRange(Range<int> range)
|
|
{
|
|
if (range != null) {
|
|
MinValue = range.Min;
|
|
MaxValue = range.Max;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// int 범위 체크, 구간설정(예: 1~4), Comma-separated(1,2,5) 지원하는 Validation
|
|
/// </summary>
|
|
public class IntRangeParsingValidationRule : ValidationRule
|
|
{
|
|
public int MinValue { get; set; } = int.MinValue;
|
|
public int MaxValue { get; set; } = int.MaxValue;
|
|
|
|
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
|
{
|
|
if (value == null || string.IsNullOrWhiteSpace(value.ToString())) {
|
|
//return new ValidationResult(false, "Input cannot be empty.");
|
|
return ValidationResult.ValidResult;
|
|
}
|
|
|
|
string input = value.ToString();
|
|
|
|
//string pattern = @"^\d+\s*~\s*\d+$"; // [int] ~ [int]
|
|
//if (Regex.IsMatch(input, pattern)) {
|
|
// return ValidationResult.ValidResult;
|
|
//}
|
|
|
|
//pattern = @"^(\d+)(,\d+)*$"; // comma-separated list
|
|
//if (Regex.IsMatch(input, pattern)) {
|
|
// return ValidationResult.ValidResult;
|
|
//}
|
|
|
|
string pattern = @"^(\d+~\d+|\d+)(,(\d+~\d+|\d+))*$"; // comma-separated list & [int] ~ [int] 조합
|
|
if (Regex.IsMatch(input, pattern)) {
|
|
|
|
var splitList = input.Split(',');
|
|
|
|
foreach (var splitToken in splitList) {
|
|
if (splitToken.Contains('~')) { // [int] ~ [int]
|
|
var splitList2 = splitToken.Split('~');
|
|
if (splitList2.Count() < 2) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
if (!int.TryParse(splitList2[0], out int value0)) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
if (!int.TryParse(splitList2[1], out int value1)) {
|
|
return new ValidationResult(false, $"Incompleted");
|
|
}
|
|
|
|
if (MinValue > value0 || MaxValue < value0) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
|
|
if (MinValue > value1 || MaxValue < value1) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
} else { // single [int]
|
|
if (!int.TryParse(splitToken, out int value0)) {
|
|
return new ValidationResult(false, $"Over Integer Range");
|
|
}
|
|
|
|
if (MinValue > value0 || MaxValue < value0) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
}
|
|
}
|
|
|
|
return ValidationResult.ValidResult;
|
|
}
|
|
|
|
return new ValidationResult(false, $"Not Supported Format [{value}]");
|
|
}
|
|
|
|
public List<int> Parse(string value)
|
|
{
|
|
string pattern = @"^(\d+~\d+|\d+)(,(\d+~\d+|\d+))*$"; // comma-separated list & [int] ~ [int] 조합
|
|
|
|
if (Regex.IsMatch(value, pattern)) {
|
|
var splitList = value.Split(',');
|
|
List<int> result = new List<int>();
|
|
|
|
foreach (var splitToken in splitList) {
|
|
if (splitToken.Contains('~')) { // [int] ~ [int]
|
|
var splitList2 = splitToken.Split('~');
|
|
if (splitList2.Count() < 2) {
|
|
continue;
|
|
}
|
|
if (!int.TryParse(splitList2[0], out int value0)) {
|
|
continue;
|
|
}
|
|
if (!int.TryParse(splitList2[1], out int value1)) {
|
|
continue;
|
|
}
|
|
|
|
// 사용자 입력은 1~1776 으로 입력받고, parsing할 때에는 -1 해서 0~1775로 저장
|
|
for (int i = Math.Min(value0, value1); i <= Math.Max(value0, value1); i++) {
|
|
if (!result.Contains(i) && MinValue <= i && MaxValue >= i) {
|
|
result.Add(i);
|
|
}
|
|
}
|
|
} else { // single [int]
|
|
if (int.TryParse(splitToken, out int value0)) {
|
|
if (!result.Contains(value0) && MinValue <= value0 && MaxValue >= value0) {
|
|
result.Add(value0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Sort();
|
|
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// double 범위 체크 Validation
|
|
/// </summary>
|
|
public class DoubleRangeValidationRule : ValidationRule
|
|
{
|
|
public double MinValue { get; set; } = double.MinValue;
|
|
public double MaxValue { get; set; } = double.MaxValue;
|
|
|
|
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
|
{
|
|
string input = value.ToString();
|
|
|
|
double parsedValue = 0;
|
|
// single [double]
|
|
if (!int.TryParse(input, out int value1)) {
|
|
if (!double.TryParse(input, out double value0)) {
|
|
return new ValidationResult(false, $"Cannot Parse to Double");
|
|
} else {
|
|
parsedValue = value0;
|
|
}
|
|
} else {
|
|
parsedValue = value1;
|
|
}
|
|
|
|
if (MinValue > parsedValue || MaxValue < parsedValue) {
|
|
return new ValidationResult(false, $"Over Range (Valid : {MinValue} ~ {MaxValue})");
|
|
}
|
|
|
|
return ValidationResult.ValidResult;
|
|
}
|
|
|
|
public void SetRange(Range<double> range)
|
|
{
|
|
if (range != null) {
|
|
MinValue = range.Min;
|
|
MaxValue = range.Max;
|
|
}
|
|
}
|
|
|
|
public void SetRange(double x, double y)
|
|
{
|
|
MinValue = Math.Min(x, y);
|
|
MaxValue = Math.Max(x, y);
|
|
}
|
|
}
|
|
}
|