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 public class SortedColumnListStore
35 public event EventHandler ColumnToggled
;
37 protected ColumnSchemaCollection columns
;
38 protected ListStore store
;
40 public const int ColSelectIndex
= 0;
41 public const int ColNameIndex
= 1;
42 public const int ColObjIndex
= 2;
44 protected bool singleCheck
;
46 public SortedColumnListStore (ColumnSchemaCollection columns
)
49 throw new ArgumentNullException ("columns");
51 this.columns
= columns
;
53 store
= new ListStore (typeof (bool), typeof (string), typeof (ColumnSchema
));
54 store
.SetSortColumnId (ColNameIndex
, SortType
.Ascending
);
55 store
.SetSortFunc (ColNameIndex
, new TreeIterCompareFunc (SortName
));
57 foreach (ColumnSchema col
in columns
)
60 columns
.ItemAdded
+= new SortedCollectionItemEventHandler
<ColumnSchema
> (OnColumnAdded
);
61 columns
.ItemRemoved
+= new SortedCollectionItemEventHandler
<ColumnSchema
> (OnColumnRemoved
);
64 public ListStore Store
{
68 public virtual bool SingleCheck
{
69 get { return singleCheck; }
71 if (value != singleCheck
) {
79 public virtual ColumnSchema
GetColumnSchema (TreeIter iter
)
81 if (!iter
.Equals (TreeIter
.Zero
))
82 return store
.GetValue (iter
, ColObjIndex
) as ColumnSchema
;
86 public virtual IEnumerable
<ColumnSchema
> CheckedColumns
{
89 if (store
.GetIterFirst (out iter
)) {
91 bool chk
= (bool)store
.GetValue (iter
, ColSelectIndex
);
93 yield return store
.GetValue (iter
, ColObjIndex
) as ColumnSchema
;
94 } while (store
.IterNext (ref iter
));
99 public virtual bool IsColumnChecked
{
102 if (store
.GetIterFirst (out iter
)) {
104 bool chk
= (bool)store
.GetValue (iter
, ColSelectIndex
);
107 } while (store
.IterNext (ref iter
));
113 public virtual void SelectAll ()
115 SetSelectState (true);
119 public virtual void DeselectAll ()
121 SetSelectState (false);
125 public virtual void Select (string name
)
128 throw new ArgumentNullException ("name");
130 ColumnSchema col
= columns
.Search (name
);
135 public virtual void Select (ColumnSchema column
)
138 throw new ArgumentNullException ("column");
140 TreeIter iter
= GetTreeIter (column
);
141 if (!iter
.Equals (TreeIter
.Zero
)) {
143 SetSelectState (false);
145 store
.SetValue (iter
, ColSelectIndex
, true);
150 public virtual void ToggleSelect (TreeIter iter
)
152 bool val
= (bool) store
.GetValue (iter
, ColSelectIndex
);
155 if (newVal
&& singleCheck
)
156 SetSelectState (false);
158 store
.SetValue (iter
, ColSelectIndex
, !val
);
162 protected virtual void SetSelectState (bool state
)
165 if (store
.GetIterFirst (out iter
)) {
167 store
.SetValue (iter
, ColSelectIndex
, state
);
168 } while (store
.IterNext (ref iter
));
172 protected virtual TreeIter
GetTreeIter (ColumnSchema column
)
175 if (store
.GetIterFirst (out iter
)) {
177 object obj
= store
.GetValue (iter
, ColObjIndex
);
181 } while (store
.IterNext (ref iter
));
183 return TreeIter
.Zero
;
186 protected virtual void OnColumnAdded (object sender
, SortedCollectionItemEventArgs
<ColumnSchema
> args
)
188 AddColumn (args
.Item
);
191 protected virtual void AddColumn (ColumnSchema column
)
193 store
.AppendValues (false, column
.Name
, column
);
195 column
.Changed
+= delegate (object sender
, EventArgs args
) {
196 TreeIter iter
= GetTreeIter (sender
as ColumnSchema
);
197 if (!iter
.Equals (TreeIter
.Zero
))
198 store
.SetValue (iter
, ColNameIndex
, column
.Name
);
202 protected virtual void OnColumnRemoved (object sender
, SortedCollectionItemEventArgs
<ColumnSchema
> args
)
204 TreeIter iter
= GetTreeIter (args
.Item
);
205 if (!iter
.Equals (TreeIter
.Zero
))
206 store
.Remove (ref iter
);
209 protected virtual int SortName (TreeModel model
, TreeIter iter1
, TreeIter iter2
)
211 string name1
= model
.GetValue (iter1
, ColNameIndex
) as string;
212 string name2
= model
.GetValue (iter2
, ColNameIndex
) as string;
214 if (name1
== null && name2
== null) return 0;
215 else if (name1
== null) return -1;
216 else if (name2
== null) return 1;
218 return name1
.CompareTo (name2
);
221 protected virtual void OnColumnToggled ()
223 if (ColumnToggled
!= null)
224 ColumnToggled (this, EventArgs
.Empty
);