Skip to content

Commit

Permalink
Add coloring feature to nodes (#468)
Browse files Browse the repository at this point in the history
* Add coloring feature to nodes

* Fix wording

* Fix null reference when updating configuration when no connection is open

Co-authored-by: Arnaud Tamaillon <arnaud.tamaillon@younited-credit.fr>
  • Loading branch information
Greybird and Arnaud Tamaillon committed Oct 7, 2020
1 parent 71d8ad6 commit 879578e
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Common/Helpers/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ static MainSettings GetMainSettingsUsingConfiguration(TwoFilesConfiguration conf
resultProperties.ProxyBypassList = configuration.GetStringValue(ConfigurationParameters.ProxyBypassList, string.Empty);
resultProperties.ProxyUserName = configuration.GetStringValue(ConfigurationParameters.ProxyUserName, string.Empty);
resultProperties.ProxyPassword = configuration.GetStringValue(ConfigurationParameters.ProxyPassword, string.Empty);
resultProperties.NodesColors = NodeColorInfo.ParseAll(configuration.GetStringValue(ConfigurationParameters.NodesColors, string.Empty));

return resultProperties;
}
Expand Down
1 change: 1 addition & 0 deletions src/Common/Helpers/ConfigurationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class ConfigurationParameters
public const string ProxyUseDefaultCredentials = "Proxy.UseDefaultCredentials";
public const string ProxyUserName = "Proxy.UserName";
public const string ProxyPassword = "Proxy.Password";
public const string NodesColors = "Colors.Nodes";

#endregion

Expand Down
10 changes: 10 additions & 0 deletions src/Common/Helpers/MainSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace ServiceBusExplorer.Helpers
{
using Utilities.Helpers;

public class MainSettings
{
#region Internal constants
Expand Down Expand Up @@ -76,6 +78,8 @@ public class MainSettings
public string ProxyUserName { get; set; }
public string ProxyPassword { get; set; }

public List<NodeColorInfo> NodesColors { get; set; } = new List<NodeColorInfo>();

#endregion

#region Public Static Methods
Expand Down Expand Up @@ -134,6 +138,8 @@ public void SetDefault()
ProxyBypassList = string.Empty;
ProxyUserName = string.Empty;
ProxyPassword = string.Empty;

NodesColors = new List<NodeColorInfo>();
}

public override bool Equals(object other)
Expand Down Expand Up @@ -180,6 +186,7 @@ public override bool Equals(object other)
if (ProxyBypassList != otherProperties.ProxyBypassList) return false;
if (ProxyUserName != otherProperties.ProxyUserName) return false;
if (ProxyPassword != otherProperties.ProxyPassword) return false;
if (NodesColors.SequenceEqual(otherProperties.NodesColors)) return false;

return true;
}
Expand Down Expand Up @@ -306,6 +313,9 @@ public object GetValue(string setting)

case ConfigurationParameters.ProxyPassword:
return ProxyPassword;

case ConfigurationParameters.NodesColors:
return NodesColors;
}

throw new InvalidOperationException(String.Format("Unexpected value for setting: {0}", setting));
Expand Down
212 changes: 212 additions & 0 deletions src/ServiceBusExplorer/Controls/DataGridViewColorPickerColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using System.Windows.Forms;

namespace ServiceBusExplorer.Controls
{
using System.ComponentModel;
using System.Drawing;

public class DataGridViewColorPickerColumn : DataGridViewColumn
{
public DataGridViewColorPickerColumn()
: base(new ColorCell())
{
}

public override DataGridViewCell CellTemplate
{
get => base.CellTemplate;
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(ColorCell)))
{
throw new InvalidCastException("Must be a ColorCell");
}
base.CellTemplate = value;
}
}
}

public class ColorCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
if (!(DataGridView.EditingControl is ColorEditingControl colorPicker))
{
return;
}
colorPicker.Value = (Color)Value;
}

public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
return formattedValue;
}

public override Type EditType => typeof(ColorEditingControl);

public override Type ValueType => typeof(Color);

public override object DefaultNewRowValue => Color.Empty;

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)
{
paintParts &= ~DataGridViewPaintParts.Focus;
var actualColor = GetActualColor(value);
var colorText = actualColor?.Name ?? string.Empty;
var actualCellStyle = actualColor == null
? cellStyle
: new DataGridViewCellStyle(cellStyle)
{
BackColor = actualColor.Value,
SelectionBackColor = actualColor.Value
};
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, colorText, errorText, actualCellStyle, advancedBorderStyle, paintParts);
}

private static Color? GetActualColor(object value)
{
Color? actualColor;
try
{
actualColor = value.Equals(Color.Empty) ? (Color?) null : (Color) value;
}
catch (Exception)
{
actualColor = null;
}
return actualColor;
}
}

class ColorEditingControl : Button, IDataGridViewEditingControl
{
public Color Value
{
get => BackColor;
set
{
var actualColor = value.Equals(Color.Empty) ? (Color?) null : (Color)value;
if (actualColor.HasValue)
{
BackColor = actualColor.Value;
Text = actualColor.Value.Name;
}
}
}

public ColorEditingControl()
{
TextAlign = ContentAlignment.MiddleLeft;
Click += ColorEditingControl_Click;
}

void ColorEditingControl_Click(object sender, EventArgs e)
{
var colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
if (Value != colorDialog.Color)
{
Value = colorDialog.Color;
EditingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
EditingControlDataGridView.EndEdit();
}
}
}

#region Public Methods

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get => Value;
set
{
if (value != null)
{
Value = (Color) value;
}
}
}

// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
}

// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
// Let the ColorPicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}

// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}

#endregion

#region Public Properties

// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex { get; set; }

// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange => false;

// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView { get; set; }

// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged { get; set; }

// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor => base.Cursor;

#endregion
}
}
Loading

0 comments on commit 879578e

Please sign in to comment.