// File: Definition.cs
// Goal: Functionality for definition in table
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace StateTransitionTestCreator
{
public class Definition : Panel, IDefinition
{
public const int DefinitionHeight = 20; // Height of definition in pixels
public const int WidthDescription = 170;
public const int WidthId = 27;
public const int WidthName = 90;
private Color ColorIdleBackground = Color.White;
private Color ColorIndicationBackground = Color.Yellow;
private ContextMenuStrip contextMenu;
private ToolStripMenuItem contextMenuDelete;
private ToolStripMenuItem contextMenuInsert;
private ToolStripMenuItem contextMenuMoveDown;
private ToolStripMenuItem contextMenuMoveUp;
private int definitionIndex;
private TextBox description;
private Color ColorIdleForeground = Color.Black;
private Color ColorIndicationForeground = Color.Black;
private TextBox labelID;
private TextBox name;
private TableDefinition tableParent;
public Definition(TableDefinition tableDefinition, int index)
{
definitionIndex = index;
tableParent = tableDefinition;
// Context menu
contextMenu = new ContextMenuStrip();
contextMenuMoveUp = new ToolStripMenuItem();
contextMenuMoveDown = new ToolStripMenuItem();
contextMenuInsert = new ToolStripMenuItem();
contextMenuInsert.Enabled = false;
contextMenuDelete = new ToolStripMenuItem();
contextMenu.Items.AddRange(new ToolStripItem[] { contextMenuMoveUp, contextMenuMoveDown, contextMenuInsert, contextMenuDelete });
contextMenu.Name = "contextMenu";
contextMenuMoveUp.Text = "Move up";
contextMenuMoveDown.Text = "Move down";
contextMenuInsert.Text = "Insert definition";
contextMenuDelete.Text = "Delete definition";
contextMenu.MouseLeave += new System.EventHandler(ContextMenuLeave);
contextMenu.MouseEnter += new System.EventHandler(ContextMenuEnter);
contextMenuMoveUp.Click += new System.EventHandler(MoveUpMenuItemClick);
contextMenuMoveDown.Click += new System.EventHandler(MoveDownMenuItemClick);
contextMenuInsert.Click += new System.EventHandler(InsertMenuItemClick);
contextMenuDelete.Click += new System.EventHandler(DeleteMenuItemClick);
// ID
labelID = new TextBox();
labelID.BackColor = ColorIdleBackground;
labelID.BorderStyle = BorderStyle.FixedSingle;
labelID.Location = new System.Drawing.Point(0, 0);
labelID.ReadOnly = true;
labelID.Size = new System.Drawing.Size(WidthId, DefinitionHeight);
labelID.TabStop = false;
labelID.Text = (definitionIndex + 1).ToString(CultureInfo.InvariantCulture);
labelID.TextAlign = HorizontalAlignment.Center;
labelID.MouseDoubleClick += new MouseEventHandler(DoubleClickLabel);
labelID.MouseEnter += new System.EventHandler(MouseEnterEvent);
labelID.MouseLeave += new System.EventHandler(MouseLeaveEvent);
labelID.ContextMenuStrip = contextMenu;
// Name
name = new TextBox();
name.BorderStyle = BorderStyle.FixedSingle;
name.Location = new System.Drawing.Point(WidthId - 1, 0);
name.Size = new System.Drawing.Size(WidthName, DefinitionHeight);
name.Text = tableParent.Text + "Name" + index.ToString(CultureInfo.InvariantCulture);
name.Enter += new System.EventHandler(NameEnter);
name.Leave += new System.EventHandler(NameLeave);
// name.MouseEnter += new System.EventHandler(MouseEnterEvent);
// name.MouseLeave += new System.EventHandler(MouseLeaveEvent);
name.TextChanged += new System.EventHandler(NameChanged);
// Description
description = new TextBox();
description.BorderStyle = BorderStyle.FixedSingle;
description.Location = new System.Drawing.Point(WidthId + WidthName - 2, 0);
description.Size = new System.Drawing.Size(WidthDescription + 2, DefinitionHeight);
description.Text = tableParent.Text + "Description" + index.ToString(CultureInfo.InvariantCulture);
description.Enter += new System.EventHandler(DescriptionEnter);
description.Leave += new System.EventHandler(DescriptionLeave);
// description.MouseEnter += new System.EventHandler(MouseEnterEvent);
// description.MouseLeave += new System.EventHandler(MouseLeaveEvent);
description.TextChanged += new System.EventHandler(DescriptionChanged);
// General
Controls.Add(labelID);
Controls.Add(name);
Controls.Add(description);
Size = new System.Drawing.Size(WidthId + WidthName + WidthDescription, DefinitionHeight);
}
public string GetDescription()
{
return description.Text;
}
public void SetDescription(string text)
{
description.Text = text;
}
public string GetName()
{
return name.Text;
}
public void SetName(string text)
{
name.Text = text;
}
public void DecreaseID()
{
definitionIndex--;
if (TabIndex > 0)
{
TabIndex--;
}
labelID.Text = (definitionIndex + 1).ToString(CultureInfo.InvariantCulture);
}
public void IncreaseID()
{
definitionIndex++;
TabIndex++;
labelID.Text = (definitionIndex + 1).ToString(CultureInfo.InvariantCulture);
}
private void DoubleClickLabel(object sender, EventArgs e)
{
tableParent.AddDefinition("?", "?");
}
private void UpdatePopupMenu()
{
// TODO Enable popup menu items
contextMenuMoveUp.Enabled = false;
contextMenuMoveDown.Enabled = false;
if (definitionIndex < (tableParent.definitions.Count - 1))
{
contextMenuMoveDown.Enabled = true;
}
if (definitionIndex > 0)
{
contextMenuMoveUp.Enabled = true;
}
contextMenuDelete.Enabled = false;
if (tableParent.definitions.Count > 1)
{
// contextMenuDelete.Enabled = true;
}
}
private void SetIndication(bool flag)
{
if (flag)
{
labelID.BackColor = ColorIndicationBackground;
labelID.ForeColor = ColorIndicationForeground;
name.BackColor = ColorIndicationBackground;
name.ForeColor = ColorIndicationForeground;
description.BackColor = ColorIndicationBackground;
description.ForeColor = ColorIndicationForeground;
}
else
{
labelID.BackColor = ColorIdleBackground;
labelID.ForeColor = ColorIdleForeground;
name.BackColor = ColorIdleBackground;
name.ForeColor = ColorIdleForeground;
description.BackColor = ColorIdleBackground;
description.ForeColor = ColorIdleForeground;
}
}
private void ContextMenuEnter(object sender, EventArgs e)
{
SetIndication(true);
UpdatePopupMenu();
}
private void ContextMenuLeave(object sender, EventArgs e)
{
SetIndication(false);
contextMenu.Hide();
}
private void MouseEnterEvent(object sender, EventArgs e)
{
SetIndication(true);
}
private void MouseLeaveEvent(object sender, EventArgs e)
{
SetIndication(false);
}
private void DeleteMenuItemClick(object sender, EventArgs e)
{
tableParent.DeleteDefinition(definitionIndex);
}
private void InsertMenuItemClick(object sender, EventArgs e)
{
tableParent.InsertDefinition(definitionIndex);
}
private void MoveUpMenuItemClick(object sender, EventArgs e)
{
tableParent.SwapDefinition(definitionIndex - 1);
}
private void MoveDownMenuItemClick(object sender, EventArgs e)
{
tableParent.SwapDefinition(definitionIndex);
}
private void DescriptionChanged(object sender, EventArgs e)
{
tableParent.ContentChanged(definitionIndex);
}
private void NameChanged(object sender, EventArgs e)
{
if (name.TextLength == 0)
{
name.Text = "?";
}
tableParent.ContentChanged(definitionIndex);
}
private void DescriptionEnter(object sender, EventArgs e)
{
description.BackColor = ColorIndicationBackground;
}
private void DescriptionLeave(object sender, EventArgs e)
{
description.BackColor = ColorIdleBackground;
}
private void NameEnter(object sender, EventArgs e)
{
name.BackColor = ColorIndicationBackground;
}
private void NameLeave(object sender, EventArgs e)
{
name.BackColor = ColorIdleBackground;
}
public void TestHandler(int testId)
{
switch (testId)
{
case 0:
MoveDownMenuItemClick(null, null);
break;
case 1:
MoveUpMenuItemClick(null, null);
break;
default:
break;
}
}
}
}