* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Components / Widgets / DatabaseConnectionContextComboBox.cs
blob6f12ed62781474a4c4b88bac7df3ba0fc616b090
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:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
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.Collections.Generic;
29 using MonoDevelop.Database.Sql;
31 namespace MonoDevelop.Database.Components
33 [System.ComponentModel.Category("widget")]
34 [System.ComponentModel.ToolboxItem(true)]
35 public class DatabaseConnectionContextComboBox : ComboBox
37 private ListStore store;
39 public DatabaseConnectionContextComboBox ()
41 store = new ListStore (typeof (string), typeof (object));
42 Model = store;
44 CellRendererText textRenderer = new CellRendererText ();
45 PackStart (textRenderer, true);
46 AddAttribute (textRenderer, "text", 0);
48 foreach (DatabaseConnectionContext context in ConnectionContextService.DatabaseConnections)
49 store.AppendValues (context.ConnectionSettings.Name, context);
50 TreeIter iter;
51 if (store.GetIterFirst (out iter))
52 SetActiveIter (iter);
54 ConnectionContextService.ConnectionContextAdded += new DatabaseConnectionContextEventHandler (OnConnectionAdded);
55 ConnectionContextService.ConnectionContextRemoved += new DatabaseConnectionContextEventHandler (OnConnectionRemoved);
56 ConnectionContextService.ConnectionContextEdited += new DatabaseConnectionContextEventHandler (OnConnectionEdited);
57 ConnectionContextService.ConnectionContextRefreshed += new DatabaseConnectionContextEventHandler (OnConnectionRefreshed);
60 public DatabaseConnectionContext DatabaseConnection {
61 get {
62 TreeIter iter;
63 if (GetActiveIter (out iter))
64 return store.GetValue (iter, 1) as DatabaseConnectionContext;
65 return null;
67 set {
68 if (value == null)
69 throw new ArgumentNullException ("DatabaseConnection");
71 TreeIter iter = GetTreeIter (value);
72 if (!iter.Equals (TreeIter.Zero))
73 SetActiveIter (iter);
77 public void AddDatabaseConnectionContext (DatabaseConnectionContext context)
79 if (context == null)
80 throw new ArgumentNullException ("context");
82 store.AppendValues (context.ConnectionSettings.Name, context);
85 private void OnConnectionAdded (object sender, DatabaseConnectionContextEventArgs args)
87 TreeIter newIter = store.AppendValues (args.ConnectionContext.ConnectionSettings.Name, args.ConnectionContext);
88 TreeIter iter;
89 if (!GetActiveIter (out iter))
90 SetActiveIter (newIter);
93 private void OnConnectionRemoved (object sender, DatabaseConnectionContextEventArgs args)
95 TreeIter iter = GetTreeIter (args.ConnectionContext);
96 TreeIter selected;
97 if (GetActiveIter (out selected)) {
98 if (iter.Equals (selected)) {
99 store.Remove (ref iter);
100 if (store.GetIterFirst (out iter))
101 SetActiveIter (iter);
104 store.Remove (ref iter);
107 private void OnConnectionEdited (object sender, DatabaseConnectionContextEventArgs args)
109 TreeIter iter = GetTreeIter (args.ConnectionContext);
110 store.SetValue (iter, 0, args.ConnectionContext.ConnectionSettings.Name);
113 private void OnConnectionRefreshed (object sender, DatabaseConnectionContextEventArgs args)
115 TreeIter iter = GetTreeIter (args.ConnectionContext);
116 store.SetValue (iter, 0, args.ConnectionContext.ConnectionSettings.Name);
119 private TreeIter GetTreeIter (DatabaseConnectionContext context)
121 TreeIter iter;
122 if (store.GetIterFirst (out iter)) {
123 do {
124 object obj = store.GetValue (iter, 1);
125 if (obj == context)
126 return iter;
127 } while (store.IterNext (ref iter));
129 return TreeIter.Zero;