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
|
//
// Authors:
// Ben Motmans <ben.motmans@gmail.com>
//
// Copyright (c) 2007 Ben Motmans
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using Gtk;
using System;
using System.Text;
using MonoDevelop.Core;
using MonoDevelop.Ide;
using MonoDevelop.Components;
using MonoDevelop.Database.Sql;
using MonoDevelop.Database.Components;
namespace MonoDevelop.Database.Designer
{
[System.ComponentModel.Category("widget")]
[System.ComponentModel.ToolboxItem(true)]
public partial class PrimaryKeyConstraintEditorWidget : Gtk.Bin
{
public event EventHandler ContentChanged;
public event EventHandler PrimaryKeyChanged;
private ISchemaProvider schemaProvider;
private TableSchema table;
private ColumnSchemaCollection columns;
private ConstraintSchemaCollection constraints;
private SchemaActions action;
private ListStore store;
private const int colNameIndex = 0;
private const int colColumnsIndex = 1;
private const int colObjIndex = 2;
public PrimaryKeyConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action)
{
if (schemaProvider == null)
throw new ArgumentNullException ("schemaProvider");
this.schemaProvider = schemaProvider;
this.action = action;
this.Build();
store = new ListStore (typeof (string), typeof (string), typeof (object));
listPK.Model = store;
TreeViewColumn colName = new TreeViewColumn ();
colName.Title = AddinCatalog.GetString ("Name");
CellRendererText nameRenderer = new CellRendererText ();
nameRenderer.Editable = true;
nameRenderer.Edited += new EditedHandler (NameEdited);
colName.PackStart (nameRenderer, true);
colName.AddAttribute (nameRenderer, "text", colNameIndex);
listPK.AppendColumn (colName);
listPK.Selection.Changed += new EventHandler (SelectionChanged);
columnSelecter.ColumnToggled += new EventHandler (ColumnToggled);
ShowAll ();
}
public void Initialize (TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
{
if (columns == null)
throw new ArgumentNullException ("columns");
if (table == null)
throw new ArgumentNullException ("table");
if (constraints == null)
throw new ArgumentNullException ("constraints");
this.table = table;
this.columns = columns;
this.constraints = constraints;
columnSelecter.Initialize (columns);
RefreshConstraints ();
}
public virtual void RefreshConstraints ()
{
store.Clear ();
foreach (PrimaryKeyConstraintSchema pk in constraints.GetConstraints (ConstraintType.PrimaryKey))
AddConstraint (pk);
}
private void NameEdited (object sender, EditedArgs args)
{
TreeIter iter;
if (store.GetIterFromString (out iter, args.Path)) {
if (!string.IsNullOrEmpty (args.NewText) && !constraints.Contains (args.NewText)) {
store.SetValue (iter, colNameIndex, args.NewText);
EmitContentChanged ();
} else {
string oldText = store.GetValue (iter, colNameIndex) as string;
(sender as CellRendererText).Text = oldText;
}
}
}
private void SelectionChanged (object sender, EventArgs args)
{
columnSelecter.DeselectAll ();
TreeIter iter;
if (listPK.Selection.GetSelected (out iter)) {
columnSelecter.Sensitive = true;
buttonRemove.Sensitive = true;
string colstr = store.GetValue (iter, colColumnsIndex) as string;
string[] cols = colstr.Split (',');
foreach (string col in cols)
columnSelecter.Select (col);
} else {
columnSelecter.Sensitive = false;
buttonRemove.Sensitive = false;
}
}
private void ColumnToggled (object sender, EventArgs args)
{
TreeIter iter;
if (listPK.Selection.GetSelected (out iter)) {
bool first = true;
StringBuilder sb = new StringBuilder ();
ConstraintSchema constraint = (ConstraintSchema)store.GetValue (iter, colObjIndex);
constraint.Columns.Clear ();
foreach (ColumnSchema column in columnSelecter.CheckedColumns) {
constraint.Columns.Add (column);
if (first)
first = false;
else
sb.Append (',');
sb.Append (column.Name);
}
store.SetValue (iter, colColumnsIndex, sb.ToString ());
EmitContentChanged ();
OnPrimaryKeyChanged ();
}
}
protected virtual void OnPrimaryKeyChanged ()
{
if (PrimaryKeyChanged != null)
PrimaryKeyChanged (this, new EventArgs ());
}
protected virtual void AddClicked (object sender, System.EventArgs e)
{
PrimaryKeyConstraintSchema pk =
schemaProvider.CreatePrimaryKeyConstraintSchema (
string.Concat (
table.Name,
"_",
AddinCatalog.GetString ("pk_new")));
int index = 1;
while (constraints.Contains (pk.Name))
pk.Name = string.Concat (table.Name,"_", AddinCatalog.GetString ("pk_new"), (index++).ToString ());
constraints.Add (pk);
AddConstraint (pk);
EmitContentChanged ();
}
protected virtual void RemoveClicked (object sender, System.EventArgs e)
{
TreeIter iter;
if (listPK.Selection.GetSelected (out iter)) {
PrimaryKeyConstraintSchema pk = store.GetValue (iter, colObjIndex) as PrimaryKeyConstraintSchema;
if (MessageService.Confirm (
AddinCatalog.GetString ("Are you sure you want to remove constraint '{0}'?", pk.Name),
AlertButton.Remove
)) {
store.Remove (ref iter);
constraints.Remove (pk);
EmitContentChanged ();
}
}
}
private void AddConstraint (PrimaryKeyConstraintSchema pk)
{
System.Text.StringBuilder pk_cols = new System.Text.StringBuilder ();
foreach (ColumnSchema col in pk.Columns) {
if (pk_cols.Length > 0)
pk_cols.Append (",");
pk_cols.Append (col.Name);
}
TreeIter iter = store.AppendValues (pk.Name, pk_cols.ToString (), pk);
}
public virtual bool ValidateSchemaObjects (out string msg)
{
TreeIter iter;
if (store.GetIterFirst (out iter)) {
do {
string name = store.GetValue (iter, colNameIndex) as string;
string columns = store.GetValue (iter, colColumnsIndex) as string;
if (String.IsNullOrEmpty (columns)) {
msg = AddinCatalog.GetString ("Primary Key constraint '{0}' must be applied to one or more columns.", name);
return false;
}
} while (store.IterNext (ref iter));
}
msg = null;
return true;
}
public virtual void FillSchemaObjects ()
{
/*
* This code isn't needed anymore, beacause PK's are added on demand in
* order to mantain the exact same thing between "ColumnsEditorWiget" and PrimaryKeyConstraintEditorWidget
*/
}
protected virtual void EmitContentChanged ()
{
if (ContentChanged != null)
ContentChanged (this, EventArgs.Empty);
}
}
}
|