* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Components / Widgets / SortedColumnListStore.cs
blobc91c0f50dba71dbc513671495a548b3a0267b428
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:
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
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 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)
48 if (columns == null)
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)
58 AddColumn (col);
60 columns.ItemAdded += new SortedCollectionItemEventHandler<ColumnSchema> (OnColumnAdded);
61 columns.ItemRemoved += new SortedCollectionItemEventHandler<ColumnSchema> (OnColumnRemoved);
64 public ListStore Store {
65 get { return store; }
68 public virtual bool SingleCheck {
69 get { return singleCheck; }
70 set {
71 if (value != singleCheck) {
72 singleCheck = value;
73 if (value)
74 DeselectAll ();
79 public virtual ColumnSchema GetColumnSchema (TreeIter iter)
81 if (!iter.Equals (TreeIter.Zero))
82 return store.GetValue (iter, ColObjIndex) as ColumnSchema;
83 return null;
86 public virtual IEnumerable<ColumnSchema> CheckedColumns {
87 get {
88 TreeIter iter;
89 if (store.GetIterFirst (out iter)) {
90 do {
91 bool chk = (bool)store.GetValue (iter, ColSelectIndex);
92 if (chk)
93 yield return store.GetValue (iter, ColObjIndex) as ColumnSchema;
94 } while (store.IterNext (ref iter));
99 public virtual bool IsColumnChecked {
100 get {
101 TreeIter iter;
102 if (store.GetIterFirst (out iter)) {
103 do {
104 bool chk = (bool)store.GetValue (iter, ColSelectIndex);
105 if (chk)
106 return true;
107 } while (store.IterNext (ref iter));
109 return false;
113 public virtual void SelectAll ()
115 SetSelectState (true);
116 OnColumnToggled ();
119 public virtual void DeselectAll ()
121 SetSelectState (false);
122 OnColumnToggled ();
125 public virtual void Select (string name)
127 if (name == null)
128 throw new ArgumentNullException ("name");
130 ColumnSchema col = columns.Search (name);
131 if (col != null)
132 Select (col);
135 public virtual void Select (ColumnSchema column)
137 if (column == null)
138 throw new ArgumentNullException ("column");
140 TreeIter iter = GetTreeIter (column);
141 if (!iter.Equals (TreeIter.Zero)) {
142 if (singleCheck)
143 SetSelectState (false);
145 store.SetValue (iter, ColSelectIndex, true);
146 OnColumnToggled ();
150 public virtual void ToggleSelect (TreeIter iter)
152 bool val = (bool) store.GetValue (iter, ColSelectIndex);
153 bool newVal = !val;
155 if (newVal && singleCheck)
156 SetSelectState (false);
158 store.SetValue (iter, ColSelectIndex, !val);
159 OnColumnToggled ();
162 protected virtual void SetSelectState (bool state)
164 TreeIter iter;
165 if (store.GetIterFirst (out iter)) {
166 do {
167 store.SetValue (iter, ColSelectIndex, state);
168 } while (store.IterNext (ref iter));
172 protected virtual TreeIter GetTreeIter (ColumnSchema column)
174 TreeIter iter;
175 if (store.GetIterFirst (out iter)) {
176 do {
177 object obj = store.GetValue (iter, ColObjIndex);
179 if (obj == column)
180 return iter;
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);