/// <summary>
        /// Called after key was changed - adds undo unit and performs refactoring of code
        /// </summary>
        public void KeyRenamed(ResXStringGridRow row, string newKey)
        {
            string oldKey = row.Status == KEY_STATUS.ERROR ? null : row.DataSourceItem.Name;

            GridRenameKeyUndoUnit unit = new GridRenameKeyUndoUnit(row, editorControl, oldKey, newKey);

            editorControl.Editor.AddUndoUnit(unit);

            if (VisualLocalizerPackage.Instance.DTE.Solution.ContainsProjectItem(editorControl.Editor.ProjectItem.InternalProjectItem))
            {
                // obtain ResX project item
                ResXProjectItem resxItem = editorControl.Editor.ProjectItem;
                resxItem.ResolveNamespaceClass(resxItem.InternalProjectItem.ContainingProject.GetResXItemsAround(false, true));

                if (row.ConflictItems.Count == 0 && resxItem != null && !resxItem.IsCultureSpecific() && !string.IsNullOrEmpty(newKey))
                {
                    int errors = 0;
                    int count  = row.CodeReferences.Count;

                    // set new key
                    row.CodeReferences.ForEach((item) => { item.KeyAfterRename = newKey.CreateIdentifier(resxItem.DesignerLanguage); });

                    // run the replacer
                    try {
                        editorControl.ReferenceCounterThreadSuspended = true;
                        BatchReferenceReplacer replacer = new BatchReferenceReplacer();
                        replacer.Inline(row.CodeReferences, true, ref errors);
                    } finally {
                        editorControl.ReferenceCounterThreadSuspended = false;
                    }
                    VLOutputWindow.VisualLocalizerPane.WriteLine("Renamed {0} key references in code, {1} errors", count, errors);
                }
            }
        }
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;
            }
        }