~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Components/ProjectSelectorWidget.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ProjectSelectorWidget.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using MonoDevelop.Projects;
 
29
using Gtk;
 
30
using System.Collections.Generic;
 
31
 
 
32
namespace MonoDevelop.Ide.Gui.Components
 
33
{
 
34
        [System.ComponentModel.ToolboxItem(true)]
 
35
        public partial class ProjectSelectorWidget : Gtk.Bin
 
36
        {
 
37
                TreeStore store;
 
38
                bool showCheckboxes;
 
39
                IBuildTarget rootItem;
 
40
                IBuildTarget currentSelection;
 
41
                HashSet<IBuildTarget> activeItems = new HashSet<IBuildTarget> ();
 
42
                HashSet<Type> selectableTypes = new HashSet<Type> ();
 
43
                
 
44
                public event EventHandler SelectionChanged;
 
45
                public event EventHandler ActiveChanged;
 
46
                
 
47
                public ProjectSelectorWidget ()
 
48
                {
 
49
                        this.Build();
 
50
                        
 
51
                        store = new TreeStore (typeof(string), typeof(string), typeof(object), typeof(bool), typeof(bool));
 
52
                        tree.Model = store;
 
53
                        
 
54
                        tree.HeadersVisible = false;
 
55
                        TreeViewColumn col = new TreeViewColumn ();
 
56
                        Gtk.CellRendererToggle ctog = new CellRendererToggle ();
 
57
                        ctog.Toggled += OnToggled;
 
58
                        col.PackStart (ctog, false);
 
59
                        Gtk.CellRendererPixbuf cr = new Gtk.CellRendererPixbuf();
 
60
                        col.PackStart (cr, false);
 
61
                        Gtk.CellRendererText crt = new Gtk.CellRendererText();
 
62
                        crt.Mode &= ~CellRendererMode.Activatable;
 
63
                        col.PackStart (crt, true);
 
64
                        col.AddAttribute (cr, "stock-id", 0);
 
65
                        col.AddAttribute (crt, "markup", 1);
 
66
                        col.AddAttribute (ctog, "active", 3);
 
67
                        col.AddAttribute (ctog, "visible", 4);
 
68
                        tree.AppendColumn (col);
 
69
                        
 
70
                        tree.Selection.Changed += HandleTreeSelectionChanged;
 
71
                }
 
72
 
 
73
                void HandleTreeSelectionChanged (object sender, EventArgs e)
 
74
                {
 
75
                        TreeIter it;
 
76
                        if (tree.Selection.GetSelected (out it))
 
77
                                currentSelection = (IBuildTarget) store.GetValue (it, 2);
 
78
                        else
 
79
                                currentSelection = null;
 
80
                        
 
81
                        if (SelectionChanged != null)
 
82
                                SelectionChanged (this, EventArgs.Empty);
 
83
                }
 
84
                
 
85
                public IBuildTarget SelectedItem {
 
86
                        get {
 
87
                                if (currentSelection != null && selectableTypes.Count > 0 && !selectableTypes.Any (t => t.IsAssignableFrom (currentSelection.GetType ())))
 
88
                                        return null;
 
89
                                else
 
90
                                        return currentSelection;
 
91
                        }
 
92
                        set {
 
93
                                currentSelection = value;
 
94
                                SetSelection (currentSelection, null);
 
95
                        }
 
96
                }
 
97
                
 
98
                public IEnumerable<IBuildTarget> ActiveItems {
 
99
                        get {
 
100
                                return activeItems;
 
101
                        }
 
102
                        set {
 
103
                                activeItems = new HashSet<IBuildTarget> ();
 
104
                                activeItems.UnionWith (value);
 
105
                                SetSelection (currentSelection, activeItems);
 
106
                        }
 
107
                }
 
108
                
 
109
                public IEnumerable<Type> SelectableItemTypes {
 
110
                        get {
 
111
                                return selectableTypes;
 
112
                        }
 
113
                        set {
 
114
                                selectableTypes = new HashSet<Type> ();
 
115
                                selectableTypes.UnionWith (value);
 
116
                                Fill ();
 
117
                        }
 
118
                }
 
119
                
 
120
                public bool ShowCheckboxes {
 
121
                        get { return showCheckboxes; }
 
122
                        set { showCheckboxes = value; Fill (); }
 
123
                }
 
124
                
 
125
                public bool CascadeCheckboxSelection { get; set; }
 
126
                
 
127
                public IBuildTarget RootItem {
 
128
                        get {
 
129
                                return this.rootItem;
 
130
                        }
 
131
                        set {
 
132
                                rootItem = value;
 
133
                                Fill ();
 
134
                        }
 
135
                }
 
136
                
 
137
                void Fill ()
 
138
                {
 
139
                        IBuildTarget sel = SelectedItem;
 
140
                        store.Clear ();
 
141
                        if (rootItem is RootWorkspace) {
 
142
                                foreach (var item in ((RootWorkspace)rootItem).Items)
 
143
                                        AddEntry (TreeIter.Zero, item);
 
144
                                SelectedItem = sel;
 
145
                        }
 
146
                        else if (rootItem != null) {
 
147
                                AddEntry (TreeIter.Zero, rootItem);
 
148
                                SelectedItem = sel;
 
149
                        }
 
150
                }
 
151
                
 
152
                void AddEntry (TreeIter iter, IBuildTarget item)
 
153
                {
 
154
                        if (!IsVisible (item))
 
155
                                return;
 
156
                        
 
157
                        string icon;
 
158
                        if (item is Solution)
 
159
                                icon = MonoDevelop.Ide.Gui.Stock.Solution;
 
160
                        else if (item is SolutionFolder)
 
161
                                icon = MonoDevelop.Ide.Gui.Stock.SolutionFolderClosed;
 
162
                        else if (item is WorkspaceItem)
 
163
                                icon = MonoDevelop.Ide.Gui.Stock.Workspace;
 
164
                        else if (item is Project)
 
165
                                icon = ((Project)item).StockIcon;
 
166
                        else
 
167
                                icon = MonoDevelop.Ide.Gui.Stock.Project;
 
168
                        
 
169
                        bool checkVisible = IsCheckboxVisible (item);
 
170
                        bool selected = activeItems.Contains (item);
 
171
                        
 
172
                        if (!iter.Equals (TreeIter.Zero))
 
173
                                iter = store.AppendValues (iter, icon, item.Name, item, selected && checkVisible, checkVisible);
 
174
                        else
 
175
                                iter = store.AppendValues (icon, item.Name, item, selected && checkVisible, checkVisible);
 
176
                        
 
177
                        if (selected)
 
178
                                tree.ExpandToPath (store.GetPath (iter));
 
179
                        
 
180
                        foreach (IBuildTarget ce in GetChildren (item))
 
181
                                AddEntry (iter, ce);
 
182
                }
 
183
                
 
184
                void SetSelection (IBuildTarget selected, HashSet<IBuildTarget> active)
 
185
                {
 
186
                        TreeIter it;
 
187
                        if (store.GetIterFirst (out it))
 
188
                                SetSelection (it, selected, active);
 
189
                }
 
190
                
 
191
                bool SetSelection (TreeIter it, IBuildTarget selected, HashSet<IBuildTarget> active)
 
192
                {
 
193
                        do {
 
194
                                IBuildTarget item = (IBuildTarget) store.GetValue (it, 2);
 
195
                                if (selected != null && item == selected) {
 
196
                                        tree.Selection.SelectIter (it);
 
197
                                        tree.ExpandToPath (store.GetPath (it));
 
198
                                        tree.ScrollToCell (store.GetPath (it), tree.Columns[0], false, 0, 0);
 
199
                                        if (active == null)
 
200
                                                return true;
 
201
                                }
 
202
                                bool val = (bool) store.GetValue (it, 3);
 
203
                                bool newVal = active != null ? active.Contains (item) : val;
 
204
                                if (val != newVal)
 
205
                                        store.SetValue (it, 3, newVal);
 
206
                                
 
207
                                TreeIter ci;
 
208
                                if (store.IterChildren (out ci, it)) {
 
209
                                        if (SetSelection (ci, selected, active))
 
210
                                                return true;
 
211
                                }
 
212
                                
 
213
                        } while (store.IterNext (ref it));
 
214
                        
 
215
                        return false;
 
216
                }
 
217
                
 
218
                void OnToggled (object sender, Gtk.ToggledArgs args)
 
219
                {
 
220
                        TreeIter iter;
 
221
                        store.GetIterFromString (out iter, args.Path);
 
222
                        IBuildTarget ob = (IBuildTarget) store.GetValue (iter, 2);
 
223
                        if (activeItems.Contains (ob)) {
 
224
                                activeItems.Remove (ob);
 
225
                                if (CascadeCheckboxSelection) {
 
226
                                        foreach (var i in GetAllChildren (ob))
 
227
                                                activeItems.Remove (i);
 
228
                                        SetSelection (iter, null, new HashSet<IBuildTarget> ());
 
229
                                } else {
 
230
                                        store.SetValue (iter, 3, false);
 
231
                                }
 
232
                        } else {
 
233
                                activeItems.Add (ob);
 
234
                                if (CascadeCheckboxSelection) {
 
235
                                        foreach (var i in GetAllChildren (ob))
 
236
                                                activeItems.Add (i);
 
237
                                        SetSelection (iter, null, activeItems);
 
238
                                }
 
239
                                else {
 
240
                                        store.SetValue (iter, 3, true);
 
241
                                }
 
242
                        }
 
243
                        if (ActiveChanged != null)
 
244
                                ActiveChanged (this, EventArgs.Empty);
 
245
                }
 
246
                
 
247
                IEnumerable<IBuildTarget> GetAllChildren (IBuildTarget item)
 
248
                {
 
249
                        IEnumerable<IBuildTarget> res = GetChildren (item);
 
250
                        return res.Concat (res.SelectMany (i => GetAllChildren (i)));
 
251
                }
 
252
                
 
253
                IEnumerable<IBuildTarget> GetChildren (IBuildTarget item)
 
254
                {
 
255
                        if (item is SolutionFolder) {
 
256
                                return ((SolutionFolder)item).Items;
 
257
                        } else if (item is Solution) {
 
258
                                return ((Solution)item).RootFolder.Items;
 
259
                        } else if (item is Workspace) {
 
260
                                return ((Workspace)item).Items;
 
261
                        } else
 
262
                                return new IBuildTarget [0];
 
263
                }
 
264
                
 
265
                protected bool IsVisible (IBuildTarget item)
 
266
                {
 
267
                        return true;
 
268
                }
 
269
                
 
270
                protected bool IsCheckboxVisible (IBuildTarget item)
 
271
                {
 
272
                        if (!ShowCheckboxes)
 
273
                                return false;
 
274
                        if (selectableTypes.Count > 0)
 
275
                                return selectableTypes.Any (t => t.IsAssignableFrom (item.GetType ()));
 
276
                        return true;
 
277
                }
 
278
        }
 
279
}
 
280