forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableDesignerDoc.cs
More file actions
1453 lines (1236 loc) · 42.4 KB
/
TableDesignerDoc.cs
File metadata and controls
1453 lines (1236 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace Npgsql.Designer.Editors
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Data;
using Npgsql.Designer.Design;
using System.ComponentModel.Design;
public partial class TableDesignerDoc : DesignerDocBase,
IVsPersistDocData,
IVsWindowPane,
IOleCommandTarget,
ISelectionContainer,
IVsWindowPaneCommit,
IVsWindowFrameNotify
{
private static Dictionary<int, string> _editingTables = new Dictionary<int, string>();
internal DataConnection _connection;
internal Microsoft.VisualStudio.Data.ServiceProvider _serviceProvider;
internal Table _table;
internal bool _dirty;
internal bool _init = false;
internal DataViewHierarchyAccessor _accessor;
internal int _itemId;
static private bool _warned = false;
public TableDesignerDoc(int itemId, DataViewHierarchyAccessor accessor, string tableName)
{
_accessor = accessor;
_connection = accessor.Connection;
_itemId = itemId;
_init = true;
InitializeComponent();
StringBuilder tables = new StringBuilder();
using (DataReader reader = _connection.Command.Execute("SELECT * FROM sqlite_master", 1, null, 30))
{
while (reader.Read())
{
tables.Append(reader.GetItem(2).ToString());
tables.Append(",");
}
}
int n = 1;
if (String.IsNullOrEmpty(tableName))
{
string alltables = tables.ToString();
do
{
tableName = String.Format("Table{0}", n);
n++;
} while (alltables.IndexOf(tableName + ",", StringComparison.OrdinalIgnoreCase) > -1 || _editingTables.ContainsValue(tableName));
_editingTables.Add(GetHashCode(), tableName);
}
_table = new Table(tableName, _connection.ConnectionSupport.ProviderObject as DbConnection, this);
foreach(Column c in _table.Columns)
{
n = _dataGrid.Rows.Add();
_dataGrid.Rows[n].Tag = c;
c.Parent = _dataGrid.Rows[n];
}
_init = false;
if (_dataGrid.Rows.Count > 0)
{
_dataGrid.EndEdit();
_sqlText.Text = _table.OriginalSql;
}
}
public override string CanonicalName
{
get
{
return _table.Name;
}
}
void SetPropertyWindow()
{
IVsTrackSelectionEx track = _serviceProvider.GetService(typeof(SVsTrackSelectionEx)) as IVsTrackSelectionEx;
if (track != null)
{
track.OnSelectChange(this);
}
}
public string Caption
{
get
{
string catalog = "main";
if (_table != null) catalog = _table.Catalog;
return String.Format("{0}.{1} Table (SQLite [{2}])", catalog, base.Name, ((DbConnection)_connection.ConnectionSupport.ProviderObject).DataSource);
}
}
public new string Name
{
get
{
if (_table != null)
return _table.Name;
else return base.Name;
}
set
{
base.Name = value;
if (_serviceProvider != null)
{
IVsWindowFrame frame = _serviceProvider.GetService(typeof(IVsWindowFrame)) as IVsWindowFrame;
if (frame != null)
{
frame.SetProperty((int)__VSFPROPID.VSFPROPID_EditorCaption, Caption);
}
}
}
}
public void NotifyChanges()
{
if (_serviceProvider == null) return;
_sqlText.Text = _table.GetSql();
// Get a reference to the Running Document Table
IVsRunningDocumentTable runningDocTable = (IVsRunningDocumentTable)_serviceProvider.GetService(typeof(SVsRunningDocumentTable));
// Lock the document
uint docCookie;
IVsHierarchy hierarchy;
uint itemID;
IntPtr docData;
int hr = runningDocTable.FindAndLockDocument(
(uint)_VSRDTFLAGS.RDT_ReadLock,
base.Name,
out hierarchy,
out itemID,
out docData,
out docCookie
);
ErrorHandler.ThrowOnFailure(hr);
IVsUIShell shell = _serviceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
if (shell != null)
{
shell.UpdateDocDataIsDirtyFeedback(docCookie, (_dirty == true) ? 1 : 0);
}
// Unlock the document.
// Note that we have to unlock the document even if the previous call failed.
runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie);
// Check ff the call to NotifyDocChanged failed.
//ErrorHandler.ThrowOnFailure(hr);
}
#region IVsPersistDocData Members
int IVsPersistDocData.Close()
{
return VSConstants.S_OK;
}
public int GetGuidEditorType(out Guid pClassID)
{
return ((IPersistFileFormat)this).GetClassID(out pClassID);
}
public int IsDocDataDirty(out int pfDirty)
{
pfDirty = _dirty == true ? 1 : 0;
return VSConstants.S_OK;
}
public int IsDocDataReloadable(out int pfReloadable)
{
pfReloadable = 0;
return VSConstants.S_OK;
}
public int LoadDocData(string pszMkDocument)
{
return ((IPersistFileFormat)this).Load(pszMkDocument, 0, 0);
}
public int OnRegisterDocData(uint docCookie, IVsHierarchy pHierNew, uint itemidNew)
{
return VSConstants.S_OK;
}
public int ReloadDocData(uint grfFlags)
{
return VSConstants.E_NOTIMPL;
}
public int RenameDocData(uint grfAttribs, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
{
return VSConstants.E_NOTIMPL;
}
private string GetChangeScript()
{
_dataGrid.EndEdit();
_init = true;
for (int n = 0; n < _table.Columns.Count; n++)
{
Column c = _table.Columns[n];
if (String.IsNullOrEmpty(c.ColumnName) == true)
{
try
{
_dataGrid.Rows.Remove(c.Parent);
}
catch
{
c.Parent.Tag = null;
}
_table.Columns.Remove(c);
n--;
continue;
}
}
for (int n = 0; n < _dataGrid.Rows.Count; n++)
{
if ((_dataGrid.Rows[n].Tag is Column) == false)
{
try
{
_dataGrid.Rows.RemoveAt(n);
}
catch
{
if (n == _dataGrid.Rows.Count - 1) break;
}
n--;
}
}
_init = false;
return _table.GetSql();
}
public int SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
{
pbstrMkDocumentNew = _table.Name;
pfSaveCanceled = 0;
if (String.IsNullOrEmpty(_table.OriginalSql) == true)
{
using (TableNameDialog dlg = new TableNameDialog("Table", _table.Name))
{
if (dlg.ShowDialog(this) == DialogResult.Cancel)
{
pfSaveCanceled = 1;
return VSConstants.S_OK;
}
_table.Name = dlg.TableName;
}
}
string sql = GetChangeScript();
using (DbTransaction trans = _table.GetConnection().BeginTransaction())
{
try
{
using (DbCommand cmd = _table.GetConnection().CreateCommand())
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
throw;
}
}
_dirty = false;
_table.Committed();
NotifyChanges();
_sqlText.Text = _table.OriginalSql;
NpgsqlCommandHandler.Refresh(_accessor, _itemId);
_dataGrid_SelectionChanged(this, EventArgs.Empty);
RefreshToolbars();
return VSConstants.S_OK;
}
public int SetUntitledDocPath(string pszDocDataPath)
{
return ((IPersistFileFormat)this).InitNew(0);
}
#endregion
#region IVsWindowPane Members
public int ClosePane()
{
this.Dispose(true);
return VSConstants.S_OK;
}
public int CreatePaneWindow(IntPtr hwndParent, int x, int y, int cx, int cy, out IntPtr hwnd)
{
Win32Methods.SetParent(Handle, hwndParent);
hwnd = Handle;
Size = new System.Drawing.Size(cx - x, cy - y);
return VSConstants.S_OK;
}
public int GetDefaultSize(Microsoft.VisualStudio.OLE.Interop.SIZE[] size)
{
if (size.Length >= 1)
{
size[0].cx = Size.Width;
size[0].cy = Size.Height;
}
return VSConstants.S_OK;
}
public int LoadViewState(Microsoft.VisualStudio.OLE.Interop.IStream pStream)
{
return VSConstants.S_OK;
}
public int SaveViewState(Microsoft.VisualStudio.OLE.Interop.IStream pStream)
{
return VSConstants.S_OK;
}
public void RefreshToolbars()
{
if (_serviceProvider == null) return;
IVsUIShell shell = _serviceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
if (shell != null)
{
shell.UpdateCommandUI(1);
}
}
public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
{
_serviceProvider = new ServiceProvider(psp);
return VSConstants.S_OK;
}
public int TranslateAccelerator(Microsoft.VisualStudio.OLE.Interop.MSG[] lpmsg)
{
return VSConstants.S_FALSE;
}
#endregion
public void MakeDirty()
{
_dirty = true;
NotifyChanges();
}
private bool IsPkSelected()
{
bool newVal = false;
foreach (Column c in _propertyGrid.SelectedObjects)
{
foreach (IndexColumn ic in _table.PrimaryKey.Columns)
{
if (String.Compare(c.ColumnName, ic.Column, StringComparison.OrdinalIgnoreCase) == 0)
{
newVal = true;
break;
}
}
}
return newVal;
}
#region IOleCommandTarget Members
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
{
switch ((VSConstants.VSStd97CmdID)nCmdID)
{
case VSConstants.VSStd97CmdID.GenerateChangeScript:
{
using (ChangeScriptDialog dlg = new ChangeScriptDialog(_table.Name, (_dirty == true) ? GetChangeScript() : String.Empty, _table.OriginalSql))
{
dlg.ShowDialog(this);
}
}
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.PrimaryKey:
bool newVal = IsPkSelected();
if (newVal == false)
{
_table.PrimaryKey.Columns.Clear();
foreach (Column c in _propertyGrid.SelectedObjects)
{
IndexColumn newcol = new IndexColumn(_table.PrimaryKey, null);
newcol.Column = c.ColumnName;
_table.PrimaryKey.Columns.Add(newcol);
}
}
else
{
foreach (Column c in _propertyGrid.SelectedObjects)
{
foreach (IndexColumn ic in _table.PrimaryKey.Columns)
{
if (String.Compare(c.ColumnName, ic.Column, StringComparison.OrdinalIgnoreCase) == 0)
{
_table.PrimaryKey.Columns.Remove(ic);
break;
}
}
}
}
_dataGrid_SelectionChanged(this, EventArgs.Empty);
_dataGrid.Invalidate();
MakeDirty();
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.Cut:
_clipRows = SelectedRows;
foreach (DataGridViewRow row in _clipRows)
{
_dataGrid.Rows.Remove(row);
_table.Columns.Remove(row.Tag as Column);
MakeDirty();
}
Clipboard.SetData("SQLiteTableDesigner", Caption);
RefreshToolbars();
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.Copy:
_clipRows = SelectedRows;
Clipboard.SetData("SQLiteTableDesigner", Caption);
RefreshToolbars();
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.Paste:
DataGridViewRow[] arr = GetClipboardRows();
int rowIndex = _dataGrid.CurrentRow.Index;
_dataGrid.EndEdit();
for (int n = arr.Length - 1; n > -1; n--)
{
DataGridViewRow row = arr[n];
Column c = row.Tag as Column;
bool hasCol = false;
foreach (Column oc in _table.Columns)
{
if (String.Compare(c.ColumnName, oc.ColumnName, StringComparison.OrdinalIgnoreCase) == 0)
{
hasCol = true;
break;
}
}
_dataGrid.Rows.Insert(rowIndex, 1);
DataGridViewRow newrow = _dataGrid.Rows[rowIndex];
if (hasCol == true)
{
Column oc = c;
c = new Column(_table, newrow);
int num = 1;
while (String.IsNullOrEmpty(c.ColumnName) == true)
{
bool found = false;
string proposed = String.Format("{0}{1}", oc.ColumnName, num);
foreach (Column cc in _table.Columns)
{
if (String.Compare(cc.ColumnName, proposed, StringComparison.OrdinalIgnoreCase) == 0)
{
found = true;
break;
}
}
if (found == true)
{
num++;
}
else
c.ColumnName = proposed;
}
c.AllowNulls = oc.AllowNulls;
c.Collate = oc.Collate;
c.DataType = oc.DataType;
c.DefaultValue = oc.DefaultValue;
c.Unique.Enabled = oc.Unique.Enabled;
c.Unique.Conflict = oc.Unique.Conflict;
}
c.Parent = newrow;
newrow.Tag = c;
newrow.SetValues(c.ColumnName, c.DataType, c.AllowNulls);
_table.Columns.Insert(rowIndex, c);
}
MakeDirty();
_dataGrid.Invalidate();
return VSConstants.S_OK;
}
}
else if (pguidCmdGroup == NpgsqlCommandHandler.guidDavinci)
{
switch ((VSConstants.VSStd97CmdID)nCmdID)
{
case VSConstants.VSStd97CmdID.ManageIndexes:
EditorHolder holder = new EditorHolder(_table);
_pg.SelectedObject = holder;
_pg.SelectedGridItem = _pg.SelectedGridItem.Parent.GridItems[0];
IndexEditor ed = new IndexEditor(_table);
ed.EditValue((ITypeDescriptorContext)_pg.SelectedGridItem, (System.IServiceProvider)_pg.SelectedGridItem, _pg.SelectedGridItem.Value);
_dataGrid_SelectionChanged(this, EventArgs.Empty);
_dataGrid.Invalidate();
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.ManageRelationships:
holder = new EditorHolder(_table);
_pg.SelectedObject = holder;
_pg.SelectedGridItem = _pg.SelectedGridItem.Parent.GridItems[1];
ForeignKeyEditor fed = new ForeignKeyEditor(_table);
fed.EditValue((ITypeDescriptorContext)_pg.SelectedGridItem, (System.IServiceProvider)_pg.SelectedGridItem, _pg.SelectedGridItem.Value);
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.ManageConstraints:
holder = new EditorHolder(_table);
_pg.SelectedObject = holder;
_pg.SelectedGridItem = _pg.SelectedGridItem.Parent.GridItems[2];
CheckEditor ced = new CheckEditor(_table);
ced.EditValue((ITypeDescriptorContext)_pg.SelectedGridItem, (System.IServiceProvider)_pg.SelectedGridItem, _pg.SelectedGridItem.Value);
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.AlignRight: // Insert Column
_dataGrid.EndEdit();
_dataGrid.Rows.Insert(_dataGrid.SelectedRows[0].Index, 1);
return VSConstants.S_OK;
case VSConstants.VSStd97CmdID.AlignToGrid: // Delete Column
_dataGrid.EndEdit();
int deleted = 0;
while (_dataGrid.SelectedRows.Count > 0)
{
try
{
DataGridViewRow row = _dataGrid.SelectedRows[0];
Column c = row.Tag as Column;
row.Selected = false;
if (c != null)
{
_table.Columns.Remove(c);
deleted++;
}
_dataGrid.Rows.Remove(row);
}
catch
{
}
}
if (deleted > 0) MakeDirty();
return VSConstants.S_OK;
}
}
else if (pguidCmdGroup == NpgsqlCommandHandler.guidSQLiteCmdSet)
{
switch ((cmdid)nCmdID)
{
case cmdid.Triggers:
EditorHolder holder = new EditorHolder(_table);
_pg.SelectedObject = holder;
_pg.SelectedGridItem = _pg.SelectedGridItem.Parent.GridItems[3];
TriggerEditor ted = new TriggerEditor(_table);
ted.EditValue((ITypeDescriptorContext)_pg.SelectedGridItem, (System.IServiceProvider)_pg.SelectedGridItem, _pg.SelectedGridItem.Value);
return VSConstants.S_OK;
}
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
private DataGridViewRow[] _clipRows;
private DataGridViewRow[] GetClipboardRows()
{
if (Clipboard.ContainsData("SQLiteTableDesigner") && Clipboard.GetData("SQLiteTableDesigner").ToString() == Caption)
{
DataGridViewRow[] arr = _clipRows;
if (arr != null)
{
for (int n = 0; n < arr.Length; n++)
{
Column c = arr[n].Tag as Column;
if (c != null && c.Table == _table) return arr;
}
}
}
return null;
}
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
{
switch ((VSConstants.VSStd97CmdID)prgCmds[0].cmdID)
{
case VSConstants.VSStd97CmdID.PrimaryKey:
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
if (IsPkSelected() == true)
prgCmds[0].cmdf |= (uint)(OLECMDF.OLECMDF_LATCHED);
break;
case VSConstants.VSStd97CmdID.GenerateChangeScript:
if (_dirty == true || String.IsNullOrEmpty(_table.OriginalSql) == false)
{
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
break;
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
case VSConstants.VSStd97CmdID.Cut:
if (SelectedRows.Length > 0)
{
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
break;
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
case VSConstants.VSStd97CmdID.Copy:
if (SelectedRows.Length > 0)
{
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
break;
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
case VSConstants.VSStd97CmdID.Paste:
DataGridViewRow[] rows = GetClipboardRows();
if (rows != null)
{
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
break;
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
default:
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
return VSConstants.S_OK;
}
if (pguidCmdGroup == NpgsqlCommandHandler.guidSQLiteCmdSet)
{
switch (prgCmds[0].cmdID)
{
case (uint)cmdid.Triggers:
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
return VSConstants.S_OK;
}
}
if (pguidCmdGroup == NpgsqlCommandHandler.guidDavinci)
{
switch (prgCmds[0].cmdID)
{
case (uint)VSConstants.VSStd97CmdID.ManageRelationships:
case (uint)VSConstants.VSStd97CmdID.ManageIndexes:
case (uint)VSConstants.VSStd97CmdID.ManageConstraints:
//case 10: // Table View -> Custom
//case 14: // Table View -> Modify Custom
//case 33: // Database Diagram -> Add Table
//case 1: // Database Diagram -> Add Related Tables
//case 12: // Database Diagram -> Delete From Database
//case 51: // Database Diagram -> Remove From Diagram
//case 13: // Database Diagram -> Autosize Selected Tables
//case 3: // Database Diagram -> Arrange Selection
//case 2: // Database Diagram -> Arrange Tables
//case 16: // Database Diagram -> Zoom -> 200%
//case 17: // Database Diagram -> Zoom -> 150%
//case 18: // Database Diagram -> Zoom -> 100%
//case 19: // Database Diagram -> Zoom -> 75%
//case 20: // Database Diagram -> Zoom -> 50%
//case 21: // Database Diagram -> Zoom -> 25%
//case 22: // Database Diagram -> Zoom -> 10%
//case 24: // Database Diagram -> Zoom -> To Fit
//case 6: // Database Diagram -> New Text Annotation
//case 15: // Database Diagram -> Set Text Annotation Font
//case 7: // Database Diagram -> Show Relationship Labels
//case 8: // Database Diagram -> View Page Breaks
//case 9: // Database Diagram -> Recalculate Page Breaks
//case 43: // Database Diagram -> Copy Diagram to Clipboard
//case 41: // Query Designer -> Table Display -> Column Names
//case 42: // Query Designer -> Table Display -> Name Only
//case 39: // Query Designer -> Add Table
case 4: // Insert Column
case 5: // Delete Column
prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
break;
default:
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
return VSConstants.S_OK;
}
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
#endregion
#region ISelectionContainer Members
int ISelectionContainer.CountObjects(uint dwFlags, out uint pc)
{
pc = 1;
return VSConstants.S_OK;
}
int ISelectionContainer.GetObjects(uint dwFlags, uint cObjects, object[] apUnkObjects)
{
apUnkObjects[0] = _table;
return VSConstants.S_OK;
}
int ISelectionContainer.SelectObjects(uint cSelect, object[] apUnkSelect, uint dwFlags)
{
return VSConstants.S_OK;
}
#endregion
#region IVsWindowPaneCommit Members
int IVsWindowPaneCommit.CommitPendingEdit(out int pfCommitFailed)
{
pfCommitFailed = 0;
return VSConstants.S_OK;
}
#endregion
#region IVsWindowFrameNotify Members
int IVsWindowFrameNotify.OnDockableChange(int fDockable)
{
return VSConstants.S_OK;
}
int IVsWindowFrameNotify.OnMove()
{
return VSConstants.S_OK;
}
int IVsWindowFrameNotify.OnShow(int fShow)
{
switch ((__FRAMESHOW)fShow)
{
case __FRAMESHOW.FRAMESHOW_WinShown:
case __FRAMESHOW.FRAMESHOW_WinRestored:
SetPropertyWindow();
if (_warned == false)
{
_warned = true;
MessageBox.Show(this, "The table designer is still in development. Please report bugs to robert@blackcastlesoft.com", "Feature Under Review", MessageBoxButtons.OK);
}
break;
}
return VSConstants.S_OK;
}
int IVsWindowFrameNotify.OnSize()
{
return VSConstants.S_OK;
}
#endregion
private void _dataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex > -1)
{
_dataGrid.BeginEdit(true);
_dataGrid_SelectionChanged(sender, e);
}
}
catch
{
}
}
private void _dataGrid_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
_dataGrid.EndEdit();
if (e.Button == MouseButtons.Right)
{
if (_dataGrid.Rows[e.RowIndex].Selected == false)
{
switch (Control.ModifierKeys)
{
case Keys.Control:
_dataGrid.Rows[e.RowIndex].Selected = true;
break;
case Keys.Shift:
int min = Math.Min(_dataGrid.CurrentRow.Index, e.RowIndex);
int max = Math.Max(_dataGrid.CurrentRow.Index, e.RowIndex);
for (int n = 0; n < _dataGrid.Rows.Count; n++)
{
_dataGrid.Rows[n].Selected = (_dataGrid.Rows[n].Index <= min || _dataGrid.Rows[n].Index <= max);
}
break;
default:
for (int n = 0; n < _dataGrid.Rows.Count; n++)
{
_dataGrid.Rows[n].Selected = (e.RowIndex == n);
}
break;
}
}
IVsUIShell shell = _serviceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
if (shell != null)
{
Guid guid;
POINTS[] p = new POINTS[1];
int ret;
p[0].x = (short)Control.MousePosition.X;
p[0].y = (short)Control.MousePosition.Y;
guid = new Guid("732abe74-cd80-11d0-a2db-00aa00a3efff");
ret = shell.ShowContextMenu(0, ref guid, 259, p, this);
}
}
}
private void _dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (_init == true) return;
if (e.ColumnIndex == -1 && e.RowIndex == -1)
{
_dataGrid.EndEdit();
}
_dataGrid_SelectionChanged(sender, e);
}
private void _dataGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (_init == true) return;
if (e.ColumnIndex > -1 || e.RowIndex < 0) return;
Column col = _dataGrid.Rows[e.RowIndex].Tag as Column;
if (col == null) return;
bool ispk = false;
foreach (IndexColumn ic in _table.PrimaryKey.Columns)
{
if (String.Compare(ic.Column, col.ColumnName, StringComparison.OrdinalIgnoreCase) == 0)
{
ispk = true;
break;
}
}
if (ispk == true)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
_imageList.Draw(e.Graphics, e.CellBounds.Left, e.CellBounds.Top + ((e.CellBounds.Bottom - e.CellBounds.Top) - _imageList.ImageSize.Height) / 2, 0);
e.Handled = true;
}
}
private DataGridViewRow[] SelectedRows
{
get
{
List<DataGridViewRow> items = new List<DataGridViewRow>();
for (int n = 0; n < _dataGrid.Rows.Count; n++)
{
if (_dataGrid.Rows[n].Selected || (_dataGrid.CurrentCell.RowIndex == n && _dataGrid.CurrentCell.Selected == true))
{
if (_dataGrid.Rows[n].Tag is Column)
items.Add(_dataGrid.Rows[n]);
}
}
DataGridViewRow[] objs = new DataGridViewRow[items.Count];
items.CopyTo(objs);
return objs;
}
}
private void _dataGrid_SelectionChanged(object sender, EventArgs e)
{
if (_init == true) return;
DataGridViewRow[] arr = SelectedRows;
object[] objs = new object[arr.Length];
for (int n = 0; n < objs.Length; n++)
objs[n] = arr[n].Tag;
_propertyGrid.SelectedObjects = objs;
RefreshToolbars();
}
private void _dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (_init == true) return;
if (e.RowIndex > -1)
{
_propertyGrid.SelectedObjects = _propertyGrid.SelectedObjects;
}
}
private void _dataGrid_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
if (_init == true) return;
if (e.Row.Tag is Column)
MakeDirty();
_dataGrid_SelectionChanged(sender, e);
}
private void _dataGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if (_init == true) return;
if (e.Row.Tag is Column)
{
_table.Columns.Remove(e.Row.Tag as Column);
}
}
private void _dataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
}
private void _dataGrid_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
}
private Rectangle _dragBoxFromMouseDown;
private int _rowIndexFromMouseDown;
private int _rowIndexOfItemUnderMouseToDrop;
private void _dataGrid_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = _dataGrid.PointToClient(new Point(e.X, e.Y));
if (_rowIndexOfItemUnderMouseToDrop != -1)
_dataGrid.Rows[_rowIndexOfItemUnderMouseToDrop].DividerHeight = 0;
_rowIndexOfItemUnderMouseToDrop = _dataGrid.HitTest(clientPoint.X, clientPoint.Y).RowIndex;