示例#1
0
        private void ReadXml(string filePath)
        {
            XmlSerializer serialize = new XmlSerializer(typeof(HuffmanTree));

            try
            {
                XDocument   xdoc = XDocument.Load(filePath);
                HuffmanTree tree = serialize.Deserialize(xdoc.CreateReader()) as HuffmanTree;


                if (tree == null)
                {
                    throw new Exception();
                }
                this.Tree    = tree;
                this.Content = tree.Content;
                this.RaisePropertyChanged(nameof(Content));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                MessageBox.Show("Nie udało się zaimportować obiektu", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#2
0
        public CodingViewModel()
        {
            Content             = Properties.Resources.Startin_Text;
            Encoded             = new BitArray(0);
            canExecuteOnEncoded = this.WhenAnyValue(x => x.IsEncoded, x => x == true);

            EncodeCommand = ReactiveCommand <Unit, Unit> .Create(() =>
            {
                Tree          = new HuffmanTree(content);
                encodedValues = Tree.GetCodes();
                Encoded       = Tree.Encode(Content);
                CreateStatsTable();
                CalculateEntropy();
                CalculateAverageWordLenth();
                NumberOfInputBits = Content.Length * 8;
                CalculateNumberOfOutputBits();
                TreeHight = Tree.Height;
                Nodes     = Tree.Nodes;
                IsEncoded = true;
                DrawCommand.Execute().Subscribe();
                SetEncodedValue();
            }, this.WhenAnyValue(c => c.Content, (c) => (!String.IsNullOrWhiteSpace(c) && (c.Any(x => x != c[0])))));

            DecodeCommand = ReactiveCommand.Create(() =>
            {
                Content = Tree.Decode(Encoded);
            }, canExecuteOnEncoded.Merge(this.WhenAnyValue(c => c.Encoded.Length, (c) => c > 0)));

            DrawCommand = ReactiveCommand <Unit, bool> .Create(() =>
            {
                return(true);
            }, this.WhenAnyValue(e => e.IsEncoded, e => e == true));

            ExportToXmlCommand = ReactiveCommand.Create(() =>
            {
                SaveFileDialog dialog = new SaveFileDialog()
                {
                    Filter = "text|*.xml"
                };
                string filePath;
                if (dialog.ShowDialog() == true)
                {
                    filePath = dialog.FileName;
                    try
                    {
                        WriteToXmlFile(filePath, Tree);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }, canExecuteOnEncoded);

            ExportToJsonCommand = ReactiveCommand.Create(() =>
            {
                SaveFileDialog dialog = new SaveFileDialog()
                {
                    Filter = "text|*.json"
                };
                string filePath;
                if (dialog.ShowDialog() == true)
                {
                    filePath = dialog.FileName;
                    try
                    {
                        WriteToJsonFIle(filePath);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }, canExecuteOnEncoded);

            ImportJson = ReactiveCommand.CreateFromTask(async() =>
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter         = "text|*.json";
                string content        = string.Empty;
                if (dialog.ShowDialog() == true)
                {
                    content = File.ReadAllText(dialog.FileName);
                }
                if (string.IsNullOrEmpty(content))
                {
                    return;
                }
                ReadJson(content);
            });

            ImportXml = ReactiveCommand.CreateFromTask(async() =>
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter         = "text|*.xml";
                string filePath       = string.Empty;
                if (dialog.ShowDialog() == true)
                {
                    filePath = dialog.FileName;
                }
                if (filePath == null)
                {
                    return;
                }
                ReadXml(filePath);
            });
        }