-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilenameInputWindow.xaml.cs
More file actions
152 lines (126 loc) · 3.72 KB
/
Copy pathFilenameInputWindow.xaml.cs
File metadata and controls
152 lines (126 loc) · 3.72 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
namespace FilePairing
{
/// <summary>
/// FilenameInputWindow.xaml の相互作用ロジック
/// </summary>
public partial class FilenameInputWindow
{
/// <summary>
/// データビューモデル
/// </summary>
public FilenameInputViewModel ViewModel
{
get;
set;
}
/// <summary>
/// マッチングファイル
/// </summary>
public ICollection<PairData> MatchingFileCollection
{
get;
set;
}
/// <summary>
/// コンストラクタ
/// </summary>
public FilenameInputWindow()
{
InitializeComponent();
}
/// <summary>
/// コンテキストを初期化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FilenameInputWindow_OnLoaded(object sender, RoutedEventArgs e)
{
FirstFilenames(out var mainFile, out var subFile);
ViewModel = new FilenameInputViewModel
{
MainFilename = ExtractBasename(mainFile, "作業前"),
SubFilename = ExtractBasename(subFile, "作業後")
};
DataContext = ViewModel;
}
/// <summary>
/// マッチング済みのファイル名を得る
/// </summary>
/// <param name="mainFile">メインファイル名</param>
/// <param name="subFile">サブファイル名</param>
private void FirstFilenames(out string mainFile, out string subFile)
{
mainFile = string.Empty;
subFile = string.Empty;
foreach (var pairData in MatchingFileCollection)
{
if (string.IsNullOrEmpty(mainFile) && !string.IsNullOrEmpty(pairData.MainFile))
{
mainFile = pairData.MainFile;
}
if (string.IsNullOrEmpty(subFile) && !string.IsNullOrEmpty(pairData.SubFile))
{
subFile = pairData.SubFile;
}
if (!string.IsNullOrEmpty(mainFile) && !string.IsNullOrEmpty(subFile))
{
break;
}
}
}
/// <summary>
/// ベースファイル名を得る
/// </summary>
/// <param name="fullname"></param>
/// <param name="whenNotFound"></param>
/// <returns></returns>
private static string ExtractBasename(string fullname, string whenNotFound)
{
var body = Path.GetFileNameWithoutExtension(fullname);
var pattern = body.StartsWith(MainWindow.ExclFilePrefix) ? $@"^{MainWindow.ExclFilePrefix}(.+)(_\d+)$" : @"(.+)(-\d+)(_\d+)?$";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
var mm = regex.Match(body);
return (!mm.Success) ? whenNotFound : mm.Groups[1].ToString();
}
/// <summary>
/// OK ボタン処理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(ViewModel.MainFilename))
{
MessageBox.Show("メインファイル名がセットされていません", "エラー", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (string.IsNullOrEmpty(ViewModel.SubFilename))
{
MessageBox.Show("サブファイル名がセットされていません", "エラー", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
DialogResult = true;
Close();
}
}
/// <summary>
/// ファイル名データビューモデル
/// </summary>
public class FilenameInputViewModel
{
public string MainFilename
{
get;
set;
}
public string SubFilename
{
get;
set;
}
}
}