279 lines
10 KiB
VB.net
279 lines
10 KiB
VB.net
Imports System.IO
|
|
Imports EgtUILib
|
|
|
|
Public Class SaveFileDialogWithListVM
|
|
Inherits VMBase
|
|
|
|
Private m_Title As String
|
|
Public Property Title As String
|
|
Get
|
|
If IsNothing(m_Title) Then
|
|
If IsFolder Then
|
|
Return EgtMsg(30011) ' Nome Cartella
|
|
Else
|
|
Return EgtMsg(30010) ' Nome File
|
|
End If
|
|
Else
|
|
Return m_Title
|
|
End If
|
|
End Get
|
|
Set(value As String)
|
|
m_Title = value
|
|
End Set
|
|
End Property
|
|
Private m_sDirectory As String
|
|
Public Property Directory As String
|
|
Get
|
|
Return m_sDirectory
|
|
End Get
|
|
Set(value As String)
|
|
m_sDirectory = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_sExtension As String = String.Empty
|
|
Public Property Extension As String
|
|
Get
|
|
Return m_sExtension
|
|
End Get
|
|
Set(value As String)
|
|
m_sExtension = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_FileName As String
|
|
Public Property FileName As String
|
|
Get
|
|
Return m_FileName
|
|
End Get
|
|
Set(value As String)
|
|
m_FileName = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_IsFolder As Boolean = False
|
|
Public Property IsFolder As Boolean
|
|
Get
|
|
Return m_IsFolder
|
|
End Get
|
|
Set(value As Boolean)
|
|
m_IsFolder = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_WriteFileName As String
|
|
Public Property WriteFileName As String
|
|
Get
|
|
Return m_WriteFileName
|
|
End Get
|
|
Set(value As String)
|
|
Dim TempName As String = value
|
|
For Each cInvalid In Path.GetInvalidFileNameChars()
|
|
TempName = TempName.Replace(cInvalid, "")
|
|
Next
|
|
If TempName.Length = value.Length Then
|
|
m_WriteFileName = value
|
|
Else
|
|
MessageBox.Show(EgtMsg(31404),
|
|
EgtMsg(31407),
|
|
MessageBoxButton.OK, MessageBoxImage.Error)
|
|
'EgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(31404), ' I caratteri \ / : * ? " < > | non sono permessi
|
|
' EgtMsg(31407), ' CANCELLA
|
|
' MessageBoxButton.OK, MessageBoxImage.Error)
|
|
End If
|
|
NotifyPropertyChanged(NameOf(WriteFileName))
|
|
End Set
|
|
End Property
|
|
|
|
Private m_sFilter As String
|
|
Public Property Filter As String
|
|
Get
|
|
Return m_sFilter
|
|
End Get
|
|
Set(value As String)
|
|
m_sFilter = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_sFileNameFilter As Predicate(Of String)
|
|
Public Property FileNameFilter As Predicate(Of String)
|
|
Get
|
|
Return m_sFileNameFilter
|
|
End Get
|
|
Set(value As Predicate(Of String))
|
|
m_sFileNameFilter = value
|
|
End Set
|
|
End Property
|
|
|
|
Private m_sExtensions As New List(Of String)
|
|
Public ReadOnly Property Extensions As List(Of String)
|
|
Get
|
|
Return m_sExtensions
|
|
End Get
|
|
End Property
|
|
|
|
Private m_FileList As New List(Of String)
|
|
Public Property FileList As List(Of String)
|
|
Get
|
|
Return m_FileList
|
|
End Get
|
|
Set(value As List(Of String))
|
|
m_FileList = value
|
|
NotifyPropertyChanged(NameOf(FileList))
|
|
End Set
|
|
End Property
|
|
|
|
Private m_SelectedFile As String
|
|
Public Property SelectedFile As String
|
|
Get
|
|
Return m_SelectedFile
|
|
End Get
|
|
Set(value As String)
|
|
m_SelectedFile = value
|
|
NotifyPropertyChanged(NameOf(SelectedFile))
|
|
|
|
If Not IsNothing(m_SelectedFile) Then
|
|
m_WriteFileName = Path.GetFileNameWithoutExtension(SelectedFile)
|
|
NotifyPropertyChanged(NameOf(WriteFileName))
|
|
End If
|
|
End Set
|
|
End Property
|
|
|
|
#Region "Messages"
|
|
|
|
Public ReadOnly Property SaveMsg As String
|
|
Get
|
|
Return EgtMsg(30001) ' Salva
|
|
End Get
|
|
End Property
|
|
Public ReadOnly Property CancelMsg As String
|
|
Get
|
|
Return EgtMsg(30002) ' Annulla
|
|
End Get
|
|
End Property
|
|
|
|
#End Region
|
|
|
|
Public Sub Initialize()
|
|
' se la cartella d'origine non è stata impostata, esco
|
|
If String.IsNullOrWhiteSpace(m_sDirectory) Then
|
|
Throw New Exception("Exception: Directory is null or white space!")
|
|
End If
|
|
' se non esiste, esco
|
|
If Not IO.Directory.Exists(m_sDirectory) Then
|
|
Throw New Exception("Exception: Directory doesn't exist!")
|
|
End If
|
|
If String.IsNullOrWhiteSpace(m_sExtension) And Not IsFolder Then
|
|
Throw New Exception("Exception: Extension is null or white space!")
|
|
End If
|
|
' verifico se è impostato un nome di default
|
|
If Not String.IsNullOrWhiteSpace(m_FileName) Then
|
|
m_WriteFileName = m_FileName
|
|
End If
|
|
NotifyPropertyChanged("WriteFileName")
|
|
If IsFolder Then m_sExtension = ""
|
|
' inizializzo la ListBox
|
|
InitializeListBox()
|
|
End Sub
|
|
|
|
Public Function InitializeListBox() As Boolean?
|
|
' se esiste la stringa filtro estensioni
|
|
If Not IsNothing(m_sFilter) Then
|
|
' creo la lista delle estensioni da considerare
|
|
m_sFilter = m_sFilter.ToLower
|
|
m_sFilter = m_sFilter.Replace("*", "")
|
|
m_sExtensions = New List(Of String)(m_sFilter.Split(","c))
|
|
For Index = m_sExtensions.Count - 1 To 0 Step -1
|
|
If Not Path.HasExtension(m_sExtensions(Index)) Then
|
|
m_sExtensions.RemoveAt(Index)
|
|
End If
|
|
Next
|
|
End If
|
|
' leggo i file che contiene
|
|
Dim AllFilesInDir() As String = IO.Directory.GetFiles(m_sDirectory)
|
|
'Dim AllVerifiedFilesInDir As New List(Of String)
|
|
For FileIndex = 0 To AllFilesInDir.Count - 1
|
|
Dim CurrFile As String = AllFilesInDir(FileIndex)
|
|
' se esistono delle estensioni da verificare
|
|
If m_sExtensions.Count > 0 Then
|
|
' verifico se ha una delle estensioni ricercate
|
|
For ExtIndex = 0 To m_sExtensions.Count - 1
|
|
If Path.GetExtension(CurrFile).ToLower = m_sExtensions(ExtIndex).ToLower Then
|
|
' verifico se esiste la regola del filtro generico
|
|
If Not IsNothing(m_sFileNameFilter) Then
|
|
' verifico che la regola del filtro generico sia rispettata
|
|
If m_sFileNameFilter(CurrFile) Then
|
|
' aggiungo il file alla lista
|
|
m_FileList.Add(AllFilesInDir(FileIndex))
|
|
End If
|
|
Else
|
|
' aggiungo il file alla lista
|
|
m_FileList.Add(AllFilesInDir(FileIndex))
|
|
End If
|
|
|
|
Exit For
|
|
End If
|
|
Next
|
|
Else
|
|
' altrimenti verifico se esiste la regola del filtro generico
|
|
If Not IsNothing(m_sFileNameFilter) Then
|
|
' verifico che la regola del filtro generico sia rispettata
|
|
If m_sFileNameFilter(CurrFile) Then
|
|
' aggiungo il file alla lista
|
|
m_FileList.Add(AllFilesInDir(FileIndex))
|
|
End If
|
|
Else
|
|
' aggiungo il file alla lista
|
|
m_FileList.Add(AllFilesInDir(FileIndex))
|
|
End If
|
|
End If
|
|
Next
|
|
' verifico se il nome file passatomi è presente nella lista
|
|
If Not String.IsNullOrWhiteSpace(m_FileName) Then
|
|
If Path.HasExtension(m_FileName) Then
|
|
If m_FileList.Contains(m_FileName) Then
|
|
SelectedFile = m_FileName
|
|
ElseIf m_FileList.Contains(m_sDirectory & "\" & m_FileName) Then
|
|
SelectedFile = m_sDirectory & "\" & m_FileName
|
|
End If
|
|
Else
|
|
For FileIndex = 0 To m_FileList.Count - 1
|
|
Dim CurrFile As String = AllFilesInDir(FileIndex)
|
|
If CurrFile = m_FileName Then
|
|
SelectedFile = m_FileName
|
|
ElseIf CurrFile = m_sDirectory & "\" & m_FileName Then
|
|
SelectedFile = m_sDirectory & "\" & m_FileName
|
|
End If
|
|
Next
|
|
End If
|
|
End If
|
|
End Function
|
|
|
|
Friend Function Save() As Boolean
|
|
Dim sCompleteFileName As String = m_sDirectory & "\" & m_WriteFileName & If(IsFolder, "", m_sExtension)
|
|
If String.IsNullOrWhiteSpace(m_WriteFileName) Then
|
|
MessageBox.Show(EgtMsg(30008), EgtMsg(30007), MessageBoxButton.OK, MessageBoxImage.Error)
|
|
'EgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(30008), EgtMsg(30007), MessageBoxButton.OK, MessageBoxImage.Error) ' Il nome non può essere vuoto! - ERRORE
|
|
Return False
|
|
End If
|
|
If IsFolder Then
|
|
If System.IO.Directory.Exists(sCompleteFileName) Then
|
|
If MessageBox.Show(m_WriteFileName & " " & EgtMsg(30003) & vbCrLf & EgtMsg(30004), EgtMsg(30009), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) <> MessageBoxResult.Yes Then
|
|
'If EgtMessageBoxV.Show(Application.Current.MainWindow, m_WriteFileName & " " & EgtMsg(30003) & vbCrLf & EgtMsg(30004), EgtMsg(30009), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) <> MessageBoxResult.Yes Then ' eiste già - Sostituirlo? - ATTENZIONE
|
|
Return False
|
|
End If
|
|
End If
|
|
Else
|
|
If File.Exists(sCompleteFileName) Then
|
|
If MessageBox.Show(m_WriteFileName & " " & EgtMsg(30003) & vbCrLf & EgtMsg(30004), EgtMsg(30009), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) <> MessageBoxResult.Yes Then
|
|
'If EgtMessageBoxV.Show(Application.Current.MainWindow, m_WriteFileName & " " & EgtMsg(30003) & vbCrLf & EgtMsg(30004), EgtMsg(30009), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) <> MessageBoxResult.Yes Then ' eiste già - Sostituirlo? - ATTENZIONE
|
|
Return False
|
|
End If
|
|
End If
|
|
End If
|
|
m_FileName = sCompleteFileName
|
|
Return True
|
|
End Function
|
|
|
|
End Class
|