Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/JsonInterfaceForm.cs @ 17599

Last change on this file since 17599 was 17599, checked in by dpiringe, 4 years ago

#3026:

  • added a simple form control for JsonInterface to use it without CLI
File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using HeuristicLab.PluginInfrastructure;
12
13namespace 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    }
40
41    private void OpenTemplate(object sender, EventArgs e) {
42      if(openDialog.ShowDialog() == DialogResult.OK) {
43        templatePath = openDialog.FileName;
44        templateTextBox.Text = templatePath;
45      }
46    }
47
48    private void OpenConfig(object sender, EventArgs e) {
49      if (openDialog.ShowDialog() == DialogResult.OK) {
50        configPath = openDialog.FileName;
51        configTextBox.Text = configPath;
52      }
53    }
54
55    private void OpenOutput(object sender, EventArgs e) {
56      if (saveDialog.ShowDialog() == DialogResult.OK) {
57        outputPath = saveDialog.FileName;
58        outputTextBox.Text = outputPath;
59      }
60    }
61
62    /// <summary>
63    /// Run button onClick logic
64    /// </summary>
65    /// <param name="sender"></param>
66    /// <param name="e"></param>
67    private async void Run(object sender, EventArgs e) {
68      runButton.Enabled = false;
69      CancellationTokenSource cts = new CancellationTokenSource();
70      var token = cts.Token;
71
72      // update text on run button to show a working program (loading/working)
73      Task uiUpdater = Task.Run(async () => {
74        while(!token.IsCancellationRequested) {
75
76          StringBuilder sb = new StringBuilder("Run");
77          sb.Append('.', loadingCounter);
78          UpdateButton(sb.ToString());
79          loadingCounter = ++loadingCounter % 3;
80          await Task.Delay(500);
81        }
82      }, token);
83
84      // performs the json interfaces run
85      Task work = Task.Run(() => {
86        if(templatePath != null && configPath != null && outputPath != null) {
87          try {
88            Runner.Run(templatePath, configPath, outputPath);
89          } catch (Exception ex) {
90            ErrorHandling.ShowErrorDialog(this, ex);
91          }
92        }
93        cts.Cancel();
94      });
95
96      await Task.WhenAll(work, uiUpdater);
97
98      runButton.Enabled = true;
99      runButton.Text = "Run";
100    }
101
102    /// <summary>
103    /// Update displayed text for run button.
104    /// </summary>
105    /// <param name="text"></param>
106    private void UpdateButton(string text) {
107      if (InvokeRequired) {
108        Invoke((Action<string>) UpdateButton, text);
109      } else {
110        runButton.Text = text;
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.