* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Designer / Widgets / UniqueConstraintEditorWidget.cs
blob64febe76e7a7d4732f9f357409d954544a791fa5
1 //
2 // Authors:
3 // Ben Motmans <ben.motmans@gmail.com>
4 //
5 // Copyright (c) 2007 Ben Motmans
6 //
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
23 // THE SOFTWARE.
26 using Gtk;
27 using System;
28 using System.Text;
29 using System.Collections.Generic;
30 using MonoDevelop.Core;
31 using MonoDevelop.Core.Gui;
32 using MonoDevelop.Components;
33 using MonoDevelop.Database.Sql;
34 using MonoDevelop.Database.Components;
36 namespace MonoDevelop.Database.Designer
38 [System.ComponentModel.Category("widget")]
39 [System.ComponentModel.ToolboxItem(true)]
40 public partial class UniqueConstraintEditorWidget : Gtk.Bin
42 public event EventHandler ContentChanged;
44 private ISchemaProvider schemaProvider;
45 private ColumnSchemaCollection columns;
46 private ConstraintSchemaCollection constraints;
47 private TableSchema table;
49 private SchemaActions action;
51 private ListStore store;
53 private const int colNameIndex = 0;
54 private const int colIsColumnConstraintIndex = 1;
55 private const int colColumnsIndex = 2;
56 private const int colObjIndex = 3;
58 public UniqueConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action)
60 if (schemaProvider == null)
61 throw new ArgumentNullException ("schemaProvider");
63 this.schemaProvider = schemaProvider;
64 this.action = action;
66 this.Build();
68 store = new ListStore (typeof (string), typeof (bool), typeof (string), typeof (object));
69 listUnique.Model = store;
70 listUnique.Selection.Changed += new EventHandler (SelectionChanged);
71 columnSelecter.ColumnToggled += new EventHandler (ColumnToggled);
73 TreeViewColumn colName = new TreeViewColumn ();
74 TreeViewColumn colIsColConstraint = new TreeViewColumn ();
76 colName.Title = AddinCatalog.GetString ("Name");
77 colIsColConstraint.Title = AddinCatalog.GetString ("Column Constraint");
79 CellRendererText nameRenderer = new CellRendererText ();
80 CellRendererToggle toggleRenderer = new CellRendererToggle ();
82 nameRenderer.Editable = true;
83 nameRenderer.Edited += new EditedHandler (NameEdited);
85 toggleRenderer.Activatable = true;
86 toggleRenderer.Toggled += new ToggledHandler (IsColumnConstraintToggled);
88 colName.PackStart (nameRenderer, true);
89 colIsColConstraint.PackStart (toggleRenderer, true);
91 colName.AddAttribute (nameRenderer, "text", colNameIndex);
92 colIsColConstraint.AddAttribute (toggleRenderer, "active", colIsColumnConstraintIndex);
94 listUnique.AppendColumn (colName);
95 listUnique.AppendColumn (colIsColConstraint);
97 ShowAll ();
100 public void Initialize (TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
102 if (columns == null)
103 throw new ArgumentNullException ("columns");
104 if (table == null)
105 throw new ArgumentNullException ("table");
106 if (constraints == null)
107 throw new ArgumentNullException ("constraints");
109 this.table = table;
110 this.columns = columns;
111 this.constraints = constraints;
113 columnSelecter.Initialize (columns);
115 foreach (UniqueConstraintSchema uni in constraints.GetConstraints (ConstraintType.Unique))
116 AddConstraint (uni);
117 //TODO: also col constraints
120 private void NameEdited (object sender, EditedArgs args)
122 TreeIter iter;
123 if (store.GetIterFromString (out iter, args.Path)) {
124 if (!string.IsNullOrEmpty (args.NewText) && !constraints.Contains (args.NewText)) {
125 store.SetValue (iter, colNameIndex, args.NewText);
126 EmitContentChanged ();
127 } else {
128 string oldText = store.GetValue (iter, colNameIndex) as string;
129 (sender as CellRendererText).Text = oldText;
134 private void IsColumnConstraintToggled (object sender, ToggledArgs args)
136 TreeIter iter;
137 if (store.GetIterFromString (out iter, args.Path)) {
138 bool val = (bool) store.GetValue (iter, colIsColumnConstraintIndex);
139 store.SetValue (iter, colIsColumnConstraintIndex, !val);
140 SetSelectionFromIter (iter);
141 EmitContentChanged ();
145 private void SelectionChanged (object sender, EventArgs args)
147 columnSelecter.DeselectAll ();
149 TreeIter iter;
150 if (listUnique.Selection.GetSelected (out iter)) {
151 columnSelecter.Sensitive = true;
152 SetSelectionFromIter (iter);
153 } else {
154 columnSelecter.Sensitive = false;
158 private void SetSelectionFromIter (TreeIter iter)
160 bool iscolc = (bool)store.GetValue (iter, colIsColumnConstraintIndex);
161 columnSelecter.SingleCheck = iscolc;
163 string colstr = store.GetValue (iter, colColumnsIndex) as string;
164 string[] cols = colstr.Split (',');
165 foreach (string col in cols)
166 columnSelecter.Select (col);
169 private void ColumnToggled (object sender, EventArgs args)
171 TreeIter iter;
172 if (listUnique.Selection.GetSelected (out iter)) {
173 store.SetValue (iter, colColumnsIndex, GetColumnsString (columnSelecter.CheckedColumns));
174 EmitContentChanged ();
178 protected virtual void AddClicked (object sender, EventArgs e)
180 UniqueConstraintSchema uni = schemaProvider.CreateUniqueConstraintSchema ("uni_new");
181 int index = 1;
182 while (constraints.Contains (uni.Name))
183 uni.Name = "uni_new" + (index++);
184 constraints.Add (uni);
185 AddConstraint (uni);
186 EmitContentChanged ();
189 protected virtual void RemoveClicked (object sender, EventArgs e)
191 TreeIter iter;
192 if (listUnique.Selection.GetSelected (out iter)) {
193 UniqueConstraintSchema uni = store.GetValue (iter, colObjIndex) as UniqueConstraintSchema;
195 if (MessageService.Confirm (
196 AddinCatalog.GetString ("Are you sure you want to remove constraint '{0}'?", uni.Name),
197 AlertButton.Remove
198 )) {
199 store.Remove (ref iter);
200 constraints.Remove (uni);
201 EmitContentChanged ();
206 private void AddConstraint (UniqueConstraintSchema uni)
208 string colstr = GetColumnsString (uni.Columns);
209 store.AppendValues (uni.Name, uni.IsColumnConstraint, colstr, uni);
212 private string GetColumnsString (IEnumerable<ColumnSchema> collection)
214 bool first = true;
215 StringBuilder sb = new StringBuilder ();
216 foreach (ColumnSchema column in collection) {
217 if (first)
218 first = false;
219 else
220 sb.Append (',');
222 sb.Append (column.Name);
224 return sb.ToString ();
227 public virtual bool ValidateSchemaObjects (out string msg)
229 TreeIter iter;
230 if (store.GetIterFirst (out iter)) {
231 do {
232 string name = store.GetValue (iter, colNameIndex) as string;
233 string columns = store.GetValue (iter, colColumnsIndex) as string;
235 if (String.IsNullOrEmpty (columns)) {
236 msg = AddinCatalog.GetString ("Unique Key constraint '{0}' must be applied to one or more columns.", name);
237 return false;
239 } while (store.IterNext (ref iter));
241 msg = null;
242 return true;
245 public virtual void FillSchemaObjects ()
247 TreeIter iter;
248 if (store.GetIterFirst (out iter)) {
249 do {
250 UniqueConstraintSchema uni = store.GetValue (iter, colObjIndex) as UniqueConstraintSchema;
252 uni.Name = store.GetValue (iter, colNameIndex) as string;
253 uni.IsColumnConstraint = (bool)store.GetValue (iter, colIsColumnConstraintIndex);
255 string colstr = store.GetValue (iter, colColumnsIndex) as string;
256 string[] cols = colstr.Split (',');
257 foreach (string col in cols) {
258 ColumnSchema column = columns.Search (col);
259 uni.Columns.Add (column);
262 table.Constraints.Add (uni);
263 } while (store.IterNext (ref iter));
267 protected virtual void EmitContentChanged ()
269 if (ContentChanged != null)
270 ContentChanged (this, EventArgs.Empty);