Exemplo n.º 1
0
        public PatternViewModel(PatternUserControl patternUserControl)
        {
            patternsListBox      = patternUserControl.Find <ListBox>("PatternsListBox");
            patternTextBox       = patternUserControl.Find <TextBox>("PatternData");
            patternErrorsListBox = patternUserControl.Find <ListBox>("PatternErrors");
            logger = patternUserControl.Find <TextBox>("Logger");

            Dispatcher.UIThread.InvokeAsync(async() =>
            {
                bool error = false;
                if (!string.IsNullOrEmpty(Settings.PatternsFileName))
                {
                    try
                    {
                        PatternsFileName = Settings.PatternsFileName;
                        SelectedPattern  = Patterns.FirstOrDefault(p => p.Key == Settings.SelectedPatternKey) ?? Patterns.FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        error = true;
                    }
                }

                if (string.IsNullOrEmpty(Settings.PatternsFileName) || error)
                {
                    Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                    SelectedPattern           = Patterns.FirstOrDefault();
                    SavePatterns();
                    Settings.Save();
                }
            });

            patternsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) =>
            {
                OpenPattern.Execute(sender);
            };

            patternErrorsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) =>
            {
                GuiHelpers.ProcessErrorOnDoubleClick(patternErrorsListBox, patternTextBox);
            };

            patternLogger = new GuiLogger(PatternErrors)
            {
                LogPatternErrors = true
            };
            patternLogger.LogEvent += PatternLogger_LogEvent;
            dslProcessor.Logger     = patternLogger;

            patternTextBox.GetObservable(TextBox.CaretIndexProperty)
            .Subscribe(UpdatePatternCaretIndex);

            patternTextBox.GetObservable(TextBox.TextProperty)
            .Throttle(TimeSpan.FromMilliseconds(250))
            .Subscribe(str => Value = str);

            OpenPatterns.Subscribe(async _ =>
            {
                SavePatterns();
                var openFileDialog = new OpenFileDialog
                {
                    Title = "Select patterns database",
                };
                var fileNames = await openFileDialog.ShowAsync(ServiceLocator.MainWindow);
                if (fileNames != null)
                {
                    try
                    {
                        PatternsFileName = fileNames.First();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                        Settings.Save();
                        SelectedPattern = Patterns.FirstOrDefault();
                        ServiceLocator.MainWindowViewModel.ActivateWindow();
                    }
                }
            });

            CreatePattern.Subscribe(_ =>
            {
                SavePatterns();
                var newPattern  = new PatternDto();
                newPattern.Key  = Guid.NewGuid().ToString();
                newPattern.Name = "New Pattern";
                Patterns.Add(newPattern);
                SelectedPattern = newPattern;
                SavePatterns();
            });

            RemovePattern.Subscribe(async _ =>
            {
                if (SelectedPattern != null && await MessageBox.ShowDialog($"Do you want to remove {SelectedPattern}?", messageBoxType: MessageBoxType.YesNo))
                {
                    Patterns.Remove(SelectedPattern);
                    SelectedPattern = Patterns.LastOrDefault();
                    SavePatterns();
                }
            });

            OpenPattern.Subscribe(_ =>
            {
                if (patternsListBox.SelectedItem != null)
                {
                    var patternDto  = (PatternDto)patternsListBox.SelectedItem;
                    SelectedPattern = patternDto;
                }
            });

            SavePattern.Subscribe(_ =>
            {
                SavePatterns();
            });
        }
Exemplo n.º 2
0
        public MainWindowViewModel(Window w)
        {
            window             = w;
            window.WindowState = Settings.WindowState;
            if (Settings.Width > 0)
            {
                window.Width = Settings.Width;
            }
            if (Settings.Height > 0)
            {
                window.Height = Settings.Height;
            }
            if (Settings.Left != -1 && Settings.Top != -1)
            {
                window.Position = new Point(Settings.Left, Settings.Top);
            }

            patternsPanelColumn     = window.Find <Grid>("MainGrid").ColumnDefinitions[0];
            sourceCodeTextBox       = window.Find <TextBox>("SourceCode");
            sourceCodeErrorsListBox = window.Find <ListBox>("SourceCodeErrors");
            matchingResultListBox   = window.Find <ListBox>("MatchingResult");
            logger = window.Find <TextBox>("Logger");

            patternsPanelColumn.Width             = GridLength.Parse(Settings.PatternsPanelWidth.ToString(), CultureInfo.InvariantCulture);
            sourceCodeErrorsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) =>
            {
                GuiHelpers.ProcessErrorOnDoubleClick(sourceCodeErrorsListBox, sourceCodeTextBox);
            };
            matchingResultListBox.DoubleTapped += MatchingResultListBox_DoubleTapped;

            sourceCodeLogger = new GuiLogger(SourceCodeErrors)
            {
                LogPatternErrors = false
            };
            languageDetector.Logger = sourceCodeLogger;

            OpenSourceCodeFile.Subscribe(async _ =>
            {
                var dialog         = new OpenFileDialog();
                string[] fileNames = await dialog.ShowAsync(window);
                if (fileNames != null)
                {
                    string fileName        = fileNames.Single();
                    OpenedFileName         = fileName;
                    fileOpened             = true;
                    sourceCodeTextBox.Text = File.ReadAllText(sourceCodeFileName);
                }
            });

            SaveSourceCodeFile.Subscribe(_ =>
            {
                if (!string.IsNullOrEmpty(sourceCodeFileName))
                {
                    File.WriteAllText(sourceCodeFileName, sourceCodeTextBox.Text);
                }
            });

            ReloadFile.Subscribe(_ =>
            {
                if (!string.IsNullOrEmpty(sourceCodeFileName))
                {
                    sourceCodeTextBox.Text = File.ReadAllText(sourceCodeFileName);
                }
            });

            Reset.Subscribe(_ =>
            {
                OpenedFileName         = "";
                sourceCodeTextBox.Text = "";
            });

            if (string.IsNullOrEmpty(Settings.SourceCodeFile) || !File.Exists(Settings.SourceCodeFile))
            {
                fileOpened             = false;
                sourceCodeFileName     = "";
                sourceCodeTextBox.Text = Settings.SourceCode;
            }
            else
            {
                fileOpened             = true;
                sourceCodeFileName     = Settings.SourceCodeFile;
                sourceCodeTextBox.Text = File.ReadAllText(Settings.SourceCodeFile);
            }

            CheckSourceCode();

            this.RaisePropertyChanged(nameof(SelectedLanguageInfo));
            this.RaisePropertyChanged(nameof(OpenedFileName));

            sourceCodeTextBox.GetObservable(TextBox.CaretIndexProperty)
            .Subscribe(UpdateSourceCodeCaretIndex);
            sourceCodeTextBox.GetObservable(TextBox.SelectionStartProperty)
            .Subscribe(selectionStart =>
            {
                if (sourceCodeTextBox.IsFocused)
                {
                    sourceCodeSelectionStart = selectionStart;
                }
            });
            sourceCodeTextBox.GetObservable(TextBox.SelectionEndProperty)
            .Subscribe(selectionEnd =>
            {
                if (sourceCodeTextBox.IsFocused)
                {
                    sourceCodeSelectionEnd = selectionEnd;
                }
            });

            sourceCodeTextBox.GetObservable(TextBox.TextProperty)
            .Throttle(TimeSpan.FromMilliseconds(500))
            .Subscribe(str => CheckSourceCode());

            SetupWindowSubscriptions();

            this.RaisePropertyChanged(nameof(IsErrorsExpanded));
            this.RaisePropertyChanged(nameof(IsTokensExpanded));
            this.RaisePropertyChanged(nameof(IsParseTreeExpanded));
            this.RaisePropertyChanged(nameof(IsUstExpanded));
            this.RaisePropertyChanged(nameof(IsMatchingsExpanded));
        }
Exemplo n.º 3
0
        public MainWindowViewModel(Window w)
        {
            window             = w;
            window.WindowState = Settings.WindowState;
            if (Settings.Width > 0)
            {
                window.Width = Settings.Width;
            }

            if (Settings.Height > 0)
            {
                window.Height = Settings.Height;
            }

            if (Settings.Left != -1 && Settings.Top != -1)
            {
                window.Position = new Point(Settings.Left, Settings.Top);
            }

            patternsPanelColumn = window.Find <Grid>("MainGrid").ColumnDefinitions[0];
            sourceTextBox       = window.Find <TextEditor>("Source");
            sourceErrorsListBox = window.Find <ListBox>("SourceErrors");
            matchResultListBox  = window.Find <ListBox>("MatchingResult");

            patternsPanelColumn.Width         = GridLength.Parse(Settings.PatternsPanelWidth.ToString());
            sourceErrorsListBox.DoubleTapped += (sender, e) =>
            {
                GuiUtils.ProcessErrorOnDoubleClick(sourceErrorsListBox, sourceTextBox);
            };
            matchResultListBox.DoubleTapped += MatchingResultListBox_DoubleTapped;

            SourceLogger            = GuiLogger.CreateSourceLogger(SourceErrors, MatchingResults);
            languageDetector.Logger = SourceLogger;

            OpenSourceFile = ReactiveCommand.Create(async() =>
            {
                var dialog = new OpenFileDialog
                {
                    Title = "Open source code file"
                };
                string[] fileNames = await dialog.ShowAsync(window);
                if (fileNames?.Any() == true)
                {
                    OpenedFileName     = fileNames[0];
                    fileOpened         = true;
                    sourceTextBox.Text = FileExt.ReadAllText(sourceFileName);
                }
            });

            SaveSourceFile = ReactiveCommand.Create(() =>
            {
                if (!string.IsNullOrEmpty(sourceFileName))
                {
                    FileExt.WriteAllText(sourceFileName, sourceTextBox.Text);
                }
            });

            ReloadFile = ReactiveCommand.Create(() =>
            {
                if (!string.IsNullOrEmpty(sourceFileName))
                {
                    sourceTextBox.Text = FileExt.ReadAllText(sourceFileName);
                }
            });

            Reset = ReactiveCommand.Create(() =>
            {
                OpenedFileName     = "";
                sourceTextBox.Text = "";
            });

            OpenDumpDirectory = ReactiveCommand.Create(() =>
            {
                try
                {
                    GuiUtils.OpenDirectory(ServiceLocator.TempDirectory);
                }
                catch (Exception ex)
                {
                    new MessageBox($"Unable to open {ServiceLocator.TempDirectory} due to {ex}").ShowDialog();
                }
            });

            if (string.IsNullOrEmpty(Settings.SourceFile) || !FileExt.Exists(Settings.SourceFile))
            {
                fileOpened         = false;
                sourceFileName     = "";
                sourceTextBox.Text = Settings.Source;
            }
            else
            {
                fileOpened         = true;
                sourceFileName     = Settings.SourceFile;
                sourceTextBox.Text = FileExt.ReadAllText(Settings.SourceFile);
            }

            CheckSource();

            this.RaisePropertyChanged(nameof(SelectedLanguage));
            this.RaisePropertyChanged(nameof(OpenedFileName));

            highlightings.TryGetValue(SelectedLanguage, out string highlighting);
            sourceTextBox.SyntaxHighlighting = highlighting != null
                ? HighlightingManager.Instance.GetDefinition(highlighting)
                : null;

            Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(200), RxApp.MainThreadScheduler)
            .Subscribe(_ => UpdateSourceSelection());

            Observable.FromEventPattern <EventHandler, EventArgs>(
                h => sourceTextBox.TextChanged += h,
                h => sourceTextBox.TextChanged -= h)
            .Throttle(TimeSpan.FromMilliseconds(500))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(str => CheckSource());

            SetupWindowSubscriptions();

            this.RaisePropertyChanged(nameof(IsErrorsExpanded));
            this.RaisePropertyChanged(nameof(IsTokensExpanded));
            this.RaisePropertyChanged(nameof(IsParseTreeExpanded));
            this.RaisePropertyChanged(nameof(IsUstExpanded));
            this.RaisePropertyChanged(nameof(IsMatchingsExpanded));
        }