~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/XamlBinding/XamlBinding/PowerToys/Commands/GroupIntoRefactorings.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections;
 
6
using System.Collections.Generic;
 
7
using System.Linq;
 
8
using System.Windows;
 
9
using System.Windows.Controls;
 
10
using System.Xml.Linq;
 
11
 
 
12
using ICSharpCode.Core;
 
13
using ICSharpCode.Core.Presentation;
 
14
using ICSharpCode.NRefactory;
 
15
using ICSharpCode.SharpDevelop.Editor;
 
16
 
 
17
namespace ICSharpCode.XamlBinding.PowerToys.Commands
 
18
{
 
19
        public class GroupIntoBorderWithoutChild : SimpleGroupIntoBase
 
20
        {
 
21
                protected override XElement CreateParent()
 
22
                {
 
23
                        return new XElement(XName.Get("Border", currentWpfNamespace));
 
24
                }
 
25
        }
 
26
        
 
27
        public class GroupIntoBorderWithGrid : GroupIntoBase
 
28
        {
 
29
                protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
30
                {
 
31
                        XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
 
32
                        XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
 
33
                        
 
34
                        newParent.Add(children);
 
35
                        parent.Add(newItem);
 
36
                        
 
37
                        foreach (var child in children)
 
38
                                child.Remove();
 
39
                        
 
40
                        return true;
 
41
                }
 
42
        }
 
43
        
 
44
        public class GroupIntoBorderWithStackPanelVertical : GroupIntoBase
 
45
        {
 
46
                protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
47
                {
 
48
                        XElement newParent = new XElement(XName.Get("StackPanel", currentWpfNamespace));
 
49
                        XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
 
50
                        
 
51
                        newParent.SetAttributeValue("Orientation", "Vertical");
 
52
                        
 
53
                        newParent.Add(children);
 
54
                        parent.Add(newItem);
 
55
                        
 
56
                        foreach (var child in children)
 
57
                                child.Remove();
 
58
                        
 
59
                        return true;
 
60
                }
 
61
        }
 
62
        
 
63
        public class GroupIntoBorderWithStackPanelHorizontal : GroupIntoBase
 
64
        {
 
65
                protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
66
                {
 
67
                        XElement newParent = new XElement(XName.Get("StackPanel", currentWpfNamespace));
 
68
                        XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
 
69
                        
 
70
                        newParent.SetAttributeValue("Orientation", "Horizontal");
 
71
                        
 
72
                        newParent.Add(children);
 
73
                        parent.Add(newItem);
 
74
                        
 
75
                        foreach (var child in children)
 
76
                                child.Remove();
 
77
                        
 
78
                        return true;
 
79
                }
 
80
        }
 
81
 
 
82
        public abstract class GroupIntoBase : XamlMenuCommand
 
83
        {
 
84
                protected string currentWpfNamespace;
 
85
                
 
86
                protected override bool Refactor(ITextEditor editor, XDocument document)
 
87
                {
 
88
                        if (editor.SelectionLength == 0) {
 
89
                                MessageService.ShowError("Nothing selected!");
 
90
                                return false;
 
91
                        }
 
92
                        
 
93
                        Location startLoc = editor.Document.OffsetToPosition(editor.SelectionStart);
 
94
                        Location endLoc = editor.Document.OffsetToPosition(editor.SelectionStart + editor.SelectionLength);
 
95
                        
 
96
                        var selectedItems = (from item in document.Root.Descendants()
 
97
                                             where item.IsInRange(startLoc, endLoc) select item).ToList();
 
98
                        
 
99
                        if (selectedItems.Any()) {
 
100
                                var parent = selectedItems.First().Parent;
 
101
                                
 
102
                                currentWpfNamespace = parent.GetCurrentNamespaces()
 
103
                                        .First(i => CompletionDataHelper.WpfXamlNamespaces.Contains(i));
 
104
                                
 
105
                                var items = selectedItems.Where(i => i.Parent == parent);
 
106
                                
 
107
                                return GroupInto(parent, items);
 
108
                        }
 
109
                        
 
110
                        return false;
 
111
                }
 
112
                
 
113
                protected abstract bool GroupInto(XElement parent, IEnumerable<XElement> children);
 
114
        }
 
115
 
 
116
        public abstract class SimpleGroupIntoBase : GroupIntoBase
 
117
        {
 
118
                protected sealed override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
119
                {
 
120
                        XElement newItem = CreateParent();
 
121
                        
 
122
                        newItem.Add(children);
 
123
                        parent.Add(newItem);
 
124
                        
 
125
                        foreach (var child in children)
 
126
                                child.Remove();
 
127
                        
 
128
                        return true;
 
129
                }
 
130
                
 
131
                protected abstract XElement CreateParent();
 
132
        }
 
133
 
 
134
        public class GroupIntoGrid : SimpleGroupIntoBase
 
135
        {
 
136
                protected override XElement CreateParent()
 
137
                {
 
138
                        return new XElement(XName.Get("Grid", currentWpfNamespace));
 
139
                }
 
140
        }
 
141
        
 
142
        public class GroupIntoCanvas : SimpleGroupIntoBase
 
143
        {
 
144
                protected override XElement CreateParent()
 
145
                {
 
146
                        return new XElement(XName.Get("Canvas", currentWpfNamespace));
 
147
                }
 
148
        }
 
149
        
 
150
        public class GroupIntoDockPanel : SimpleGroupIntoBase
 
151
        {
 
152
                protected override XElement CreateParent()
 
153
                {
 
154
                        return new XElement(XName.Get("DockPanel", currentWpfNamespace));
 
155
                }
 
156
        }
 
157
        
 
158
        public class GroupIntoUniformGrid : SimpleGroupIntoBase
 
159
        {
 
160
                protected override XElement CreateParent()
 
161
                {
 
162
                        return new XElement(XName.Get("UniformGrid", currentWpfNamespace));
 
163
                }
 
164
        }
 
165
        
 
166
        public class GroupIntoViewbox : SimpleGroupIntoBase
 
167
        {
 
168
                protected override XElement CreateParent()
 
169
                {
 
170
                        return new XElement(XName.Get("Viewbox", currentWpfNamespace));
 
171
                }
 
172
        }
 
173
        
 
174
        public class GroupIntoWrapPanel : SimpleGroupIntoBase
 
175
        {
 
176
                protected override XElement CreateParent()
 
177
                {
 
178
                        return new XElement(XName.Get("WrapPanel", currentWpfNamespace));
 
179
                }
 
180
        }
 
181
        
 
182
        public class GroupIntoStackPanelVertical : SimpleGroupIntoBase
 
183
        {
 
184
                protected override XElement CreateParent()
 
185
                {
 
186
                        var element = new XElement(XName.Get("StackPanel", currentWpfNamespace));
 
187
                        element.SetAttributeValue("Orientation", "Vertical");
 
188
                        return element;
 
189
                }
 
190
        }
 
191
        
 
192
        public class GroupIntoStackPanelHorizontal : SimpleGroupIntoBase
 
193
        {
 
194
                protected override XElement CreateParent()
 
195
                {
 
196
                        var element = new XElement(XName.Get("StackPanel", currentWpfNamespace));
 
197
                        element.SetAttributeValue("Orientation", "Horizontal");
 
198
                        return element;
 
199
                }
 
200
        }
 
201
        
 
202
        public class GroupIntoScrollViewerWithGrid : GroupIntoBase
 
203
        {
 
204
                protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
205
                {
 
206
                        XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
 
207
                        XElement newItem = new XElement(XName.Get("ScrollViewer", currentWpfNamespace), newParent);
 
208
                        
 
209
                        newParent.Add(children);
 
210
                        parent.Add(newItem);
 
211
                        
 
212
                        foreach (var child in children)
 
213
                                child.Remove();
 
214
                        
 
215
                        return true;
 
216
                }
 
217
        }
 
218
        
 
219
        public class GroupIntoGroupBoxWithGrid : GroupIntoBase
 
220
        {
 
221
                protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
 
222
                {
 
223
                        XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
 
224
                        XElement newItem = new XElement(XName.Get("GroupBox", currentWpfNamespace), newParent);
 
225
                        
 
226
                        newParent.Add(children);
 
227
                        parent.Add(newItem);
 
228
                        
 
229
                        foreach (var child in children)
 
230
                                child.Remove();
 
231
                        
 
232
                        return true;
 
233
                }
 
234
        }
 
235
}