~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// 
// RefactoringPreviewDialog.cs
//  
// Author:
//       Mike Krüger <mkrueger@novell.com>
// 
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using Gtk;
using Gdk;

using MonoDevelop.Core;
using Mono.TextEditor;
using MonoDevelop.Ide;
using ICSharpCode.NRefactory.TypeSystem;


namespace MonoDevelop.Refactoring
{
	public partial class RefactoringPreviewDialog : Gtk.Dialog
	{
		TreeStore store = new TreeStore (typeof(Gdk.Pixbuf), typeof(string), typeof(object), typeof (bool));

		const int pixbufColumn = 0;
		const int textColumn = 1;
		const int objColumn = 2;
		const int statusVisibleColumn = 3;

		List<Change> changes;

		public RefactoringPreviewDialog (List<Change> changes)
		{
			this.Build ();
			this.changes = changes;
			treeviewPreview.Model = store;

			TreeViewColumn column = new TreeViewColumn ();

			// pixbuf column
			var pixbufCellRenderer = new CellRendererPixbuf ();
			column.PackStart (pixbufCellRenderer, false);
			column.SetAttributes (pixbufCellRenderer, "pixbuf", pixbufColumn);
			column.AddAttribute (pixbufCellRenderer, "visible", statusVisibleColumn);
			
			// text column
			CellRendererText cellRendererText = new CellRendererText ();
			column.PackStart (cellRendererText, false);
			column.SetAttributes (cellRendererText, "text", textColumn);
			column.AddAttribute (cellRendererText, "visible", statusVisibleColumn);
			
			// location column
			CellRendererText cellRendererText2 = new CellRendererText ();
			column.PackStart (cellRendererText2, false);
			column.SetCellDataFunc (cellRendererText2, new TreeCellDataFunc (SetLocationTextData));
			
			CellRendererDiff cellRendererDiff = new CellRendererDiff ();
			column.PackStart (cellRendererDiff, true);
			column.SetCellDataFunc (cellRendererDiff, new TreeCellDataFunc (SetDiffCellData));

			treeviewPreview.AppendColumn (column);
			treeviewPreview.HeadersVisible = false;
			
			buttonCancel.Clicked += delegate {
				Destroy ();
			};
			
			buttonOk.Clicked += delegate {
				IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor (this.Title, null);
				RefactoringService.AcceptChanges (monitor, changes);
				
				Destroy ();
			};
			
			FillChanges ();
		}
		
		void SetLocationTextData (Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			CellRendererText cellRendererText = (CellRendererText)cell;
			Change change = store.GetValue (iter, objColumn) as Change;
			cellRendererText.Visible = (bool)store.GetValue (iter, statusVisibleColumn);
			TextReplaceChange replaceChange = change as TextReplaceChange;
			if (replaceChange == null) {
				cellRendererText.Text = "";
				return;
			}
			
			Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument ();
			doc.Text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText (replaceChange.FileName);
			DocumentLocation loc = doc.OffsetToLocation (replaceChange.Offset);
			
			string text = string.Format (GettextCatalog.GetString ("(Line:{0}, Column:{1})"), loc.Line, loc.Column);
			if (treeviewPreview.Selection.IterIsSelected (iter)) {
				cellRendererText.Text = text;
			} else {
				cellRendererText.Markup = "<span foreground=\"" + MonoDevelop.Components.PangoCairoHelper.GetColorString (Style.Text (StateType.Insensitive)) + "\">" + text + "</span>";
			}
		}
		
		void SetDiffCellData (Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			try {
				CellRendererDiff cellRendererDiff = (CellRendererDiff)cell;
				Change change = store.GetValue (iter, objColumn) as Change;
				cellRendererDiff.Visible = !(bool)store.GetValue (iter, statusVisibleColumn);
				if (change == null || !cellRendererDiff.Visible) {
					cellRendererDiff.InitCell (treeviewPreview, false, "", "");
					return;
				}
				TextReplaceChange replaceChange = change as TextReplaceChange;
				if (replaceChange == null) 
					return;
			
				var openDocument = IdeApp.Workbench.GetDocument (replaceChange.FileName);
				Mono.TextEditor.TextDocument originalDocument = new Mono.TextEditor.TextDocument ();
				originalDocument.FileName = replaceChange.FileName;
				if (openDocument == null) {
					originalDocument.Text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText (replaceChange.FileName);
				} else {
					originalDocument.Text = openDocument.Editor.Document.Text;
				}
				
				Mono.TextEditor.TextDocument changedDocument = new Mono.TextEditor.TextDocument ();
				changedDocument.FileName = replaceChange.FileName;
				changedDocument.Text = originalDocument.Text;
				
				changedDocument.Replace (replaceChange.Offset, replaceChange.RemovedChars, replaceChange.InsertedText);
				
				string diffString = Mono.TextEditor.Utils.Diff.GetDiffString (originalDocument, changedDocument);
				
				cellRendererDiff.InitCell (treeviewPreview, true, diffString, replaceChange.FileName);
			} catch (Exception e) {
				Console.WriteLine (e);
			}
		}

		Dictionary<string, TreeIter> fileDictionary = new Dictionary<string, TreeIter> ();
		TreeIter GetFile (Change change)
		{
			TextReplaceChange replaceChange = change as TextReplaceChange;
			if (replaceChange == null) 
				return TreeIter.Zero;
			
			TreeIter result;
			if (!fileDictionary.TryGetValue (replaceChange.FileName, out result))
				fileDictionary[replaceChange.FileName] = result = store.AppendValues (DesktopService.GetPixbufForFile (replaceChange.FileName, IconSize.Menu), System.IO.Path.GetFileName (replaceChange.FileName), null, true);
			return result;
		}

		void FillChanges ()
		{
			foreach (Change change in changes) {
				TreeIter iter = GetFile (change);
				if (iter.Equals (TreeIter.Zero)) {
					iter = store.AppendValues (ImageService.GetPixbuf (MonoDevelop.Ide.Gui.Stock.ReplaceIcon, IconSize.Menu), change.Description, change, true);
				} else {
					iter = store.AppendValues (iter, ImageService.GetPixbuf (MonoDevelop.Ide.Gui.Stock.ReplaceIcon, IconSize.Menu), change.Description, change, true);
				}
				TextReplaceChange replaceChange = change as TextReplaceChange;
				if (replaceChange != null && replaceChange.Offset >= 0)
					store.AppendValues (iter, null, null, change, false);
			}
			if (changes.Count < 4) {
				treeviewPreview.ExpandAll ();
			} else {
				foreach (TreeIter iter in fileDictionary.Values) {
					treeviewPreview.ExpandRow (store.GetPath (iter), false);
				}
			}
		}

		class CellRendererDiff : Gtk.CellRendererText
		{
			Pango.Layout layout;
			Pango.FontDescription font;
			bool diffMode;
			int width, height, lineHeight;
			string[] lines;

			public CellRendererDiff ()
			{
				font = Pango.FontDescription.FromString (DesktopService.DefaultMonospaceFont);
			}

			void DisposeLayout ()
			{
				if (layout != null) {
					layout.Dispose ();
					layout = null;
				}
			}

			bool isDisposed = false;
			protected override void OnDestroyed ()
			{
				isDisposed = true;
				DisposeLayout ();
				if (font != null) {
					font.Dispose ();
					font = null;
				}
				base.OnDestroyed ();
			}

			public void Reset ()
			{
			}

			public void InitCell (Widget container, bool diffMode, string text, string path)
			{
				if (isDisposed)
					return;
				this.diffMode = diffMode;

				if (diffMode) {
					if (text.Length > 0) {
						lines = text.Split ('\n');
						int maxlen = -1;
						int maxlin = -1;
						for (int n = 0; n < lines.Length; n++) {
							if (lines[n].Length > maxlen) {
								maxlen = lines[n].Length;
								maxlin = n;
							}
						}
						DisposeLayout ();
						layout = CreateLayout (container, lines[maxlin]);
						layout.GetPixelSize (out width, out lineHeight);
						height = lineHeight * lines.Length;
					} else
						width = height = 0;
				} else {
					DisposeLayout ();
					layout = CreateLayout (container, text);
					layout.GetPixelSize (out width, out height);
				}
			}

			Pango.Layout CreateLayout (Widget container, string text)
			{
				Pango.Layout layout = new Pango.Layout (container.PangoContext);
				layout.SingleParagraphMode = false;
				if (diffMode) {
					layout.FontDescription = font;
					layout.SetText (text);
				} else
					layout.SetMarkup (text);
				return layout;
			}

			protected override void Render (Drawable window, Widget widget, Gdk.Rectangle background_area, Gdk.Rectangle cell_area, Gdk.Rectangle expose_area, CellRendererState flags)
			{
				if (isDisposed)
					return;
				try {
					if (diffMode) {
						int w, maxy;
						window.GetSize (out w, out maxy);

						int recty = cell_area.Y;
						int recth = cell_area.Height - 1;
						if (recty < 0) {
							recth += recty + 1;
							recty = -1;
						}
						if (recth > maxy + 2)
							recth = maxy + 2;

						window.DrawRectangle (widget.Style.BaseGC (Gtk.StateType.Normal), true, cell_area.X, recty, cell_area.Width - 1, recth);

						Gdk.GC normalGC = widget.Style.TextGC (StateType.Normal);
						Gdk.GC removedGC = new Gdk.GC (window);
						removedGC.Copy (normalGC);
						removedGC.RgbFgColor = new Color (255, 0, 0);
						Gdk.GC addedGC = new Gdk.GC (window);
						addedGC.Copy (normalGC);
						addedGC.RgbFgColor = new Color (0, 0, 255);
						Gdk.GC infoGC = new Gdk.GC (window);
						infoGC.Copy (normalGC);
						infoGC.RgbFgColor = new Color (0xa5, 0x2a, 0x2a);

						int y = cell_area.Y + 2;

						for (int n = 0; n < lines.Length; n++,y += lineHeight) {
							if (y + lineHeight < 0)
								continue;
							if (y > maxy)
								break;
							string line = lines[n];
							if (line.Length == 0)
								continue;

							Gdk.GC gc;
							switch (line[0]) {
							case '-':
								gc = removedGC;
								break;
							case '+':
								gc = addedGC;
								break;
							case '@':
								gc = infoGC;
								break;
							default:
								gc = normalGC;
								break;
							}

							layout.SetText (line);
							window.DrawLayout (gc, cell_area.X + 2, y, layout);
						}
						window.DrawRectangle (widget.Style.DarkGC (Gtk.StateType.Prelight), false, cell_area.X, recty, cell_area.Width - 1, recth);
						removedGC.Dispose ();
						addedGC.Dispose ();
						infoGC.Dispose ();
					} else {
						int y = cell_area.Y + (cell_area.Height - height) / 2;
						window.DrawLayout (widget.Style.TextGC (GetState (flags)), cell_area.X, y, layout);
					}
				} catch (Exception e) {
					Console.WriteLine (e);
				}
			}

			public override void GetSize (Widget widget, ref Rectangle cell_area, out int x_offset, out int y_offset, out int c_width, out int c_height)
			{
				x_offset = y_offset = 0;
				c_width = width;
				c_height = height;

				if (diffMode) {
					// Add some spacing for the margin
					c_width += 4;
					c_height += 4;
				}
			}

			StateType GetState (CellRendererState flags)
			{
				if ((flags & CellRendererState.Selected) != 0)
					return StateType.Selected; else
					return StateType.Normal;
			}
		}
	}
}