~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/Reports/ICSharpCode.Reports.Addin/Project/ReportWizard/WizardPanels/PullModelPanel.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Data;
 
6
using System.Globalization;
 
7
using System.Reflection;
 
8
using System.Windows.Forms;
 
9
using System.Windows.Forms.Integration;
 
10
 
 
11
using ICSharpCode.Core;
 
12
using ICSharpCode.Data.Core.Interfaces;
 
13
using ICSharpCode.Data.Core.UI.UserControls;
 
14
using ICSharpCode.SharpDevelop;
 
15
 
 
16
 
 
17
namespace ICSharpCode.Reports.Addin.ReportWizard
 
18
{
 
19
        /// <summary>
 
20
        /// Description of PullModelPanel.
 
21
        /// </summary>
 
22
        public class PullModelPanel : AbstractWizardPanel
 
23
        {
 
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;
 
36
                
 
37
                private enum NodeType
 
38
                {
 
39
                        TableImage,
 
40
                        ViewImage,
 
41
                        ProcedureImage,
 
42
                        ColumnImage,
 
43
                        NodeError
 
44
                }
 
45
                
 
46
                
 
47
                public PullModelPanel()
 
48
                {
 
49
                        InitializeComponent();
 
50
          
 
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;
 
57
 
 
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);
 
63
 
 
64
                        Localize();
 
65
                }       
 
66
        
 
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"));
 
72
                }
 
73
                
 
74
 
 
75
                #region overrides
 
76
                
 
77
                public override bool ReceiveDialogMessage(DialogMessage message)
 
78
                {
 
79
                        if (customizer == null) {
 
80
                                customizer = (Properties)base.CustomizationObject;
 
81
                                reportStructure = (ReportStructure)customizer.Get("Generator");
 
82
                        }
 
83
                        
 
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;
 
89
                        }
 
90
                        return true;
 
91
                }
 
92
                
 
93
                #endregion
 
94
                
 
95
                #region events
 
96
                
 
97
                private  void TxtSqlStringChanged (object sender,EventArgs e) {
 
98
                        
 
99
                        if ((this.txtSqlString.Text.Length == 0) && (this.connectionString.Length > 0)) {
 
100
                                base.EnableNext = false;
 
101
                        } else {
 
102
                                base.EnableNext = true;
 
103
                        }
 
104
                        
 
105
                }
 
106
                
 
107
                
 
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)
 
111
            {
 
112
                string draggedFormat = e.Data.GetFormats()[0];
 
113
                
 
114
                String str = String.Format("drag {0}",draggedFormat);
 
115
                System.Diagnostics.Trace.WriteLine(str);
 
116
 
 
117
                Type draggedType = null;
 
118
 
 
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 =>
 
122
                {
 
123
                    if (draggedType == null && assembly.GetName().Name == "ICSharpCode.Data.Core")
 
124
                        draggedType = assembly.GetType(draggedFormat);
 
125
                });
 
126
 
 
127
                if (draggedType != null && draggedType.GetInterface("IDatabaseObjectBase") != null)
 
128
                {
 
129
                    e.Effect = DragDropEffects.Copy;
 
130
                    return;
 
131
                }
 
132
            }
 
133
                        
 
134
            e.Effect = DragDropEffects.None;
 
135
                }
 
136
                
 
137
                
 
138
                private void TxtSqlStringDragDrop(object sender, System.Windows.Forms.DragEventArgs e){
 
139
                        if (firstDrag == true) {
 
140
                                this.txtSqlString.Clear();
 
141
                                firstDrag = false;
 
142
                        }
 
143
 
 
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;
 
148
 
 
149
                        if (e.Data.GetDataPresent(draggedFormat))
 
150
                        {
 
151
                                object tempDraggedObject = null;
 
152
                                FieldInfo info;
 
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;
 
158
                        }
 
159
 
 
160
                        switch (CheckCurrentNode(draggedObject))
 
161
                        {
 
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;
 
170
                                        break;
 
171
 
 
172
                                case NodeType.ColumnImage:
 
173
                                        IColumn column = draggedObject as IColumn;
 
174
                                        string colName = column.Name;
 
175
                                        
 
176
                                        if (this.txtSqlString.Text.Length == 0)
 
177
                                        {
 
178
                                                this.txtSqlString.AppendText("SELECT ");
 
179
                                                this.txtSqlString.AppendText(colName);
 
180
                                        }
 
181
                                        
 
182
                                        else if (this.txtSqlString.Text.ToUpper(CultureInfo.InvariantCulture).IndexOf("where", StringComparison.OrdinalIgnoreCase) > 0)
 
183
                                        {
 
184
                                                this.txtSqlString.AppendText(colName + " = ?");
 
185
                                        }
 
186
                                        else
 
187
                                        {
 
188
                                                this.txtSqlString.AppendText(", ");
 
189
                                                this.txtSqlString.AppendText(colName);
 
190
                                        }
 
191
                                        reportStructure.CommandType = CommandType.Text;
 
192
                                        if (reportStructure.IDatabaseObjectBase == null)
 
193
                                        {
 
194
                                                reportStructure.IDatabaseObjectBase = column;
 
195
                                        }
 
196
                                        break;
 
197
 
 
198
                                case NodeType.ProcedureImage:
 
199
                                        this.txtSqlString.Clear();
 
200
 
 
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;
 
206
                                        break;
 
207
                                default:
 
208
                                        break;
 
209
                        }
 
210
                        base.EnableNext = true;
 
211
                }
 
212
 
 
213
 
 
214
        private void databasesTree_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
 
215
        {
 
216
                if (e.NewValue is IDatabaseObjectBase)
 
217
            {
 
218
                IDatabase parentDatabase = e.NewValue as IDatabase;
 
219
 
 
220
                if (parentDatabase == null)
 
221
                {
 
222
                    IDatabaseObjectBase currentDatabaseObject = e.NewValue as IDatabaseObjectBase;
 
223
 
 
224
                    while (parentDatabase == null)
 
225
                    {
 
226
                        if (currentDatabaseObject.Parent == null)
 
227
                            break;
 
228
                        else if (currentDatabaseObject.Parent is IDatabase)
 
229
                        {
 
230
                            parentDatabase = currentDatabaseObject.Parent as IDatabase;
 
231
                            break;
 
232
                        }
 
233
                        else
 
234
                            currentDatabaseObject = currentDatabaseObject.Parent;                        
 
235
                    }
 
236
                }
 
237
 
 
238
 
 
239
                if (parentDatabase != null)
 
240
                    this.currentNode = parentDatabase;
 
241
 
 
242
                if (this.currentNode is IDatabase)
 
243
                {
 
244
                        if (parentDatabase != null)
 
245
                        {
 
246
                                this.connectionString = "Provider=" + parentDatabase.Datasource.DatabaseDriver.ODBCProviderName + ";" + parentDatabase.ConnectionString;
 
247
                                this.txtSqlString.Enabled = true;
 
248
 
 
249
                                if (this.firstDrag)
 
250
                                        this.txtSqlString.Text = string.Empty;
 
251
                                
 
252
                                firstDrag = false;
 
253
                        }
 
254
                }
 
255
                else
 
256
                {
 
257
                    this.EnableNext = false;
 
258
                }
 
259
            }
 
260
        }
 
261
                
 
262
                // check witch type of node we dragg
 
263
                private static NodeType CheckCurrentNode (IDatabaseObjectBase node) {
 
264
                        NodeType enm;
 
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;
 
273
                        }
 
274
                        else {
 
275
                                enm = NodeType.NodeError;
 
276
                        }
 
277
                        return enm;
 
278
                }
 
279
                
 
280
                #endregion
 
281
                
 
282
                
 
283
                #region Windows Forms Designer generated code
 
284
                /// <summary>
 
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.
 
288
                /// </summary>
 
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();
 
298
                        // 
 
299
                        // label3
 
300
                        // 
 
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;
 
305
                        // 
 
306
                        // txtSqlString
 
307
                        // 
 
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);
 
319
                        // 
 
320
                        // label1
 
321
                        // 
 
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;
 
326
                        // 
 
327
                        // textBox1
 
328
                        // 
 
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;
 
333
                        // 
 
334
                        // label2
 
335
                        // 
 
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";
 
343
                        // 
 
344
                        // PullModelPanel
 
345
                        // 
 
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();
 
354
                }
 
355
                private System.Windows.Forms.Label label2;
 
356
                private System.Windows.Forms.TextBox textBox1;
 
357
                #endregion
 
358
        }
 
359
}