-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsWindow.cs
88 lines (79 loc) · 3.03 KB
/
SettingsWindow.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
using Microsoft.VisualStudio.Shell;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JSONExtension
{
public partial class SettingsWindow : Form
{
public SettingsWindow(string oldLanguageCode = "", string oldLanguagePath = "")
{
InitializeComponent();
languageCodeText.Text = oldLanguageCode;
languagePathText.Text = oldLanguagePath;
this.Cursor = new Cursor(Cursor.Current.Handle);
this.Location = Cursor.Position; //move window to the cursor location
}
private void AcceptButton_Click(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
string languageCode = languageCodeText.Text;
string languagePath = languagePathText.Text;
string projectPath = JSONExtensionPackage.settings.projectPath; //get project path from settings
if (!string.IsNullOrEmpty(projectPath) && !string.IsNullOrEmpty(languageCode) && !string.IsNullOrEmpty(languagePath))
{
SettingsJSON settingsJSON = new SettingsJSON
{
languageCode = languageCode,
languagePath = languagePath
};
File.WriteAllText(Path.Combine(projectPath, ".JSONExSettings"), JsonConvert.SerializeObject(settingsJSON));
JSONExtensionPackage.settings.LoadLangFile(true); //force reload lang file
Dispose();
}
else
{
MessageBox.Show("Error selecting JSON file\n1. Open project for which you want to set JSON file.\n2. Select valid JSON.", "JSONEx");
}
}
private void BrowseButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog(); //open file explorer
if (openFileDialog.ShowDialog() == DialogResult.OK) //if OK clicked save path to text box
{
languagePathText.Text = openFileDialog.FileName;
}
JSchema schemanet = JSchema.Parse(@"
{
'type': 'object',
'required': ['en'],
'properties': {
'en': {
'type': 'object',
'patternProperties': {
'^[a-zA-Z0-9-_]*$': { 'type':'string'}
}
}
}
}
");
JObject jsonToVerify = JObject.Parse(File.ReadAllText(languagePathText.Text));
bool valid = jsonToVerify.IsValid(schemanet);
if (!valid)
{
MessageBox.Show("Error selecting JSON file\nFile does not match schema.", "JSONEx");
return;
}
}
}
}