FrmProxyCmdConfig: Basic ProxyCmd Configurator.

This commit is contained in:
2018-11-02 12:52:12 +01:00
parent bdd857d489
commit abaa7e5e88
5 changed files with 348 additions and 60 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using VAR.Toolbox.Code;
@@ -53,7 +52,7 @@ namespace VAR.Toolbox.UI
}
return;
}
if(e.KeyCode == Keys.Enter)
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
return;
@@ -72,7 +71,7 @@ namespace VAR.Toolbox.UI
{
_currentHistoryIndex = 0;
}
if (_currentHistoryIndex>=0 && _currentHistoryIndex < _cmdHistory.Count)
if (_currentHistoryIndex >= 0 && _currentHistoryIndex < _cmdHistory.Count)
{
txtInput.Text = _cmdHistory[_currentHistoryIndex];
txtInput.SelectionStart = txtInput.Text.Length;
@@ -109,7 +108,7 @@ namespace VAR.Toolbox.UI
private void btnConfig_Click(object sender, EventArgs e)
{
throw new NotImplementedException("Implement btnConfig_Click");
FrmToolbox.StaticCreateWindow(typeof(FrmProxyCmdConfig));
}
#endregion UI events
@@ -128,7 +127,7 @@ namespace VAR.Toolbox.UI
private void CleanProxyCmdExecutor()
{
IDisposable disposableProxyCmdExecutor = _proxyCmdExecutor as IDisposable;
if (disposableProxyCmdExecutor !=null)
if (disposableProxyCmdExecutor != null)
{
disposableProxyCmdExecutor.Dispose();
}
@@ -155,7 +154,7 @@ namespace VAR.Toolbox.UI
}
Monitor.Exit(_executionLock);
}
public void OutputLine(string line)
{
BeginInvoke(new MethodInvoker(delegate
@@ -173,61 +172,28 @@ namespace VAR.Toolbox.UI
public void LoadConfig()
{
CleanProxyCmdExecutor();
string configFile = GetConfigFileName();
string[] config = null;
if (File.Exists(configFile) == false)
{
config = new string[] { "Dummy|Dummy:" };
}
else
{
config = File.ReadAllLines(configFile);
}
SetLoadedConfig(config);
}
List<ProxyCmdConfigItem> configItems = FrmProxyCmdConfig.GetConfigurationItems();
private void SetLoadedConfig(string[] configLines)
{
string previousSelectedName = null;
ProxyCmdConfigItem selectedConfig = ddlCurrentConfig.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig != null) { previousSelectedName = selectedConfig.Name; }
ddlCurrentConfig.Items.Clear();
foreach (string configLine in configLines)
ddlCurrentConfig.Items.AddRange(configItems.ToArray());
ddlCurrentConfig.SelectedIndex = 0;
if (string.IsNullOrEmpty(previousSelectedName) == false)
{
int idxSplit = configLine.IndexOf('|');
if(idxSplit < 0) { continue; }
string configName = configLine.Substring(0, idxSplit);
string configData = configLine.Substring(idxSplit + 1);
ddlCurrentConfig.Items.Add(new ProxyCmdConfigItem { Name = configName, Config = configData, });
}
if (string.IsNullOrEmpty(previousSelectedName))
{
ddlCurrentConfig.SelectedIndex = 0;
return;
}
foreach(ProxyCmdConfigItem configItem in ddlCurrentConfig.Items)
{
if(configItem.Name == previousSelectedName)
foreach (ProxyCmdConfigItem configItem in ddlCurrentConfig.Items)
{
ddlCurrentConfig.SelectedItem = configItem;
break;
if (configItem.Name == previousSelectedName)
{
ddlCurrentConfig.SelectedItem = configItem;
break;
}
}
}
}
private string GetConfigFileName()
{
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string path = Path.GetDirectoryName(location);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
string configFile = string.Format("{0}/{1}.ProxyCmd.cfg", path, filenameWithoutExtension);
return configFile;
}
private string GetCurrentConfig()
{
ProxyCmdConfigItem selectedConfig = ddlCurrentConfig.SelectedItem as ProxyCmdConfigItem;
@@ -237,11 +203,4 @@ namespace VAR.Toolbox.UI
#endregion Config
}
public class ProxyCmdConfigItem
{
public string Name { get; set; }
public string Config { get; set; }
public override string ToString() { return Name; }
}
}

View File

@@ -0,0 +1,131 @@
namespace VAR.Toolbox.UI
{
partial class FrmProxyCmdConfig
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lsvCmdProxyConfigs = new System.Windows.Forms.ListBox();
this.txtCmdProxyConfigName = new System.Windows.Forms.TextBox();
this.btnSave = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnNew = new System.Windows.Forms.Button();
this.txtCmdProxyConfigContent = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lsvCmdProxyConfigs
//
this.lsvCmdProxyConfigs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.lsvCmdProxyConfigs.FormattingEnabled = true;
this.lsvCmdProxyConfigs.Location = new System.Drawing.Point(13, 13);
this.lsvCmdProxyConfigs.Name = "lsvCmdProxyConfigs";
this.lsvCmdProxyConfigs.Size = new System.Drawing.Size(149, 355);
this.lsvCmdProxyConfigs.TabIndex = 0;
this.lsvCmdProxyConfigs.SelectedIndexChanged += new System.EventHandler(this.lsvCmdProxyConfigs_SelectedIndexChanged);
//
// txtCmdProxyConfigName
//
this.txtCmdProxyConfigName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtCmdProxyConfigName.Location = new System.Drawing.Point(169, 13);
this.txtCmdProxyConfigName.Name = "txtCmdProxyConfigName";
this.txtCmdProxyConfigName.Size = new System.Drawing.Size(315, 20);
this.txtCmdProxyConfigName.TabIndex = 2;
//
// btnSave
//
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnSave.Location = new System.Drawing.Point(409, 351);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 3;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnDelete
//
this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnDelete.Location = new System.Drawing.Point(328, 351);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(75, 23);
this.btnDelete.TabIndex = 4;
this.btnDelete.Text = "Delete";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnNew
//
this.btnNew.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnNew.Location = new System.Drawing.Point(247, 351);
this.btnNew.Name = "btnNew";
this.btnNew.Size = new System.Drawing.Size(75, 23);
this.btnNew.TabIndex = 5;
this.btnNew.Text = "New";
this.btnNew.UseVisualStyleBackColor = true;
this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
//
// txtCmdProxyConfigContent
//
this.txtCmdProxyConfigContent.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtCmdProxyConfigContent.Location = new System.Drawing.Point(168, 39);
this.txtCmdProxyConfigContent.Multiline = true;
this.txtCmdProxyConfigContent.Name = "txtCmdProxyConfigContent";
this.txtCmdProxyConfigContent.Size = new System.Drawing.Size(316, 306);
this.txtCmdProxyConfigContent.TabIndex = 6;
//
// FrmProxyCmdConfig
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(496, 386);
this.Controls.Add(this.txtCmdProxyConfigContent);
this.Controls.Add(this.btnNew);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.txtCmdProxyConfigName);
this.Controls.Add(this.lsvCmdProxyConfigs);
this.MinimumSize = new System.Drawing.Size(440, 250);
this.Name = "FrmProxyCmdConfig";
this.Text = "ProxyCmdConfig";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox lsvCmdProxyConfigs;
private System.Windows.Forms.TextBox txtCmdProxyConfigName;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnNew;
private System.Windows.Forms.TextBox txtCmdProxyConfigContent;
}
}

View File

@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace VAR.Toolbox.UI
{
public partial class FrmProxyCmdConfig : Form
{
public FrmProxyCmdConfig()
{
InitializeComponent();
LoadData();
}
private void lsvCmdProxyConfigs_SelectedIndexChanged(object sender, EventArgs e)
{
ProxyCmdConfigItem selectedConfig = lsvCmdProxyConfigs.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig == null) { CleanConfig(); return; }
ShowConfig(selectedConfig);
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveConfig();
}
private void btnDelete_Click(object sender, EventArgs e)
{
DeleteSelected();
}
private void btnNew_Click(object sender, EventArgs e)
{
CreateNew();
}
private void LoadData()
{
lsvCmdProxyConfigs.Items.Clear();
List<ProxyCmdConfigItem> configItems = FrmProxyCmdConfig.GetConfigurationItems();
lsvCmdProxyConfigs.Items.AddRange(configItems.ToArray());
}
private void SaveData()
{
StringBuilder sbConfig = new StringBuilder();
foreach (object o in lsvCmdProxyConfigs.Items)
{
ProxyCmdConfigItem config = o as ProxyCmdConfigItem;
if (config == null) { continue; }
sbConfig.AppendFormat("{0}|{1}\n", config.Name, config.Config);
}
string configFileName = GetConfigFileName();
File.WriteAllText(configFileName, sbConfig.ToString());
List<FrmProxyCmd> listForms = FrmToolbox.StaticGetWindowsOfType<FrmProxyCmd>();
foreach (FrmProxyCmd frm in listForms)
{
frm.LoadConfig();
}
}
private void CleanConfig()
{
txtCmdProxyConfigName.Text = string.Empty;
txtCmdProxyConfigContent.Text = string.Empty;
}
private void ShowConfig(ProxyCmdConfigItem config)
{
txtCmdProxyConfigName.Text = config.Name;
txtCmdProxyConfigContent.Text = config.Config;
}
private void SaveConfig()
{
ProxyCmdConfigItem selectedConfig = lsvCmdProxyConfigs.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig == null)
{
selectedConfig = new ProxyCmdConfigItem();
selectedConfig.Name = txtCmdProxyConfigName.Text;
selectedConfig.Config = txtCmdProxyConfigContent.Text;
lsvCmdProxyConfigs.Items.Add(selectedConfig);
}
else
{
selectedConfig.Name = txtCmdProxyConfigName.Text;
selectedConfig.Config = txtCmdProxyConfigContent.Text;
}
SaveData();
}
private void DeleteSelected()
{
ProxyCmdConfigItem selectedConfig = lsvCmdProxyConfigs.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig == null) { return; }
List<ProxyCmdConfigItem> configItems = new List<ProxyCmdConfigItem>();
foreach (object o in lsvCmdProxyConfigs.Items)
{
ProxyCmdConfigItem config = o as ProxyCmdConfigItem;
if (config == null) { continue; }
if (config.Name == selectedConfig.Name) { continue; }
configItems.Add(config);
}
lsvCmdProxyConfigs.Items.Clear();
lsvCmdProxyConfigs.Items.AddRange(configItems.ToArray());
SaveData();
CleanConfig();
}
private void CreateNew()
{
lsvCmdProxyConfigs.SelectedIndex = -1;
CleanConfig();
}
public static string GetConfigFileName()
{
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string path = Path.GetDirectoryName(location);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
string configFile = string.Format("{0}/{1}.ProxyCmd.cfg", path, filenameWithoutExtension);
return configFile;
}
public static string[] GetConfigurationLines()
{
string configFile = GetConfigFileName();
string[] config = null;
if (File.Exists(configFile) == false)
{
config = new string[] { "Dummy|Dummy:" };
}
else
{
config = File.ReadAllLines(configFile);
}
return config;
}
public static List<ProxyCmdConfigItem> GetConfigurationItems()
{
string[] configLines = GetConfigurationLines();
List<ProxyCmdConfigItem> configItems = new List<ProxyCmdConfigItem>();
foreach (string configLine in configLines)
{
int idxSplit = configLine.IndexOf('|');
if (idxSplit < 0) { continue; }
string configName = configLine.Substring(0, idxSplit);
string configData = configLine.Substring(idxSplit + 1);
configItems.Add(new ProxyCmdConfigItem { Name = configName, Config = configData, });
}
return configItems;
}
}
public class ProxyCmdConfigItem
{
public string Name { get; set; }
public string Config { get; set; }
public override string ToString() { return Name; }
}
}

View File

@@ -14,6 +14,8 @@ namespace VAR.Toolbox.UI
private NotifyIcon niTray = null;
private static FrmToolbox _currentInstance = null;
#endregion Declarations
#region Form life cycle
@@ -26,6 +28,8 @@ namespace VAR.Toolbox.UI
MouseDown += DragWindow_MouseDown;
lblToolbox.MouseDown += DragWindow_MouseDown;
_currentInstance = this;
}
private void InitializeCustomControls()
@@ -94,7 +98,7 @@ namespace VAR.Toolbox.UI
ShowChildWindows();
WindowState = FormWindowState.Normal;
}
private void btnCoder_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmCoder));
@@ -142,7 +146,7 @@ namespace VAR.Toolbox.UI
private Form CreateWindow(Type type)
{
Form frm = Activator.CreateInstance(type) as Form;
if(frm== null)
if (frm == null)
{
return null;
}
@@ -155,7 +159,7 @@ namespace VAR.Toolbox.UI
frm.Show();
return frm;
}
private List<Form> _forms = new List<Form>();
private void frmChild_FormClosing(object sender, FormClosingEventArgs e)
@@ -208,6 +212,25 @@ namespace VAR.Toolbox.UI
}
}
public static void StaticCreateWindow(Type type)
{
_currentInstance?.CreateWindow(type);
}
public static List<T> StaticGetWindowsOfType<T>()
{
List<T> list = new List<T>();
if (_currentInstance == null) { return list; }
foreach (Form frm in _currentInstance._forms)
{
if (frm is T)
{
list.Add((T)(object)frm);
}
}
return list;
}
#endregion Window handling
}

View File

@@ -129,6 +129,12 @@
<Compile Include="UI\FrmProxyCmd.Designer.cs">
<DependentUpon>FrmProxyCmd.cs</DependentUpon>
</Compile>
<Compile Include="UI\FrmProxyCmdConfig.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\FrmProxyCmdConfig.Designer.cs">
<DependentUpon>FrmProxyCmdConfig.cs</DependentUpon>
</Compile>
<Compile Include="UI\FrmScreenshooter.cs">
<SubType>Form</SubType>
</Compile>