2
// ExtractMethodDialog.cs
5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
using MonoDevelop.Core;
32
using MonoDevelop.Projects.Dom;
33
using MonoDevelop.Projects.CodeGeneration;
34
using System.Collections.Generic;
35
using MonoDevelop.Ide;
36
using Mono.TextEditor;
37
using Mono.TextEditor.PopupWindow;
38
using MonoDevelop.Refactoring;
40
namespace MonoDevelop.CSharp.Refactoring.ExtractMethod
42
public partial class ExtractMethodDialog : Gtk.Dialog
44
ExtractMethodRefactoring extractMethod;
45
ExtractMethodRefactoring.ExtractMethodParameters properties;
47
RefactoringOptions options;
50
bool generateComments;
52
public ExtractMethodDialog (RefactoringOptions options, ExtractMethodRefactoring extractMethod, ExtractMethodRefactoring.ExtractMethodParameters properties)
55
this.options = options;
56
this.properties = properties;
57
this.extractMethod = extractMethod;
59
store = new ListStore (typeof (string), typeof (string));
60
treeviewParameters.Model = store;
61
treeviewParameters.AppendColumn ("Type", new CellRendererText (), "text", 0);
62
treeviewParameters.AppendColumn ("Name", new CellRendererText (), "text", 1);
64
buttonPreview.Sensitive = buttonOk.Sensitive = false;
65
entry.Changed += delegate { buttonPreview.Sensitive = buttonOk.Sensitive = ValidateName (); };
68
buttonOk.Clicked += OnOKClicked;
69
buttonCancel.Clicked += OnCancelClicked;
70
buttonPreview.Clicked += OnPreviewClicked;
72
buttonUp.Clicked += delegate {
73
List<int> indices = new List<int> ();
74
foreach (TreePath path in treeviewParameters.Selection.GetSelectedRows ()) {
75
int index = Int32.Parse (path.ToString ());
77
VariableDescriptor tmp = properties.Parameters [index - 1];
78
properties.Parameters [index - 1] = properties.Parameters [index];
79
properties.Parameters [index] = tmp;
80
indices.Add (index - 1);
84
treeviewParameters.Selection.SelectPath (new TreePath (indices.ToArray ()));
86
buttonDown.Clicked += delegate {
87
List<int> indices = new List<int> ();
88
foreach (TreePath path in treeviewParameters.Selection.GetSelectedRows ()) {
89
int index = Int32.Parse (path.ToString ());
90
if (index + 1 < properties.Parameters.Count) {
91
VariableDescriptor tmp = properties.Parameters [index + 1];
92
properties.Parameters [index + 1] = properties.Parameters [index];
93
properties.Parameters [index] = tmp;
94
indices.Add (index + 1);
98
treeviewParameters.Selection.SelectPath (new TreePath (indices.ToArray ()));
100
ListStore modifiers = new ListStore (typeof (string));
101
modifiers.AppendValues ("");
102
modifiers.AppendValues ("public");
103
modifiers.AppendValues ("private");
104
modifiers.AppendValues ("protected");
105
modifiers.AppendValues ("internal");
106
comboboxModifiers.Model = modifiers;
107
comboboxModifiers.Active = PropertyService.Get<int> ("MonoDevelop.Refactoring.ExtractMethod.ExtractMethodDialog.DefaultModifier");
108
entry.Activated += delegate {
109
if (buttonOk.Sensitive)
117
foreach (VariableDescriptor var in properties.Parameters) {
118
store.AppendValues (var.ReturnType != null ? var.ReturnType.ToInvariantString () : "<null>" , var.Name);
124
string fileName = properties.DeclaringMember.DeclaringType.CompilationUnit.FileName;
125
string methodName = entry.Text;
126
if (HasMember (methodName)) {
127
labelWarning.Text = GettextCatalog.GetString ("A member with the name '{0}' already exists.", methodName);
128
imageWarning.IconName = Gtk.Stock.DialogError;
132
INameValidator nameValidator = MonoDevelop.Projects.LanguageBindingService.GetRefactorerForFile (fileName ?? "default.cs");
133
if (nameValidator == null)
135
ValidationResult result = nameValidator.ValidateName (new DomMethod (), entry.Text);
136
if (!result.IsValid) {
137
imageWarning.IconName = Gtk.Stock.DialogError;
138
} else if (result.HasWarning) {
139
imageWarning.IconName = Gtk.Stock.DialogWarning;
141
imageWarning.IconName = Gtk.Stock.Apply;
143
labelWarning.Text = result.Message;
144
return result.IsValid;
147
bool HasMember (string name)
149
foreach (var member in properties.DeclaringMember.DeclaringType.SearchMember (name, true)) {
150
var method = member as IMethod;
153
if (method.Parameters.Count != properties.Parameters.Count)
156
for (int i = 0; i < method.Parameters.Count; i++) {
157
if (properties.Parameters[i].ReturnType.ToInvariantString () != member.Parameters[i].ReturnType.ToInvariantString ()) {
168
void OnCancelClicked (object sender, EventArgs e)
173
void SetProperties ()
175
properties.Name = methodName;
176
properties.GenerateComment = generateComments;
177
switch (activeModifier) {
179
properties.Modifiers = Modifiers.None;
182
properties.Modifiers = Modifiers.Public;
185
properties.Modifiers = Modifiers.Private;
188
properties.Modifiers = Modifiers.Protected;
191
properties.Modifiers = Modifiers.Internal;
194
PropertyService.Set ("MonoDevelop.Refactoring.ExtractMethod.ExtractMethodDialog.DefaultModifier", comboboxModifiers.Active);
197
void OnOKClicked (object sender, EventArgs e)
199
TextEditorData data = options.GetTextEditorData ();
200
Mono.TextEditor.TextEditor editor = data.Parent;
201
// Insertion cursor mode test:
202
if (editor != null) {
203
IType type = properties.DeclaringMember.DeclaringType;
205
InsertionCursorEditMode mode = new InsertionCursorEditMode (editor, CodeGenerationService.GetInsertionPoints (options.Document, type));
206
for (int i = 0; i < mode.InsertionPoints.Count; i++) {
207
var point = mode.InsertionPoints[i];
208
if (point.Location < editor.Caret.Location) {
214
ModeHelpWindow helpWindow = new ModeHelpWindow ();
215
helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
216
helpWindow.TitleText = GettextCatalog.GetString ("<b>Extract Method -- Targeting</b>");
217
helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Key</b>"), GettextCatalog.GetString ("<b>Behavior</b>")));
218
helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Up</b>"), GettextCatalog.GetString ("Move to <b>previous</b> target point.")));
219
helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Down</b>"), GettextCatalog.GetString ("Move to <b>next</b> target point.")));
220
helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Enter</b>"), GettextCatalog.GetString ("<b>Declare new method</b> at target point.")));
221
helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Esc</b>"), GettextCatalog.GetString ("<b>Cancel</b> this refactoring.")));
222
mode.HelpWindow = helpWindow;
224
methodName = entry.Text;
225
activeModifier = comboboxModifiers.Active;
226
generateComments = checkbuttonGenerateComment.Active;
228
mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
231
properties.InsertionPoint = args.InsertionPoint;
232
List<Change> changes = extractMethod.PerformChanges (options, properties);
233
IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor (this.Title, null);
234
RefactoringService.AcceptChanges (monitor, options.Dom, changes);
239
((Widget)this).Destroy ();
242
void OnPreviewClicked (object sender, EventArgs e)
244
methodName = entry.Text;
246
List<Change> changes = extractMethod.PerformChanges (options, properties);
247
((Widget)this).Destroy ();
248
MessageService.ShowCustomDialog (new RefactoringPreviewDialog (options.Dom, changes));