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.Generic;
6
using System.ComponentModel;
7
using System.Globalization;
10
using System.Windows.Controls;
11
using System.Windows.Data;
12
using System.Windows.Input;
13
using System.Windows.Threading;
15
using ICSharpCode.AvalonEdit.Snippets;
16
using ICSharpCode.NRefactory.Ast;
17
using ICSharpCode.SharpDevelop.Dom;
18
using ICSharpCode.SharpDevelop.Dom.Refactoring;
19
using ICSharpCode.SharpDevelop.Editor;
21
namespace SharpRefactoring.Gui
24
/// Interaction logic for InsertCtorDialog.xaml
26
public partial class InsertCtorDialog : AbstractInlineRefactorDialog
28
IList<PropertyOrFieldWrapper> parameterList;
30
public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList<PropertyOrFieldWrapper> possibleParameters)
31
: base(context, editor, anchor)
33
InitializeComponent();
35
this.varList.ItemsSource = parameterList = possibleParameters;
37
if (!parameterList.Any())
38
Visibility = System.Windows.Visibility.Collapsed;
41
protected override string GenerateCode(LanguageProperties language, IClass currentClass)
43
IDocumentLine line = editor.Document.GetLineForOffset(anchor.Offset);
45
string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset);
47
List<PropertyOrFieldWrapper> filtered = parameterList
48
.Where(p => p.IsSelected)
49
.OrderBy(p => p.Index)
52
BlockStatement block = new BlockStatement();
54
foreach (PropertyOrFieldWrapper w in filtered) {
55
if (w.AddCheckForNull) {
56
// true = reference, null = generic or unknown
57
if (w.Type.IsReferenceType != false)
60
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)),
61
new ThrowStatement(new ObjectCreateExpression(new TypeReference("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
67
new UnaryOperatorExpression(new MemberReferenceExpression(new IdentifierExpression(w.MemberName), "HasValue"), UnaryOperatorType.Not),
68
new ThrowStatement(new ObjectCreateExpression(new TypeReference("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
72
if (w.AddRangeCheck) {
75
new BinaryOperatorExpression(
76
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.LessThan, new IdentifierExpression("lower")),
77
BinaryOperatorType.LogicalOr,
78
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper"))
81
new ObjectCreateExpression(
82
new TypeReference("ArgumentOutOfRangeException"),
83
new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"'), new IdentifierExpression(w.ParameterName), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Concat, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Concat, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Concat, new IdentifierExpression("upper")))) }
91
foreach (PropertyOrFieldWrapper w in filtered)
92
block.AddChild(new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName))));
94
AnchorElement parameterListElement = context.ActiveElements
95
.OfType<AnchorElement>()
96
.FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));
98
if (parameterListElement != null) {
99
StringBuilder pList = new StringBuilder();
101
var parameters = filtered
102
.Select(p => new ParameterDeclarationExpression(ConvertType(p.Type), p.ParameterName))
105
for (int i = 0; i < parameters.Count; i++) {
108
pList.Append(language.CodeGenerator.GenerateCode(parameters[i], ""));
111
parameterListElement.Text = pList.ToString();
114
StringBuilder builder = new StringBuilder();
116
foreach (var element in block.Children.OfType<AbstractNode>()) {
117
builder.Append(language.CodeGenerator.GenerateCode(element, indent));
120
return builder.ToString().Trim();
123
void UpClick(object sender, System.Windows.RoutedEventArgs e)
125
int selection = varList.SelectedIndex;
130
var curItem = parameterList.First(p => p.Index == selection);
131
var exchangeItem = parameterList.First(p => p.Index == selection - 1);
133
curItem.Index = selection - 1;
134
exchangeItem.Index = selection;
136
varList.ItemsSource = parameterList.OrderBy(p => p.Index);
137
varList.SelectedIndex = selection - 1;
140
void DownClick(object sender, System.Windows.RoutedEventArgs e)
142
int selection = varList.SelectedIndex;
144
if (selection < 0 || selection >= parameterList.Count - 1)
147
var curItem = parameterList.First(p => p.Index == selection);
148
var exchangeItem = parameterList.First(p => p.Index == selection + 1);
150
curItem.Index = selection + 1;
151
exchangeItem.Index = selection;
153
varList.ItemsSource = parameterList.OrderBy(p => p.Index);
154
varList.SelectedIndex = selection + 1;
157
protected override void FocusFirstElement()
159
Dispatcher.BeginInvoke((Action)TryFocusAndSelectItem, DispatcherPriority.Background);
162
void TryFocusAndSelectItem()
164
if (!parameterList.Any())
167
object ctorParamWrapper = varList.Items.GetItemAt(0);
168
if (ctorParamWrapper != null) {
169
ListBoxItem item = (ListBoxItem)varList.ItemContainerGenerator.ContainerFromItem(ctorParamWrapper);
172
varList.ScrollIntoView(item);
173
varList.SelectedItem = item;
174
Keyboard.Focus(item);
178
protected override void OnInsertionCompleted()
180
base.OnInsertionCompleted();
182
Dispatcher.BeginInvoke(
183
DispatcherPriority.Background,
186
if (!parameterList.Any())
187
context.Deactivate(null);
189
insertionEndAnchor = editor.Document.CreateAnchor(anchor.Offset);
190
insertionEndAnchor.MovementType = AnchorMovementType.AfterInsertion;
197
void SelectAllChecked(object sender, System.Windows.RoutedEventArgs e)
199
foreach (PropertyOrFieldWrapper param in parameterList) {
200
param.IsSelected = true;
204
void SelectAllUnchecked(object sender, System.Windows.RoutedEventArgs e)
206
foreach (PropertyOrFieldWrapper param in parameterList) {
207
param.IsSelected = false;
211
public bool AllSelected {
212
get { return parameterList.All(p => p.IsSelected); }
215
protected override void CancelButtonClick(object sender, System.Windows.RoutedEventArgs e)
217
base.CancelButtonClick(sender, e);
219
editor.Caret.Offset = anchor.Offset;
222
protected override void OKButtonClick(object sender, System.Windows.RoutedEventArgs e)
224
base.OKButtonClick(sender, e);
226
editor.Caret.Offset = insertionEndAnchor.Offset;
230
[ValueConversion(typeof(int), typeof(bool))]
231
public class IntToBoolConverter : IValueConverter
233
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
235
return ((int)value) != -1;
238
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
240
return ((bool)value) ? 0 : -1;