<commit_before>﻿Imports System.Reflection
Imports System.Windows.Input
Imports SkyEditor.Core.IO
Imports SkyEditor.Core.Utilities

Namespace UI
    ''' <summary>
    ''' The view model for an open file
    ''' </summary>
    Public Class FileViewModel
        Implements INotifyPropertyChanged

        Public Sub New()
            IsFileModified = False
            CloseCommand = New RelayCommand(AddressOf OnClosed)
        End Sub
        Public Sub New(file As Object)
            Me.New
            Me.File = file
        End Sub

#Region "Events"
        Public Event CloseCommandExecuted(sender As Object, e As EventArgs)
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Protected Sub RaisePropertyChanged(e As PropertyChangedEventArgs)
            RaiseEvent PropertyChanged(Me, e)
        End Sub
#End Region

#Region "Event Handlers"
        Private Sub File_OnSaved(sender As Object, e As EventArgs)
            IsFileModified = False
        End Sub

        Private Sub File_OnModified(sender As Object, e As EventArgs)
            IsFileModified = True
        End Sub
#End Region

#Region "Properties"
        Public Property File As Object
            Get
                Return _file
            End Get
            Set(value As Object)
                If TypeOf _file Is ISavable Then
                    RemoveHandler DirectCast(_file, ISavable).FileSaved, AddressOf File_OnSaved
                End If
                If TypeOf _file Is INotifyPropertyChanged Then
                    RemoveHandler DirectCast(_file, INotifyPropertyChanged).PropertyChanged, AddressOf File_OnModified
                End If
                If TypeOf _file Is INotifyModified Then
                    RemoveHandler DirectCast(_file, INotifyModified).Modified, AddressOf File_OnModified
                End If
                ResetViewModels()

                _file = value

                IsFileModified = False

                If TypeOf _file Is ISavable Then
                    AddHandler DirectCast(_file, ISavable).FileSaved, AddressOf File_OnSaved
                End If
                If TypeOf _file Is INotifyPropertyChanged Then
                    AddHandler DirectCast(_file, INotifyPropertyChanged).PropertyChanged, AddressOf File_OnModified
                End If
                If TypeOf _file Is INotifyModified Then
                    AddHandler DirectCast(_file, INotifyModified).Modified, AddressOf File_OnModified
                End If
            End Set
        End Property
        Dim _file As Object

        Public Property IsFileModified As Boolean
            Get
                Return _isFileModified
            End Get
            Set(value As Boolean)
                If Not _isFileModified = value Then
                    _isFileModified = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(IsFileModified)))

                    'Title is dependant on this property, so notify that it changed too
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(Title)))
                End If
            End Set
        End Property
        Dim _isFileModified As Boolean

        Public Overridable ReadOnly Property Title As String
            Get
                Dim out As String
                If TypeOf File Is INamed Then
                    out = DirectCast(File, INamed).Name
                Else
                    out = ReflectionHelpers.GetTypeFriendlyName(File.GetType)
                End If
                If IsFileModified Then
                    Return "* " & out
                Else<commit_msg>Fixed Issue #29: FileViewModel not using filename for title<commit_after>﻿Imports System.Reflection
Imports System.Windows.Input
Imports SkyEditor.Core.IO
Imports SkyEditor.Core.Utilities

Namespace UI
    ''' <summary>
    ''' The view model for an open file
    ''' </summary>
    Public Class FileViewModel
        Implements INotifyPropertyChanged

        Public Sub New()
            IsFileModified = False
            CloseCommand = New RelayCommand(AddressOf OnClosed)
        End Sub
        Public Sub New(file As Object)
            Me.New
            Me.File = file
        End Sub

#Region "Events"
        Public Event CloseCommandExecuted(sender As Object, e As EventArgs)
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Protected Sub RaisePropertyChanged(e As PropertyChangedEventArgs)
            RaiseEvent PropertyChanged(Me, e)
        End Sub
#End Region

#Region "Event Handlers"
        Private Sub File_OnSaved(sender As Object, e As EventArgs)
            IsFileModified = False
        End Sub

        Private Sub File_OnModified(sender As Object, e As EventArgs)
            IsFileModified = True
        End Sub
#End Region

#Region "Properties"
        Public Property File As Object
            Get
                Return _file
            End Get
            Set(value As Object)
                If TypeOf _file Is ISavable Then
                    RemoveHandler DirectCast(_file, ISavable).FileSaved, AddressOf File_OnSaved
                End If
                If TypeOf _file Is INotifyPropertyChanged Then
                    RemoveHandler DirectCast(_file, INotifyPropertyChanged).PropertyChanged, AddressOf File_OnModified
                End If
                If TypeOf _file Is INotifyModified Then
                    RemoveHandler DirectCast(_file, INotifyModified).Modified, AddressOf File_OnModified
                End If
                ResetViewModels()

                _file = value

                IsFileModified = False

                If TypeOf _file Is ISavable Then
                    AddHandler DirectCast(_file, ISavable).FileSaved, AddressOf File_OnSaved
                End If
                If TypeOf _file Is INotifyPropertyChanged Then
                    AddHandler DirectCast(_file, INotifyPropertyChanged).PropertyChanged, AddressOf File_OnModified
                End If
                If TypeOf _file Is INotifyModified Then
                    AddHandler DirectCast(_file, INotifyModified).Modified, AddressOf File_OnModified
                End If
            End Set
        End Property
        Dim _file As Object

        Public Property IsFileModified As Boolean
            Get
                Return _isFileModified
            End Get
            Set(value As Boolean)
                If Not _isFileModified = value Then
                    _isFileModified = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(IsFileModified)))

                    'Title is dependant on this property, so notify that it changed too
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(Title)))
                End If
            End Set
        End Property
        Dim _isFileModified As Boolean

        Public Overridable ReadOnly Property Title As String
            Get
                Dim out As String
                If Not String.IsNullOrEmpty(Filename) Then
                    out = Path.GetFileName(Filename)
                ElseIf TypeOf File Is INamed Then
                    out = DirectCast(File, INamed).Name
                Else
                    out = ReflectionHelpers.GetTypeFriendlyName(File.GetType)
                End If
                If IsFileModified Then
                    Return "* " & out
                Else
{'fraction_non_alphanumeric': 0.03657248481647742, 'fraction_numerical': 0.0002640612622128334, 'mean_word_length': 1.7170014347202296, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2344040', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1681, 'n_words': 595}