using CADPythonShell.App; using System.IO; namespace CADPythonShell { public partial class ConfigureCommandsForm : Form { private List _commands; private List _searchPaths; private List> _variables; public ConfigureCommandsForm() { InitializeComponent(); } /// /// Close the Dialog without saving changes. /// private void btnCancel_Click(object sender, EventArgs e) { Close(); } /// /// Read in the commands from the XML file and display them in the /// list. /// private void ConfigureCommandsForm_Load(object sender, EventArgs e) { _commands = CADPythonShellApplication.GetCommands( CADPythonShellApplication.GetSettings()).ToList(); lstCommands.DataSource = _commands; _searchPaths = CADPythonShellApplication.GetConfig().GetSearchPaths().ToList(); lstSearchPaths.DataSource = _searchPaths; _variables = CADPythonShellApplication.GetConfig().GetVariables().AsEnumerable().ToList(); lstVariables.DataSource = _variables; lstVariables.DisplayMember = "Key"; string initScriptPath = CADPythonShellApplication.GetInitScriptPath(); txtInitScript.Text = initScriptPath; string startupScriptPath = CADPythonShellApplication.GetStartupScriptPath(); txtStartupScript.Text = startupScriptPath; } /// /// Display information about the selected command in the textboxes (Name, Path). /// private void lstCommands_SelectedIndexChanged(object sender, EventArgs e) { var command = (App.Command)lstCommands.SelectedItem; txtCommandName.Text = command.Name; txtCommandPath.Text = command.Source; txtCommandGroup.Text = command.Group; } /// /// Update changes in list. /// private void txtCommandName_TextChanged(object sender, EventArgs e) { var command = (App.Command)lstCommands.SelectedItem; command.Name = txtCommandName.Text; RefreshBindingContext(lstCommands, _commands); } private void txtCommandGroup_TextChanged(object sender, EventArgs e) { var command = (App.Command)lstCommands.SelectedItem; command.Group = txtCommandGroup.Text; RefreshBindingContext(lstCommands, _commands); } /// /// Update changes in list. /// private void txtCommandPath_TextChanged(object sender, EventArgs e) { var command = (App.Command)lstCommands.SelectedItem; command.Source = txtCommandPath.Text; RefreshBindingContext(lstCommands, _commands); } /// /// show a FileOpen Dialog. /// private void btnCommandBrowse_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; dialog.FileName = txtCommandPath.Text; dialog.ShowDialog(this); txtCommandPath.Text = dialog.FileName; } /// /// Add a new command. /// private void btnCommandAdd_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; // chances are, the user wants to add a script from the same folder // as the other scripts if (_commands.Count > 0) { dialog.InitialDirectory = Path.GetDirectoryName(_commands.First().Source); } if (dialog.ShowDialog(this) == DialogResult.OK) { var command = new App.Command(); command.Name = ""; command.Group = ""; command.Source = dialog.FileName; _commands.Add(command); RefreshBindingContext(lstCommands, _commands); lstCommands.SelectedIndex = _commands.Count - 1; txtCommandPath.Text = command.Source; txtCommandName.Text = command.Name; txtCommandName.Focus(); } } /// /// Add a new search path. /// private void btnSearchPathAdd_Click(object sender, EventArgs e) { var dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { _searchPaths.Add(dialog.SelectedPath); RefreshBindingContext(lstSearchPaths, _searchPaths); lstSearchPaths.SelectedIndex = _searchPaths.Count - 1; txtSearchPath.Text = dialog.SelectedPath; } } private void RefreshBindingContext(ListBox listBox, object dataSource) { ((CurrencyManager)listBox.BindingContext[dataSource]).Refresh(); } /// /// Remove the selected item. /// private void btnCommandRemove_Click(object sender, EventArgs e) { if (lstCommands.SelectedIndex >= 0) { _commands.RemoveAt(lstCommands.SelectedIndex); RefreshBindingContext(lstCommands, _commands); } } /// /// Move the selected item up one. /// private void btnCommandMoveUp_Click(object sender, EventArgs e) { int oldPosition = lstCommands.SelectedIndex; int newPosition = Math.Max(0, oldPosition - 1); SwapPositions(_commands, oldPosition, newPosition); RefreshBindingContext(lstCommands, _commands); lstCommands.SelectedIndex = newPosition; } private void SwapPositions(List container, int oldPosition, int newPosition) { var temp = container[newPosition]; container[newPosition] = container[oldPosition]; container[oldPosition] = temp; } private void btnCommandMoveDown_Click(object sender, EventArgs e) { int oldPosition = lstCommands.SelectedIndex; int newPosition = Math.Min(_commands.Count - 1, oldPosition + 1); SwapPositions(_commands, oldPosition, newPosition); RefreshBindingContext(lstCommands, _commands); lstCommands.SelectedIndex = newPosition; } /// /// Save changes to CADPythonShell.xml and close Dialog. /// private void btnCommandSave_Click(object sender, EventArgs e) { CADPythonShellApplication.WriteSettings(_commands, _searchPaths, _variables, txtInitScript.Text, txtStartupScript.Text); Close(); } private void btnSearchPathRemove_Click(object sender, EventArgs e) { if (lstSearchPaths.SelectedIndex >= 0) { _searchPaths.RemoveAt(lstSearchPaths.SelectedIndex); RefreshBindingContext(lstSearchPaths, _searchPaths); } } private void btnSearchPathBrowse_Click(object sender, EventArgs e) { if (lstSearchPaths.SelectedIndex < 0) { return; } var dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { _searchPaths[lstSearchPaths.SelectedIndex] = dialog.SelectedPath; RefreshBindingContext(lstSearchPaths, _searchPaths); txtSearchPath.Text = dialog.SelectedPath; } } /// /// Show the currently selected search path in the textbox for editing. /// private void lstSearchPaths_SelectedIndexChanged(object sender, EventArgs e) { if (lstSearchPaths.SelectedIndex < 0) { return; } txtSearchPath.Text = _searchPaths[lstSearchPaths.SelectedIndex]; } /// /// Update the search path to reflect changes. /// private void txtSearchPath_TextChanged(object sender, EventArgs e) { if (lstSearchPaths.SelectedIndex < 0) { return; } _searchPaths[lstSearchPaths.SelectedIndex] = txtSearchPath.Text; RefreshBindingContext(lstSearchPaths, _searchPaths); } /// /// Display selected variable in the textboxes for editing. /// private void lstVariables_SelectedIndexChanged(object sender, EventArgs e) { if (lstVariables.SelectedIndex < 0) { return; } var variable = _variables[lstVariables.SelectedIndex]; txtVariableName.Text = variable.Key; txtVariableValue.Text = variable.Value; } /// /// Update variable name to reflect changes in textbox. /// private void txtVariableName_TextChanged(object sender, EventArgs e) { if (lstVariables.SelectedIndex < 0) { return; } var variable = _variables[lstVariables.SelectedIndex]; _variables[lstVariables.SelectedIndex] = new KeyValuePair(txtVariableName.Text, variable.Value); RefreshBindingContext(lstVariables, _variables); } /// /// Update variable value to reflect changes in textbox. /// private void txtVariableValue_TextChanged(object sender, EventArgs e) { if (lstVariables.SelectedIndex < 0) { return; } var variable = _variables[lstVariables.SelectedIndex]; _variables[lstVariables.SelectedIndex] = new KeyValuePair(variable.Key, txtVariableValue.Text); RefreshBindingContext(lstVariables, _variables); } /// /// Add a new variable to the list and select it for editing. /// private void btnVariableAdd_Click(object sender, EventArgs e) { _variables.Add(new KeyValuePair("", "")); RefreshBindingContext(lstVariables, _variables); lstVariables.SelectedIndex = _variables.Count - 1; } /// /// Remove variable from the list. /// private void btnVariableRemove_Click(object sender, EventArgs e) { if (lstVariables.SelectedIndex < 0) { return; } _variables.RemoveAt(lstVariables.SelectedIndex); RefreshBindingContext(lstVariables, _variables); } private void btnInitScriptBrowse_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; dialog.FileName = txtCommandPath.Text; if (dialog.ShowDialog(this) == DialogResult.OK) { txtInitScript.Text = dialog.FileName; } } private void btnStartupScriptBrowse_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; dialog.FileName = txtCommandPath.Text; if (dialog.ShowDialog(this) == DialogResult.OK) { txtStartupScript.Text = dialog.FileName; } } } }