Ejemplo n.º 1
0
        /// <summary>
        /// Validates given row and updates its error messages
        /// </summary>
        protected override void Validate(DataGridViewKeyValueRow <CodeStringResultItem> row)
        {
            object dest            = row.Cells[DestinationColumnName].Value;
            bool   existsSameValue = false;

            if (dest == null)   // no destination file was selected
            {
                row.ErrorMessages.Add(NoDestinationFileError);
            }
            else
            {
                row.ErrorMessages.Remove(NoDestinationFileError);

                ResXProjectItem resxItem = resxItemsCache[dest.ToString()];
                if (!resxItem.IsLoaded)
                {
                    resxItem.Load();                                                                          // load the ResX file
                    VLDocumentViewsManager.SetFileReadonly(resxItem.InternalProjectItem.GetFullPath(), true); // lock it
                    loadedItems.Add(resxItem);
                }

                string key   = row.Key;
                string value = row.Value;

                CONTAINS_KEY_RESULT keyConflict = resxItem.GetKeyConflictType(key, value, true); // get conflict type
                switch (keyConflict)
                {
                case CONTAINS_KEY_RESULT.EXISTS_WITH_SAME_VALUE:
                    row.ErrorMessages.Remove(DuplicateKeyError);
                    existsSameValue = true;
                    break;

                case CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE:
                    row.ErrorMessages.Add(DuplicateKeyError);
                    break;

                case CONTAINS_KEY_RESULT.DOESNT_EXIST:
                    row.ErrorMessages.Remove(DuplicateKeyError);
                    break;
                }

                string originalValue = (string)row.Cells[KeyColumnName].Tag;
                ((DestinationKeyValueConflictResolver)ConflictResolver).TryAdd(originalValue, key, row, resxItem, row.DataSourceItem.Language);
                if (originalValue == null)
                {
                    row.Cells[KeyColumnName].Tag = key;
                }
            }

            row.UpdateErrorSetDisplay(); // update error messages

            if (row.ErrorMessages.Count == 0)
            {
                if (existsSameValue)   // set background color according to conflict type
                {
                    row.DefaultCellStyle.BackColor = ExistingKeySameValueColor;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.White;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executed on every change in window form
        /// </summary>
        private void ValidateData()
        {
            bool   existsFile = comboBox.Items.Count > 0; // there's at least one possible destination file
            string errorText  = null;
            bool   ok         = true;

            if (!existsFile)   // no destination file
            {
                ok        = false;
                errorText = "Project does not contain any useable resource files";
            }
            else
            {
                ResXProjectItem item = comboBox.SelectedItem as ResXProjectItem;
                if (!item.IsLoaded)
                {
                    item.Load();
                    VLDocumentViewsManager.SetFileReadonly(item.InternalProjectItem.GetFullPath(), true);
                }
                resultItem.DestinationItem = item;

                bool isKeyEmpty            = string.IsNullOrEmpty(keyBox.Text);
                bool isValidIdentifier     = keyBox.Text.IsValidIdentifier(resultItem.Language);
                bool hasOwnDesigner        = (item.DesignerItem != null || item.HasImplicitDesignerFile) && !item.IsCultureSpecific();
                bool identifierErrorExists = false;

                // determine whether current key name is valid
                switch (SettingsObject.Instance.BadKeyNamePolicy)
                {
                case BAD_KEY_NAME_POLICY.IGNORE_COMPLETELY:
                    identifierErrorExists = isKeyEmpty;     // only empty keys are invalid
                    break;

                case BAD_KEY_NAME_POLICY.IGNORE_ON_NO_DESIGNER:
                    identifierErrorExists = isKeyEmpty || (!isValidIdentifier && hasOwnDesigner);     // empty keys and invalid identifiers in ResX files with their own designer file
                    break;

                case BAD_KEY_NAME_POLICY.WARN_ALWAYS:
                    identifierErrorExists = isKeyEmpty || !isValidIdentifier;     // empty keys and invalid identifiers
                    break;
                }

                if (!identifierErrorExists)   // identifier ok - check for key name conflicts
                {
                    keyConflict = item.GetKeyConflictType(keyBox.Text, valueBox.Text, true);

                    Color backColor = Color.White;
                    switch (keyConflict)
                    {
                    case CONTAINS_KEY_RESULT.EXISTS_WITH_SAME_VALUE:     // key already exists and has the same value - ok
                        backColor = EXISTING_KEY_COLOR;
                        break;

                    case CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE:     // key exists with different value - error
                        errorText             = "Key is already present and has different value";
                        existingValueBox.Text = item.GetString(keyBox.Text);
                        backColor             = ERROR_COLOR;
                        break;

                    case CONTAINS_KEY_RESULT.DOESNT_EXIST:     // key doesn't exists - ok
                        backColor = Color.White;
                        break;
                    }

                    overwriteButton.Visible  = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    inlineButton.Visible     = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    existingValueBox.Visible = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    existingLabel.Visible    = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;

                    keyBox.BackColor   = backColor;
                    valueBox.BackColor = backColor;
                }
                else
                {
                    errorText          = "Key is not a valid identifier";
                    keyBox.BackColor   = ERROR_COLOR;
                    valueBox.BackColor = Color.White;
                }

                ok = !identifierErrorExists && !overwriteButton.Visible;

                referenceText.ClassPart = item.Class;
                referenceText.KeyPart   = keyBox.Text;

                if (string.IsNullOrEmpty(item.Namespace))   // no namespace was found in designer file - error
                {
                    ok        = false;
                    errorText = "Cannot reference resources in this file, missing namespace";
                }
                else
                {
                    if (!usingBox.Checked || resultItem.MustUseFullName)   // force using full reference
                    {
                        referenceText.NamespacePart = item.Namespace;
                    }
                    else
                    {
                        referenceText.NamespacePart = null;
                    }
                }

                referenceLabel.Text = resultItem.GetReferenceText(referenceText);
            }

            okButton.Enabled = ok;
            if (ok)
            {
                errorLabel.Text = string.Empty;
            }
            else
            {
                errorLabel.Text = errorText;
            }
        }