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
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));
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
);
51 if (store
.GetIterFirst (out 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
{
63 if (GetActiveIter (out iter
))
64 return store
.GetValue (iter
, 1) as DatabaseConnectionContext
;
69 throw new ArgumentNullException ("DatabaseConnection");
71 TreeIter iter
= GetTreeIter (value);
72 if (!iter
.Equals (TreeIter
.Zero
))
77 public void AddDatabaseConnectionContext (DatabaseConnectionContext context
)
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
);
89 if (!GetActiveIter (out iter
))
90 SetActiveIter (newIter
);
93 private void OnConnectionRemoved (object sender
, DatabaseConnectionContextEventArgs args
)
95 TreeIter iter
= GetTreeIter (args
.ConnectionContext
);
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
)
122 if (store
.GetIterFirst (out iter
)) {
124 object obj
= store
.GetValue (iter
, 1);
127 } while (store
.IterNext (ref iter
));
129 return TreeIter
.Zero
;