~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.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.Generic;
 
6
using System.ComponentModel;
 
7
using System.Globalization;
 
8
using System.Linq;
 
9
using System.Text;
 
10
using System.Windows.Controls;
 
11
using System.Windows.Data;
 
12
using System.Windows.Input;
 
13
using System.Windows.Threading;
 
14
 
 
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;
 
20
 
 
21
namespace SharpRefactoring.Gui
 
22
{
 
23
        /// <summary>
 
24
        /// Interaction logic for InsertCtorDialog.xaml
 
25
        /// </summary>
 
26
        public partial class InsertCtorDialog : AbstractInlineRefactorDialog
 
27
        {
 
28
                IList<PropertyOrFieldWrapper> parameterList;
 
29
                
 
30
                public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList<PropertyOrFieldWrapper> possibleParameters)
 
31
                        : base(context, editor, anchor)
 
32
                {
 
33
                        InitializeComponent();
 
34
                        
 
35
                        this.varList.ItemsSource = parameterList = possibleParameters;
 
36
                        
 
37
                        if (!parameterList.Any())
 
38
                                Visibility = System.Windows.Visibility.Collapsed;
 
39
                }
 
40
                
 
41
                protected override string GenerateCode(LanguageProperties language, IClass currentClass)
 
42
                {
 
43
                        IDocumentLine line = editor.Document.GetLineForOffset(anchor.Offset);
 
44
                        
 
45
                        string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset);
 
46
                        
 
47
                        List<PropertyOrFieldWrapper> filtered = parameterList
 
48
                                .Where(p => p.IsSelected)
 
49
                                .OrderBy(p => p.Index)
 
50
                                .ToList();
 
51
                        
 
52
                        BlockStatement block = new BlockStatement();
 
53
                        
 
54
                        foreach (PropertyOrFieldWrapper w in filtered) {
 
55
                                if (w.AddCheckForNull) {
 
56
                                        // true = reference, null = generic or unknown
 
57
                                        if (w.Type.IsReferenceType != false)
 
58
                                                block.AddChild(
 
59
                                                        new IfElseStatement(
 
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 + '"') }))
 
62
                                                        )
 
63
                                                );
 
64
                                        else
 
65
                                                block.AddChild(
 
66
                                                        new IfElseStatement(
 
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 + '"') }))
 
69
                                                        )
 
70
                                                );
 
71
                                }
 
72
                                if (w.AddRangeCheck) {
 
73
                                        block.AddChild(
 
74
                                                new IfElseStatement(
 
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"))
 
79
                                                        ),
 
80
                                                        new ThrowStatement(
 
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")))) }
 
84
                                                                )
 
85
                                                        )
 
86
                                                )
 
87
                                        );
 
88
                                }
 
89
                        }
 
90
                        
 
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))));
 
93
                        
 
94
                        AnchorElement parameterListElement = context.ActiveElements
 
95
                                .OfType<AnchorElement>()
 
96
                                .FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));
 
97
                        
 
98
                        if (parameterListElement != null) {
 
99
                                StringBuilder pList = new StringBuilder();
 
100
                                
 
101
                                var parameters = filtered
 
102
                                        .Select(p => new ParameterDeclarationExpression(ConvertType(p.Type), p.ParameterName))
 
103
                                        .ToList();
 
104
                                
 
105
                                for (int i = 0; i < parameters.Count; i++) {
 
106
                                        if (i > 0)
 
107
                                                pList.Append(", ");
 
108
                                        pList.Append(language.CodeGenerator.GenerateCode(parameters[i], ""));
 
109
                                }
 
110
                                
 
111
                                parameterListElement.Text = pList.ToString();
 
112
                        }
 
113
                        
 
114
                        StringBuilder builder = new StringBuilder();
 
115
                        
 
116
                        foreach (var element in block.Children.OfType<AbstractNode>()) {
 
117
                                builder.Append(language.CodeGenerator.GenerateCode(element, indent));
 
118
                        }
 
119
                        
 
120
                        return builder.ToString().Trim();
 
121
                }
 
122
                
 
123
                void UpClick(object sender, System.Windows.RoutedEventArgs e)
 
124
                {
 
125
                        int selection = varList.SelectedIndex;
 
126
                        
 
127
                        if (selection <= 0)
 
128
                                return;
 
129
                        
 
130
                        var curItem = parameterList.First(p => p.Index == selection);
 
131
                        var exchangeItem = parameterList.First(p => p.Index == selection - 1);
 
132
                        
 
133
                        curItem.Index = selection - 1;
 
134
                        exchangeItem.Index = selection;
 
135
                        
 
136
                        varList.ItemsSource = parameterList.OrderBy(p => p.Index);
 
137
                        varList.SelectedIndex = selection - 1;
 
138
                }
 
139
                
 
140
                void DownClick(object sender, System.Windows.RoutedEventArgs e)
 
141
                {
 
142
                        int selection = varList.SelectedIndex;
 
143
                        
 
144
                        if (selection < 0 || selection >= parameterList.Count - 1)
 
145
                                return;
 
146
                        
 
147
                        var curItem = parameterList.First(p => p.Index == selection);
 
148
                        var exchangeItem = parameterList.First(p => p.Index == selection + 1);
 
149
                        
 
150
                        curItem.Index = selection + 1;
 
151
                        exchangeItem.Index = selection;
 
152
                        
 
153
                        varList.ItemsSource = parameterList.OrderBy(p => p.Index);
 
154
                        varList.SelectedIndex = selection + 1;
 
155
                }
 
156
                
 
157
                protected override void FocusFirstElement()
 
158
                {
 
159
                        Dispatcher.BeginInvoke((Action)TryFocusAndSelectItem, DispatcherPriority.Background);
 
160
                }
 
161
                
 
162
                void TryFocusAndSelectItem()
 
163
                {
 
164
                        if (!parameterList.Any())
 
165
                                return;
 
166
                        
 
167
                        object ctorParamWrapper = varList.Items.GetItemAt(0);
 
168
                        if (ctorParamWrapper != null) {
 
169
                                ListBoxItem item = (ListBoxItem)varList.ItemContainerGenerator.ContainerFromItem(ctorParamWrapper);
 
170
                                item.Focus();
 
171
                                
 
172
                                varList.ScrollIntoView(item);
 
173
                                varList.SelectedItem = item;
 
174
                                Keyboard.Focus(item);
 
175
                        }
 
176
                }
 
177
                
 
178
                protected override void OnInsertionCompleted()
 
179
                {
 
180
                        base.OnInsertionCompleted();
 
181
                        
 
182
                        Dispatcher.BeginInvoke(
 
183
                                DispatcherPriority.Background,
 
184
                                (Action)(
 
185
                                        () => {
 
186
                                                if (!parameterList.Any())
 
187
                                                        context.Deactivate(null);
 
188
                                                else {
 
189
                                                        insertionEndAnchor = editor.Document.CreateAnchor(anchor.Offset);
 
190
                                                        insertionEndAnchor.MovementType = AnchorMovementType.AfterInsertion;
 
191
                                                }
 
192
                                        }
 
193
                                )
 
194
                        );
 
195
                }
 
196
                
 
197
                void SelectAllChecked(object sender, System.Windows.RoutedEventArgs e)
 
198
                {
 
199
                        foreach (PropertyOrFieldWrapper param in parameterList) {
 
200
                                param.IsSelected = true;
 
201
                        }
 
202
                }
 
203
                
 
204
                void SelectAllUnchecked(object sender, System.Windows.RoutedEventArgs e)
 
205
                {
 
206
                        foreach (PropertyOrFieldWrapper param in parameterList) {
 
207
                                param.IsSelected = false;
 
208
                        }
 
209
                }
 
210
                
 
211
                public bool AllSelected {
 
212
                        get { return parameterList.All(p => p.IsSelected); }
 
213
                }
 
214
                
 
215
                protected override void CancelButtonClick(object sender, System.Windows.RoutedEventArgs e)
 
216
                {
 
217
                        base.CancelButtonClick(sender, e);
 
218
                        
 
219
                        editor.Caret.Offset = anchor.Offset;
 
220
                }
 
221
                
 
222
                protected override void OKButtonClick(object sender, System.Windows.RoutedEventArgs e)
 
223
                {
 
224
                        base.OKButtonClick(sender, e);
 
225
                        
 
226
                        editor.Caret.Offset = insertionEndAnchor.Offset;
 
227
                }
 
228
        }
 
229
        
 
230
        [ValueConversion(typeof(int), typeof(bool))]
 
231
        public class IntToBoolConverter : IValueConverter
 
232
        {
 
233
                public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 
234
                {
 
235
                        return ((int)value) != -1;
 
236
                }
 
237
                
 
238
                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
 
239
                {
 
240
                        return ((bool)value) ? 0 : -1;
 
241
                }
 
242
        }
 
243
}