Exemplo n.º 1
0
        private void addButton_Click(object sender, EventArgs e)
        {
            // check if form is filled out correctly
            if (!isValidInput())
            {
                return;
            }

            // if so, add the rule (unless abort on collision).
            // unless in edit mode
            if (!editMode && printRuleDictionary.hasRule(matchTextBox.Text))
            {
                var overwriteDecision = MessageBox.Show(
                    $"There is already a rewrite rule matching {matchTextBox.Text}." +
                    " Do you want to replace this rule?",
                    "Rule already exists!",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information);
                if (overwriteDecision == DialogResult.No)
                {
                    // do not close yet, as the user must have the possibility to correct the match value.
                    return;
                }
            }
            printRuleDictionary.addRule(matchTextBox.Text, buildRuleFromForm());
            DialogResult = DialogResult.OK;
            Close();
        }
        private void importButton_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog()
            {
                DefaultExt = ".csv",
                Filter     = "Comma separated Values (*.csv)|*.csv"
            };
            var dialogResult = dialog.ShowDialog();

            if (dialogResult != DialogResult.OK)
            {
                return;
            }

            var inStream     = new StreamReader(dialog.OpenFile());
            var invalidLines = false;

            while (!inStream.EndOfStream)
            {
                var line = inStream.ReadLine();

                // skip empty lines
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                var lines = line.Split(';');
                // validate
                if (lines.Length != 13)
                {
                    invalidLines = true;
                    continue;
                }

                // parse bools & ints
                bool printChildren;
                bool associative;
                bool indent;
                int  precedence;
                int  colorArgb;
                if (!bool.TryParse(lines[5], out printChildren) ||
                    !bool.TryParse(lines[9], out associative) ||
                    !bool.TryParse(lines[12], out indent) ||
                    !int.TryParse(lines[11], out precedence) ||
                    !int.TryParse(lines[4], out colorArgb))
                {
                    invalidLines = true;
                    continue;
                }

                // parse enums
                PrintRule.LineBreakSetting   prefixLinebreaks;
                PrintRule.LineBreakSetting   infixLinebreaks;
                PrintRule.LineBreakSetting   suffixLinebreaks;
                PrintRule.ParenthesesSetting parenthesesSettings;
                try
                {
                    prefixLinebreaks    = PrintRule.lineBreakSettingFromString(lines[6]);
                    infixLinebreaks     = PrintRule.lineBreakSettingFromString(lines[7]);
                    suffixLinebreaks    = PrintRule.lineBreakSettingFromString(lines[8]);
                    parenthesesSettings = PrintRule.parenthesesSettingsFromString(lines[10]);
                }
                catch (ArgumentException)
                {
                    invalidLines = true;
                    continue;
                }

                printRuleDict.addRule(lines[0], new PrintRule
                {
                    prefix             = new Func <bool, string>(_ => lines[1]),
                    infix              = new Func <bool, string>(_ => lines[2]),
                    suffix             = new Func <bool, string>(_ => lines[3]),
                    color              = Color.FromArgb(colorArgb),
                    printChildren      = printChildren,
                    prefixLineBreak    = prefixLinebreaks,
                    infixLineBreak     = infixLinebreaks,
                    suffixLineBreak    = suffixLinebreaks,
                    associative        = associative,
                    parentheses        = parenthesesSettings,
                    precedence         = precedence,
                    indent             = indent,
                    historyConstraints = new ConstraintType(),
                    isDefault          = false,
                    isUserdefined      = true
                });
            }

            if (invalidLines)
            {
                MessageBox.Show("Some unparseable lines were skipped!",
                                "There were unparseable lines!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            updateRulesList();
        }