1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Data;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading;
|
---|
9 | using System.Threading.Tasks;
|
---|
10 | using System.Windows.Forms;
|
---|
11 | using HeuristicLab.PluginInfrastructure;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.JsonInterface.App {
|
---|
14 | public partial class JsonInterfaceForm : Form {
|
---|
15 | private OpenFileDialog openDialog = null;
|
---|
16 | private SaveFileDialog saveDialog = null;
|
---|
17 | private string templatePath = null;
|
---|
18 | private string configPath = null;
|
---|
19 | private string outputPath = null;
|
---|
20 | private int loadingCounter = 0;
|
---|
21 |
|
---|
22 | public JsonInterfaceForm() {
|
---|
23 | openDialog = new OpenFileDialog();
|
---|
24 | openDialog.Multiselect = false;
|
---|
25 | openDialog.DefaultExt = ".json";
|
---|
26 | openDialog.AddExtension = true;
|
---|
27 | openDialog.Filter = "Json-File (*.json)|*.json";
|
---|
28 |
|
---|
29 | saveDialog = new SaveFileDialog();
|
---|
30 | saveDialog.DefaultExt = ".json";
|
---|
31 | saveDialog.AddExtension = true;
|
---|
32 | saveDialog.Filter = "Json-File (*.json)|*.json";
|
---|
33 |
|
---|
34 | InitializeComponent();
|
---|
35 |
|
---|
36 | this.templateOpenButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Open;
|
---|
37 | this.configOpenButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Open;
|
---|
38 | this.outputOpenButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Save;
|
---|
39 | this.Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void OpenTemplate(object sender, EventArgs e) {
|
---|
43 | if(openDialog.ShowDialog() == DialogResult.OK) {
|
---|
44 | templatePath = openDialog.FileName;
|
---|
45 | templateTextBox.Text = templatePath;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void OpenConfig(object sender, EventArgs e) {
|
---|
50 | if (openDialog.ShowDialog() == DialogResult.OK) {
|
---|
51 | configPath = openDialog.FileName;
|
---|
52 | configTextBox.Text = configPath;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void OpenOutput(object sender, EventArgs e) {
|
---|
57 | if (saveDialog.ShowDialog() == DialogResult.OK) {
|
---|
58 | outputPath = saveDialog.FileName;
|
---|
59 | outputTextBox.Text = outputPath;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Run button onClick logic
|
---|
65 | /// </summary>
|
---|
66 | /// <param name="sender"></param>
|
---|
67 | /// <param name="e"></param>
|
---|
68 | private async void Run(object sender, EventArgs e) {
|
---|
69 | runButton.Enabled = false;
|
---|
70 | CancellationTokenSource cts = new CancellationTokenSource();
|
---|
71 | var token = cts.Token;
|
---|
72 |
|
---|
73 | // update text on run button to show a working program (loading/working)
|
---|
74 | Task uiUpdater = Task.Run(async () => {
|
---|
75 | while(!token.IsCancellationRequested) {
|
---|
76 |
|
---|
77 | StringBuilder sb = new StringBuilder("Run");
|
---|
78 | sb.Append('.', loadingCounter);
|
---|
79 | UpdateButton(sb.ToString());
|
---|
80 | loadingCounter = ++loadingCounter % 3;
|
---|
81 | await Task.Delay(500);
|
---|
82 | }
|
---|
83 | }, token);
|
---|
84 |
|
---|
85 | // performs the json interfaces run
|
---|
86 | Task work = Task.Run(() => {
|
---|
87 | if(templatePath != null && configPath != null && outputPath != null) {
|
---|
88 | try {
|
---|
89 | Runner.Run(templatePath, configPath, outputPath);
|
---|
90 | } catch (Exception ex) {
|
---|
91 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | cts.Cancel();
|
---|
95 | });
|
---|
96 |
|
---|
97 | await Task.WhenAll(work, uiUpdater);
|
---|
98 |
|
---|
99 | runButton.Enabled = true;
|
---|
100 | runButton.Text = "Run";
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// Update displayed text for run button.
|
---|
105 | /// </summary>
|
---|
106 | /// <param name="text"></param>
|
---|
107 | private void UpdateButton(string text) {
|
---|
108 | if (InvokeRequired) {
|
---|
109 | Invoke((Action<string>) UpdateButton, text);
|
---|
110 | } else {
|
---|
111 | runButton.Text = text;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|