using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Kbt.Tool.TextEditHelpTool.TextEditHelpForm.Editors;
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm
{
public partial class TextEditHelpForm : System.Windows.Forms.Form
{
#region 定数
/// <summary>
/// エンコードリスト
/// </summary>
private static readonly List<EncodeType> ENCODE_LIST;
/// <summary>
/// エディタリスト
/// </summary>
private static readonly List<ITextEditor> EDITOR_LIST;
#endregion
#region 静的コンストラクタ
/// <summary>
/// 静的コンストラクタ
/// </summary>
static TextEditHelpForm()
{
TextEditHelpForm.ENCODE_LIST = new List<EncodeType>();
TextEditHelpForm.ENCODE_LIST.Add(new EncodeType("Shift_JIS", Encoding.GetEncoding("Shift_Jis")));
TextEditHelpForm.ENCODE_LIST.Add(new EncodeType("UTF-8", Encoding.UTF8));
TextEditHelpForm.EDITOR_LIST = new List<ITextEditor>();
TextEditHelpForm.EDITOR_LIST.Add(new SqlSelectEditor());
}
#endregion
#region コンストラクタ
/// <summary>
/// コンストラクタ
/// </summary>
public TextEditHelpForm()
{
InitializeComponent();
this.InitializeForm();
}
#endregion
//====================
// EVENT
//====================
#region 参照ボタンクリック
/// <summary>
/// 参照ボタンクリック
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFilePath_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//ofd.FileName = "○○.txt";
//[ファイルの種類]に表示される選択肢を指定する
//指定しないとすべてのファイルが表示される
ofd.Filter =
"テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
ofd.FilterIndex = 1;
ofd.Title = "対象のテキストファイルを選択してください";
//ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
this.txtFilePath.Text = ofd.FileName;
}
}
#endregion
#region ファイル読込ボタンクリック
/// <summary>
/// ファイル読込ボタンクリック
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReadFile_Click(object sender, EventArgs e)
{
// 存在チェック
if (File.Exists(this.txtFilePath.Text) == false)
{
MessageBox.Show(string.Format("指定したファイルは存在しません\r\n{0}", this.txtFilePath.Text),
"ファイル読込エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtFilePath.Focus();
return;
}
try
{
EncodeType selectedEncode = (EncodeType)this.cmbEncode.SelectedValue;
this.txtBefore.Text = File.ReadAllText(this.txtFilePath.Text, selectedEncode.Type);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(),
"ファイル読込エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtFilePath.Focus();
}
}
#endregion
#region 編集ボタンクリック
/// <summary>
/// 編集ボタンクリック
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
try
{
ITextEditor textEditor = (ITextEditor)this.cmbEditor.SelectedValue;
this.txtAfter.Text = textEditor.Edit(this.txtBefore.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(),
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtBefore.Focus();
}
}
#endregion
//====================
// PRIVATE
//====================
#region フォーム初期化
/// <summary>
/// フォーム初期化
/// </summary>
private void InitializeForm()
{
// エンコードの型
this.cmbEncode.DataSource = TextEditHelpForm.ENCODE_LIST;
this.cmbEncode.DisplayMember = "Name";
// エディタ
this.cmbEditor.DataSource = TextEditHelpForm.EDITOR_LIST;
this.cmbEditor.DisplayMember = "Name";
}
#endregion
}
}
=============================================================
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm
{
partial class TextEditHelpForm
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.txtFilePath = new System.Windows.Forms.TextBox();
this.btnReadFile = new System.Windows.Forms.Button();
this.cmbEditor = new System.Windows.Forms.ComboBox();
this.btnEdit = new System.Windows.Forms.Button();
this.cmbEncode = new System.Windows.Forms.ComboBox();
this.btnFilePath = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.grpBefore = new System.Windows.Forms.GroupBox();
this.grpAfter = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.txtBefore = new System.Windows.Forms.RichTextBox();
this.txtAfter = new System.Windows.Forms.RichTextBox();
this.grpBefore.SuspendLayout();
this.grpAfter.SuspendLayout();
this.SuspendLayout();
//
// txtFilePath
//
this.txtFilePath.Location = new System.Drawing.Point(18, 22);
this.txtFilePath.Name = "txtFilePath";
this.txtFilePath.Size = new System.Drawing.Size(386, 22);
this.txtFilePath.TabIndex = 1;
//
// btnReadFile
//
this.btnReadFile.Location = new System.Drawing.Point(18, 49);
this.btnReadFile.Name = "btnReadFile";
this.btnReadFile.Size = new System.Drawing.Size(386, 23);
this.btnReadFile.TabIndex = 3;
this.btnReadFile.Text = "ファイル読込";
this.btnReadFile.UseVisualStyleBackColor = true;
this.btnReadFile.Click += new System.EventHandler(this.btnReadFile_Click);
//
// cmbEditor
//
this.cmbEditor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbEditor.FormattingEnabled = true;
this.cmbEditor.Location = new System.Drawing.Point(94, 12);
this.cmbEditor.Name = "cmbEditor";
this.cmbEditor.Size = new System.Drawing.Size(420, 23);
this.cmbEditor.TabIndex = 4;
//
// btnEdit
//
this.btnEdit.Location = new System.Drawing.Point(558, 132);
this.btnEdit.Name = "btnEdit";
this.btnEdit.Size = new System.Drawing.Size(34, 665);
this.btnEdit.TabIndex = 5;
this.btnEdit.Text = "編 集 →";
this.btnEdit.UseVisualStyleBackColor = true;
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
//
// cmbEncode
//
this.cmbEncode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbEncode.FormattingEnabled = true;
this.cmbEncode.Location = new System.Drawing.Point(410, 49);
this.cmbEncode.Name = "cmbEncode";
this.cmbEncode.Size = new System.Drawing.Size(92, 23);
this.cmbEncode.TabIndex = 10;
//
// btnFilePath
//
this.btnFilePath.Location = new System.Drawing.Point(410, 21);
this.btnFilePath.Name = "btnFilePath";
this.btnFilePath.Size = new System.Drawing.Size(92, 23);
this.btnFilePath.TabIndex = 2;
this.btnFilePath.Text = "参照";
this.btnFilePath.UseVisualStyleBackColor = true;
this.btnFilePath.Click += new System.EventHandler(this.btnFilePath_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("MS Pゴシック", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, );
this.label1.Location = new System.Drawing.Point(12, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(76, 17);
this.label1.TabIndex = 7;
this.label1.Text = "モード :";
//
// grpBefore
//
this.grpBefore.Controls.Add(this.txtBefore);
this.grpBefore.Controls.Add(this.txtFilePath);
this.grpBefore.Controls.Add(this.btnFilePath);
this.grpBefore.Controls.Add(this.cmbEncode);
this.grpBefore.Controls.Add(this.btnReadFile);
this.grpBefore.Location = new System.Drawing.Point(12, 41);
this.grpBefore.Name = "grpBefore";
this.grpBefore.Size = new System.Drawing.Size(530, 772);
this.grpBefore.TabIndex = 12;
this.grpBefore.TabStop = false;
this.grpBefore.Text = "編集前";
//
// grpAfter
//
this.grpAfter.Controls.Add(this.txtAfter);
this.grpAfter.Controls.Add(this.button1);
this.grpAfter.Location = new System.Drawing.Point(607, 43);
this.grpAfter.Name = "grpAfter";
this.grpAfter.Size = new System.Drawing.Size(530, 772);
this.grpAfter.TabIndex = 13;
this.grpAfter.TabStop = false;
this.grpAfter.Text = "編集後";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 49);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(484, 23);
this.button1.TabIndex = 12;
this.button1.Text = "ファイル出力";
this.button1.UseVisualStyleBackColor = true;
//
// txtBefore
//
this.txtBefore.Location = new System.Drawing.Point(18, 91);
this.txtBefore.Name = "txtBefore";
this.txtBefore.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.txtBefore.Size = new System.Drawing.Size(484, 665);
this.txtBefore.TabIndex = 11;
this.txtBefore.Text = "";
this.txtBefore.WordWrap = false;
//
// txtAfter
//
this.txtAfter.Location = new System.Drawing.Point(32, 89);
this.txtAfter.Name = "txtAfter";
this.txtAfter.Size = new System.Drawing.Size(484, 665);
this.txtAfter.TabIndex = 12;
this.txtAfter.Text = "";
this.txtAfter.WordWrap = false;
//
// TextEditHelpForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1161, 827);
this.Controls.Add(this.grpAfter);
this.Controls.Add(this.grpBefore);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnEdit);
this.Controls.Add(this.cmbEditor);
this.Name = "TextEditHelpForm";
this.Text = "テキスト編集補助ツール";
this.grpBefore.ResumeLayout(false);
this.grpBefore.PerformLayout();
this.grpAfter.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtFilePath;
private System.Windows.Forms.Button btnReadFile;
private System.Windows.Forms.ComboBox cmbEditor;
private System.Windows.Forms.Button btnEdit;
private System.Windows.Forms.ComboBox cmbEncode;
private System.Windows.Forms.Button btnFilePath;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox grpBefore;
private System.Windows.Forms.GroupBox grpAfter;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox txtBefore;
private System.Windows.Forms.RichTextBox txtAfter;
}
}
=============================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm
{
internal class EncodeType
{
#region 属性
/// <summary>
/// エンコード名
/// </summary>
private string _name = string.Empty;
/// <summary>
/// エンコードの型
/// </summary>
private Encoding _encodeType = Encoding.UTF8;
#endregion
#region コンストラクタ
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="name">型名</param>
/// <param name="encodeType">エンコード</param>
public EncodeType(string name, Encoding encodeType)
{
this._name = name;
this._encodeType = encodeType;
}
#endregion
//====================
// PUBLIC
//====================
#region エンコード名
/// <summary>
/// エンコード名
/// </summary>
public string Name
{
get { return this._name; }
}
#endregion
#region エンコード型
/// <summary>
/// エンコード型
/// </summary>
public Encoding Type
{
get { return this._encodeType; }
}
#endregion
}
}
=============================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm.Editors
{
public interface ITextEditor
{
/// <summary>
/// 編集する
/// </summary>
/// <param name="value">編集対象の文字列</param>
/// <returns>編集後の文字列</returns>
string Edit(string value);
/// <summary>
/// エディタ名
/// </summary>
string Name { get; }
/// <summary>
/// エディタのコメント
/// </summary>
string Comment { get; }
}
}
=============================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm.Editors
{
/// <summary>
/// 1行単位で編集するベースクラス
/// </summary>
public abstract class LineEditorBase
: ITextEditor
{
#region コンストラクタ
/// <summary>
/// コンストラクタ
/// </summary>
public LineEditorBase()
{
}
#endregion
//====================
// PUBLIC
//====================
#region 編集する
/// <summary>
/// 編集する
/// </summary>
/// <param name="value">編集対象の文字列</param>
/// <returns>編集後の文字列</returns>
public string Edit(string value)
{
if (string.IsNullOrEmpty(value) == true) { return string.Empty; }
List<string> resultList = new List<string>();
// 行リストに変換
List<string> lineList = new List<string>(value.Replace(@"\r\n", @"\n").Split('\n'));
lineList.ForEach(
delegate(string line)
{
resultList.Add(this.EditLine(line));
}
);
return string.Join(Environment.NewLine, resultList.ToArray());
}
#endregion
#region エディタ名
/// <summary>
/// エディタ名
/// </summary>
public abstract string Name { get; }
#endregion
#region エディタのコメント
/// <summary>
/// エディタのコメント
/// </summary>
public abstract string Comment { get; }
#endregion
//====================
// PROTECTED ABSTRACT
//====================
#region 行編集
/// <summary>
/// 行編集
/// </summary>
/// <param name="line">1行の文字列</param>
/// <returns>編集結果文字列</returns>
protected abstract string EditLine(string line);
#endregion
}
}
=============================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Kbt.Tool.TextEditHelpTool.TextEditHelpForm.Editors
{
/// <summary>
/// SELECT文作成
/// </summary>
public class SqlSelectEditor
: LineEditorBase
{
#region コンストラクタ
/// <summary>
/// コンストラクタ
/// </summary>
public SqlSelectEditor()
:base()
{
}
#endregion
//====================
// PUBLIC OVERRIDE
//====================
#region エディタ名
/// <summary>
/// エディタ名
/// </summary>
public override string Name
{
get
{
return "SELECT文作成";
}
}
#endregion
#region エディタのコメント
/// <summary>
/// エディタのコメント
/// </summary>
public override string Comment
{
get
{
return "";
}
}
#endregion
//====================
// PROTECTED OVERRIDE
//====================
#region 行編集
/// <summary>
/// 行編集
/// </summary>
/// <param name="line">1行の文字列</param>
/// <returns>編集結果文字列</returns>
protected override string EditLine(string line)
{
string result = line.ToUpper().Trim();
const string ymdFormat = "TO_CHAR({0}, 'YYYY/MM/DD')";
const string ymFormat = "TO_CHAR({0}, 'YYYY/MM')";
const string suryoFormat = "NVL({0}, 0)";
if (Regex.IsMatch(result, "_YMD$") == true)
{
result = string.Format(ymdFormat, result);
}
else if (Regex.IsMatch(result, "_YM$") == true)
{
result = string.Format(ymFormat, result);
}
else if (Regex.IsMatch(result, "_S$") == true)
{
result = string.Format(suryoFormat, result);
}
return result;
}
#endregion
}
}
最終更新:2010年07月04日 19:31