comparison Chronosv2/source/Presentation/Windows/SelectionService.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.Collections.Generic;
27 using System.Linq;
28 using Chronos.Presentation.Windows.Controls;
29
30 namespace Chronos.Presentation.Windows
31 {
32 internal sealed class SelectionService
33 {
34 #region · Fields ·
35
36 private Desktop canvas;
37 private List<ISelectable> currentSelection;
38
39 #endregion
40
41 #region · Internal Properties ·
42
43 internal List<ISelectable> CurrentSelection
44 {
45 get
46 {
47 if (currentSelection == null)
48 {
49 currentSelection = new List<ISelectable>();
50 }
51
52 return currentSelection;
53 }
54 }
55
56 #endregion
57
58 #region · Constructors ·
59
60 public SelectionService(Desktop canvas)
61 {
62 this.canvas = canvas;
63 }
64
65 #endregion
66
67 #region · Internal Methods ·
68
69 internal void SelectItem(ISelectable item)
70 {
71 this.ClearSelection();
72 this.AddToSelection(item);
73 }
74
75 internal void AddToSelection(ISelectable item)
76 {
77 if (item is IGroupable)
78 {
79 List<IGroupable> groupItems = this.GetGroupMembers(item as IGroupable);
80
81 foreach (ISelectable groupItem in groupItems)
82 {
83 groupItem.IsSelected = true;
84 this.CurrentSelection.Add(groupItem);
85 }
86 }
87 else
88 {
89 item.IsSelected = true;
90 this.CurrentSelection.Add(item);
91 }
92 }
93
94 internal void RemoveFromSelection(ISelectable item)
95 {
96 if (item is IGroupable)
97 {
98 List<IGroupable> groupItems = GetGroupMembers(item as IGroupable);
99
100 foreach (ISelectable groupItem in groupItems)
101 {
102 groupItem.IsSelected = false;
103 this.CurrentSelection.Remove(groupItem);
104 }
105 }
106 else
107 {
108 item.IsSelected = false;
109 this.CurrentSelection.Remove(item);
110 }
111 }
112
113 internal void ClearSelection()
114 {
115 this.CurrentSelection.ForEach(item => item.IsSelected = false);
116 this.CurrentSelection.Clear();
117 }
118
119 internal void SelectAll()
120 {
121 this.ClearSelection();
122 this.CurrentSelection.AddRange(canvas.Children.OfType<ISelectable>());
123 this.CurrentSelection.ForEach(item => item.IsSelected = true);
124 }
125
126 internal List<IGroupable> GetGroupMembers(IGroupable item)
127 {
128 IEnumerable<IGroupable> list = canvas.Children.OfType<IGroupable>();
129 IGroupable rootItem = this.GetRoot(list, item);
130
131 return GetGroupMembers(list, rootItem);
132 }
133
134 internal IGroupable GetGroupRoot(IGroupable item)
135 {
136 IEnumerable<IGroupable> list = this.canvas.Children.OfType<IGroupable>();
137
138 return this.GetRoot(list, item);
139 }
140
141 #endregion
142
143 #region · Private Methods ·
144
145 private IGroupable GetRoot(IEnumerable<IGroupable> list, IGroupable node)
146 {
147 if (node == null || node.ParentId == Guid.Empty)
148 {
149 return node;
150 }
151 else
152 {
153 foreach (IGroupable item in list)
154 {
155 if (item.Id == node.ParentId)
156 {
157 return this.GetRoot(list, item);
158 }
159 }
160
161 return null;
162 }
163 }
164
165 private List<IGroupable> GetGroupMembers(IEnumerable<IGroupable> list, IGroupable parent)
166 {
167 List<IGroupable> groupMembers = new List<IGroupable>();
168 groupMembers.Add(parent);
169
170 var children = list.Where(node => node.ParentId == parent.Id);
171
172 foreach (IGroupable child in children)
173 {
174 groupMembers.AddRange(this.GetGroupMembers(list, child));
175 }
176
177 return groupMembers;
178 }
179
180 #endregion
181 }
182 }