Browse Source

fixed selection handling in ExtractPropertiesAsStyleCommand

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6151 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 15 years ago
parent
commit
f8797e9c04
  1. 15
      data/resources/StringResources.resx
  2. 5
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/Extensions.cs
  3. 88
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/PowerToys/Commands/ExtractPropertiesAsStyleCommand.cs
  4. 8
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/PowerToys/Dialogs/ExtractPropertiesAsStyleDialog.xaml

15
data/resources/StringResources.resx

@ -890,6 +890,21 @@ SharpDevelop can collect this information and upload it automatically.</value> @@ -890,6 +890,21 @@ SharpDevelop can collect this information and upload it automatically.</value>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle" xml:space="preserve">
<value>Extract properties as style</value>
</data>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ErrorNoSelection" xml:space="preserve">
<value>No valid element selected!</value>
</data>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.PropertyColumn" xml:space="preserve">
<value>Property/Event Name</value>
</data>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ReplaceQuestion" xml:space="preserve">
<value>The selected control has already a style assigned. Do you want to replace the style with the newly created style?</value>
</data>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.StyleName" xml:space="preserve">
<value>Style name:</value>
</data>
<data name="AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ValueColumn" xml:space="preserve">
<value>Property Value</value>
</data>
<data name="AddIns.XamlBinding.Menu.GroupInto" xml:space="preserve">
<value>Group into</value>
</data>

5
src/AddIns/BackendBindings/XamlBinding/XamlBinding/Extensions.cs

@ -240,6 +240,11 @@ namespace ICSharpCode.XamlBinding @@ -240,6 +240,11 @@ namespace ICSharpCode.XamlBinding
return thisValue.LinePosition;
}
public static Location GetLocation(this IXmlLineInfo thisValue)
{
return new Location(thisValue.GetLinePosition(), thisValue.GetLineNumber());
}
public static bool IsInRange(this IXmlLineInfo item, Location begin, Location end)
{
return IsInRange(item, begin.Line, begin.Column, end.Line, end.Column);

88
src/AddIns/BackendBindings/XamlBinding/XamlBinding/PowerToys/Commands/ExtractPropertiesAsStyleCommand.cs

@ -25,54 +25,64 @@ namespace ICSharpCode.XamlBinding.PowerToys.Commands @@ -25,54 +25,64 @@ namespace ICSharpCode.XamlBinding.PowerToys.Commands
{
protected override bool Refactor(ITextEditor editor, XDocument document)
{
if (editor.SelectionLength == 0) {
MessageService.ShowError("Nothing selected!");
XElement selectedItem = GetInnermostElement(document.Root, editor.Document, editor.SelectionStart);
if (selectedItem == null) {
MessageService.ShowError("${res:AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ErrorNoSelection}");
return false;
}
Location startLoc = editor.Document.OffsetToPosition(editor.SelectionStart);
Location endLoc = editor.Document.OffsetToPosition(editor.SelectionStart + editor.SelectionLength);
XElement selectedItem = (from item in document.Root.Descendants()
where item.IsInRange(startLoc, endLoc) select item).FirstOrDefault();
if (selectedItem != null) {
var attributes = selectedItem.Attributes().Select(item => new PropertyEntry(item, Resolve(item, editor))).ToList();
attributes = attributes.Concat(selectedItem.Elements().Where(el => el.Name.LocalName.Contains("."))
.Select(element => new PropertyEntry(element, Resolve(element, editor)))
).ToList();
ExtractPropertiesAsStyleDialog dialog = new ExtractPropertiesAsStyleDialog(attributes);
if (dialog.ShowDialog() ?? false) {
XElement resourcesRoot = document.Root.Descendants(document.Root.Name + ".Resources").FirstOrDefault();
if (resourcesRoot == null) {
resourcesRoot = new XElement(document.Root.Name + ".Resources");
document.Root.AddFirst(resourcesRoot);
}
string currentWpfXamlNamespace = resourcesRoot.GetCurrentNamespaces()
.First(i => CompletionDataHelper.WpfXamlNamespaces.Contains(i));
var selectedAttributes = attributes.Where(item => item.Selected);
resourcesRoot.AddFirst(CreateStyle(dialog.StyleName, selectedItem.Name.LocalName, selectedAttributes, currentWpfXamlNamespace));
selectedAttributes.Where(p => p.Attribute != null).Select(prop => prop.Attribute).Remove();
selectedAttributes.Where(p => p.Element != null).Select(prop => prop.Element).Remove();
if (!string.IsNullOrEmpty(dialog.StyleName) && selectedItem.Attributes(XName.Get("Style")).Any()) {
if (MessageService.AskQuestion("The selected control has a style assigned already. " +
"Do you want to replace the style with the newly created style?"))
selectedItem.SetAttributeValue("Style", "{StaticResource " + dialog.StyleName + "}");
}
return true;
var attributes = selectedItem.Attributes().Select(item => new PropertyEntry(item, Resolve(item, editor))).ToList();
attributes = attributes.Concat(selectedItem.Elements().Where(el => el.Name.LocalName.Contains("."))
.Select(element => new PropertyEntry(element, Resolve(element, editor)))
).ToList();
ExtractPropertiesAsStyleDialog dialog = new ExtractPropertiesAsStyleDialog(attributes);
if (dialog.ShowDialog() ?? false) {
XElement resourcesRoot = document.Root.Descendants(document.Root.Name + ".Resources").FirstOrDefault();
if (resourcesRoot == null) {
resourcesRoot = new XElement(document.Root.Name + ".Resources");
document.Root.AddFirst(resourcesRoot);
}
string currentWpfXamlNamespace = resourcesRoot.GetCurrentNamespaces()
.First(i => CompletionDataHelper.WpfXamlNamespaces.Contains(i));
var selectedAttributes = attributes.Where(item => item.Selected);
resourcesRoot.AddFirst(CreateStyle(dialog.StyleName, selectedItem.Name.LocalName, selectedAttributes, currentWpfXamlNamespace));
selectedAttributes.Where(p => p.Attribute != null).Select(prop => prop.Attribute).Remove();
selectedAttributes.Where(p => p.Element != null).Select(prop => prop.Element).Remove();
if (!string.IsNullOrEmpty(dialog.StyleName) && selectedItem.Attributes(XName.Get("Style")).Any()) {
if (MessageService.AskQuestion("${res:AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ReplaceQuestion}"))
selectedItem.SetAttributeValue("Style", "{StaticResource " + dialog.StyleName + "}");
}
return true;
}
return false;
}
XElement GetInnermostElement(XElement parent, IDocument document, int offset)
{
int startOffset = document.PositionToOffset(parent.GetLineNumber(), parent.GetLinePosition());
int endOffset = parent.ToString().Length + startOffset;
if (startOffset > offset || endOffset < offset)
return null;
foreach (XElement element in parent.Elements()) {
XElement innermostElement = GetInnermostElement(element, document, offset);
if (innermostElement != null)
return innermostElement;
}
return parent;
}
static IMember Resolve(XAttribute attribute, ITextEditor editor)
{
XamlContext context = CompletionDataHelper.ResolveCompletionContext(editor, default(char), editor.Document.PositionToOffset(attribute.GetLineNumber(), attribute.GetLinePosition()));

8
src/AddIns/BackendBindings/XamlBinding/XamlBinding/PowerToys/Dialogs/ExtractPropertiesAsStyleDialog.xaml

@ -3,12 +3,12 @@ @@ -3,12 +3,12 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sd="http://icsharpcode.net/sharpdevelop/core"
xmlns:widgets="http://icsharpcode.net/sharpdevelop/widgets"
Title="ICSharpCode.XamlBinding.PowerToys.Dialogs" Height="300" Width="500"
Title="{sd:Localize AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle}" Height="300" Width="500"
Style="{x:Static sd:GlobalStyles.DialogWindowStyle}"
WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen">
<DockPanel>
<DockPanel DockPanel.Dock="Top" Margin="5">
<Label Content="Style Name:" />
<Label Content="{sd:Localize AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.StyleName}" />
<TextBox x:Name="txtStyleName" />
</DockPanel>
<widgets:UniformGridWithSpacing Columns="2" DockPanel.Dock="Bottom" Margin="5" HorizontalAlignment="Center">
@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Property/Event Name">
<GridViewColumn Header="{sd:Localize AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.PropertyColumn}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding PropertyName}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Property Value">
<GridViewColumn Header="{sd:Localize AddIns.XamlBinding.Menu.ExtractPropertiesAsStyle.ValueColumn}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding PropertyValue}" />

Loading…
Cancel
Save