1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
6
using System.Globalization;
7
using System.Reflection;
8
using System.Windows.Forms;
9
using System.Windows.Forms.Integration;
11
using ICSharpCode.Core;
12
using ICSharpCode.Data.Core.Interfaces;
13
using ICSharpCode.Data.Core.UI.UserControls;
14
using ICSharpCode.SharpDevelop;
17
namespace ICSharpCode.Reports.Addin.ReportWizard
20
/// Description of PullModelPanel.
22
public class PullModelPanel : AbstractWizardPanel
24
private System.ComponentModel.IContainer components;
25
private System.Windows.Forms.ToolTip toolTip1;
26
private System.Windows.Forms.Label label1;
27
private System.Windows.Forms.TextBox txtSqlString;
28
private System.Windows.Forms.Label label3;
29
private bool firstDrag;
30
private string connectionString;
31
private ReportStructure reportStructure;
32
private Properties customizer;
33
private IDatabaseObjectBase currentNode;
34
private ElementHost databasesTreeHost;
35
private DatabasesTreeView databasesTree;
47
public PullModelPanel()
49
InitializeComponent();
51
base.EnableFinish = false;
52
base.EnableNext = false;
53
base.EnableCancel = true;
54
this.firstDrag = true;
55
base.IsLastPanel = false;
56
this.txtSqlString.Enabled = false;
58
this.databasesTreeHost = new ElementHost() { Dock = DockStyle.Fill };
59
this.databasesTree = new DatabasesTreeView();
60
this.databasesTree.SelectedItemChanged += new System.Windows.RoutedPropertyChangedEventHandler<object>(databasesTree_SelectedItemChanged);
61
this.databasesTreeHost.Child = this.databasesTree;
62
this.label2.Controls.Add(databasesTreeHost);
67
private void Localize() {
68
this.label1.Text = ResourceService.GetString("SharpQuery.Label.SharpQuery");
69
this.label3.Text = ResourceService.GetString("SharpReport.Wizard.PullModel.CommandText");
70
this.toolTip1.SetToolTip(this.txtSqlString,
71
ResourceService.GetString("SharpReport.Wizard.PullModel.CommandText.ToolTip"));
77
public override bool ReceiveDialogMessage(DialogMessage message)
79
if (customizer == null) {
80
customizer = (Properties)base.CustomizationObject;
81
reportStructure = (ReportStructure)customizer.Get("Generator");
84
if (message == DialogMessage.Next) {
85
customizer.Set("SqlString", this.txtSqlString.Text.Trim());
86
reportStructure.SqlString = this.txtSqlString.Text.Trim();
87
reportStructure.ConnectionString = connectionString;
88
base.EnableFinish = true;
97
private void TxtSqlStringChanged (object sender,EventArgs e) {
99
if ((this.txtSqlString.Text.Length == 0) && (this.connectionString.Length > 0)) {
100
base.EnableNext = false;
102
base.EnableNext = true;
108
private void TxtSqlStringDragEnter(object sender, System.Windows.Forms.DragEventArgs e){
109
// Handle the Drag effect when the listbox is entered
110
if (e.Data.GetFormats().Length > 0)
112
string draggedFormat = e.Data.GetFormats()[0];
114
String str = String.Format("drag {0}",draggedFormat);
115
System.Diagnostics.Trace.WriteLine(str);
117
Type draggedType = null;
119
// I'm doing this ugly thing because we are checking if the IDatabaseObjectBase is implemented,
120
// obviously Microsoft hasn't really considered using interfaces or base classes for drag and drop
121
AppDomain.CurrentDomain.GetAssemblies().ForEach(assembly =>
123
if (draggedType == null && assembly.GetName().Name == "ICSharpCode.Data.Core")
124
draggedType = assembly.GetType(draggedFormat);
127
if (draggedType != null && draggedType.GetInterface("IDatabaseObjectBase") != null)
129
e.Effect = DragDropEffects.Copy;
134
e.Effect = DragDropEffects.None;
138
private void TxtSqlStringDragDrop(object sender, System.Windows.Forms.DragEventArgs e){
139
if (firstDrag == true) {
140
this.txtSqlString.Clear();
144
// Drag and drop isn't working via e.Data.GetData, so I'm using reflection here - took me a lot of time to figure out how this works...
145
// Still don't know why they implemented dnd so buggy and uncomfortable...
146
string draggedFormat = e.Data.GetFormats()[0];
147
IDatabaseObjectBase draggedObject = null;
149
if (e.Data.GetDataPresent(draggedFormat))
151
object tempDraggedObject = null;
153
info = e.Data.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);
154
tempDraggedObject = info.GetValue(e.Data);
155
info = tempDraggedObject.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);
156
System.Windows.DataObject dataObject = info.GetValue(tempDraggedObject) as System.Windows.DataObject;
157
draggedObject = dataObject.GetData(draggedFormat) as IDatabaseObjectBase;
160
switch (CheckCurrentNode(draggedObject))
162
case NodeType.TableImage:
163
// we insert Select * from.... otherwise we have to scan
164
//the whole string for incorrect columnNames
165
this.txtSqlString.Clear();
166
ITable table = draggedObject as ITable;
167
this.txtSqlString.Text = "SELECT * FROM " + table.Name;
168
reportStructure.CommandType = CommandType.Text;
169
reportStructure.IDatabaseObjectBase = table;
172
case NodeType.ColumnImage:
173
IColumn column = draggedObject as IColumn;
174
string colName = column.Name;
176
if (this.txtSqlString.Text.Length == 0)
178
this.txtSqlString.AppendText("SELECT ");
179
this.txtSqlString.AppendText(colName);
182
else if (this.txtSqlString.Text.ToUpper(CultureInfo.InvariantCulture).IndexOf("where", StringComparison.OrdinalIgnoreCase) > 0)
184
this.txtSqlString.AppendText(colName + " = ?");
188
this.txtSqlString.AppendText(", ");
189
this.txtSqlString.AppendText(colName);
191
reportStructure.CommandType = CommandType.Text;
192
if (reportStructure.IDatabaseObjectBase == null)
194
reportStructure.IDatabaseObjectBase = column;
198
case NodeType.ProcedureImage:
199
this.txtSqlString.Clear();
201
// we can't use the dragobject because it returns an string like 'EXECUTE ProcName'
202
IProcedure procedure = draggedObject as IProcedure;
203
this.txtSqlString.Text = "[" + procedure.Name + "]";
204
reportStructure.CommandType = CommandType.StoredProcedure;
205
reportStructure.IDatabaseObjectBase = procedure;
210
base.EnableNext = true;
214
private void databasesTree_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
216
if (e.NewValue is IDatabaseObjectBase)
218
IDatabase parentDatabase = e.NewValue as IDatabase;
220
if (parentDatabase == null)
222
IDatabaseObjectBase currentDatabaseObject = e.NewValue as IDatabaseObjectBase;
224
while (parentDatabase == null)
226
if (currentDatabaseObject.Parent == null)
228
else if (currentDatabaseObject.Parent is IDatabase)
230
parentDatabase = currentDatabaseObject.Parent as IDatabase;
234
currentDatabaseObject = currentDatabaseObject.Parent;
239
if (parentDatabase != null)
240
this.currentNode = parentDatabase;
242
if (this.currentNode is IDatabase)
244
if (parentDatabase != null)
246
this.connectionString = "Provider=" + parentDatabase.Datasource.DatabaseDriver.ODBCProviderName + ";" + parentDatabase.ConnectionString;
247
this.txtSqlString.Enabled = true;
250
this.txtSqlString.Text = string.Empty;
257
this.EnableNext = false;
262
// check witch type of node we dragg
263
private static NodeType CheckCurrentNode (IDatabaseObjectBase node) {
265
if (node is IColumn) {
266
enm = NodeType.ColumnImage;
267
} else if (node is ITable) {
268
enm = NodeType.TableImage;
269
} else if (node is IProcedure) {
270
enm = NodeType.ProcedureImage;
271
} else if (node is IView) {
272
enm = NodeType.ViewImage;
275
enm = NodeType.NodeError;
283
#region Windows Forms Designer generated code
285
/// This method is required for Windows Forms designer support.
286
/// Do not change the method connectiontents inside the source code editor. The Forms designer might
287
/// not be able to load this method if it was changed manually.
289
private void InitializeComponent() {
290
this.components = new System.ComponentModel.Container();
291
this.label3 = new System.Windows.Forms.Label();
292
this.txtSqlString = new System.Windows.Forms.TextBox();
293
this.label1 = new System.Windows.Forms.Label();
294
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
295
this.textBox1 = new System.Windows.Forms.TextBox();
296
this.label2 = new System.Windows.Forms.Label();
297
this.SuspendLayout();
301
this.label3.Location = new System.Drawing.Point(8, 200);
302
this.label3.Name = "label3";
303
this.label3.Size = new System.Drawing.Size(104, 24);
304
this.label3.TabIndex = 11;
308
this.txtSqlString.AllowDrop = true;
309
this.txtSqlString.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
310
| System.Windows.Forms.AnchorStyles.Right)));
311
this.txtSqlString.Location = new System.Drawing.Point(120, 192);
312
this.txtSqlString.Multiline = true;
313
this.txtSqlString.Name = "txtSqlString";
314
this.txtSqlString.Size = new System.Drawing.Size(264, 144);
315
this.txtSqlString.TabIndex = 8;
316
this.txtSqlString.TextChanged += new System.EventHandler(this.TxtSqlStringChanged);
317
this.txtSqlString.DragDrop += new System.Windows.Forms.DragEventHandler(this.TxtSqlStringDragDrop);
318
this.txtSqlString.DragEnter += new System.Windows.Forms.DragEventHandler(this.TxtSqlStringDragEnter);
322
this.label1.Location = new System.Drawing.Point(8, 16);
323
this.label1.Name = "label1";
324
this.label1.Size = new System.Drawing.Size(104, 16);
325
this.label1.TabIndex = 9;
329
this.textBox1.Location = new System.Drawing.Point(0, 0);
330
this.textBox1.Name = "textBox1";
331
this.textBox1.Size = new System.Drawing.Size(100, 20);
332
this.textBox1.TabIndex = 0;
336
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
337
| System.Windows.Forms.AnchorStyles.Right)));
338
this.label2.Location = new System.Drawing.Point(116, 16);
339
this.label2.Name = "label2";
340
this.label2.Size = new System.Drawing.Size(267, 159);
341
this.label2.TabIndex = 12;
342
this.label2.Text = "label2";
346
this.Controls.Add(this.label2);
347
this.Controls.Add(this.label3);
348
this.Controls.Add(this.label1);
349
this.Controls.Add(this.txtSqlString);
350
this.Name = "PullModelPanel";
351
this.Size = new System.Drawing.Size(432, 344);
352
this.ResumeLayout(false);
353
this.PerformLayout();
355
private System.Windows.Forms.Label label2;
356
private System.Windows.Forms.TextBox textBox1;