File: TableEditorDialog.cs

package info (click to toggle)
monodevelop-database 3.0.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,208 kB
  • sloc: cs: 25,909; xml: 4,050; makefile: 792; sh: 652
file content (340 lines) | stat: -rw-r--r-- 10,571 bytes parent folder | download | duplicates (3)
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
//
// 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.Threading;
using System.Collections.Generic;
using MonoDevelop.Core;
using MonoDevelop.Ide;
using MonoDevelop.Components;
using MonoDevelop.Database.Sql;
using MonoDevelop.Database.Components;

namespace MonoDevelop.Database.Designer
{
	public partial class TableEditorDialog : Gtk.Dialog
	{
		private SchemaActions action;
		
		private IEditSchemaProvider schemaProvider;
		private TableSchemaCollection tables;
		private TableSchema table;
		private TableSchema originalTable;
		private ColumnSchemaCollection columns;
		private ConstraintSchemaCollection constraints;
		private IndexSchemaCollection indexes;
		private TriggerSchemaCollection triggers;
		private DataTypeSchemaCollection dataTypes;

		private Notebook notebook;

		private ColumnsEditorWidget columnEditor;
		private ConstraintsEditorWidget constraintEditor;
		private IndicesEditorWidget indexEditor;
		private TriggersEditorWidget triggerEditor;
		private CommentEditorWidget commentEditor;
		TableEditorSettings settings;
		
		public TableEditorDialog (IEditSchemaProvider schemaProvider, bool create, TableEditorSettings settings)
		{
			if (schemaProvider == null)
				throw new ArgumentNullException ("schemaProvider");
			
			this.schemaProvider = schemaProvider;
			this.action = create ? SchemaActions.Create : SchemaActions.Alter;
			this.settings = settings;
			
			this.Build();
			
			if (create)
				Title = AddinCatalog.GetString ("Create Table");
			else
				Title = AddinCatalog.GetString ("Alter Table");
			
			notebook = new Notebook ();
			vboxContent.PackStart (notebook, true, true, 0);

			notebook.Sensitive = false;
			ThreadPool.QueueUserWorkItem (new WaitCallback (InitializeThreaded));
			vboxContent.ShowAll ();
		}
		
		public void Initialize (TableSchema table)
		{
			if (table == null)
				throw new ArgumentNullException ("table");
			
			this.originalTable = table;
			this.table = table;
			
			SetWarning (null);
			columnEditor = new ColumnsEditorWidget (schemaProvider, action, settings.ColumnSettings);
			columnEditor.ContentChanged += new EventHandler (OnContentChanged);
			// When primary Key are selected on the "Column Editor", it has to refresh the "Primary Key" Widget.
			columnEditor.PrimaryKeyChanged += delegate(object sender, EventArgs e) {
				if (constraintEditor != null)
					constraintEditor.RefreshConstraints ();
			};
			
			notebook.AppendPage (columnEditor, new Label (AddinCatalog.GetString ("Columns")));
			
			if (settings.ShowConstraints) {
				constraintEditor = new ConstraintsEditorWidget (schemaProvider, action, settings.ConstraintSettings);
				constraintEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (constraintEditor, new Label (AddinCatalog.GetString ("Constraints")));
				// If Primary Key are changed on it has to refresh the "Column Editor" Widget to select the correct 
				// columns
				constraintEditor.PrimaryKeyChanged += delegate(object sender, EventArgs e) {
					columnEditor.RefreshConstraints ();
				};
			}

			//TODO: Implement Index
			/*
			if (settings.ShowIndices) {
				indexEditor = new IndicesEditorWidget (schemaProvider, action);
				indexEditor.ContentChanged += OnContentChanged;
				notebook.AppendPage (indexEditor, new Label (AddinCatalog.GetString ("Indexes")));
			}
			*/
						
			if (settings.ShowTriggers) {
				triggerEditor = new TriggersEditorWidget (schemaProvider, action);
				triggerEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (triggerEditor, new Label (AddinCatalog.GetString ("Triggers")));
			}
			
			if (settings.ShowComment) {
				commentEditor = new CommentEditorWidget ();
				notebook.AppendPage (commentEditor, new Label (AddinCatalog.GetString ("Comment")));
			}
			notebook.Page = 0;

			entryName.Text = originalTable.Name;

			WaitDialog.ShowDialog ("Loading table data ...");
		}
		
		private void InitializeThreaded (object state)
		{
			tables = schemaProvider.GetTables ();
			dataTypes = schemaProvider.GetDataTypes ();
			columns = originalTable.Columns;
			constraints = originalTable.Constraints;
			triggers = originalTable.Triggers;
			//TODO: indices
			indexes = new IndexSchemaCollection ();
			
			System.Text.StringBuilder builder = new System.Text.StringBuilder ();
			builder.Append ("Loading editor for TABLE ");
			builder.Append (originalTable.Name);
			builder.AppendLine ();
			builder.Append ("    columns = ");
			builder.Append (columns.Count);
			builder.AppendLine ();
			builder.Append ("constraints = ");
			builder.Append (constraints.Count);
			builder.AppendLine ();

			try {
				foreach (ColumnSchema col in columns) {				
					int dummy = col.Constraints.Count; //get column constraints
					builder.Append ("CONSTRAINTS ");
					builder.Append (col.Name);
					builder.Append (" ");
					builder.Append (dummy);
					builder.AppendLine ();
				}
				LoggingService.LogDebug (builder.ToString ());
			} catch (Exception ee) {
				LoggingService.LogDebug (builder.ToString ());
				LoggingService.LogError (ee.ToString ());
			}

			if (action == SchemaActions.Alter) //make a duplicate if we are going to alter the table
				this.table = originalTable.Clone () as TableSchema;

			DispatchService.GuiDispatch (delegate () {
				InitializeGui ();
			});
		}
		
		private void InitializeGui ()
		{
			notebook.Sensitive = true;
			WaitDialog.HideDialog ();
			
			LoggingService.LogDebug ("TableEditorDialog: entering InitializeGui");
			columnEditor.Initialize (table, columns, constraints, dataTypes);
			
			if (constraintEditor != null)
				constraintEditor.Initialize (tables, table, columns, constraints, dataTypes);
			if (triggerEditor != null)
				triggerEditor.Initialize (table, triggers);
			LoggingService.LogDebug ("TableEditorDialog: leaving InitializeGui");
		}

		protected virtual void CancelClicked (object sender, System.EventArgs e)
		{
			Respond (ResponseType.Cancel);
			Hide ();
		}

		protected virtual void OkClicked (object sender, System.EventArgs e)
		{
			columnEditor.FillSchemaObjects ();
			if (constraintEditor != null)
				constraintEditor.FillSchemaObjects ();
			if (triggerEditor != null)
				triggerEditor.FillSchemaObjects ();
			if (commentEditor != null)
				table.Comment = commentEditor.Comment;
			
			if (action == SchemaActions.Create)
				table.Definition = schemaProvider.GetTableCreateStatement (table);
//			else
//				table.Definition = schemaProvider.GetTableAlterStatement (table);

			if (checkPreview.Active) {
				// Preview Dialog: If it's canceled the response to the previous dialog should be None to know that it
				// isn't OK and don't close the table editor dialog.
				PreviewDialog dlg = new PreviewDialog (table.Definition);
				int resp = 0;
				while ((resp = dlg.Run ()) != (int)ResponseType.Ok) 
					if (resp == (int)ResponseType.Cancel) 
						break;
					else
						Respond (ResponseType.Cancel);
						
				if (resp == (int)ResponseType.Ok)  {
					table.Definition = dlg.Text;
					Respond (ResponseType.Ok);
					Hide ();
				} else 
					Respond (ResponseType.None);
				
				dlg.Destroy ();
			} else {
				Respond (ResponseType.Ok);
				Hide ();
			}
		}

		protected virtual void NameChanged (object sender, System.EventArgs e)
		{
			table.Name = entryName.Text;
		}
		
		protected virtual void OnContentChanged (object sender, EventArgs args)
		{
			string msg;
			
			bool val = entryName.Text.Length > 0;
			if (!val) {
				msg = AddinCatalog.GetString ("No name specified.");
				goto sens;
			}
			
			val &= columnEditor.ValidateSchemaObjects (out msg);
			if (!val) goto sens;
			
			if (constraintEditor != null) {
				val &= constraintEditor.ValidateSchemaObjects (out msg);
				if (!val) goto sens;
			}
			
			if (triggerEditor != null) {
				val &= triggerEditor.ValidateSchemaObjects (out msg);
				if (!val) goto sens;
			}
			
			//TODO: validate indexEditor
		 sens:
			SetWarning (msg);
			buttonOk.Sensitive = val;
		}
		
		protected virtual void SetWarning (string msg)
		{
			if (msg == null) {
				hboxWarning.Hide ();
				labelWarning.Text = "";
			} else {
				hboxWarning.ShowAll ();
				labelWarning.Text = msg;
			}
		}
	}
	
	public class TableEditorSettings
	{
		private bool showConstraints = true;
		private bool showComment = false;
		private bool showTriggers = true;
		private bool showIndices = true;
		
		private ConstraintEditorSettings constraintSettings;
		private ColumnEditorSettings columnSettings;
		
		public TableEditorSettings ()
		{
			constraintSettings = new ConstraintEditorSettings ();
			columnSettings = new ColumnEditorSettings ();
		}
		
		public ConstraintEditorSettings ConstraintSettings {
			get { return constraintSettings; }
		}
		
		public ColumnEditorSettings ColumnSettings {
			get { return columnSettings; }
		}
		
		public bool ShowConstraints {
			get {
				return constraintSettings.ShowPrimaryKeyConstraints |
					constraintSettings.ShowForeignKeyConstraints |
					constraintSettings.ShowCheckConstraints |
					constraintSettings.ShowUniqueConstraints;
			}
		}

		public bool ShowComment {
			get { return showComment; }
			set { showComment = value; }
		}

		public bool ShowTriggers {
			get { return showTriggers; }
			set { showTriggers = value; }
		}

		public bool ShowIndices {
			get { return showIndices; }
			set { showIndices = value; }
		}
	}
}