Commit 20293697 authored by Joachim Ehbrecht's avatar Joachim Ehbrecht
Browse files

Add the RowBasedTextEditor and derived IntelliSenseEditor

parent 43898dca
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -38,8 +38,8 @@ public partial class App : Application
        //this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/TreeView/MainWindow.xaml", UriKind.Relative );
        //this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/Assistances/TextHighlightingWindow.xaml", UriKind.Relative );
        //this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/LocalizedInput/LocalizedIdentifierInputWindow.xaml", UriKind.Relative );
        this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/Editors/RowBasedTextEditorMainWindow.xaml", UriKind.Relative );
        //this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/Editors/IntelliSenseEditorMainWindow.xaml", UriKind.Relative );
        //this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/Editors/RowBasedTextEditorMainWindow.xaml", UriKind.Relative );
        this.StartupUri = new Uri( "/Common.UI.ViewTest;component/Views/Editors/IntelliSenseEditorMainWindow.xaml", UriKind.Relative );

        #endregion Simple controls

+156 −4
Original line number Diff line number Diff line
using Common.UI.Models.Editors.IntelliSenseEditor.ViewAdapter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows.Documents;

using Common.UI.Models.Editors.IntelliSenseEditor.ViewAdapter;
using Common.UI.Models.RowBasedTextEditor.ViewAdapter;
using Common.UI.ViewModels.Editors.IntelliSenseEditor;
using Common.UI.ViewTest.Utilities.Extensions;

using CommunityToolkit.Mvvm.Input;

namespace Common.UI.ViewTest.ViewModels.Editors;
public partial class IntelliSenseEditorMainWindowViewModel : IntelliSenseEditorViewModelBase
{
    #region Properties

    public List<string> SpecialFilterItems => new( [">", "<", "="] );

    #endregion Properties


    #region Constructors

    public IntelliSenseEditorMainWindowViewModel()
        : base() {
        this.FilteredTextSegments = new( [new FilteredTextSegmentViewAdapter( 0, "Setze" ),
                                          new FilteredTextSegmentViewAdapter( 1, "Warte" ),
                                          new FilteredTextSegmentViewAdapter( 2, "Klemme 15" )] );
        ChangeThemeExtension.ChangeTheme();
    }

    #endregion Constructors

    #region Methods

    #region 'RowBasedTextEditorViewModelBase' services

    /// <summary> Processes the row-validation and updates the view items: '<see cref="RowValidationViewAdapter.ErrorCodes"/>' for Tool-Tip and the validation-state image (implicitly).
    /// </summary>
    /// <param name="row"> The row, which needs to be validated. </param>
    public override void Validate( RowViewAdapter row ) {
        PseudoValidation( row );
    }

    #endregion 'RowBasedTextEditorViewModelBase' services

    #region 'IntelliSenseEditorViewModelBase' services

    public override FilteredTextSegmentViewAdapter[] GetSelectableTextSegments( IntelliSenseRowTextSegmentViewAdapter[] parentTextSegments ) {
        return PseudoKeyWordCollection( parentTextSegments );
    }

    public override bool IsValidatedTextSegment( IntelliSenseRowTextSegmentViewAdapter? textSegment ) {
        return textSegment?.Ident != null &&
               ( textSegment.Ident.Code == 33 || textSegment.Ident.Code == 34 );    // Is Numerical OR Is FreeText?
        //Or view type, like this: textSegment.Ident.Type == (long)SKWType.InstructionValue;
    }

    public override FilteredTextSegmentViewAdapter? ValidateNextWord( string word, ObservableCollection<FilteredTextSegmentViewAdapter> filteredTextSegments ) {

        if ( filteredTextSegments.FirstOrDefault( ftsItem => ftsItem.Id == 33 ) is FilteredTextSegmentViewAdapter numericalTextSegment )
        {
            if ( Double.TryParse( word, CultureInfo.InvariantCulture, out double _ ) )
            {
                numericalTextSegment = numericalTextSegment.Deep();
                numericalTextSegment.PlainText = word.Replace( ',', '.' );
                return numericalTextSegment;
            }
        }

        if ( filteredTextSegments.FirstOrDefault( ftsItem => ftsItem.Id == 34 ) is FilteredTextSegmentViewAdapter freeTextTextSegment )
        {
            if ( word.StartsWith( freeTextTextSegment.SyntaxContent.Prefix ) && word.EndsWith( freeTextTextSegment.SyntaxContent.Postfix ) )
            {
                freeTextTextSegment = freeTextTextSegment.Deep();
                freeTextTextSegment.PlainText = word[1..^1];
                return freeTextTextSegment;
            }
        }
        return null;
    }

    #endregion 'IntelliSenseEditorViewModelBase' services

    #region Commands / Delegates

    [RelayCommand]
    public void ChangeTheme() {
        ChangeThemeExtension.ChangeTheme();
    }

    #endregion Commands / Delegates

    #region Helper

    private FilteredTextSegmentViewAdapter[] PseudoKeyWordCollection( IntelliSenseRowTextSegmentViewAdapter[] parentTextSegments ) {
        if ( parentTextSegments.Length == 0 )
        {
            return [new FilteredTextSegmentViewAdapter( 0, "Setze" ),
                    new FilteredTextSegmentViewAdapter( 1, "Vergleiche" )];
        }
        if ( parentTextSegments.Length == 1 )
        {
            return [new FilteredTextSegmentViewAdapter( 10, "Klemme 15" ),
                    new FilteredTextSegmentViewAdapter( 11, "Geschwindigkeit" ),
                    new FilteredTextSegmentViewAdapter( 12, "Fehlerspeicher" )];
        }
        if ( parentTextSegments.Length == 2 )
        {
            if ( parentTextSegments[0].Ident.Code == 0 )
            {
                return [new FilteredTextSegmentViewAdapter( 21, "=" )];
            }
            else
            {
                return [new FilteredTextSegmentViewAdapter( 22, "==" ),
                        new FilteredTextSegmentViewAdapter( 23, "!=" ),
                        new FilteredTextSegmentViewAdapter( 24, ">" ),
                        new FilteredTextSegmentViewAdapter( 25, "<" )];
            }
        }
        if ( parentTextSegments.Length == 3 )
        {
            if ( parentTextSegments[1].Ident.Code == 10 )       // Is 'Klemme 15'?
            {
                return [new FilteredTextSegmentViewAdapter( 31, "Ein" ),
                        new FilteredTextSegmentViewAdapter( 32, "Aus" )];
            }
            else if ( parentTextSegments[1].Ident.Code == 11 )  // Is 'Geschwindigkeit'?
            {
                return [new FilteredTextSegmentViewAdapter( 33, "Numerical [0.0..210.1]", behavior: new( true ) )];
            }
            else                                                // Is 'Fehlerspeicher'!
            {
                return [new FilteredTextSegmentViewAdapter( 33, "Numerical [0..65535]", behavior: new( true ) ),
                        new FilteredTextSegmentViewAdapter( 34, "FreeText", new(), new( "\"", "\"", true, String.Empty ), new( true ) )];
            }
        }
        if ( parentTextSegments.Length == 4 )
        {
            if ( parentTextSegments[3].Ident.Code == 33 )       // Is 'Numerical'?
                return [new FilteredTextSegmentViewAdapter( 41, "km/h" )];
        }
        return Array.Empty<FilteredTextSegmentViewAdapter>();
    }

    private Random _r = new Random();
    private void PseudoValidation( RowViewAdapter row ) {
        if ( String.IsNullOrEmpty( row.PlainText ) )
        {
            row.Validation.ErrorCodes = -1;
        }
        else
        {
            if ( this._r.Next( 0, 3 ) == 0 )
                row.Validation.ErrorCodes = 1;
            else
                row.Validation.ErrorCodes = 0;
        }
    }

    #endregion Helper

    #endregion Methods
}
+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ public partial class RowBasedTextEditorMainWindowViewModel : RowBasedTextEditorV
        RowTextSegmentViewAdapter[] resultTextSegments = new RowTextSegmentViewAdapter[textSegments.Length];
        for ( int i = 0; i < textSegments.Length; i++ )
        {
            resultTextSegments[i] = new( new(), new( startIndex, textSegments[i] ), new() );
            resultTextSegments[i] = new( new( startIndex, textSegments[i] ) );
            startIndex += textSegments[i].Length + 1;
        }

+12 −6
Original line number Diff line number Diff line
@@ -3,13 +3,14 @@
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:uiCntl="clr-namespace:Common.UI.Controls.Editors.IntelliSenseEditor;assembly=Common.UI"
        xmlns:local="clr-namespace:Common.UI.ViewTest.Views.Editors"
        xmlns:viewModels="clr-namespace:Common.UI.ViewTest.ViewModels.Editors"
        mc:Ignorable="d"
        Title="IntelliSenseEditorMainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen"
        Style="{StaticResource MaterialDesignWindow}"
        Loaded="Window_Loaded">
        Style="{StaticResource MaterialDesignWindow}">
    <Window.DataContext>
        <viewModels:IntelliSenseEditorMainWindowViewModel/>
    </Window.DataContext>
@@ -18,9 +19,14 @@
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" >
            <TextBox/>
        </StackPanel>
        <uiCntl:IntelliSenseEditor Grid.Row="1"/>
        <DockPanel Grid.Row="0" LastChildFill="True" >
            <Button DockPanel.Dock="Right" Width="40" Padding="0" Margin="6,0,0,0" VerticalAlignment="Top"
                    Command="{Binding ChangeThemeCommand}">
                <materialDesign:PackIcon Kind="CogOutline" Width="20" Height="20" />
            </Button>
            <TextBox materialDesign:HintAssist.Hint="Lost focus control" Padding="4,2" Height="32" VerticalAlignment="Top" HorizontalAlignment="Stretch"
                     Style="{StaticResource MaterialDesignOutlinedTextBox}"/>
        </DockPanel>
        <uiCntl:IntelliSenseEditor Grid.Row="1" SpecialFilterItems="{Binding SpecialFilterItems}"/>
    </Grid>
</Window>
+3 −11
Original line number Diff line number Diff line
using System.Windows;
using System.Windows.Media;

using Common.UI.Models;

using MaterialDesignColors.Recommended;

@@ -13,15 +16,4 @@ public partial class IntelliSenseEditorMainWindow : Window
    public IntelliSenseEditorMainWindow() {
        InitializeComponent();
    }

    private void Window_Loaded( object sender, RoutedEventArgs e ) {
        PaletteHelper paletteHelper = new PaletteHelper();
        Theme currentTheme = paletteHelper.GetTheme();

        currentTheme.SetBaseTheme( BaseTheme.Light );
        currentTheme.SetPrimaryColor( GreySwatch.Grey700 );
        currentTheme.SetSecondaryColor( TealSwatch.Teal400 );

        paletteHelper.SetTheme( currentTheme );
    }
}
Loading