Skip to content

Commit bda4f9d

Browse files
committed
Replace MS event handler with our own for variables expression editor.
1 parent 008865f commit bda4f9d

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

SSIS/FindVariables.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ private void ScanProperties(IDTSPropertiesProvider provider, TreeNode parent)
670670
VariableFoundEventArgs foundArgument = new VariableFoundEventArgs();
671671
foundArgument.Match = match;
672672
OnRaiseVariableFound(foundArgument);
673-
AddNode(properties, propertyName, GetImageIndex(IconKeyProperty), new PropertyInfo(property, value), true);
673+
AddNode(properties, propertyName, GetImageIndex(IconKeyProperty), new DisplayProperty(property, value), true);
674674
}
675675
}
676676
}
@@ -894,7 +894,7 @@ private void PropertyAsExpressionMatch(DtsProperty property, string expression,
894894
{
895895
TreeNode propertiesNode = parent.Nodes["Properties"];
896896
System.Diagnostics.Debug.Assert(!(parent != null && propertiesNode == null), "Properties node doesn't exist when it should already. We will lose this property match. Find the Properties node.");
897-
AddNode(propertiesNode, property.Name, GetImageIndex(IconKeyVariableExpression), new PropertyInfo(property, expression), true);
897+
AddNode(propertiesNode, property.Name, GetImageIndex(IconKeyVariableExpression), new DisplayProperty(property, expression), true);
898898
}
899899
}
900900
}
@@ -945,9 +945,9 @@ public PropertyExpression(string name, string expression, Type type)
945945
/// DtsProperty doesn't include the value, hence we use this wrapper for the TreeView tag object
946946
/// </summary>
947947
[DisplayName("Property")]
948-
public class PropertyInfo
948+
public class DisplayProperty
949949
{
950-
public PropertyInfo(DtsProperty property, object value)
950+
public DisplayProperty(DtsProperty property, object value)
951951
{
952952
this.Name = property.Name;
953953
this.Value = value;

SSIS/VariablesWindowPlugin.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ namespace BIDSHelper.SSIS
1414
using Microsoft.SqlServer.Dts.Design;
1515
using Microsoft.SqlServer.Management.UI.Grid;
1616

17-
#if KATMAI || DENALI || SQL2014
17+
#if KATMAI || DENALI || SQL2014
1818
using IDTSInfoEventsXX = Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSInfoEvents100;
19-
#else
19+
using System.Reflection;
20+
#else
2021
using IDTSInfoEventsXX = Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSInfoEvents90;
21-
#endif
22-
22+
#endif
23+
2324
public partial class VariablesWindowPlugin : BIDSHelperWindowActivatedPluginBase
2425
{
2526
/// <summary>
@@ -125,6 +126,23 @@ private void HookupVariablesWindow(Window GotFocus)
125126
// If buttons already added, no need to do it again so exit
126127
if (this.moveCopyButton != null && toolbar.Buttons.Contains(this.moveCopyButton)) return;
127128

129+
#if DENALI || SQL2014
130+
// When you clock the edit expression ellipsis button in the variables grid, we want to use our own expression editor, not the MS one.
131+
// The following section removes their event handler and adds our own
132+
// Get the type of the variables grid, and get the MouseButtonClicked clicked event info
133+
// Then get the private dlgGridControl1_MouseButtonClicked event handler method, and get an instance delegate
134+
Type gridType = grid.GetType();
135+
System.Reflection.EventInfo eventInfo = gridType.GetEvent("MouseButtonClicked");
136+
Type handlerType = eventInfo.EventHandlerType;
137+
MethodInfo legacyMethod = variablesToolWindowControl.GetType().GetMethod("dlgGridControl1_MouseButtonClicked", BindingFlags.NonPublic | BindingFlags.Instance);
138+
Delegate del = Delegate.CreateDelegate(handlerType, variablesToolWindowControl, legacyMethod, false);
139+
140+
// Finally remove the interal MS event handler from the event, and add our own
141+
eventInfo.RemoveEventHandler(grid, del);
142+
grid.MouseButtonClicked += grid_MouseButtonClicked;
143+
#endif
144+
145+
// Now build tool bar buttons and add them
128146
ToolBarButton separator = new ToolBarButton();
129147
separator.Style = ToolBarButtonStyle.Separator;
130148
toolbar.Buttons.Add(separator);
@@ -180,6 +198,16 @@ private void HookupVariablesWindow(Window GotFocus)
180198
}
181199
}
182200

201+
private void grid_MouseButtonClicked(object sender, MouseButtonClickedEventArgs args)
202+
{
203+
// Fragile, as relies on hardcoded index. We are trying to replicate Microsoft.DataTransformationServices.Design.VariablesToolWindow.dlgGridControl1_MouseButtonClicked method check
204+
if (args.Button == MouseButtons.Left && args.ColumnIndex == 6)
205+
{
206+
// Dumbass, the args.RowIndex is a long, but all the grid methods that accept a row index are int!
207+
EditExpressionButtonClick((int)args.RowIndex, args.ColumnIndex);
208+
}
209+
}
210+
183211
//only way I could find to monitor when row data in the grid changes
184212
void grid_Invalidated(object sender, InvalidateEventArgs e)
185213
{
@@ -358,16 +386,21 @@ void toolbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
358386

359387

360388
private void EditExpressionButtonClick()
389+
{
390+
int selectedRow;
391+
int selectedCol;
392+
grid.GetSelectedCell(out selectedRow, out selectedCol);
393+
394+
EditExpressionButtonClick(selectedRow, selectedCol);
395+
}
396+
397+
private void EditExpressionButtonClick(int selectedRow, int selectedCol)
361398
{
362399
try
363400
{
364401
Package package = GetCurrentPackage();
365402
if (package == null) return;
366403

367-
int selectedRow;
368-
int selectedCol;
369-
grid.GetSelectedCell(out selectedRow, out selectedCol);
370-
371404
if (selectedRow < 0) return;
372405

373406
Variable variable = GetVariableForRow(selectedRow);

0 commit comments

Comments
 (0)