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)
5
using System.Collections;
6
using System.Collections.Generic;
9
using System.Windows.Controls;
10
using System.Xml.Linq;
12
using ICSharpCode.Core;
13
using ICSharpCode.Core.Presentation;
14
using ICSharpCode.NRefactory;
15
using ICSharpCode.SharpDevelop.Editor;
17
namespace ICSharpCode.XamlBinding.PowerToys.Commands
19
public class GroupIntoBorderWithoutChild : SimpleGroupIntoBase
21
protected override XElement CreateParent()
23
return new XElement(XName.Get("Border", currentWpfNamespace));
27
public class GroupIntoBorderWithGrid : GroupIntoBase
29
protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
31
XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
32
XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
34
newParent.Add(children);
37
foreach (var child in children)
44
public class GroupIntoBorderWithStackPanelVertical : GroupIntoBase
46
protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
48
XElement newParent = new XElement(XName.Get("StackPanel", currentWpfNamespace));
49
XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
51
newParent.SetAttributeValue("Orientation", "Vertical");
53
newParent.Add(children);
56
foreach (var child in children)
63
public class GroupIntoBorderWithStackPanelHorizontal : GroupIntoBase
65
protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
67
XElement newParent = new XElement(XName.Get("StackPanel", currentWpfNamespace));
68
XElement newItem = new XElement(XName.Get("Border", currentWpfNamespace), newParent);
70
newParent.SetAttributeValue("Orientation", "Horizontal");
72
newParent.Add(children);
75
foreach (var child in children)
82
public abstract class GroupIntoBase : XamlMenuCommand
84
protected string currentWpfNamespace;
86
protected override bool Refactor(ITextEditor editor, XDocument document)
88
if (editor.SelectionLength == 0) {
89
MessageService.ShowError("Nothing selected!");
93
Location startLoc = editor.Document.OffsetToPosition(editor.SelectionStart);
94
Location endLoc = editor.Document.OffsetToPosition(editor.SelectionStart + editor.SelectionLength);
96
var selectedItems = (from item in document.Root.Descendants()
97
where item.IsInRange(startLoc, endLoc) select item).ToList();
99
if (selectedItems.Any()) {
100
var parent = selectedItems.First().Parent;
102
currentWpfNamespace = parent.GetCurrentNamespaces()
103
.First(i => CompletionDataHelper.WpfXamlNamespaces.Contains(i));
105
var items = selectedItems.Where(i => i.Parent == parent);
107
return GroupInto(parent, items);
113
protected abstract bool GroupInto(XElement parent, IEnumerable<XElement> children);
116
public abstract class SimpleGroupIntoBase : GroupIntoBase
118
protected sealed override bool GroupInto(XElement parent, IEnumerable<XElement> children)
120
XElement newItem = CreateParent();
122
newItem.Add(children);
125
foreach (var child in children)
131
protected abstract XElement CreateParent();
134
public class GroupIntoGrid : SimpleGroupIntoBase
136
protected override XElement CreateParent()
138
return new XElement(XName.Get("Grid", currentWpfNamespace));
142
public class GroupIntoCanvas : SimpleGroupIntoBase
144
protected override XElement CreateParent()
146
return new XElement(XName.Get("Canvas", currentWpfNamespace));
150
public class GroupIntoDockPanel : SimpleGroupIntoBase
152
protected override XElement CreateParent()
154
return new XElement(XName.Get("DockPanel", currentWpfNamespace));
158
public class GroupIntoUniformGrid : SimpleGroupIntoBase
160
protected override XElement CreateParent()
162
return new XElement(XName.Get("UniformGrid", currentWpfNamespace));
166
public class GroupIntoViewbox : SimpleGroupIntoBase
168
protected override XElement CreateParent()
170
return new XElement(XName.Get("Viewbox", currentWpfNamespace));
174
public class GroupIntoWrapPanel : SimpleGroupIntoBase
176
protected override XElement CreateParent()
178
return new XElement(XName.Get("WrapPanel", currentWpfNamespace));
182
public class GroupIntoStackPanelVertical : SimpleGroupIntoBase
184
protected override XElement CreateParent()
186
var element = new XElement(XName.Get("StackPanel", currentWpfNamespace));
187
element.SetAttributeValue("Orientation", "Vertical");
192
public class GroupIntoStackPanelHorizontal : SimpleGroupIntoBase
194
protected override XElement CreateParent()
196
var element = new XElement(XName.Get("StackPanel", currentWpfNamespace));
197
element.SetAttributeValue("Orientation", "Horizontal");
202
public class GroupIntoScrollViewerWithGrid : GroupIntoBase
204
protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
206
XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
207
XElement newItem = new XElement(XName.Get("ScrollViewer", currentWpfNamespace), newParent);
209
newParent.Add(children);
212
foreach (var child in children)
219
public class GroupIntoGroupBoxWithGrid : GroupIntoBase
221
protected override bool GroupInto(XElement parent, IEnumerable<XElement> children)
223
XElement newParent = new XElement(XName.Get("Grid", currentWpfNamespace));
224
XElement newItem = new XElement(XName.Get("GroupBox", currentWpfNamespace), newParent);
226
newParent.Add(children);
229
foreach (var child in children)