3 // Ben Motmans <ben.motmans@gmail.com>
5 // Copyright (c) 2007 Ben Motmans
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using MonoDevelop
.Core
;
30 using MonoDevelop
.Core
.Gui
;
31 using MonoDevelop
.Components
;
32 using MonoDevelop
.Database
.Sql
;
33 using MonoDevelop
.Database
.Components
;
35 namespace MonoDevelop
.Database
.Designer
37 [System
.ComponentModel
.Category("widget")]
38 [System
.ComponentModel
.ToolboxItem(true)]
39 public partial class PrimaryKeyConstraintEditorWidget
: Gtk
.Bin
41 public event EventHandler ContentChanged
;
43 private ISchemaProvider schemaProvider
;
44 private TableSchema table
;
45 private ColumnSchemaCollection columns
;
46 private ConstraintSchemaCollection constraints
;
48 private SchemaActions action
;
50 private ListStore store
;
52 private const int colNameIndex
= 0;
53 private const int colColumnsIndex
= 1;
54 private const int colObjIndex
= 2;
56 public PrimaryKeyConstraintEditorWidget (ISchemaProvider schemaProvider
, SchemaActions action
)
58 if (schemaProvider
== null)
59 throw new ArgumentNullException ("schemaProvider");
61 this.schemaProvider
= schemaProvider
;
66 store
= new ListStore (typeof (string), typeof (string), typeof (object));
69 TreeViewColumn colName
= new TreeViewColumn ();
71 colName
.Title
= AddinCatalog
.GetString ("Name");
72 CellRendererText nameRenderer
= new CellRendererText ();
74 nameRenderer
.Editable
= true;
75 nameRenderer
.Edited
+= new EditedHandler (NameEdited
);
77 colName
.PackStart (nameRenderer
, true);
78 colName
.AddAttribute (nameRenderer
, "text", colNameIndex
);
79 listPK
.AppendColumn (colName
);
81 columnSelecter
.Initialize (columns
);
83 listPK
.Selection
.Changed
+= new EventHandler (SelectionChanged
);
84 columnSelecter
.ColumnToggled
+= new EventHandler (ColumnToggled
);
86 foreach (PrimaryKeyConstraintSchema pk
in constraints
.GetConstraints (ConstraintType
.PrimaryKey
))
92 public void Initialize (TableSchema table
, ColumnSchemaCollection columns
, ConstraintSchemaCollection constraints
)
95 throw new ArgumentNullException ("columns");
97 throw new ArgumentNullException ("table");
98 if (constraints
== null)
99 throw new ArgumentNullException ("constraints");
102 this.columns
= columns
;
103 this.constraints
= constraints
;
106 private void NameEdited (object sender
, EditedArgs args
)
109 if (store
.GetIterFromString (out iter
, args
.Path
)) {
110 if (!string.IsNullOrEmpty (args
.NewText
) && !constraints
.Contains (args
.NewText
)) {
111 store
.SetValue (iter
, colNameIndex
, args
.NewText
);
112 EmitContentChanged ();
114 string oldText
= store
.GetValue (iter
, colNameIndex
) as string;
115 (sender
as CellRendererText
).Text
= oldText
;
120 private void SelectionChanged (object sender
, EventArgs args
)
122 columnSelecter
.DeselectAll ();
125 if (listPK
.Selection
.GetSelected (out iter
)) {
126 columnSelecter
.Sensitive
= true;
127 buttonRemove
.Sensitive
= true;
129 string colstr
= store
.GetValue (iter
, colColumnsIndex
) as string;
130 string[] cols
= colstr
.Split (',');
131 foreach (string col
in cols
)
132 columnSelecter
.Select (col
);
134 columnSelecter
.Sensitive
= false;
135 buttonRemove
.Sensitive
= false;
139 private void ColumnToggled (object sender
, EventArgs args
)
142 if (listPK
.Selection
.GetSelected (out iter
)) {
144 StringBuilder sb
= new StringBuilder ();
145 foreach (ColumnSchema column
in columnSelecter
.CheckedColumns
) {
151 sb
.Append (column
.Name
);
154 store
.SetValue (iter
, colColumnsIndex
, sb
.ToString ());
155 EmitContentChanged ();
159 protected virtual void AddClicked (object sender
, System
.EventArgs e
)
161 PrimaryKeyConstraintSchema pk
= schemaProvider
.CreatePrimaryKeyConstraintSchema ("pk_new");
163 while (constraints
.Contains (pk
.Name
))
164 pk
.Name
= "pk_new" + (index
++);
165 constraints
.Add (pk
);
167 EmitContentChanged ();
170 protected virtual void RemoveClicked (object sender
, System
.EventArgs e
)
173 if (listPK
.Selection
.GetSelected (out iter
)) {
174 PrimaryKeyConstraintSchema pk
= store
.GetValue (iter
, colObjIndex
) as PrimaryKeyConstraintSchema
;
176 if (MessageService
.Confirm (
177 AddinCatalog
.GetString ("Are you sure you want to remove constraint '{0}'?", pk
.Name
),
180 store
.Remove (ref iter
);
181 constraints
.Remove (pk
);
182 EmitContentChanged ();
187 private void AddConstraint (PrimaryKeyConstraintSchema pk
)
189 store
.AppendValues (pk
.Name
, String
.Empty
, pk
);
192 public virtual bool ValidateSchemaObjects (out string msg
)
195 if (store
.GetIterFirst (out iter
)) {
197 string name
= store
.GetValue (iter
, colNameIndex
) as string;
198 string columns
= store
.GetValue (iter
, colColumnsIndex
) as string;
200 if (String
.IsNullOrEmpty (columns
)) {
201 msg
= AddinCatalog
.GetString ("Primary Key constraint '{0}' must be applied to one or more columns.", name
);
204 } while (store
.IterNext (ref iter
));
210 public virtual void FillSchemaObjects ()
213 if (store
.GetIterFirst (out iter
)) {
215 PrimaryKeyConstraintSchema pk
= store
.GetValue (iter
, colObjIndex
) as PrimaryKeyConstraintSchema
;
217 pk
.Name
= store
.GetValue (iter
, colNameIndex
) as string;
218 string colstr
= store
.GetValue (iter
, colColumnsIndex
) as string;
219 string[] cols
= colstr
.Split (',');
220 foreach (string col
in cols
) {
221 ColumnSchema column
= columns
.Search (col
);
222 pk
.Columns
.Add (column
);
225 table
.Constraints
.Add (pk
);
226 } while (store
.IterNext (ref iter
));
230 protected virtual void EmitContentChanged ()
232 if (ContentChanged
!= null)
233 ContentChanged (this, EventArgs
.Empty
);