/*********************************************************************************************
 * Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: FieldTypeDefFrm.cs $     $Revision: 11 $
 * $Author: Kathy $   $Date: 5/21/03 12:49p $
 *
 * $History: FieldTypeDefFrm.cs $
 * 
 * *****************  Version 11  *****************
 * User: Kathy        Date: 5/21/03    Time: 12:49p
 * Updated in $/EXE/RefObj/ROEditor
 * B2003-041: edit of fieldname didn't always save
 * 
 * *****************  Version 10  *****************
 * User: Kathy        Date: 12/10/02   Time: 2:25p
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname special chars
 * 
 * *****************  Version 9  *****************
 * User: Kathy        Date: 12/02/02   Time: 8:29a
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname replace chars
 * 
 * *****************  Version 8  *****************
 * User: Kathy        Date: 12/02/02   Time: 6:17a
 * Updated in $/EXE/RefObj/ROEditor
 * added arg and close form
 * 
 * *****************  Version 7  *****************
 * User: Kathy        Date: 9/27/02    Time: 1:13p
 * Updated in $/EXE/RefObj/ROEditor
 * fix digit as first char in fieldname
 * 
 * *****************  Version 6  *****************
 * User: Kathy        Date: 9/25/02    Time: 2:30p
 * Updated in $/EXE/RefObj/ROEditor
 * multitxt->variable
 * 
 * *****************  Version 5  *****************
 * User: Kathy        Date: 9/25/02    Time: 9:56a
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 9/11/02    Time: 1:15p
 * Updated in $/EXE/RefObj/ROEditor
 * vlnxml
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 9/05/02    Time: 12:41p
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 8/30/02    Time: 9:43a
 * Updated in $/EXE/RefObj/ROEditor
 * image def
 * 
 * *****************  Version 1  *****************
 * User: Jsj          Date: 8/23/02    Time: 3:34p
 * Created in $/EXE/RefObj/ROEditor
 *********************************************************************************************/


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.Text;
using RODBInterface;
using ROFields;

namespace ROEditor
{
	/// <summary>
	/// Summary description for FieldType.
	/// </summary>
	///

	public class FieldTypeDefFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblFldName;
		private System.Windows.Forms.TextBox tbFieldName;
		private System.Windows.Forms.GroupBox gbFldType;
		private System.Windows.Forms.RadioButton rbSingleLine;
		private System.Windows.Forms.RadioButton rbFormSingleLine;
		private System.Windows.Forms.RadioButton rbVariableTxt;
		private System.Windows.Forms.RadioButton rbTable;
		private System.Windows.Forms.RadioButton rbXYPlot;
		private System.Windows.Forms.RadioButton rbImage;
		private System.Windows.Forms.RadioButton rbCombination;
		private System.Windows.Forms.RadioButton rbMultiple;
		private System.Windows.Forms.Button btnContinue;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private ROField myrof;
		private RODB myrodb;
		private VlnXmlElement myelem;
		private uint original_type=0;
		private string origname;
		private uint editlevel;
		private ArrayList avlist;	// available list from rodef form
		private ArrayList iulist;  // inuse list from rodef form
		public FieldTypeDefFrm(ROField rof, RODB rodb, VlnXmlElement elem, uint elevel, ArrayList alist, ArrayList ilist)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			myrof = rof;
			myrodb = rodb;
			myelem = elem;
			editlevel = elevel;
			avlist = alist;
			iulist = ilist;
			origname = null;
			
			if (rof.GetFieldname != null)   // initialize some values.
			{
				origname = CvtUserFldToFld(rof.GetFieldname);
				original_type = rof.GetFieldType;
				uint ftype = rof.GetFieldType;
				this.tbFieldName.Text = rof.GetFieldname;
				// disable image, because it's only enabled for image type. 
				this.rbImage.Enabled = false;
				if (ftype == (uint)FieldTypes.Combination)
					this.rbCombination.Checked = true;
				else if (ftype == (uint)FieldTypes.FrmtSingleTxt)
					this.rbFormSingleLine.Checked = true;
				else if (ftype == (uint)FieldTypes.Image)
				{
					this.rbImage.Enabled = true;
					this.rbImage.Checked = true;
					// for image - you can't change it's type.
					this.rbCombination.Enabled = false;
					this.rbFormSingleLine.Enabled = false;
					this.rbVariableTxt.Enabled = false;
					this.rbSingleLine.Enabled = false;
					this.rbTable.Enabled = false;
					this.rbXYPlot.Enabled = false;
				}
				else if (ftype == (uint)FieldTypes.VariableTxt)
					this.rbVariableTxt.Checked = true;
				else if (ftype == (uint)FieldTypes.SingleTxt)
					this.rbSingleLine.Checked = true;
				else if (ftype == (uint)FieldTypes.Table)
					this.rbTable.Checked = true;
				else if (ftype == (uint)FieldTypes.XYPlot)
					this.rbXYPlot.Checked = true;
				else
					return;
			}
		}

		private string CvtUserFldToFld(string fldname)
		{
			if (fldname.Length < 2)
				return fldname;
			// a digit cannot start an xml fieldname, prepend a "__" to it.
			string tmp0;
			if (char.IsDigit(fldname,0))
				tmp0 = "__" + fldname;
			else
				tmp0 = fldname;
			// an xml fieldname cannot have a space, change it to a "__"
			string tmpstr = tmp0.Replace(" ","__");
			int len = tmpstr.Length;
			int cnt = 0;

			// this is also our sequence that tells us the follow 3 digits is the ascii number (base 10)
			// of the character we replaced.
			string OKpunch = "-._"; 

			string outstr = "";
			int decval;

			while (cnt < len)
			{
				char tmpchr = tmpstr[cnt];
				if(!char.IsLetterOrDigit(tmpchr)&& (OKpunch.IndexOf(tmpchr) == -1) )
				{
					decval = tmpchr;
					outstr += OKpunch + decval.ToString("D3");
				}
				else
				{
					outstr += tmpchr.ToString();
				}
				cnt++;
			}
			return outstr;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void btnContinue_Click(object sender, System.EventArgs e)
		{
			if (this.tbFieldName.Text == "") 
			{
				MessageBox.Show("Cannot have empty field name.");
				return;
			}

			myrof.SetFieldname(this.tbFieldName.Text);
			uint ftype=0;
			if (this.rbSingleLine.Checked == true) 
				ftype = (uint)FieldTypes.SingleTxt;
			else if (this.rbFormSingleLine.Checked == true)
				ftype = (uint)FieldTypes.FrmtSingleTxt;
			else if (this.rbVariableTxt.Checked == true)
				ftype = (uint)FieldTypes.VariableTxt;
			else if (this.rbTable.Checked == true)
				ftype = (uint)FieldTypes.Table;
			else if (this.rbXYPlot.Checked == true)
				ftype = (uint)FieldTypes.XYPlot;
			else if (this.rbImage.Checked == true)
			{
				bool success;
				string savschemapiece;
				string fname=CvtUserFldToFld(this.tbFieldName.Text);
				myrof.SetFieldType((uint)FieldTypes.Image);
				myrof.SetFieldname(fname);
				savschemapiece = myrof.MakeImageSchemaString(this.tbFieldName.Text);
				if (original_type == (uint)FieldTypes.Nil)  // new one.
					success = myrodb.RODB_NewFieldRecord(myrof, myelem, savschemapiece, editlevel, null);
				else     // modification of existing....
					success = myrodb.RODB_UpdateFieldRecord(myrof, myelem, savschemapiece, origname, fname, editlevel, false);
			
				if (success != true)
				{
					return; // DO YET process an error.
				}
				return;
		
			}
			else if (this.rbCombination.Checked == true)
			{
				ftype = (uint)FieldTypes.Combination;
				myrof.SetFieldType(ftype);
				FieldCombFrm comb = new FieldCombFrm(myrof, myrodb, myelem, editlevel, origname, avlist, iulist);
				comb.ShowDialog();
				if (comb.DialogResult == DialogResult.OK) 
				{
				}
				return;
			}
			else 
			{
				MessageBox.Show("Must define a field type.");
				return;
			}
			myrof.SetFieldType(ftype);
			FieldTextFrm ftxt = new FieldTextFrm(myrof, myrodb, myelem, original_type, editlevel, origname, avlist, iulist);
			ftxt.ShowDialog();
			this.Close();
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lblFldName = new System.Windows.Forms.Label();
			this.tbFieldName = new System.Windows.Forms.TextBox();
			this.gbFldType = new System.Windows.Forms.GroupBox();
			this.rbMultiple = new System.Windows.Forms.RadioButton();
			this.rbCombination = new System.Windows.Forms.RadioButton();
			this.rbImage = new System.Windows.Forms.RadioButton();
			this.rbXYPlot = new System.Windows.Forms.RadioButton();
			this.rbTable = new System.Windows.Forms.RadioButton();
			this.rbVariableTxt = new System.Windows.Forms.RadioButton();
			this.rbFormSingleLine = new System.Windows.Forms.RadioButton();
			this.rbSingleLine = new System.Windows.Forms.RadioButton();
			this.btnContinue = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.gbFldType.SuspendLayout();
			this.SuspendLayout();
			// 
			// lblFldName
			// 
			this.lblFldName.Location = new System.Drawing.Point(16, 8);
			this.lblFldName.Name = "lblFldName";
			this.lblFldName.Size = new System.Drawing.Size(88, 16);
			this.lblFldName.TabIndex = 0;
			this.lblFldName.Text = "Field Name:";
			// 
			// tbFieldName
			// 
			this.tbFieldName.Location = new System.Drawing.Point(120, 8);
			this.tbFieldName.Name = "tbFieldName";
			this.tbFieldName.Size = new System.Drawing.Size(368, 22);
			this.tbFieldName.TabIndex = 1;
			this.tbFieldName.Text = "";
			// 
			// gbFldType
			// 
			this.gbFldType.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rbMultiple,
																					this.rbCombination,
																					this.rbImage,
																					this.rbXYPlot,
																					this.rbTable,
																					this.rbVariableTxt,
																					this.rbFormSingleLine,
																					this.rbSingleLine});
			this.gbFldType.Location = new System.Drawing.Point(24, 40);
			this.gbFldType.Name = "gbFldType";
			this.gbFldType.Size = new System.Drawing.Size(464, 120);
			this.gbFldType.TabIndex = 2;
			this.gbFldType.TabStop = false;
			this.gbFldType.Text = "Field Type";
			// 
			// rbMultiple
			// 
			this.rbMultiple.Enabled = false;
			this.rbMultiple.Location = new System.Drawing.Point(248, 96);
			this.rbMultiple.Name = "rbMultiple";
			this.rbMultiple.Size = new System.Drawing.Size(128, 16);
			this.rbMultiple.TabIndex = 7;
			this.rbMultiple.Text = "Multiple Field";
			// 
			// rbCombination
			// 
			this.rbCombination.Location = new System.Drawing.Point(248, 72);
			this.rbCombination.Name = "rbCombination";
			this.rbCombination.Size = new System.Drawing.Size(128, 16);
			this.rbCombination.TabIndex = 6;
			this.rbCombination.Text = "Combination Field";
			// 
			// rbImage
			// 
			this.rbImage.Location = new System.Drawing.Point(248, 48);
			this.rbImage.Name = "rbImage";
			this.rbImage.Size = new System.Drawing.Size(128, 16);
			this.rbImage.TabIndex = 5;
			this.rbImage.Text = "Image";
			// 
			// rbXYPlot
			// 
			this.rbXYPlot.Location = new System.Drawing.Point(248, 24);
			this.rbXYPlot.Name = "rbXYPlot";
			this.rbXYPlot.Size = new System.Drawing.Size(128, 16);
			this.rbXYPlot.TabIndex = 4;
			this.rbXYPlot.Text = "X/Y Plot";
			// 
			// rbTable
			// 
			this.rbTable.Location = new System.Drawing.Point(24, 96);
			this.rbTable.Name = "rbTable";
			this.rbTable.Size = new System.Drawing.Size(128, 16);
			this.rbTable.TabIndex = 3;
			this.rbTable.Text = "Table";
			// 
			// rbVariableTxt
			// 
			this.rbVariableTxt.Location = new System.Drawing.Point(24, 72);
			this.rbVariableTxt.Name = "rbVariableTxt";
			this.rbVariableTxt.Size = new System.Drawing.Size(160, 16);
			this.rbVariableTxt.TabIndex = 2;
			this.rbVariableTxt.Text = "Variable Length Text";
			// 
			// rbFormSingleLine
			// 
			this.rbFormSingleLine.Location = new System.Drawing.Point(24, 48);
			this.rbFormSingleLine.Name = "rbFormSingleLine";
			this.rbFormSingleLine.Size = new System.Drawing.Size(184, 16);
			this.rbFormSingleLine.TabIndex = 1;
			this.rbFormSingleLine.Text = "Formatted Single Line Text";
			// 
			// rbSingleLine
			// 
			this.rbSingleLine.Location = new System.Drawing.Point(24, 24);
			this.rbSingleLine.Name = "rbSingleLine";
			this.rbSingleLine.Size = new System.Drawing.Size(128, 16);
			this.rbSingleLine.TabIndex = 0;
			this.rbSingleLine.Text = "Single Line Text";
			// 
			// btnContinue
			// 
			this.btnContinue.Location = new System.Drawing.Point(248, 176);
			this.btnContinue.Name = "btnContinue";
			this.btnContinue.Size = new System.Drawing.Size(104, 24);
			this.btnContinue.TabIndex = 3;
			this.btnContinue.Text = "Continue ...";
			this.btnContinue.Click += new System.EventHandler(this.btnContinue_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(384, 176);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(104, 24);
			this.btnCancel.TabIndex = 4;
			this.btnCancel.Text = "Cancel";
			// 
			// FieldTypeDefFrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(512, 221);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnContinue,
																		  this.gbFldType,
																		  this.tbFieldName,
																		  this.lblFldName});
			this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "FieldTypeDefFrm";
			this.Text = " Field Type Selection";
			this.gbFldType.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
	}
}
