-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectProfileForm.cs
713 lines (643 loc) · 33 KB
/
ConnectProfileForm.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
* Copyright 2015-2016 yoshikixxxx.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
*/
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace Contrib.ConnectProfile {
/// <summary>
/// ConnectProfileメインフォームクラス
/// </summary>
public partial class ConnectProfileForm : Form {
// メンバー変数
private Commands _cmd = new Commands();
private int _selectCnt = 0;
private bool _doubleClickFlg = false;
private bool _refreshingFlg = false;
private MouseButtons _clickButton;
private ListViewItemComparer _listViewItemSorter;
private static bool _connectCancelFlg;
/// <summary>
/// コンストラクタ
/// </summary>
public ConnectProfileForm() {
this.InitializeComponent();
InitializeComponentValue();
// ListViewダブルバッファリング
System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
controlProperty.SetValue(_profileListView, true, null);
// 設定ファイル読み込み
if (ConnectProfilePlugin.Instance.ConnectProfileOptionSupplier.PreferenceLoaded != true) {
ConnectProfilePlugin.Instance.ConnectProfileOptionSupplier.LoadFromPreference();
}
RefreshAllProfiles();
}
/// <summary>
/// オブジェクト各値設定
/// </summary>
private void InitializeComponentValue() {
// テキスト
this.Text = ConnectProfilePlugin.Strings.GetString("Caption.ConnectProfile");
this._addProfileButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._addProfileButton");
this._autoLoginColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._autoLoginColumn");
this._cancelButton.Text = ConnectProfilePlugin.Strings.GetString("Form.Common._cancelButton");
this._checkAllOffButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._checkAllOffButton");
this._copyButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._copyButton");
this._csvExportButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._csvExportButton");
this._csvImportButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._csvImportButton");
this._delProfileButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._delProfileButton");
this._descriptionColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._descriptionColumn");
this._displaySelectedOnlyCheck.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._displaySelectedOnlyCheck");
this._editProfileButton.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._editProfileButton");
this._execCommandColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._execCommandColumn");
this._filterLabel.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._filterLabel");
this._hintLabel.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._hintLabel");
this._hostNameColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._hostNameColumn");
this._okButton.Text = ConnectProfilePlugin.Strings.GetString("Form.Common._okButton");
this._openCSVFileDialog.Filter = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._openCSVFileDialog.Filter");
this._openCSVFileDialog.Title = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._openCSVFileDialog.Caption");
this._portColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._portColumn");
this._profileCountLabel.Text = String.Format(ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._profileCountLabel"), 0);
this._protocolColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._protocolColumn");
this._saveCSVFileDialog.Filter = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._saveCSVFileDialog.Filter");
this._saveCSVFileDialog.Title = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._saveCSVFileDialog.Caption");
this._selectedProfileCountLabel.Text = String.Format(ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._selectedProfileCountLabel"), 0);
this._suSwitchColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._suSwitchColumn");
this._terminalDisplayColorColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._terminalDisplayColorColumn");
this._userNameColumn.Text = ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._userNameColumn");
// ファイル保存/開くダイアログ設定
this._saveCSVFileDialog.DefaultExt = "csv";
this._saveCSVFileDialog.AddExtension = true;
this._openCSVFileDialog.DefaultExt = "csv";
this._openCSVFileDialog.CheckFileExists = true;
this._openCSVFileDialog.Multiselect = false;
// プロファイルリストカラム幅
this._hostNameColumn.Width = -2;
this._userNameColumn.Width = -2;
this._autoLoginColumn.Width = -2;
this._protocolColumn.Width = -2;
this._portColumn.Width = -2;
this._suSwitchColumn.Width = -2;
this._execCommandColumn.Width = -1;
this._terminalDisplayColorColumn.Width = -2;
this._descriptionColumn.Width = -2;
// プロファイルリストソートイベント作成/設定
_listViewItemSorter = new ListViewItemComparer();
_listViewItemSorter.ColumnModes = new ListViewItemComparer.ComparerMode[] {
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.Integer,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
ListViewItemComparer.ComparerMode.String,
};
_profileListView.ListViewItemSorter = _listViewItemSorter;
}
/// <summary>
/// プロファイルリスト更新
/// </summary>
private void RefreshAllProfiles() {
string str = "";
_refreshingFlg = true;
_profileListView.BeginUpdate();
_profileListView.Items.Clear();
// 各データ代入
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
str = string.Format("{0} {1} {2} {3} {4} {5} {6}", prof.HostName, prof.UserName, prof.Protocol.ToString(), prof.Port.ToString(), prof.SUUserName, prof.ExecCommand, prof.Description);
// フィルタ
if (IsVisibleItem(str) == true) {
// チェックONのみ表示
if (_displaySelectedOnlyCheck.Checked == true) {
if (prof.Check != true) continue;
}
// リストオブジェクト作成
ListViewItem li = new ListViewItem();
li.UseItemStyleForSubItems = false; // サブアイテム書式設定用
// データ代入
li.Text = prof.HostName;
li.SubItems.Add(prof.UserName);
li.SubItems.Add((prof.AutoLogin == true) ? ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile_profileListView.Yes") : ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile_profileListView.No"));
li.SubItems.Add(prof.Protocol.ToString());
li.SubItems.Add(prof.Port.ToString());
li.SubItems.Add((prof.SUUserName != "") ? prof.SUUserName : "");
li.SubItems.Add((prof.ExecCommand != "") ? prof.ExecCommand : "");
li.SubItems.Add(""); // 背景色
li.SubItems.Add((prof.Description != "") ? prof.Description : "");
// 項目/背景色設定
li.ForeColor = prof.ProfileItemColor;
for (int i = 0; i < li.SubItems.Count; i++) {
li.SubItems[i].ForeColor = prof.ProfileItemColor;
if (i == 7) {
li.SubItems[i].BackColor = prof.RenderProfile.BackColor;
li.SubItems[i].ForeColor = prof.RenderProfile.ForeColor;
System.Drawing.FontStyle fs;
if (prof.RenderProfile.ForceBoldStyle == true) fs = li.SubItems[i].Font.Style | System.Drawing.FontStyle.Bold;
else fs = li.SubItems[i].Font.Style;
li.SubItems[i].Font = new System.Drawing.Font(prof.RenderProfile.FontName, prof.RenderProfile.FontSize, fs);
li.SubItems[i].Text = "ABC012";
}
}
// チェック状態復元
li.Checked = prof.Check;
// タグ追加
li.Tag = prof;
// アイテム追加
_profileListView.Items.Add(li);
}
}
_profileListView.EndUpdate();
_profileListView.SelectedItems.Clear();
// プロファイル数ラベル更新
_profileCountLabel.Text = String.Format(ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._profileCountLabel"), ConnectProfilePlugin.Profiles.Count);
_refreshingFlg = false;
}
/// <summary>
/// プロファイルリスト選択数更新
/// </summary>
private void RefreshSelectCount() {
_selectCnt = 0;
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
if (prof.Check == true) _selectCnt++;
}
_selectedProfileCountLabel.Text = String.Format(ConnectProfilePlugin.Strings.GetString("Form.ConnectProfile._selectedProfileCountLabel"), _selectCnt);
}
/// <summary>
/// フィルター対象抽出(大小文字区別なし, 空白区切りでAND条件)
/// </summary>
private bool IsVisibleItem(string targetstr) {
string[] keys = _filterTextBox.Text.Split(' ');
int index = 0;
foreach (string key in keys) {
index = targetstr.IndexOf(key, StringComparison.OrdinalIgnoreCase);
if (index < 0) break;
}
return (index >= 0) ? true : false;
}
/// <summary>
/// 選択中のプロファイルリストを取得
/// </summary>
private ConnectProfileStruct GetSelectedProfile() {
return (_profileListView.SelectedItems.Count == 0) ? null : (ConnectProfileStruct)_profileListView.SelectedItems[0].Tag;
}
/// <summary>
/// 選択済みプロファイルリストのホスト名を取得
/// </summary>
private string GetSelectedProfileHostNames() {
string str = "";
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
if (prof.Check == true) str += prof.HostName + ", ";
}
return str;
}
/// <summary>
/// プロファイル接続
/// </summary>
private void Connect(ConnectProfileStruct prof) {
if (prof != null) {
// 接続中はダイアログタイトルにホスト名を表示
this.Text = string.Format(ConnectProfilePlugin.Strings.GetString("Caption.ConnectProfile.Connecting"), prof.HostName);
_cmd.ConnectProfile(prof);
this.Text = ConnectProfilePlugin.Strings.GetString("Caption.ConnectProfile");
}
}
/// <summary>
/// プロファイル追加
/// </summary>
private void AddProfile() {
_cmd.NewProfileCommand(ConnectProfilePlugin.Profiles);
RefreshAllProfiles();
}
/// <summary>
/// プロファイル編集
/// </summary>
private void EditProfile() {
ConnectProfileStruct prof = GetSelectedProfile();
if (prof != null) {
_cmd.EditProfileCommand(ConnectProfilePlugin.Profiles, prof);
RefreshAllProfiles();
}
}
/// <summary>
/// プロファイルコピー/編集
/// </summary>
private void CopyEditProfile() {
ConnectProfileStruct prof = GetSelectedProfile();
if (prof != null) {
_cmd.CopyEditProfileCommand(ConnectProfilePlugin.Profiles, prof);
RefreshAllProfiles();
}
}
/// <summary>
/// プロファイル削除
/// </summary>
private void DeleteProfile() {
ConnectProfileStruct prof = GetSelectedProfile();
if (prof != null) {
if (ConnectProfilePlugin.AskUserYesNoInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.ConfirmProfileDelete"), MessageBoxIcon.Question) == DialogResult.Yes) {
_cmd.DeleteProfileCommand(ConnectProfilePlugin.Profiles, prof);
RefreshAllProfiles();
RefreshSelectCount();
}
}
}
/// <summary>
/// オブジェクト有効/無効化
/// </summary>
private void EnableControl(bool val) {
_okButton.Enabled = val;
_filterTextBox.Enabled = val;
_checkAllOffButton.Enabled = val;
_addProfileButton.Enabled = val;
_editProfileButton.Enabled = val;
_delProfileButton.Enabled = val;
_copyButton.Enabled = val;
_csvExportButton.Enabled = val;
_csvImportButton.Enabled = val;
_displaySelectedOnlyCheck.Enabled = val;
_profileListView.Enabled = val;
}
/// <summary>
/// フィルターテキスト変更イベント
/// </summary>
private void _filterTextBox_TextChanged(object sender, System.EventArgs e) {
_filterTimer.Stop();
_filterTimer.Start();
}
/// <summary>
/// フィルターテキストキー押下イベント
/// </summary>
private void _filterTextBox_KeyDown(object sender, KeyEventArgs e) {
// 上下キー押下時はリストビューにフォーカス(リストアイテムが0以外の場合のみ)
if (((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down)) && (_profileListView.Items.Count > 0)) {
// ウィンドウ表示後にListViewをアクティブにしたかどうか(一度でもアイテムを選択したかどうか)
if (_profileListView.FocusedItem == null) {
// 一度もアクティブにしていない場合は最上部を選択
_profileListView.Items[0].Selected = true;
} else {
// FocusedItem.Indexの値を選択
_profileListView.Items[_profileListView.FocusedItem.Index].Selected = true;
}
_profileListView.Focus();
}
}
/// <summary>
/// OKボタンイベント
/// </summary>
private void _okButton_Click(object sender, EventArgs e) {
ArrayList hostlist = new ArrayList();
this.DialogResult = DialogResult.None;
_connectCancelFlg = false;
EnableControl(false);
if ((_selectCnt == 0) && (_profileListView.Items.Count == 1)) {
// 選択数0 & リスト1行のみ
_profileListView.Items[0].Selected = true;
Connect(GetSelectedProfile());
this.DialogResult = DialogResult.OK;
} else if ((_selectCnt == 0) && (_profileListView.SelectedItems.Count == 1)) {
// 選択数0 & リスト1行選択
Connect(GetSelectedProfile());
this.DialogResult = DialogResult.OK;
} else if (_selectCnt > 0) {
// 選択数1以上(複数接続)
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
if (prof.Check == true) hostlist.Add(prof.HostName);
}
// 接続確認
if (ConnectProfilePlugin.AskUserYesNoInvoke(String.Format(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.ConnectConfirm"), _selectCnt, String.Join(", ", (string[])hostlist.ToArray(typeof(string)))), MessageBoxIcon.Information) == DialogResult.Yes) {
// 接続
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
if (_connectCancelFlg != true) {
if (prof.Check == true) Connect(prof);
} else {
// キャンセルボタン押下
ConnectProfilePlugin.MessageBoxInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.ConnectCancel"), MessageBoxIcon.Warning);
this.DialogResult = DialogResult.None;
EnableControl(true);
_okButton.Focus();
return;
}
}
this.DialogResult = DialogResult.OK;
} else {
// キャンセル
EnableControl(true);
_okButton.Focus();
}
} else {
// リスト未選択
ConnectProfilePlugin.AskUserYesNoInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.ProfileNotSelected"), MessageBoxIcon.Warning);
EnableControl(true);
_okButton.Focus();
}
}
/// <summary>
/// キャンセルボタンイベント
/// </summary>
private void _cancelButton_Click(object sender, EventArgs e) {
_connectCancelFlg = true; // フォーム用
_cmd.ConnectCancel(); // スレッド用
}
/// <summary>
/// チェック全解除ボタンイベント
/// </summary>
private void _checkAllOffButton_Click(object sender, EventArgs e) {
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
prof.Check = false;
}
RefreshAllProfiles();
RefreshSelectCount();
}
/// <summary>
/// 追加ボタンイベント
/// </summary>
private void _addProfileButton_Click(object sender, EventArgs e) {
AddProfile();
}
/// <summary>
/// 編集ボタンイベント
/// </summary>
private void _editProfileButton_Click(object sender, EventArgs e) {
EditProfile();
}
/// <summary>
/// 削除ボタンイベント
/// </summary>
private void _delProfileButton_Click(object sender, EventArgs e) {
DeleteProfile();
}
/// <summary>
/// コピーボタンイベント
/// </summary>
private void _copyButton_Click(object sender, EventArgs e) {
CopyEditProfile();
}
/// <summary>
/// CSVエクスポートクリックイベント
/// </summary>
private void _csvExportButton_Click(object sender, System.EventArgs e) {
StreamWriter sw = null;
bool delPWFlg = false;
if (_saveCSVFileDialog.ShowDialog() == DialogResult.OK) {
// パスワード出力/削除確認
if (ConnectProfilePlugin.AskUserYesNoInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVExportDetetePasssword"), MessageBoxIcon.Question) == DialogResult.Yes) {
delPWFlg = true;
}
// CSVファイル出力
try {
sw = new System.IO.StreamWriter(_saveCSVFileDialog.FileName, false, System.Text.Encoding.UTF8);
sw.WriteLine(_cmd.CSVHeader);
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
sw.WriteLine(_cmd.ConvertCSV(prof, delPWFlg));
}
ConnectProfilePlugin.MessageBoxInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVExportComplete"), MessageBoxIcon.Information);
} catch (Exception ex) {
ConnectProfilePlugin.MessageBoxInvoke(ex.Message, MessageBoxIcon.Error);
} finally {
if (sw != null) sw.Close();
}
}
}
/// <summary>
/// CSVインポートクリックイベント
/// </summary>
private void _csvImportButton_Click(object sender, System.EventArgs e) {
ConnectProfileList profList = new ConnectProfileList();
ConnectProfileStruct prof = new ConnectProfileStruct();
StreamReader sr = null;
bool appendFlg = false;
int lineCnt = 1;
if (_openCSVFileDialog.ShowDialog() == DialogResult.OK) {
// インポート実行確認
if (ConnectProfilePlugin.AskUserYesNoInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportConfirm"), MessageBoxIcon.Question) == DialogResult.Yes) {
// 追加/削除確認
if (ConnectProfilePlugin.AskUserYesNoInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportAppendProfileList"), MessageBoxIcon.Question) == DialogResult.Yes) {
appendFlg = true;
}
// インポート実行
try {
sr = new System.IO.StreamReader(_openCSVFileDialog.FileName, System.Text.Encoding.UTF8);
while (!sr.EndOfStream) {
string line = sr.ReadLine();
// 各種チェック
if (lineCnt == 1) {
// ヘッダーチェック(1行目固定)
if (_cmd.CheckCSVHeader(line) != true) {
ConnectProfilePlugin.MessageBoxInvoke(string.Format(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportInvalidHeader"), lineCnt.ToString()), MessageBoxIcon.Error);
return;
}
} else if ((line == "") || (line.IndexOf("#") == 0)) {
// 空白行/行頭シャープの行はスキップ
lineCnt++;
continue;
} else {
// フィールド数チェック
if (_cmd.CheckCSVFieldCount(line) != true) {
ConnectProfilePlugin.MessageBoxInvoke(string.Format(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportInvalidFieldCount"), lineCnt.ToString()), MessageBoxIcon.Error);
return;
}
// 各項目チェック
prof = _cmd.CheckCSVData(line);
if (prof == null) {
ConnectProfilePlugin.MessageBoxInvoke(string.Format(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportFailed"), lineCnt.ToString()), MessageBoxIcon.Error);
return;
}
// プロファイル追加
if (appendFlg == true) _cmd.AddProfileCommand(ConnectProfilePlugin.Profiles, prof);
else _cmd.AddProfileCommand(profList, prof);
}
lineCnt++;
}
// プロファイルリスト全置換
if (appendFlg == false) _cmd.ReplaceAllProfileCommand(ConnectProfilePlugin.Profiles, profList);
RefreshAllProfiles();
RefreshSelectCount();
ConnectProfilePlugin.MessageBoxInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportComplete"), MessageBoxIcon.Information);
} catch (Exception ex) {
ConnectProfilePlugin.MessageBoxInvoke(ex.Message, MessageBoxIcon.Error);
} finally {
if (sr != null) sr.Close();
}
} else {
// キャンセル
ConnectProfilePlugin.MessageBoxInvoke(ConnectProfilePlugin.Strings.GetString("Message.ConnectProfile.CSVImportCancel"), MessageBoxIcon.Warning);
}
}
}
/// <summary>
/// 選択されているプロファイルのみ表示チェックボックスクリックイベント
/// </summary>
private void _displaySelectedOnlyCheck_CheckedChanged(object sender, EventArgs e) {
if (_selectCnt == 0) _profileListView.SelectedItems.Clear();
RefreshAllProfiles();
}
/// <summary>
/// プロファイルリストカラムクリックイベント
/// </summary>
private void _profileListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e) {
_listViewItemSorter.Column = e.Column;
_profileListView.Sort();
}
/// <summary>
/// プロファイルリストマウス押下イベント
/// </summary>
private void _profileListView_MouseDown(object sender, MouseEventArgs e) {
// 押下ボタン/ダブルクリックチェック
_doubleClickFlg = (2 <= e.Clicks) ? true : false;
_clickButton = e.Button;
// 左ダブルクリック=接続, 中ダブルクリック=追加, 右ダブルクリック=編集
if ((_doubleClickFlg == true) && (_clickButton == MouseButtons.Left)) {
if (_selectCnt == 0) {
EnableControl(false);
Connect(GetSelectedProfile());
EnableControl(true);
}
} else if ((_doubleClickFlg == true) && (_clickButton == MouseButtons.Middle)) {
AddProfile();
} else if ((_doubleClickFlg == true) && (_clickButton == MouseButtons.Right)) {
EditProfile();
}
}
/// <summary>
/// プロファイルリストチェックステータス変更前イベント
/// </summary>
private void _profileListView_ItemCheck(object sender, ItemCheckEventArgs e) {
// 何も選択されていない状態でダブルクリックした場合はチェックステータスを変更しない(左ダブルクリック接続用)
if ((_selectCnt == 0) && (_doubleClickFlg == true)) e.NewValue = e.CurrentValue;
}
/// <summary>
/// プロファイルリストチェックステータス変更後イベント
/// </summary>
private void _profileListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
// リスト更新実行中はスキップ(RefreshAllProfiles実行中にも度々イベントが発生してしまうため)
if (_refreshingFlg != true) {
e.Item.Selected = true; // チェックボックスクリック時は選択状態にならないため明示的に選択状態にする
ConnectProfileStruct prof = GetSelectedProfile();
if (prof != null) {
prof.Check = e.Item.Checked;
RefreshSelectCount();
}
}
}
/// <summary>
/// フィルタータイマーイベント
/// </summary>
private void _filterTime_Tick(object sender, EventArgs e) {
_filterTimer.Stop();
RefreshAllProfiles();
}
/// <summary>
/// フォームロードイベント
/// </summary>
private void ConnectProfileForm_Load(object sender, EventArgs e) {
// フォーム表示時はリスト未選択状態
_profileListView.SelectedItems.Clear();
}
/// <summary>
/// フォームクローズイベント
/// </summary>
private void ConnectProfileForm_FormClosing(object sender, FormClosingEventArgs e) {
// 全プロファイルのチェック状態をOFFに設定
foreach (ConnectProfileStruct prof in ConnectProfilePlugin.Profiles) {
prof.Check = false;
}
}
}
/// <summary>
/// ListViewカラムソートクラス
/// </summary>
public class ListViewItemComparer : IComparer {
// メンバー変数
public enum ComparerMode { String, Integer, DateTime };
private int _column;
private SortOrder _order;
private ComparerMode _mode;
private ComparerMode[] _columnModes;
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="col">ソート列番号</param>
/// <param name="ord">ソートオーダー</param>
/// <param name="cmod">ソート方法</param>
public ListViewItemComparer(int col, SortOrder ord, ComparerMode cmod) {
_column = col;
_order = ord;
_mode = cmod;
}
public ListViewItemComparer() {
_column = 0;
_order = SortOrder.Ascending;
_mode = ComparerMode.String;
}
/// <summary>
/// ソート
/// </summary>
/// <param name="x">ListViewItem</param>
/// <param name="y">ListViewItem</param>
public int Compare(object x, object y) {
int result = 0;
ListViewItem itemx = (ListViewItem)x;
ListViewItem itemy = (ListViewItem)y;
if (_order == SortOrder.None) return 0;
// ソート方法決定
if (_columnModes != null && _columnModes.Length > _column) _mode = _columnModes[_column];
// ソート方法別にxとyを比較
switch (_mode) {
case ComparerMode.String: // 文字列
result = string.Compare(itemx.SubItems[_column].Text, itemy.SubItems[_column].Text);
break;
case ComparerMode.Integer: // 数値
result = int.Parse(itemx.SubItems[_column].Text).CompareTo(int.Parse(itemy.SubItems[_column].Text));
break;
case ComparerMode.DateTime: // 日付
result = DateTime.Compare(DateTime.Parse(itemx.SubItems[_column].Text), DateTime.Parse(itemy.SubItems[_column].Text));
break;
}
// 降順時は結果を+-逆にする
return (_order == SortOrder.Descending) ? -result : result;
}
/// <summary>
/// ソート列番号
/// </summary>
public int Column {
set {
// 同一列の時は昇順降順をスイッチ
if (_column == value) {
if (_order == SortOrder.Ascending) _order = SortOrder.Descending;
else if (_order == SortOrder.Descending) _order = SortOrder.Ascending;
}
_column = value;
}
get { return _column; }
}
/// <summary>
/// ソートオーダー
/// </summary>
public SortOrder Order {
set { _order = value; }
get { return _order; }
}
/// <summary>
/// ソート方法
/// </summary>
public ComparerMode Mode {
set { _mode = value; }
get { return _mode; }
}
/// <summary>
/// 列毎のソート方法
/// </summary>
public ComparerMode[] ColumnModes {
set { _columnModes = value; }
}
}
}