OSDN Git Service

ce5ccd8d55647aacbf8cd6a0bf47e2a2fa6badf4
[wptscs/wpts.git] / Wptscs / AddConfigDialog.cs
1 // ================================================================================================
2 // <summary>
3 //      Wikipedia翻訳支援ツール設定名入力ダイアログクラスソース</summary>
4 //
5 // <copyright file="AddConfigDialog.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Wptscs
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.IO;
16     using System.Windows.Forms;
17     using Honememo.Models;
18     using Honememo.Wptscs.Logics;
19     using Honememo.Wptscs.Models;
20     using Honememo.Wptscs.Properties;
21     using Honememo.Wptscs.Utilities;
22
23     /// <summary>
24     /// Wikipedia翻訳支援ツール設定名入力ダイアログのクラスです。
25     /// </summary>
26     public partial class AddConfigDialog : Form
27     {
28         #region private変数
29
30         /// <summary>
31         /// 登録済みの設定ファイル名。
32         /// </summary>
33         private IgnoreCaseSet configNames;
34
35         #endregion
36
37         #region コンストラクタ
38
39         /// <summary>
40         /// コンストラクタ。初期化メソッド呼び出しのみ。
41         /// </summary>
42         /// <param name="configNames">登録済みの設定ファイル名。</param>
43         /// <exception cref="ArgumentNullException"><paramref name="configNames"/>が<c>null</c>。</exception>
44         public AddConfigDialog(IEnumerable<string> configNames)
45         {
46             // Windows フォーム デザイナで生成されたコード
47             this.InitializeComponent();
48
49             // 重複チェック用の既存の設定名一覧を受け取る
50             this.configNames = new IgnoreCaseSet(configNames);
51         }
52
53         #endregion
54
55         #region プロパティ
56
57         /// <summary>
58         /// 設定名(データやり取り用)。
59         /// </summary>
60         public string ConfigName
61         {
62             get;
63             set;
64         }
65
66         #endregion
67
68         #region フォームの各イベントのメソッド
69
70         /// <summary>
71         /// OKボタン押下時の処理。データ保存。
72         /// </summary>
73         /// <param name="sender">イベント発生オブジェクト。</param>
74         /// <param name="e">発生したイベント。</param>
75         private void ButtonOk_Click(object sender, EventArgs e)
76         {
77             // 入力値チェック
78             this.ConfigName = this.textBoxName.Text.Trim();
79             if (String.IsNullOrEmpty(this.ConfigName))
80             {
81                 FormUtils.WarningDialog(Resources.WarningMessageEmptyConfigName);
82                 this.textBoxName.Focus();
83                 return;
84             }
85             else if (this.ConfigName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
86             {
87                 FormUtils.WarningDialog(
88                     Resources.WarningMessageInvalidConfigName,
89                     String.Join(", ", Path.GetInvalidFileNameChars()));
90                 this.textBoxName.Focus();
91                 return;
92             }
93             else if (this.configNames.Contains(this.ConfigName))
94             {
95                 FormUtils.WarningDialog(Resources.WarningMessageDuplicateConfigName);
96                 this.textBoxName.Focus();
97                 return;
98             }
99
100             // テキストボックスの設定名を保存、設定名からMediaWiki用のパラメータで設定を作成
101             Config config = new Config();
102             config.File = this.ConfigName + Settings.Default.ConfigurationExtension;
103             config.Translator = typeof(MediaWikiTranslator);
104             try
105             {
106                 // 設定ファイルを一旦保存、成功なら画面を閉じる
107                 // ※ エラーの場合、どうしても駄目ならキャンセルボタンで閉じてもらう
108                 config.Save();
109                 this.DialogResult = DialogResult.OK;
110             }
111             catch (Exception ex)
112             {
113                 // 異常時はエラーメッセージを表示
114                 System.Diagnostics.Debug.WriteLine(ex.ToString());
115                 FormUtils.ErrorDialog(Resources.ErrorMessageConfigSaveFailed, ex.Message);
116             }
117         }
118
119         #endregion
120     }
121 }