예제 #1
0
        /// <summary>
        /// Called after editting a cell
        /// </summary>
        protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
        {
            ResXOthersGridRow row = null;

            try {
                if (e.RowIndex == Rows.Count - 1)
                {
                    return;
                }

                base.OnCellEndEdit(e);

                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    row = Rows[e.RowIndex] as ResXOthersGridRow;

                    bool isNewRow = false;
                    if (row.DataSourceItem == null)   // last empty row was edited, new row has been added
                    {
                        isNewRow     = true;
                        row.DataType = typeof(string);
                        row.Cells[TypeColumnName].Value = row.DataType.FullName;
                        row.DataSourceItem = new ResXDataNode("(new)", string.Empty);
                    }
                    ResXDataNode node = row.DataSourceItem;

                    if (Columns[e.ColumnIndex].Name == KeyColumnName)   // key was edited
                    {
                        string newKey = (string)row.Cells[KeyColumnName].Value;

                        if (isNewRow)
                        {
                            SetNewKey(row, newKey);
                            row.Cells[ReferencesColumnName].Value = "?";
                            NewRowAdded(row);
                            NotifyDataChanged();
                        }
                        else if (string.Compare(newKey, node.Name) != 0)
                        {
                            // key has changed
                            KeyRenamed(row, newKey);
                            SetNewKey(row, newKey);
                            NotifyDataChanged();
                        }
                    }
                    else if (Columns[e.ColumnIndex].Name == ValueColumnName)     // value was edited
                    {
                        string newValue = (string)row.Cells[ValueColumnName].Value;
                        if (newValue == null)
                        {
                            newValue = string.Empty;
                        }

                        if (isNewRow)
                        {
                            row.Status = KEY_STATUS.ERROR;
                            row.Cells[ReferencesColumnName].Value = "?";
                            NewRowAdded(row);
                            NotifyDataChanged();
                        }
                        else if (string.Compare(newValue, node.GetValue <string>()) != 0)
                        {
                            // value has changed
                            ValueChanged(row, node.GetValue <string>(), newValue);
                            NotifyDataChanged();

                            string       key     = (string)row.Cells[KeyColumnName].Value;
                            ResXDataNode newNode = null;
                            try {
                                if (string.IsNullOrEmpty(key))
                                {
                                    newNode    = new ResXDataNode("A", TypeDescriptor.GetConverter(row.DataType).ConvertFromString(newValue));
                                    row.Status = KEY_STATUS.ERROR;
                                }
                                else
                                {
                                    newNode          = new ResXDataNode(key, TypeDescriptor.GetConverter(row.DataType).ConvertFromString(newValue));
                                    row.Status       = KEY_STATUS.OK;
                                    row.LastValidKey = key;
                                }
                            } catch {
                            }

                            if (newNode != null)
                            {
                                newNode.Comment    = (string)row.Cells[CommentColumnName].Value;
                                row.DataSourceItem = newNode;
                            }
                        }
                    }
                    else     // comment was edited
                    {
                        string newComment = (string)row.Cells[CommentColumnName].Value;
                        if (isNewRow)
                        {
                            row.Status = KEY_STATUS.ERROR;
                            row.Cells[ReferencesColumnName].Value = "?";
                            NewRowAdded(row);
                            NotifyDataChanged();
                        }
                        else if (string.Compare(newComment, node.Comment) != 0)
                        {
                            CommentChanged(row, node.Comment, newComment);
                            NotifyDataChanged();

                            node.Comment = newComment;
                        }
                    }
                }
            } catch (Exception ex) {
                VLOutputWindow.VisualLocalizerPane.WriteException(ex);
                VisualLocalizer.Library.Components.MessageBox.ShowException(ex);
            } finally {
                editorControl.ReferenceCounterThreadSuspended = false;
                if (row != null)
                {
                    row.UpdateErrorSetDisplay();
                }
                NotifyItemsStateChanged();
            }
        }
        /// <summary>
        /// Add string data from given ResX file to the list of data for translation
        /// </summary>
        private void AddDataForTranslation(GlobalTranslateProjectItem item, List <AbstractTranslateInfoItem> data)
        {
            string path = item.ProjectItem.GetFullPath();

            if (RDTManager.IsFileOpen(path))                              // file is open
            {
                object docData = VLDocumentViewsManager.GetDocData(path); // get document buffer
                if (docData is ResXEditor)                                // document is opened in ResX editor -> use custom method to get string data
                {
                    ResXEditor editor = (ResXEditor)docData;
                    editor.UIControl.AddForTranslation(data);
                }
                else     // document is opened in original VS editor
                {
                    IVsTextLines lines = VLDocumentViewsManager.GetTextLinesForFile(path, false);
                    string       text  = VLDocumentViewsManager.GetTextFrom(lines); // get plain text of ResX file

                    ResXResourceReader reader = null;

                    BufferTranslateInfoItem prev  = null;
                    BufferTranslateInfoItem first = null;

                    try {
                        reader = ResXResourceReader.FromFileContents(text);
                        reader.UseResXDataNodes = true;

                        // add all string resources to the list
                        // items are linked like a linked-list, allowing ApplyTranslation to work
                        foreach (DictionaryEntry entry in reader)
                        {
                            ResXDataNode node = (entry.Value as ResXDataNode);
                            if (node.HasValue <string>())
                            {
                                BufferTranslateInfoItem translateItem = new BufferTranslateInfoItem();
                                translateItem.ResourceKey         = entry.Key.ToString();
                                translateItem.Value               = node.GetValue <string>();
                                translateItem.Filename            = path;
                                translateItem.Applied             = false;
                                translateItem.GlobalTranslateItem = item;
                                translateItem.Prev         = prev;
                                translateItem.IVsTextLines = lines;

                                data.Add(translateItem);
                                prev = translateItem;
                                if (first == null)
                                {
                                    first = translateItem;
                                }
                            }
                            else
                            {
                                item.NonStringData.Add(node);
                            }
                        }
                        if (first != null)
                        {
                            first.Prev = prev;
                        }
                    } finally {
                        if (reader != null)
                        {
                            reader.Close();
                        }
                    }
                }
            }
            else     // file is closed
            {
                ResXProjectItem resxItem = ResXProjectItem.ConvertToResXItem(item.ProjectItem, item.ProjectItem.ContainingProject);
                resxItem.Load();
                loadedResxItems.Add(resxItem);

                // add string data from ResX file
                resxItem.AddAllStringReferencesUnique(data);
            }
        }
예제 #3
0
 public StringResxNode(ResXDataNode resXDataNode)
 {
     Name    = resXDataNode.Name;
     Value   = (string)resXDataNode.GetValue(new AssemblyName[0]);
     Comment = resXDataNode.Comment;
 }
예제 #4
0
        public static string GetValueAsString(this ResXDataNode dataNode)
        {
            var valueObject = dataNode.GetValue((ITypeResolutionService)null);

            return(valueObject?.ToString() ?? string.Empty);
        }