Guest User

Untitled

a guest
Nov 23rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.51 KB | None | 0 0
  1.     internal class MetaFieldTable
  2.     {
  3.         public MetaFieldTable(DataGridView dataGridView)
  4.         {
  5.             this.dataGridView = dataGridView;
  6.  
  7.             dataGridView.AllowDrop = false;
  8.             dataGridView.AllowUserToAddRows = false;
  9.             dataGridView.AllowUserToDeleteRows = false;
  10.             dataGridView.AllowUserToOrderColumns = false;
  11.             dataGridView.AllowUserToResizeRows = false;
  12.  
  13.             DataGridViewTextBoxColumn metaFieldColumn = new DataGridViewTextBoxColumn()
  14.             {
  15.                 HeaderText = "MetaFields",
  16.                 ReadOnly = true,
  17.                 SortMode = DataGridViewColumnSortMode.NotSortable
  18.             };
  19.  
  20.             this.dataGridView.Columns.Add(metaFieldColumn);
  21.  
  22.             DataGridViewAllocationColumn allocationColumn = new DataGridViewAllocationColumn()
  23.             {
  24.                 HeaderText = "Allocation",
  25.                 ReadOnly = true,
  26.                 SortMode = DataGridViewColumnSortMode.NotSortable,
  27.                 AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
  28.             };
  29.  
  30.             this.dataGridView.Columns.Add(allocationColumn);
  31.         }
  32.  
  33.         private DataGridView dataGridView;
  34.  
  35.         public void CreateMetaFieldTable(MetaField[] metaFields, ReleaseSetupData releaseSetupData)
  36.         {
  37.             //for (int i = 0; i < dataGridView.Rows.Count; i++)
  38.             //{
  39.             //    double metaFieldID = (dataGridView.Rows[i].Cells[0].Tag as MetaField).ID;
  40.  
  41.             //    if (metaFields.Any(field => field.ID == metaFieldID))
  42.             //        metaFields = metaFields.Where(field => field.ID != metaFieldID).ToArray();
  43.             //    else
  44.             //        dataGridView.Rows.Remove(dataGridView.Rows[i]);
  45.             //}
  46.  
  47.             dataGridView.Rows.Clear();
  48.  
  49.             for (int i = 0; i < metaFields.Length; i++)
  50.             {
  51.                 MetaField currentMetaField = metaFields[i];
  52.  
  53.                 dataGridView.Rows.Add(currentMetaField.Name, null);
  54.  
  55.                 DataGridViewRow newRow = dataGridView.Rows[i];
  56.  
  57.                 DataGridViewCell metaFieldCell = newRow.Cells[0];
  58.                 metaFieldCell.Tag = currentMetaField;
  59.  
  60.                 (newRow.Cells[1] as DataGridViewAllocationCell).Initialize(releaseSetupData);
  61.             }
  62.         }
  63.  
  64.         private class DataGridViewAllocationColumn : DataGridViewButtonColumn
  65.         {
  66.             public DataGridViewAllocationColumn()
  67.             {
  68.                 CellTemplate = new DataGridViewAllocationCell();
  69.             }
  70.  
  71.             public override object Clone()
  72.             {
  73.                 return base.Clone() as DataGridViewAllocationColumn;
  74.             }
  75.         }
  76.  
  77.         private class DataGridViewAllocationCell : DataGridViewButtonCell
  78.         {
  79.             public void Initialize(ReleaseSetupData releaseSetupData)
  80.             {
  81.                 FieldName = NOTHING_SELECTED_TEXT;
  82.                 FieldType = AllocationFieldType.None;
  83.  
  84.                 contextMenu = new ContextMenuStrip();
  85.  
  86.                 ToolStripMenuItem clearSelectionItem = RegisterMenuItem(NOTHING_SELECTED_TEXT);
  87.                 clearSelectionItem.Click += (object sender, EventArgs e) => MenuItem_OnClick(sender, e, NOTHING_SELECTED_TEXT, AllocationFieldType.None);
  88.  
  89.                 contextMenu.Items.Add(new ToolStripSeparator());
  90.  
  91.                 ToolStripMenuItem menuItemIndexField = RegisterMenuItem("Indexfields");
  92.                 foreach (IndexField field in releaseSetupData.IndexFields)
  93.                 {
  94.                     RegisterSubMenuItem(menuItemIndexField, field.Name, AllocationFieldType.IndexField);
  95.                 }
  96.  
  97.                 ToolStripMenuItem menuItemBatchField = RegisterMenuItem("Batchfields");
  98.                 foreach (BatchField field in releaseSetupData.BatchFields)
  99.                 {
  100.                     RegisterSubMenuItem(menuItemBatchField, field.Name, AllocationFieldType.BatchField);
  101.                 }
  102.             }
  103.  
  104.             private ContextMenuStrip contextMenu;
  105.  
  106.             private const string BUTTON_TEXT = "...";
  107.             private const string NOTHING_SELECTED_TEXT = "[ Nothing Selected ]";
  108.  
  109.             public string FieldName { get; private set; }
  110.             public AllocationFieldType FieldType { get; private set; }
  111.  
  112.             private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
  113.  
  114.             private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }
  115.  
  116.             private ToolStripMenuItem RegisterMenuItem(string itemName)
  117.             {
  118.                 return contextMenu.Items.Add(itemName) as ToolStripMenuItem;
  119.             }
  120.  
  121.             private void RegisterSubMenuItem(ToolStripMenuItem menuItem, string fieldName, AllocationFieldType fieldType)
  122.             {
  123.                 menuItem.DropDownItems.Add(fieldName, null, (object sender, EventArgs e) => MenuItem_OnClick(sender, e, fieldName, fieldType));
  124.             }
  125.  
  126.             private void MenuItem_OnClick(object sender, EventArgs e, string fieldName, AllocationFieldType fieldType)
  127.             {
  128.                 FieldName = fieldName;
  129.                 FieldType = fieldType;
  130.                 DataGridView.Refresh();
  131.             }
  132.  
  133.             protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
  134.             {
  135.                 base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
  136.                 Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
  137.                 Rectangle cellRectangle = GetContentBounds(rowIndex);
  138.                 Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
  139.                 cellRectangle.Offset(displayRectangle.Location);
  140.                 base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
  141.                 TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
  142.             }
  143.  
  144.             protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
  145.             {
  146.                 Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
  147.                 return new Rectangle(rectangle.Width - rectangle.Height, rectangle.Top, rectangle.Height, rectangle.Height);
  148.             }
  149.  
  150.             protected override void OnContentClick(DataGridViewCellEventArgs e)
  151.             {
  152.                 base.OnContentClick(e);
  153.                 Rectangle contentRectangle = GetContentBounds(e.RowIndex);
  154.                 Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
  155.                 Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
  156.                 contextMenu.Show(DataGridView, location);
  157.             }
  158.         }
  159.     }
Add Comment
Please, Sign In to add comment