Esempio n. 1
0
        protected override void SetupCommands()
        {
            LoadSettings();
            // Build the Run command
            _run_command = new Command("Prompt", Command.PriorityType.High);
            _run_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                string s = parameters.ToLower().Trim();
                if (s.Length > 0)
                {
                    if (s[0] == '>')
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }));
            _run_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return("Run command" + (RunAndClose(parameters) ? " and close" : string.Empty));
            }));
            _run_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(BuildCommandDscpr(parameters));
            }));
            _run_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(parameters);
            }));
            _run_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(_prompt_icon.ToBitmap());
            }));
            _run_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>(new string[] { "command" });
                Dictionary <string, bool> comp = new Dictionary <string, bool>();
                foreach (string arg in args)
                {
                    comp.Add(arg, false);
                }

                if (BuildCommandDscpr(parameters) != string.Empty)
                {
                    comp["command"] = true;
                }

                return(new CommandUsage(_run_command.Name, args, comp));
            }));
            _run_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                ProcessStartInfo info = new ProcessStartInfo(Environment.GetEnvironmentVariable("COMSPEC"));
                //if (UserContext.Instance.IsWindowsExplorerOnTop)
                info.WorkingDirectory = UserContext.Instance.GetExplorerPath(true);
                info.Arguments        = BuildCommand(parameters);
                info.UseShellExecute  = true;
                info.ErrorDialog      = true;
                Process.Start(info);
                info = null;
            }));
            Commands.Add(_run_command);

            // Build the Add command
            _add_command = new Command("Add New Command", "Add a new command.", Command.PriorityType.Medium);
            _add_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                return(false);
            }));
            _add_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Name);
            }));
            _add_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Description);
            }));
            _add_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Name);
            }));
            _add_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(_add_icon.ToBitmap());
            }));
            _add_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>();
                Dictionary <string, bool> comp = new Dictionary <string, bool>();

                return(new CommandUsage(_add_command.Name, args, comp));
            }));
            _add_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                CommandPicker ep = new CommandPicker(this);
                if (ep.ShowDialog() == DialogResult.OK)
                {
                    SaveSettings();
                }
                ep.Dispose();
            }));
            Commands.Add(_add_command);

            // Build user commands
            foreach (PromptCommand prompt_command in _pompt_commands)
            {
                PromptCommand pcommand = new PromptCommand(prompt_command);
                Command       cmd      = new Command(pcommand.Name);

                cmd.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
                {
                    return(true);
                }));
                cmd.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    return(pcommand.Name);
                }));
                cmd.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    if (parameters == string.Empty)
                    {
                        if (System.IO.Directory.Exists(pcommand.Path))
                        {
                            return("Open " + pcommand.Path);
                        }
                        else
                        {
                            return("Run " + pcommand.Name);
                        }
                    }
                    else
                    {
                        return("Arguments: " + pcommand.GetArguments(parameters));
                    }
                }));
                cmd.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    if (parameters == string.Empty)
                    {
                        return(pcommand.Name);
                    }
                    else
                    {
                        return(pcommand.Name + " " + parameters);
                    }
                }));
                cmd.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
                {
                    return(_prompt_icon.ToBitmap());
                }));
                cmd.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
                {
                    List <string> args             = new List <string>();
                    Dictionary <string, bool> comp = new Dictionary <string, bool>();

                    if (pcommand.Arguments.Contains(PromptCommand.ArgumentsToken))
                    {
                        args.Add("arguments");
                        if (parameters != string.Empty)
                        {
                            comp.Add("arguments", true);
                        }
                        else
                        {
                            comp.Add("arguments", false);
                        }
                    }

                    return(new CommandUsage(cmd.Name, args, comp));
                }));
                cmd.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
                {
                    try
                    {
                        ProcessStartInfo info;
                        if ((modifiers & Keys.Shift) == Keys.Shift)
                        {
                            info = new ProcessStartInfo(FileSearcher.GetItemFolder(pcommand.Path));
                        }
                        else
                        {
                            info = new ProcessStartInfo(pcommand.Path);
                        }
                        string wd             = System.IO.Path.GetDirectoryName(pcommand.Path);
                        info.WorkingDirectory = (System.IO.Directory.Exists(wd) ? wd : string.Empty);
                        info.Arguments        = pcommand.GetArguments(parameters);
                        info.UseShellExecute  = true;
                        info.ErrorDialog      = true;
                        Process.Start(info);
                        info = null;
                    }
                    catch
                    {
                    }
                }));
                _commands.Add(cmd);
            }
        }
Esempio n. 2
0
        private void okButton_Click(object sender, EventArgs e)
        {
            if (nameTextBox.Text == string.Empty)
            {
                nameTextBox.Focus();
                _tooltip.SetToolTip(nameTextBox, "Error");
                _tooltip.Show("You must specify a name.", nameTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (_parent.PromptCommandNames.Contains(nameTextBox.Text))
            {
                nameTextBox.Focus();
                _tooltip.SetToolTip(nameTextBox, "Error");
                _tooltip.Show("A command with this name alreay exists. Please specify a different one.", nameTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (pathTextBox.Text == string.Empty)
            {
                pathTextBox.Focus();
                _tooltip.SetToolTip(pathTextBox, "Error");
                _tooltip.Show("You must specify a path.", pathTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (!File.Exists(pathTextBox.Text) && !Directory.Exists(pathTextBox.Text))
            {
                pathTextBox.Focus();
                _tooltip.SetToolTip(pathTextBox, "Error");
                _tooltip.Show("This is not a valid path.", pathTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            //else
            //{
            //    FileInfo finfo = new FileInfo(pathTextBox.Text);
            //    if (finfo.Extension.ToLower() != ".exe")
            //    {
            //        pathTextBox.Focus();
            //        _tooltip.SetToolTip(pathTextBox, "Error");
            //        _tooltip.Show("This is not an .exe file.", pathTextBox, 3000);
            //        return;
            //    }
            //}
            //else if (argumentsTextBox.Text != string.Empty)
            //{
            //    if (!argumentsTextBox.Text.Contains(PromptCommand.ArgumentsToken))
            //    {
            //        argumentsTextBox.Focus();
            //        _tooltip.SetToolTip(argumentsTextBox, "Error");
            //        _tooltip.Show("Arguments must have a user input location specified ('" + PromptCommand.ArgumentsToken + "').", argumentsTextBox, 3000);
            //        //_tooltip.RemoveAll();
            //        return;
            //    }
            //}
            _parent.PromptCommandNames.Add(nameTextBox.Text);
            PromptCommand new_pcommand = new PromptCommand(nameTextBox.Text, pathTextBox.Text, argumentsTextBox.Text);

            _parent.PromptCommands.Add(new_pcommand);

            Command new_command = new Command(nameTextBox.Text);

            new_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                return(true);
            }));
            new_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(new_pcommand.Name);
            }));
            new_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                if (parameters == string.Empty)
                {
                    if (System.IO.Directory.Exists(new_pcommand.Path))
                    {
                        return("Open " + new_pcommand.Path);
                    }
                    else
                    {
                        return("Run " + new_pcommand.Name);
                    }
                }
                else
                {
                    return("Arguments: " + new_pcommand.GetArguments(parameters));
                }
            }));
            new_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                if (parameters == string.Empty)
                {
                    return(new_pcommand.Name);
                }
                else
                {
                    return(new_pcommand.Name + " " + parameters);
                }
            }));
            new_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(Properties.Resources.prompt.ToBitmap());
            }));
            new_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>();
                Dictionary <string, bool> comp = new Dictionary <string, bool>();

                if (new_pcommand.Arguments.Contains(PromptCommand.ArgumentsToken))
                {
                    args.Add("arguments");
                    if (parameters != string.Empty)
                    {
                        comp.Add("arguments", true);
                    }
                    else
                    {
                        comp.Add("arguments", false);
                    }
                }

                return(new CommandUsage(new_command.Name, args, comp));
            }));
            new_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                try
                {
                    ProcessStartInfo info;
                    if ((modifiers & Keys.Shift) == Keys.Shift)
                    {
                        info = new ProcessStartInfo(FileSearcher.GetItemFolder(new_pcommand.Path));
                    }
                    else
                    {
                        info = new ProcessStartInfo(new_pcommand.Path);
                    }
                    string wd             = System.IO.Path.GetDirectoryName(new_pcommand.Path);
                    info.WorkingDirectory = (System.IO.Directory.Exists(wd) ? wd : string.Empty);
                    info.Arguments        = new_pcommand.GetArguments(parameters);
                    info.UseShellExecute  = true;
                    info.ErrorDialog      = true;
                    Process.Start(info);
                    info = null;
                }
                catch
                {
                }
            }));
            _parent.Commands.Add(new_command);

            DialogResult = DialogResult.OK;
            Close();
        }