1 // ================================================================================================
3 // Wikipedia翻訳支援ツール設定画面クラスソース</summary>
5 // <copyright file="ConfigForm.cs" company="honeplusのメモ帳">
6 // Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
9 // ================================================================================================
11 namespace Honememo.Wptscs
14 using System.Collections.Generic;
15 using System.ComponentModel;
19 using System.Reflection;
21 using System.Windows.Forms;
22 using Honememo.Utilities;
23 using Honememo.Wptscs.Models;
24 using Honememo.Wptscs.Properties;
25 using Honememo.Wptscs.Utilities;
26 using Honememo.Wptscs.Websites;
29 /// Wikipedia翻訳支援ツール設定画面のクラスです。
31 public partial class ConfigForm : Form
36 /// 現在設定中のアプリケーションの設定。
38 /// <remarks>設定画面を閉じた後は再読み込みされるので、必要に応じて随時更新してよい。</remarks>
39 private Config config;
42 /// <seealso cref="comboBoxLanguage"/>で選択していたアイテムのバックアップ。
44 private string comboBoxLanguageSelectedText;
53 /// <param name="config">設定対象のConfig。</param>
54 /// <remarks>configは設定画面の操作により随時更新される。呼び出し元では再読み込みすること。</remarks>
55 public ConfigForm(Config config)
57 this.InitializeComponent();
60 this.config = Honememo.Utilities.Validate.NotNull(config, "config");
65 #region フォームの各イベントのメソッド
70 /// <param name="sender">イベント発生オブジェクト。</param>
71 /// <param name="e">発生したイベント。</param>
72 private void ConfigForm_Load(object sender, EventArgs e)
79 this.ImportTranslationDictionaryView(this.dataGridViewItems, this.config.ItemTables);
82 this.ImportTranslationTableView(this.dataGridViewHeading, this.config.HeadingTable);
85 foreach (Website site in this.config.Websites)
87 this.comboBoxLanguage.Items.Add(site.Language.Code);
91 this.textBoxCacheExpire.Text = Settings.Default.CacheExpire.Days.ToString();
92 this.textBoxUserAgent.Text = Settings.Default.UserAgent;
93 this.textBoxReferer.Text = Settings.Default.Referer;
94 this.textBoxMaxConnectRetries.Text = Settings.Default.MaxConnectRetries.ToString();
95 this.textBoxConnectRetryTime.Text = Settings.Default.ConnectRetryTime.ToString();
96 this.checkBoxIgnoreError.Checked = Settings.Default.IgnoreError;
97 this.labelApplicationName.Text = FormUtils.ApplicationName();
98 AssemblyCopyrightAttribute copyright = Attribute.GetCustomAttribute(
99 Assembly.GetExecutingAssembly(),
100 typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
101 if (copyright != null)
103 this.labelCopyright.Text = copyright.Copyright;
108 // 通常この処理では例外は発生しないはず(Configに読めているので)。想定外のエラー用
109 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
116 /// <param name="sender">イベント発生オブジェクト。</param>
117 /// <param name="e">発生したイベント。</param>
118 private void ButtonOk_Click(object sender, EventArgs e)
122 // 各タブの内容を設定ファイルに保存する
125 this.config.ItemTables = this.ExportTranslationDictionaryView(this.dataGridViewItems);
128 this.config.HeadingTable = this.ExportTranslationTableView(this.dataGridViewHeading);
131 // ※ このタブはコンボボックス変更のタイミングで保存されるので、そのメソッドを呼ぶ
132 this.ComboBoxLanguuage_SelectedIndexChanged(sender, e);
135 Settings.Default.CacheExpire = new TimeSpan(int.Parse(this.textBoxCacheExpire.Text), 0, 0, 0);
136 Settings.Default.UserAgent = this.textBoxUserAgent.Text;
137 Settings.Default.Referer = this.textBoxReferer.Text;
138 Settings.Default.MaxConnectRetries = int.Parse(this.textBoxMaxConnectRetries.Text);
139 Settings.Default.ConnectRetryTime = int.Parse(this.textBoxConnectRetryTime.Text);
140 Settings.Default.IgnoreError = this.checkBoxIgnoreError.Checked;
143 Settings.Default.Save();
149 // ※ エラーの場合、どうしても駄目ならキャンセルボタンで閉じてもらう
155 System.Diagnostics.Debug.WriteLine(ex.ToString());
156 FormUtils.ErrorDialog(Resources.ErrorMessageConfigSaveFailed, ex.Message);
161 // 通常ファイル保存以外では例外は発生しないはず。想定外のエラー用
162 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
168 #region 記事の置き換えタブのイベントのメソッド
171 /// 記事の置き換え対訳表への行追加時の処理。
173 /// <param name="sender">イベント発生オブジェクト。</param>
174 /// <param name="e">発生したイベント。</param>
175 private void DataGridViewItems_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
177 for (int i = e.RowIndex - 1; i < this.dataGridViewItems.Rows.Count; i++)
179 // プログラムから追加された場合は現在のインデックス、画面から追加した場合は+1したインデックスが来る
182 this.dataGridViewItems.Rows[i].Cells["ColumnArrow"].Value = Resources.RightArrow;
188 /// 記事の置き換え対訳表のセル編集時のバリデート処理。
190 /// <param name="sender">イベント発生オブジェクト。</param>
191 /// <param name="e">発生したイベント。</param>
192 private void DataGridViewItems_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
195 if (this.dataGridViewItems.Columns[e.ColumnIndex].Name != "ColumnTimestamp")
200 // 空または日付として認識可能な値の場合OK
201 string value = e.FormattedValue.ToString();
203 if (String.IsNullOrWhiteSpace(value) || DateTime.TryParse(value, out dummy))
208 // 不許可値の場合、NGメッセージを表示
209 this.dataGridViewItems.Rows[e.RowIndex].ErrorText = Resources.WarningMessageUnformatedTimestamp;
214 /// 記事の置き換え対訳表のセル編集時のバリデート成功時の処理。
216 /// <param name="sender">イベント発生オブジェクト。</param>
217 /// <param name="e">発生したイベント。</param>
218 private void DataGridViewItems_CellValidated(object sender, DataGridViewCellEventArgs e)
220 // 取得日時列の場合、バリデートNGメッセージを消す
221 // ※ 他の列で消さないのは、エラーを出しているのがRowValidatingの場合もあるから
222 if (this.dataGridViewItems.Columns[e.ColumnIndex].Name == "ColumnTimestamp")
224 this.dataGridViewItems.Rows[e.RowIndex].ErrorText = String.Empty;
229 /// 記事の置き換え対訳表のセル変更時の処理。
231 /// <param name="sender">イベント発生オブジェクト。</param>
232 /// <param name="e">発生したイベント。</param>
233 private void DataGridViewItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
235 // 取得日時列が空の場合、有効期限が無期限として背景色を変更
236 // ※ ただし全列が空(新規行など)の場合は無視
239 DataGridViewRow row = this.dataGridViewItems.Rows[e.RowIndex];
240 if (String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnTimestamp"]))
241 && !this.IsEmptyDataGridViewItemsRow(row))
244 row.DefaultCellStyle.BackColor = Color.Bisque;
246 else if (row.InheritedStyle.BackColor != this.dataGridViewItems.DefaultCellStyle.BackColor)
249 // ※ DefaultCellStyleプロパティにアクセスしたタイミングでインスタンスが
250 // 作成されてしまうため、InheritedStyleを調べて変更が必要な場合だけアクセス
251 row.DefaultCellStyle.BackColor = this.dataGridViewItems.DefaultCellStyle.BackColor;
257 /// 記事の置き換え対訳表の行編集時のバリデート処理。
259 /// <param name="sender">イベント発生オブジェクト。</param>
260 /// <param name="e">発生したイベント。</param>
261 private void DataGridViewItems_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
263 // 翻訳元、記事名、翻訳先が未入力の場合、バリデートNGメッセージを表示
264 // ※ ただし全列が空(新規行など)の場合は無視
265 DataGridViewRow row = this.dataGridViewItems.Rows[e.RowIndex];
266 if ((String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromCode"]))
267 || String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToCode"]))
268 || String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromTitle"])))
269 && !this.IsEmptyDataGridViewItemsRow(row))
271 row.ErrorText = Resources.WarningMessageEmptyTranslationDictionary;
277 /// 記事の置き換え対訳表を使用する<see cref="DataGridView"/>の値設定を行う。
279 /// <param name="view">対訳表を表示するビュー。</param>
280 /// <param name="dictionaries">対訳表データ。</param>
281 private void ImportTranslationDictionaryView(DataGridView view, IList<TranslationDictionary> dictionaries)
283 // 初期設定以外の場合も想定して最初にクリア
285 foreach (TranslationDictionary dic in dictionaries)
287 foreach (KeyValuePair<string, TranslationDictionary.Item> item in dic)
290 DataGridViewRow row = view.Rows[view.Rows.Add()];
292 // 1行分の初期値を設定。右矢印は別途イベントで追加すること
293 row.Cells["ColumnFromCode"].Value = dic.From;
294 row.Cells["ColumnFromTitle"].Value = item.Key;
295 row.Cells["ColumnAlias"].Value = item.Value.Alias;
296 row.Cells["ColumnToCode"].Value = dic.To;
297 row.Cells["ColumnToTitle"].Value = item.Value.Word;
298 if (item.Value.Timestamp.HasValue)
300 row.Cells["ColumnTimestamp"].Value = item.Value.Timestamp.Value.ToLocalTime().ToString("G");
305 // 取得日時の降順でソート、空の列は先頭にする
306 this.dataGridViewItems.Sort(new TranslationDictionaryViewComparer());
309 // ※ 常に行ってしまうと、読み込みに非常に時間がかかるため
310 view.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
314 /// 記事の置き換え対訳表を使用する<see cref="DataGridView"/>からデータを抽出する。
316 /// <param name="view">対訳表を表示するビュー。</param>
317 /// <returns>対訳表データ。</returns>
318 private IList<TranslationDictionary> ExportTranslationDictionaryView(DataGridView view)
320 IList<TranslationDictionary> dictionaries = new List<TranslationDictionary>();
321 foreach (DataGridViewRow row in view.Rows)
323 // 画面での追加用の最終行が空で渡されてくるので無視
324 if (this.IsEmptyDataGridViewItemsRow(row))
329 // その行で対象とする言語を探索、無ければ新規作成
330 string from = FormUtils.ToString(row.Cells["ColumnFromCode"]);
331 string to = FormUtils.ToString(row.Cells["ColumnToCode"]);
332 TranslationDictionary dic
333 = TranslationDictionary.GetDictionaryNeedCreate(dictionaries, from, to);
336 TranslationDictionary.Item item = new TranslationDictionary.Item
338 Word = FormUtils.ToString(row.Cells["ColumnToTitle"]),
339 Alias = FormUtils.ToString(row.Cells["ColumnAlias"])
342 string timestamp = FormUtils.ToString(row.Cells["ColumnTimestamp"]);
343 if (!String.IsNullOrWhiteSpace(timestamp))
345 item.Timestamp = DateTime.Parse(timestamp);
347 // UTCでもなくタイムゾーンでも無い場合、ローカル時刻として設定する
348 if (item.Timestamp.Value.Kind == DateTimeKind.Unspecified)
350 item.Timestamp = DateTime.SpecifyKind(item.Timestamp.Value, DateTimeKind.Local);
354 dic[FormUtils.ToString(row.Cells["ColumnFromTitle"])] = item;
361 /// 記事の置き換え対訳表の行が空かを判定する。
363 /// <param name="row">対訳表の1行。</param>
364 /// <returns>空の場合<c>true</c>。</returns>
365 private bool IsEmptyDataGridViewItemsRow(DataGridViewRow row)
367 return String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromCode"]))
368 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromTitle"]))
369 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnAlias"]))
370 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToCode"]))
371 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToTitle"]))
372 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnTimestamp"]));
377 #region 見出しの置き換えタブのイベントのメソッド
380 /// 見出しの置き換え対訳表を使用する<see cref="DataGridView"/>の値設定を行う。
382 /// <param name="view">対訳表を表示するビュー。</param>
383 /// <param name="table">対訳表データ。</param>
384 private void ImportTranslationTableView(DataGridView view, TranslationTable table)
386 // 初期設定以外の場合も想定して最初にクリア
387 view.Columns.Clear();
389 // 言語コードを列、語句を行とする。登録されている全言語分の列を作成する
390 foreach (Website site in this.config.Websites)
392 this.AddTranslationTableColumn(view.Columns, site.Language.Code, this.GetHeaderLanguage(site.Language));
396 foreach (IDictionary<string, string> record in table)
399 DataGridViewRow row = view.Rows[view.Rows.Add()];
401 foreach (KeyValuePair<string, string> cell in record)
403 // 上で登録した列では足りなかった場合、その都度生成する
404 if (!view.Columns.Contains(cell.Key))
406 this.AddTranslationTableColumn(view.Columns, cell.Key, cell.Key);
409 row.Cells[cell.Key].Value = cell.Value;
413 // 可能であれば現在表示中の言語の列の昇順でソートする
415 string code = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
416 if (view.Columns.Contains(code))
418 view.Sort(view.Columns[code], ListSortDirection.Ascending);
420 else if (view.Columns.Contains("en"))
422 view.Sort(view.Columns["en"], ListSortDirection.Ascending);
426 // ※ 常に行ってしまうと、読み込みに時間がかかるため
427 view.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
431 /// 見出しの置き換え対訳表を使用する<see cref="DataGridView"/>からデータを抽出する。
433 /// <param name="view">対訳表を表示するビュー。</param>
434 /// <returns>対訳表データ。</returns>
435 private TranslationTable ExportTranslationTableView(DataGridView view)
437 TranslationTable table = new TranslationTable();
438 foreach (DataGridViewRow row in view.Rows)
440 IDictionary<string, string> record = new SortedDictionary<string, string>();
441 foreach (DataGridViewCell cell in row.Cells)
443 // 空のセルは格納しない、該当の組み合わせは消える
444 string value = FormUtils.ToString(cell);
445 if (!String.IsNullOrWhiteSpace(value))
447 record[cell.OwningColumn.Name] = value;
451 // 1件もデータが無い行は丸々カットする
452 if (record.Count > 0)
462 /// 指定された情報を元に見出しの置き換え対訳表の列を追加する。
464 /// <param name="columns">列コレクション。</param>
465 /// <param name="columnName">列名。</param>
466 /// <param name="headerText">列見出し。</param>
467 private void AddTranslationTableColumn(DataGridViewColumnCollection columns, string columnName, string headerText)
469 columns.Add(columnName, headerText);
475 /// <param name="lang">表示言語コード。</param>
476 /// <returns>表示名、無ければ言語コード。</returns>
477 private string GetHeaderLanguage(Language lang)
479 Language.LanguageName name;
480 if (lang.Names.TryGetValue(
481 System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName, out name))
483 if (!String.IsNullOrEmpty(name.Name))
485 return String.Format(Resources.HeadingViewHeaderText, name.Name, lang.Code);
494 #region 言語/サーバータブのイベントのメソッド
499 /// <param name="sender">イベント発生オブジェクト。</param>
500 /// <param name="e">発生したイベント。</param>
501 private void ComboBoxLanguuage_SelectedIndexChanged(object sender, EventArgs e)
506 if (!String.IsNullOrEmpty(this.comboBoxLanguageSelectedText))
509 this.SaveChangedValue(this.GetMediaWikiNeedCreate(this.config.Websites, this.comboBoxLanguageSelectedText));
513 if (!String.IsNullOrEmpty(this.comboBoxLanguage.Text))
515 // 設定が存在しなければ基本的に自動生成されるのでそのまま使用
516 this.LoadCurrentValue(this.GetMediaWikiNeedCreate(this.config.Websites, this.comboBoxLanguage.Text));
519 this.buttonLanguageRemove.Enabled = true;
520 this.groupBoxServer.Enabled = true;
521 this.groupBoxLanguage.Enabled = true;
524 this.comboBoxLanguageSelectedText = this.comboBoxLanguage.Text;
529 this.buttonLanguageRemove.Enabled = false;
530 this.groupBoxServer.Enabled = false;
531 this.groupBoxLanguage.Enabled = false;
534 this.comboBoxLanguageSelectedText = String.Empty;
539 // 通常この処理では例外は発生しないはず。想定外のエラー用
540 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
547 /// <param name="sender">イベント発生オブジェクト。</param>
548 /// <param name="e">発生したイベント。</param>
549 private void ButtonLunguageAdd_Click(object sender, EventArgs e)
552 InputLanguageCodeDialog form = new InputLanguageCodeDialog(this.config);
556 if (!String.IsNullOrWhiteSpace(form.LanguageCode))
558 // 値を一覧・見出しの対訳表に追加、登録した値を選択状態に変更
559 this.comboBoxLanguage.Items.Add(form.LanguageCode);
560 this.dataGridViewHeading.Columns.Add(form.LanguageCode, form.LanguageCode);
561 this.comboBoxLanguage.SelectedItem = form.LanguageCode;
568 /// <param name="sender">イベント発生オブジェクト。</param>
569 /// <param name="e">発生したイベント。</param>
570 private void ButtonLanguageRemove_Click(object sender, EventArgs e)
572 // 表示されている言語を設定から削除する
573 for (int i = this.config.Websites.Count - 1; i >= 0; i--)
575 if (this.config.Websites[i].Language.Code == this.comboBoxLanguage.Text)
578 this.config.Websites.RemoveAt(i);
582 // コンボボックスからも削除し、表示を更新する
583 this.comboBoxLanguageSelectedText = null;
584 this.comboBoxLanguage.Items.Remove(this.comboBoxLanguage.Text);
585 this.ComboBoxLanguuage_SelectedIndexChanged(sender, e);
589 /// 各名前空間のIDボックスバリデート処理。
591 /// <param name="sender">イベント発生オブジェクト。</param>
592 /// <param name="e">発生したイベント。</param>
593 private void TextBoxNamespace_Validating(object sender, CancelEventArgs e)
596 TextBox box = (TextBox)sender;
597 box.Text = StringUtils.DefaultString(box.Text).Trim();
599 if (!String.IsNullOrEmpty(box.Text) && !int.TryParse(box.Text, out value))
601 this.errorProvider.SetError(box, Resources.WarningMessageIgnoreNumericNamespace);
607 /// 括弧のスタイルボックスバリデート処理。
609 /// <param name="sender">イベント発生オブジェクト。</param>
610 /// <param name="e">発生したイベント。</param>
611 private void TextBoxBracket_Validating(object sender, CancelEventArgs e)
614 TextBox box = (TextBox)sender;
615 if (!String.IsNullOrEmpty(box.Text) && !box.Text.Contains("$1"))
617 this.errorProvider.SetError(box, Resources.WarningMessageUnformatedBracket);
623 /// 言語の設定表の行編集時のバリデート処理。
625 /// <param name="sender">イベント発生オブジェクト。</param>
626 /// <param name="e">発生したイベント。</param>
627 private void DataGridViewLanguageName_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
629 DataGridViewRow row = this.dataGridViewLanguageName.Rows[e.RowIndex];
632 if (FormUtils.IsEmptyRow(row))
637 // 言語コードは必須、またトリムして小文字に変換
638 string code = FormUtils.ToString(row.Cells["ColumnCode"]).Trim().ToLower();
639 row.Cells["ColumnCode"].Value = code;
640 if (String.IsNullOrEmpty(code))
642 row.ErrorText = Resources.WarningMessageEmptyCodeColumn;
647 // 略称を設定する場合、呼称を必須とする
648 if (!String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnShortName"]))
649 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnName"])))
651 row.ErrorText = Resources.WarningMessageShortNameColumnOnly;
659 /// <param name="sender">イベント発生オブジェクト。</param>
660 /// <param name="e">発生したイベント。</param>
661 private void DataGridViewLanguageName_Validating(object sender, CancelEventArgs e)
664 IDictionary<string, int> codeMap = new Dictionary<string, int>();
665 for (int i = 0; i < this.dataGridViewLanguageName.RowCount - 1; i++)
667 string code = FormUtils.ToString(this.dataGridViewLanguageName["ColumnCode", i]);
669 if (codeMap.TryGetValue(code, out y))
672 this.dataGridViewLanguageName.Rows[i].ErrorText = Resources.WarningMessageDuplicateCodeColumn;
673 this.dataGridViewLanguageName.Rows[y].ErrorText = Resources.WarningMessageDuplicateCodeColumn;
678 // それ以外はマップに出現行とともに追加
684 #region イベント実装支援用メソッド
687 /// コレクションから指定された言語のMediaWikiを取得する。
688 /// 存在しない場合は空のインスタンスを生成、コレクションに追加して返す。
690 /// <param name="collection">翻訳元言語。</param>
691 /// <param name="lang">言語コード。</param>
692 /// <returns>翻訳パターン。存在しない場合は新たに作成した翻訳パターンを返す。</returns>
693 private MediaWiki GetMediaWikiNeedCreate(ICollection<Website> collection, string lang)
696 foreach (Website s in collection)
698 if (s.Language.Code == lang)
702 return s as MediaWiki;
705 // 万が一同じ言語コードで違う型の値があったら上書き
706 collection.Remove(s);
711 // 存在しないか上書きの場合、作成した翻訳パターンをコレクションに追加し、返す
712 MediaWiki site = new MediaWiki(new Language(lang));
713 collection.Add(site);
718 /// 指定されたLanguage設定を画面表示/編集用に読み込む。
720 /// <param name="lang">読込元Language設定。</param>
721 /// <remarks>一部パラメータには初期値が存在するが、格納時に対処するため全て読み込む。</remarks>
722 private void LoadCurrentValue(Language lang)
725 // ※ Bracketは初期値があるパラメータのため、必ず値が返る
726 this.textBoxBracket.Text = lang.Bracket;
729 this.dataGridViewLanguageName.Rows.Clear();
730 foreach (KeyValuePair<string, Language.LanguageName> name in lang.Names)
732 int index = this.dataGridViewLanguageName.Rows.Add();
733 this.dataGridViewLanguageName["ColumnCode", index].Value = name.Key;
734 this.dataGridViewLanguageName["ColumnName", index].Value = name.Value.Name;
735 this.dataGridViewLanguageName["ColumnShortName", index].Value = name.Value.ShortName;
739 this.dataGridViewLanguageName.Sort(this.dataGridViewLanguageName.Columns["ColumnCode"], ListSortDirection.Ascending);
743 /// 指定されたWebsite設定を画面表示/編集用に読み込む。
745 /// <param name="site">読込元Website設定。</param>
746 private void LoadCurrentValue(Website site)
748 // Languageクラス分の読み込みを行う
749 this.LoadCurrentValue(site.Language);
752 this.textBoxLocation.Text = site.Location;
756 /// 指定されたMediaWiki設定を画面表示/編集用に読み込む。
758 /// <param name="site">読込元MediaWiki設定。</param>
759 /// <remarks>一部パラメータには初期値が存在するが、格納時に対処するため全て読み込む。</remarks>
760 private void LoadCurrentValue(MediaWiki site)
762 // Websiteクラス分の読み込みを行う
763 this.LoadCurrentValue((Website)site);
765 // MediaWikiクラス分の読み込み
766 this.textBoxExportPath.Text = StringUtils.DefaultString(site.ExportPath);
767 this.textBoxMetaApi.Text = StringUtils.DefaultString(site.MetaApi);
768 this.textBoxTemplateNamespace.Text = site.TemplateNamespace.ToString();
769 this.textBoxCategoryNamespace.Text = site.CategoryNamespace.ToString();
770 this.textBoxFileNamespace.Text = site.FileNamespace.ToString();
771 this.textBoxRedirect.Text = StringUtils.DefaultString(site.Redirect);
773 // Template:Documentionは改行区切りのマルチテキストとして扱う
774 StringBuilder b = new StringBuilder();
775 foreach (string s in site.DocumentationTemplates)
777 b.Append(s).Append(Environment.NewLine);
780 this.textBoxDocumentationTemplate.Text = b.ToString();
781 this.textBoxDocumentationTemplateDefaultPage.Text = StringUtils.DefaultString(site.DocumentationTemplateDefaultPage);
782 this.textBoxLinkInterwikiFormat.Text = StringUtils.DefaultString(site.LinkInterwikiFormat);
783 this.textBoxLangFormat.Text = StringUtils.DefaultString(site.LangFormat);
787 /// 指定されたLanguage設定に画面上で変更された値の格納を行う。
789 /// <param name="lang">格納先Language設定。</param>
790 /// <remarks>一部パラメータには初期値が存在するため、変更がある場合のみ格納する。</remarks>
791 private void SaveChangedValue(Language lang)
793 // Bracketは初期値を持つパラメータのため、変更された場合のみ格納する。
794 // ※ この値は前後の空白に意味があるため、Trimしてはいけない
795 string str = StringUtils.DefaultString(this.textBoxBracket.Text);
796 if (str != lang.Bracket)
803 for (int y = 0; y < this.dataGridViewLanguageName.RowCount - 1; y++)
805 // 値が入ってないとかはガードしているはずだが、一応チェック
806 string code = FormUtils.ToString(this.dataGridViewLanguageName["ColumnCode", y]).Trim();
807 if (!String.IsNullOrEmpty(code))
809 Language.LanguageName name = new Language.LanguageName();
810 name.Name = FormUtils.ToString(this.dataGridViewLanguageName["ColumnName", y]).Trim();
811 name.ShortName = FormUtils.ToString(this.dataGridViewLanguageName["ColumnShortName", y]).Trim();
812 lang.Names[code] = name;
818 /// 指定されたWebsite設定に画面上で変更された値の格納を行う。
820 /// <param name="site">格納先Website設定。</param>
821 /// <remarks>Websiteについては特に特殊な処理は無いため全て上書きする。</remarks>
822 private void SaveChangedValue(Website site)
824 // Languageクラス分の設定を行う
825 this.SaveChangedValue(site.Language);
828 site.Location = StringUtils.DefaultString(this.textBoxLocation.Text).Trim();
832 /// 指定されたMediaWiki設定に画面上で変更された値の格納を行う。
834 /// <param name="site">格納先MediaWiki設定。</param>
835 /// <remarks>一部パラメータには初期値が存在するため、変更がある場合のみ格納する。</remarks>
836 private void SaveChangedValue(MediaWiki site)
839 this.SaveChangedValue((Website)site);
841 // 初期値を持つパラメータがあるため、全て変更された場合のみ格納する。
842 // ※ もうちょっと綺麗に書きたかったが、うまい手が思いつかなかったので力技
843 // MediaWikiクラス側で行わないのは、場合によっては意図的に初期値と同じ値を設定すること
844 // もありえるから(初期値が変わる可能性がある場合など)。
845 string str = StringUtils.DefaultString(this.textBoxExportPath.Text).Trim();
846 if (str != site.ExportPath)
848 site.ExportPath = str;
851 str = StringUtils.DefaultString(this.textBoxMetaApi.Text).Trim();
852 if (str != site.MetaApi)
857 str = StringUtils.DefaultString(this.textBoxRedirect.Text).Trim();
858 if (str != site.Redirect)
863 // Template:Documentionの設定は行ごとに格納
864 // ※ この値は初期値を持たないパラメータ
865 site.DocumentationTemplates.Clear();
866 foreach (string s in StringUtils.DefaultString(this.textBoxDocumentationTemplate.Text).Split('\n'))
868 if (!String.IsNullOrWhiteSpace(s))
870 site.DocumentationTemplates.Add(s.Trim());
874 str = StringUtils.DefaultString(this.textBoxDocumentationTemplateDefaultPage.Text).Trim();
875 if (str != site.DocumentationTemplateDefaultPage)
877 site.DocumentationTemplateDefaultPage = str;
880 str = StringUtils.DefaultString(this.textBoxLinkInterwikiFormat.Text).Trim();
881 if (str != site.LinkInterwikiFormat)
883 site.LinkInterwikiFormat = str;
886 str = StringUtils.DefaultString(this.textBoxLangFormat.Text).Trim();
887 if (str != site.LangFormat)
889 site.LangFormat = str;
892 // 以下、数値へのparseは事前にチェックしてあるので、ここではチェックしない
893 if (!String.IsNullOrWhiteSpace(this.textBoxTemplateNamespace.Text))
895 int num = int.Parse(this.textBoxTemplateNamespace.Text);
896 if (site.TemplateNamespace != num)
898 site.TemplateNamespace = num;
902 if (!String.IsNullOrWhiteSpace(this.textBoxCategoryNamespace.Text))
904 int num = int.Parse(this.textBoxCategoryNamespace.Text);
905 if (site.CategoryNamespace != num)
907 site.CategoryNamespace = num;
911 if (!String.IsNullOrWhiteSpace(this.textBoxFileNamespace.Text))
913 int num = int.Parse(this.textBoxFileNamespace.Text);
914 if (site.FileNamespace != num)
916 site.FileNamespace = num;
925 #region その他タブのイベントのメソッド
928 /// キャッシュ有効期限ボックスバリデート処理。
930 /// <param name="sender">イベント発生オブジェクト。</param>
931 /// <param name="e">発生したイベント。</param>
932 private void TextBoxCacheExpire_Validating(object sender, CancelEventArgs e)
935 this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreCacheExpire);
939 /// リトライ回数ボックスバリデート処理。
941 /// <param name="sender">イベント発生オブジェクト。</param>
942 /// <param name="e">発生したイベント。</param>
943 private void TextBoxMaxConnectRetries_Validating(object sender, CancelEventArgs e)
946 this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreMaxConnectRetries);
950 /// ウェイト時間ボックスバリデート処理。
952 /// <param name="sender">イベント発生オブジェクト。</param>
953 /// <param name="e">発生したイベント。</param>
954 private void TextBoxConnectRetryTime_Validating(object sender, CancelEventArgs e)
957 this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreConnectRetryTime);
961 /// ウェブサイトURLクリック時の処理。
963 /// <param name="sender">イベント発生オブジェクト。</param>
964 /// <param name="e">発生したイベント。</param>
965 private void LinkLabelWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
968 System.Diagnostics.Process.Start(((LinkLabel)sender).Text);
971 #region イベント実装支援用メソッド
974 /// メッセージのみ差し替え可能なテキストボックス用の値がxx以上の数値か、のバリデート処理。
976 /// <param name="box">イベント発生テキストボックス。</param>
977 /// <param name="e">発生したイベント。</param>
978 /// <param name="num">比較対象の数値。</param>
979 /// <param name="message">バリデートメッセージ。</param>
980 private void TextBoxGreaterThanValidating(TextBox box, CancelEventArgs e, int num, string message)
982 box.Text = StringUtils.DefaultString(box.Text).Trim();
984 if (!int.TryParse(box.Text, out value) || value < num)
986 this.errorProvider.SetError(box, message);
998 /// 汎用のエラープロバイダ初期化処理。
1000 /// <param name="sender">イベント発生オブジェクト。</param>
1001 /// <param name="e">発生したイベント。</param>
1002 private void ResetErrorProvider_Validated(object sender, EventArgs e)
1004 this.errorProvider.SetError((Control)sender, null);
1008 /// 汎用の行編集時のエラーテキスト初期化処理。
1010 /// <param name="sender">イベント発生オブジェクト。</param>
1011 /// <param name="e">発生したイベント。</param>
1012 private void ResetErrorText_RowValidated(object sender, DataGridViewCellEventArgs e)
1014 ((DataGridView)sender).Rows[e.RowIndex].ErrorText = String.Empty;
1018 /// 汎用のテーブルエラーテキスト初期化処理。
1020 /// <param name="sender">イベント発生オブジェクト。</param>
1021 /// <param name="e">発生したイベント。</param>
1022 private void ResetErrorText_Validated(object sender, EventArgs e)
1025 foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
1027 row.ErrorText = String.Empty;
1036 /// 記事の置き換え対訳表の日付並び替え用クラスです。
1039 /// 取得日時の降順でソート、空の列は先頭にします。
1040 /// 取得日時が同じ場合、先頭の列から順に昇順でソート。
1042 public class TranslationDictionaryViewComparer : System.Collections.IComparer
1045 /// 取得日時が同じ場合にソートに用いる列名。
1047 private static readonly string[] sortOrder = new string[] { "ColumnFromCode", "ColumnToCode", "ColumnFromTitle" };
1050 /// 2行を比較し、一方が他方より小さいか、等しいか、大きいかを示す値を返します。
1052 /// <param name="x">比較する最初の行です。</param>
1053 /// <param name="y">比較する2番目の行。</param>
1054 /// <returns>0未満:xはyより小さい, 0:xとyは等しい, 0より大きい:xはyより大きい。</returns>
1055 public int Compare(object x, object y)
1057 DataGridViewRow xrow = (DataGridViewRow)x;
1058 DataGridViewRow yrow = (DataGridViewRow)y;
1060 // 取得日時列の降順でソート、ただし空の列は先頭にする
1061 int compare = StringUtils.CompareNullsLast(
1062 FormUtils.ToString(xrow.Cells["ColumnTimestamp"]),
1063 FormUtils.ToString(yrow.Cells["ColumnTimestamp"]));
1066 return compare * -1;
1069 // 取得日時列が同じ場合、残りの列の昇順でソート
1070 foreach (string column in sortOrder)
1072 compare = String.Compare(
1073 FormUtils.ToString(xrow.Cells[column]),
1074 FormUtils.ToString(yrow.Cells[column]));