-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
384 lines (339 loc) · 17 KB
/
MainWindow.xaml.cs
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using AENC.Models;
using iTextSharp.text;
using iTextSharp.text.pdf;
using PdfSharp;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Test;
using Document = iTextSharp.text.Document;
using PdfReader = iTextSharp.text.pdf.PdfReader;
using Rectangle = iTextSharp.text.Rectangle;
namespace AENC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public List<Metric> AllMetrics = new List<Metric>();
public List<Metric> SelectedMetrics = new List<Metric>();
int auxswitchTab = 0;
string filename, printer, path;
bool allmetrics = false;
public MainWindow()
{
InitializeComponent();
FillMetricsList();
}
#region Metrics
private void combobox_lvl_test_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(combobox_lvl_test.SelectedValue != null)
{
SelectedMetrics.Clear();
checkboxAllMetrics.IsChecked = false;
foreach (var selectallmetrics in AllMetrics)
{
selectallmetrics.IsSelected = false;
}
object LvlMetric = combobox_lvl_test.SelectedValue;
Metric MetricPattern = (Metric)LvlMetric;
if (MetricPattern.MetricLvl == "All")
SelectedMetrics = AllMetrics.Where(x => x.MetricLvl != "All").ToList();
else
SelectedMetrics = AllMetrics.Where(x => x.MetricLvl == MetricPattern.MetricLvl).ToList();
dg_metrics.ItemsSource = null;
dg_metrics.ItemsSource = SelectedMetrics;
UploadFile.IsEnabled = true;
btn_scancode.IsEnabled = true;
btn_scancode.IsEnabled = false;
lvl.Content = MetricPattern.MetricLvl;
}
else
{
UploadFile.IsEnabled = false;
btn_scancode.IsEnabled = false;
dg_metrics.ItemsSource = null;
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
foreach (var selectallmetrics in SelectedMetrics)
{
selectallmetrics.IsSelected = true;
}
dg_metrics.ItemsSource = null;
dg_metrics.ItemsSource = SelectedMetrics;
allmetrics = true;
}
private void Checkboxall_Unchecked(object sender, RoutedEventArgs e)
{
foreach (var selectallmetrics in SelectedMetrics)
{
selectallmetrics.IsSelected = false;
}
dg_metrics.ItemsSource = null;
dg_metrics.ItemsSource = SelectedMetrics;
allmetrics = false;
}
#endregion
#region Code Analysis
public void UploadFile_Click(object sender, RoutedEventArgs e)
{
// Create a Flow Document
FlowDocument ObjFdoc = new FlowDocument();
filename = "";
Stream mystream;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".cs"; // Default file extension
dlg.Filter = "CS documents (.cs)|*.cs"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
if ((mystream = dlg.OpenFile()) != null)
{
// Open document
filename = dlg.FileName;
OriginalCode.Text = File.ReadAllText(filename);
btn_scancode.IsEnabled = true;
/*FlowDocument contentForStoring =
new FlowDocument(new Paragraph(new Run(File.ReadAllText(filename))));*/
/*var line = OriginalCode.Document.GetLineByOffset(50);
var lineNumber = line.LineNumber;
//Select the text.
OriginalCode.SelectionStart = 1;
OriginalCode.SelectionLength = 2;
//Scroll the textEditor to the selected line.
var visualTop = OriginalCode.TextArea.TextView.GetVisualTopByDocumentLine(lineNumber);
OriginalCode.ScrollToVerticalOffset(visualTop);*/
}
lvUsers.ItemsSource = null;
}
}
private void btn_scancode_Click(object sender, RoutedEventArgs e)
{
List<CodeSmellDetected> itemss = new List<CodeSmellDetected>();
printer = "";
/*itemss.Add(new CodeSmellDetected() { CodeSmellName = "dores do pe", CodeSmellClass = "John asdfas", CodeSmellFunction = "John ttt", CodeSmellLine = 1 });
itemss.Add(new CodeSmellDetected() { CodeSmellName = "de cabeça", CodeSmellClass = "John Doe", CodeSmellFunction = "John ghkfgjk", CodeSmellLine = 2 });
itemss.Add(new CodeSmellDetected() { CodeSmellName = "de cabeça", CodeSmellClass = "John uiiu", CodeSmellFunction = "gsa", CodeSmellLine = 3 });*/
CodeSmeller my = new CodeSmeller(filename);
foreach (Namespace_t var in my.namespacesArray)
{
if (var != null)
{
foreach (Class_t var1 in var.classArray)
{
if (var1 != null)
{
if (SelectedMetrics.Where(x => x.MetricID == 7).ToList()[0].IsSelected & var1.hasToManyLines())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Too Many Lines", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = "---", CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 1).ToList()[0].IsSelected & var1.hasToManyMethods())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Too Many Methods", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = "---", CodeSmellLine = 1 });
}
foreach (Methods_t var2 in var1.methodArray)
{
if (var2 != null)
{
List<string> unusedparam = var2.hasUnusedParameters();
if (SelectedMetrics.Where(x => x.MetricID == 3).ToList()[0].IsSelected & unusedparam.Count != 0)
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Unused Parameters", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 7).ToList()[0].IsSelected & var2.hasToManyLines())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Too Many Lines", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 2).ToList()[0].IsSelected & !var2.isTheLineToCommentRatioGood())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Bad Line to Comment Ratio", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 5).ToList()[0].IsSelected & var2.toMuchNesting())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Too Much Nesting", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 6).ToList()[0].IsSelected & var2.switchCaseToComplex())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "High Switch Case Complexity", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
if (SelectedMetrics.Where(x => x.MetricID == 4).ToList()[0].IsSelected & var2.hasToManyParameters())
{
itemss.Add(new CodeSmellDetected() { CodeSmellName = "Too Many Parameters", CodeSmellClass = var1.classname.Replace("()", ""), CodeSmellFunction = var2.functionName, CodeSmellLine = 1 });
}
}
}
}
}
}
}
printer = "Code Smells found on code!\n\n";
foreach (CodeSmellDetected var in itemss)
{
printer += var.CodeSmellName + " Smell found on class: " + var.CodeSmellFunction + "\n";
}
lvUsers.ItemsSource = itemss;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("CodeSmellName");
view.GroupDescriptions.Add(groupDescription);
}
private void Printer_Click(object sender, RoutedEventArgs e)
{
iTextSharp.text.Document oDoc = new iTextSharp.text.Document();
PdfWriter.GetInstance(oDoc, new FileStream(@"C:\Users\pedro\Desktop\4Ano\TVSI\AENC.pdf", FileMode.Create));
oDoc.Open();
oDoc.Add(new iTextSharp.text.Paragraph(printer));
oDoc.Close();
}
#endregion
#region About Code Smells
private void combobox_txt_name_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(combobox_txt_name.SelectedValue != null)
{
object Metric = combobox_txt_name.SelectedValue;
Metric MetricPattern = (Metric)Metric;
AboutMetrics.Text = MetricPattern.MetricDescription;
AboutMetrics.Text += "\n\n";
AboutMetrics.Text += MetricPattern.MetricDetails;
}
else
{
AboutMetrics.Text = null;
}
}
#endregion
#region Others
public void FillMetricsList()
{
AllMetrics.Clear();
SelectedMetrics.Clear();
UploadFile.IsEnabled = false;
btn_scancode.IsEnabled = false;
printer = "";
AllMetrics.Add(new Metric
{
MetricID = 1,
MetricLvl = "2",
MetricName = "Amount of Class Methods",
MetricDescription = "Evaluate if the class has to many methods",
MetricDetails = "It is good practice to avoid creating classes in code with too many associated methods. " +
"At some point it may be a little difficult to realize the usefulness of each function within the class.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 2,
MetricLvl = "2",
MetricName = "Line to Comment Ratio",
MetricDescription = "Evaluate if there is a good ratio between code and comments",
MetricDetails = "Each piece of code should be properly commented for a better external perception of its functionality. " +
"On the other hand, overly commented code can mean its poor quality.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 3,
MetricLvl = "1",
MetricName = "Parameters Usability",
MetricDescription = "Evaluate if all function parameters are used",
MetricDetails = "If a function has input parameters, these must be used within it, " +
"otherwise the parameters are useless for the functionality of the function.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 4,
MetricLvl = "2",
MetricName = "Amount of Method Parameters",
MetricDescription = "Evaluate if the method has to many parameters",
MetricDetails = "Too many input parameters of a method is an indicator of a bad coding practice. " +
"There are certain limits to the use of input parameters in a function and quantity is one of them.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 5,
MetricLvl = "3",
MetricName = "Method Nesting",
MetricDescription = "Evaluate the method nesting level",
MetricDetails = "The level of nesting in a program's code is something that can be measured to evaluate its simplicity. " +
"Apart from certain exceptions the nesting level of a code should not be too high to be easier to maintain.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 6,
MetricLvl = "3",
MetricName = "Decision Blocks Complexity",
MetricDescription = "Evaluate the overall complexity of decision blocks",
MetricDetails = "The complexity of the decision blocks in the code should be as small as possible.\n " +
"Huge complexity in these blocks brings problems of comprehension and maintenance.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 7,
MetricLvl = "1",
MetricName = "Amount of Code Lines",
MetricDescription = "Evaluate the number of lines of code in the class and function.",
MetricDetails = "Number of lines of code in a function or class is important when it comes to code cleanup. " +
"Obviously, an excessively large number of lines of code make it more difficult to interpret.",
IsSelected = false
});
AllMetrics.Add(new Metric
{
MetricID = 8,
MetricLvl = "All",
MetricName = "",
IsSelected = false
});
combobox_lvl_test.ItemsSource = null;
combobox_lvl_test.ItemsSource = AllMetrics.GroupBy(x => x.MetricLvl).Select(y => y.First());
combobox_txt_name.ItemsSource = null;
combobox_txt_name.ItemsSource = AllMetrics.Where(x => x.MetricLvl != "All").ToList();
}
private void Tabcontrol_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource == Tabcontrol)
{
if (Tabcontrol.SelectedIndex == 1)
{
if (dg_metrics.SelectedCells.Count() != 0 || allmetrics)
return;
if(auxswitchTab == 1)
auxswitchTab = 0;
else
{
SelectedMetrics win = new SelectedMetrics();
win.Show();
auxswitchTab = 1;
}
Tabcontrol.SelectedIndex = 0;
}
}
}
#endregion
}
}