Files
EgtDOORCreator/MainWindow/MainWindowVM.vb
T
Dario Sassi 30497d3ee6 EgtDOORCreator :
- correzioni varie per decrementare sempre il conteggio di istanza all'uscita.
2026-01-27 16:22:38 +01:00

502 lines
20 KiB
VB.net

Imports EgtUILib
Imports System.ComponentModel
Imports System.IO
Imports EgtWPFLib5
Public Class MainWindowVM
Implements INotifyPropertyChanged
' Modello del MainWindow (classe vera e propria)
Private m_MainWindowModel As New MainWindowModel
Friend ReadOnly Property MainWindowModel As MainWindowModel
Get
Return m_MainWindowModel
End Get
End Property
Public ReadOnly Property Instance As Integer
Get
Return m_MainWindowModel.m_nInstance
End Get
End Property
Private m_VisibilityAssemblyManager As Visibility = Visibility.Visible
Public Property VisibilityAssemblyManager As Visibility
Get
Return m_VisibilityAssemblyManager
End Get
Set(value As Visibility)
m_VisibilityAssemblyManager = value
NotifyPropertyChanged("VisibilityAssemblyManager")
End Set
End Property
Private m_ProjectNameMsg As String
Public Property ProjectNameMsg As String
Get
Return m_ProjectNameMsg
End Get
Set(value As String)
m_ProjectNameMsg = "EgtDOORCreator - " & value
NotifyPropertyChanged("ProjectNameMsg")
End Set
End Property
Private m_Launcher As LauncherV
Public ReadOnly Property Launcher As LauncherV
Get
If IsNothing(m_Launcher) Then
m_Launcher = New LauncherV
m_Launcher.DataContext = New LauncherVM(Me)
End If
Return m_Launcher
End Get
End Property
Private m_OptionPage As OptionsVM
Public ReadOnly Property OptionPage As OptionsVM
Get
If IsNothing(m_OptionPage) Then
m_OptionPage = New OptionsVM
End If
Return m_OptionPage
End Get
End Property
Enum ListPageEnum As Integer
nNothingSelected
nAssemblyPage
nDDFPage
nHardwarePage
End Enum
Private m_SelectedPage As ListPageEnum
Public Property SelectedPage As ListPageEnum
Get
Return m_SelectedPage
End Get
Set(value As ListPageEnum)
' Verifico che sia una vera modifica
If m_SelectedPage <> value Then
m_SelectedPage = value
NotifyPropertyChanged("PageControl")
If Not IsNothing(Map.refInstrumentPanelVM) Then
Map.refInstrumentPanelVM.NotifyPropertyChanged("DimensionVisibility")
End If
End If
End Set
End Property
Private m_ProjectManagerHardware As ProjectManagerHardwareV
Private m_ProjectManager As ProjectManagerV
Private m_AssemblyManager As AssemblyManagerV
Private m_PartPage As PartPageV
Private m_AssemblyPage As AssemblyPageV
Private m_HardwarePage As HardwarePageV
Private m_SceneManager As SceneManagerV
Private Watcher As FileSystemWatcher = New FileSystemWatcher(IniFile.m_CompoDir, "*" & LUA_EXTENSION)
Public ReadOnly Property PageControl As ContentControl
Get
If m_SelectedPage = ListPageEnum.nDDFPage Then
If OptionModule.m_SingleDoor Then
VisibilityAssemblyManager = Visibility.Hidden
Else
VisibilityAssemblyManager = Visibility.Visible
End If
NotifyPropertyChanged("AssemblyManagerControl")
NotifyPropertyChanged("ProjectManagerControl")
Return m_PartPage
ElseIf m_SelectedPage = ListPageEnum.nAssemblyPage Then
If OptionModule.m_SingleDoor Then
VisibilityAssemblyManager = Visibility.Hidden
Else
VisibilityAssemblyManager = Visibility.Visible
End If
NotifyPropertyChanged("AssemblyManagerControl")
NotifyPropertyChanged("ProjectManagerControl")
Return m_AssemblyPage
ElseIf m_SelectedPage = ListPageEnum.nHardwarePage Then
VisibilityAssemblyManager = Visibility.Collapsed
NotifyPropertyChanged("AssemblyManagerControl")
NotifyPropertyChanged("ProjectManagerControl")
Return m_HardwarePage
Else
If OptionModule.m_SingleDoor Then
VisibilityAssemblyManager = Visibility.Hidden
Else
VisibilityAssemblyManager = Visibility.Visible
End If
NotifyPropertyChanged("AssemblyManagerControl")
NotifyPropertyChanged("ProjectManagerControl")
Return Nothing
End If
End Get
End Property
Public ReadOnly Property AssemblyManagerControl As ContentControl
Get
If VisibilityAssemblyManager = Visibility.Visible Then
Return m_AssemblyManager
Else
Return Nothing
End If
End Get
End Property
Public ReadOnly Property SceneManagerControl As ContentControl
Get
Return m_SceneManager
End Get
End Property
Public ReadOnly Property ProjectManagerControl As ContentControl
Get
If m_SelectedPage <> ListPageEnum.nHardwarePage Then
Return m_ProjectManager
Else
ProjectNameMsg = "HardwareManager"
Return m_ProjectManagerHardware
End If
'If VisibilityAssemblyManager = Visibility.Visible Then
' Return m_ProjectManager
'Else
' ProjectNameMsg = "HardwareManager"
' Return m_ProjectManagerHardware
'End If
End Get
End Property
Sub New()
EgtOutLog(" Creazione e inizializzazone MainWondowVM ")
Map.SetRefMainWindowVM(Me)
m_AssemblyPage = New AssemblyPageV
m_AssemblyPage.DataContext = New AssemblyPageVM
m_ProjectManagerHardware = New ProjectManagerHardwareV
m_ProjectManagerHardware.DataContext = New ProjectManagerHardwareVM
m_ProjectManager = New ProjectManagerV
m_ProjectManager.DataContext = New ProjectManagerVM
m_PartPage = New PartPageV
m_PartPage.DataContext = New PartPageVM
m_HardwarePage = New HardwarePageV
m_HardwarePage.DataContext = New HardwarePageVM
m_AssemblyManager = New AssemblyManagerV
m_AssemblyManager.DataContext = New AssemblyManagerVM
m_SceneManager = New SceneManagerV
m_SceneManager.DataContext = New SceneManagerVM
Dim OptionsPage As OptionsVM = OptionPage
InitWatcher()
End Sub
#Region "DISPOSITION SCREEN"
Public ReadOnly Property WidthColumn1 As String
Get
If OptionModule.m_nDispostionScreen = 1 Or OptionModule.m_nDispostionScreen = 2 Then
Return "5*"
Else
Return "6*"
End If
End Get
End Property
Public ReadOnly Property WidthColumn2 As String
Get
If OptionModule.m_nDispostionScreen = 1 Or OptionModule.m_nDispostionScreen = 2 Then
Return "6*"
Else
Return "5*"
End If
End Get
End Property
Public ReadOnly Property AssemblyManagerDisposition As String
Get
If OptionModule.m_nDispostionScreen = 1 Or OptionModule.m_nDispostionScreen = 2 Then
Return "1"
Else
Return "0"
End If
End Get
End Property
Public ReadOnly Property PageControlDisposition As String
Get
If OptionModule.m_nDispostionScreen = 1 Or OptionModule.m_nDispostionScreen = 2 Then
Return "1"
Else
Return "0"
End If
End Get
End Property
Public ReadOnly Property SceneManagerDiposition As String
Get
If OptionModule.m_nDispostionScreen = 1 Or OptionModule.m_nDispostionScreen = 2 Then
Return "0"
Else
Return "1"
End If
End Get
End Property
#End Region ' Disposition Screen
#Region "Methods"
Public Sub ShowErrorCompoLoad()
Dim sError As String = Map.refCompoPanelVM.ErrorLoadCompo
If Not String.IsNullOrEmpty(sError) Then
MessageBox.Show(sError, EgtMsg(50101), MessageBoxButton.OK, MessageBoxImage.Error)
EgtOutLog(sError)
End If
End Sub
#End Region ' Methods
#Region "Watcher"
Public Enum WatcherCalling
OnCreated
OnDeleted
OnChanged
OnRenamed
OnLoadTempFile
NoEvents
End Enum
Public selectedWatcher As WatcherCalling = WatcherCalling.NoEvents
Public CompoDDFName As String = String.Empty
Public Event Er(message As String, sPath As String)
Public StartReload As Boolean = False
Public Sub PrintMessagesFromWatcher(message As String, sPath As String) Handles Me.Er
StartReload = True
' MessageBox.Show(Application.Current.MainWindow, message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning)
If sPath.Contains(IniFile.m_CompoDir) Then
sPath = Trim(sPath.Replace(IniFile.m_CompoDir & "\", ""))
End If
Dim sItems() As String = sPath.Split("\"c)
If sItems.Count > 1 Then
If Not IsNothing(Map.refCompoPanelVM) AndAlso Not IsNothing(Map.refCompoPanelVM.CompoTypeList) Then
For Each ItemCompo In Map.refCompoPanelVM.CompoTypeList
If ItemCompo.Path.Contains(sItems(0)) Then
' ricarico l'elenco dei direttori e dei file
ItemCompo.LoadListTemplate()
CompoDDFName = ItemCompo.DDFName
If Not IsNothing(Map.refProjectManagerVM) Then Map.refProjectManagerVM.ReloadCmd()
CompoDDFName = String.Empty
StartReload = False
Return
End If
Next
End If
End If
If Not IsNothing(Map.refProjectManagerVM) Then Map.refProjectManagerVM.ReloadCmd()
CompoDDFName = String.Empty
StartReload = False
End Sub
' Inizializzatore dell'evento Watcher (controllo dei direttori e dei files) su un nuovo Thread
Friend Sub InitWatcher(Optional ByVal StopWatcher As Boolean = False)
Watcher.IncludeSubdirectories = True
Watcher.Path = IniFile.m_CompoDir
' AddHandler Watcher.Changed, New FileSystemEventHandler(AddressOf OnChanged)
AddHandler Watcher.Deleted, New FileSystemEventHandler(AddressOf OnDeleted)
AddHandler Watcher.Created, New FileSystemEventHandler(AddressOf OnCreated)
AddHandler Watcher.Renamed, New RenamedEventHandler(AddressOf OnRenamed)
Watcher.EnableRaisingEvents = Not StopWatcher
End Sub
' comunico una modifica (forzo l'utilizzo del Thread principale richiamando un evento Er)
Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
If m_SelectedPage = ListPageEnum.nHardwarePage Then Return
If StartReload Then Return
Dim Message As String = "File: " & e.FullPath & " " & e.ChangeType
selectedWatcher = WatcherCalling.OnChanged
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
RaiseEvent Er(Message, e.FullPath)
End Sub), System.Windows.Threading.DispatcherPriority.ContextIdle, Nothing)
'If Not IsNothing(Map.refProjectManagerVM) Then Map.refProjectManagerVM.ReloadCmd()
End Sub
' cominico una eliminazione(forzo l'utilizzo del Thread principale richiamando un evento Er)
Private Sub OnDeleted(source As Object, e As FileSystemEventArgs)
If m_SelectedPage = ListPageEnum.nHardwarePage Then Return
If StartReload Then Return
' 50545 = It has been deleted file: {0}, {1}.
Dim Message As String = String.Format(EgtMsg(50545), e.FullPath, e.ChangeType)
selectedWatcher = WatcherCalling.OnDeleted
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
RaiseEvent Er(Message, e.FullPath)
End Sub), System.Windows.Threading.DispatcherPriority.ContextIdle, Nothing)
' comunico che nessun evento particolare è stato selezionato
End Sub
' cominico una rinomina (forzo l'utilizzo del Thread principale richiamando un evento Er)
Private Sub OnRenamed(source As Object, e As FileSystemEventArgs)
If m_SelectedPage = ListPageEnum.nHardwarePage Then Return
If StartReload Then Return
' 50543 = It has been renamed file: {0}, {1}.
Dim Message As String = String.Format(EgtMsg(50543), e.FullPath, e.ChangeType)
selectedWatcher = WatcherCalling.OnRenamed
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
RaiseEvent Er(Message, e.FullPath)
End Sub), System.Windows.Threading.DispatcherPriority.ContextIdle, Nothing)
'If Not IsNothing(Map.refProjectManagerVM) Then Map.refProjectManagerVM.ReloadCmd()
End Sub
' cominico una creazione (forzo l'utilizzo del Thread principale richiamando un evento Er)
Private Sub OnCreated(source As Object, e As FileSystemEventArgs)
If m_SelectedPage = ListPageEnum.nHardwarePage Then Return
If StartReload Then Return
' 50544 = It has been created new file: {0}, {1}.
Dim Message As String = String.Format(EgtMsg(50544), e.FullPath, e.ChangeType)
selectedWatcher = WatcherCalling.OnCreated
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
RaiseEvent Er(Message, e.FullPath)
End Sub), System.Windows.Threading.DispatcherPriority.ContextIdle, Nothing)
'If Not IsNothing(Map.refProjectManagerVM) Then Map.refProjectManagerVM.ReloadCmd()
End Sub
#End Region ' Watcher
' Comandi
Private m_cmdAboutBox As ICommand
Private m_cmdClose As ICommand
Public Sub SetLauncher()
Dim nLauncher As Integer = If(OptionModule.m_bLauncherOpenOnce, LauncherOpt.Open_last_project, OptionModule.m_SelectedOptionLauncher)
Select Case nLauncher
Case LauncherOpt.Open_window
' apre la finestra
Dim Launcher As New LauncherV
Launcher.Height = 200
Launcher.Width = 250
LauncherModule.InitLauncherModule()
Launcher.DataContext = New LauncherVM(Me)
Launcher.Owner = Application.Current.MainWindow
Launcher.ShowDialog()
Case LauncherOpt.Open_last_project
' se definito ultimo progetto, lo apro
If GetMainPrivateProfileString(S_LAUNCHERWINDOW, K_LASTPROJECT, "", m_sLastProject) > 0 Then
' se caricato il nome in avvio del programma allora sovrascrivo
If OptionModule.AdjustDDT Then
m_sLastProject = OptionModule.FileNameFromArgs
End If
Map.refProjectManagerVM.OpenLastProject()
End If
Case Else
' non fa assolutamente nulla
End Select
End Sub
#Region "AboutBoxCommand"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property AboutBoxCommand() As ICommand
Get
If m_cmdAboutBox Is Nothing Then
m_cmdAboutBox = New Command(AddressOf AboutBox)
End If
Return m_cmdAboutBox
End Get
End Property
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Public Sub AboutBox(ByVal param As Object)
Dim AboutBoxWindow As New AboutBoxV
AboutBoxWindow.Owner = Application.Current.MainWindow
AboutBoxWindow.ShowDialog()
End Sub
#End Region ' AboutBoxCommand
#Region "CloseApplicationCommand"
Public ReadOnly Property CloseApplicationCommand() As ICommand
Get
If m_cmdClose Is Nothing Then
m_cmdClose = New Command(AddressOf CloseApplication)
End If
Return m_cmdClose
End Get
End Property
'Public Sub CloseApplication(ByVal param As Object)
Public Sub CloseApplication()
m_StopClosingApplication = False
' verifico se selezionato esiste e modificato
Select Case SelectedPage
Case ListPageEnum.nHardwarePage
If Not IsNothing(Map.refHardwarePageVM) And Not IsNothing(Map.refHardwarePageVM.CurrHardware) Then
If Map.refHardwarePageVM.CurrHardware.SaveControl() = Hardware.SaveResult.nCancel Then
m_StopClosingApplication = True
Return
ElseIf Map.refHardwarePageVM.CurrHardware.SaveControl() = Hardware.SaveResult.nYes Then
Map.refHardwarePageVM.CurrHardware.Save()
End If
Map.refHardwarePageVM.CurrHardware.DeleteTempFile()
End If
Case Else
If Not IsNothing(Map.refAssemblyManagerVM.CurrProject) AndAlso Not IsNothing(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName) AndAlso Map.refAssemblyManagerVM.CurrProject.SelAssemblyName.IsModified Then
Dim sText As String = String.Format(EgtMsg(50109), Path.GetFileNameWithoutExtension(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName.Name))
Select Case MessageBox.Show(sText, "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question)
Case MessageBoxResult.Yes
Map.refAssemblyManagerVM.Save(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName)
' se il ddf non è corretto non permetto il salvataggio
If Not DdfFile.DDFIsCorrect Then
m_StopClosingApplication = True
Return
End If
Case MessageBoxResult.No
' Non faccio alcunchè
Case MessageBoxResult.Cancel
m_StopClosingApplication = True
Return
End Select
End If
End Select
If Not IsNothing(Map.refAssemblyManagerVM.CurrProject) AndAlso Not IsNothing(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName) AndAlso
Not String.IsNullOrWhiteSpace(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName.Name) Then
If File.Exists(Map.refAssemblyManagerVM.CurrProject.SelAssemblyName.Name) Then
WriteMainPrivateProfileString(S_LAUNCHERWINDOW, K_LASTPROJECT, Map.refAssemblyManagerVM.CurrProject.SelAssemblyName.Name)
Else
WriteMainPrivateProfileString(S_LAUNCHERWINDOW, K_LASTPROJECT, Map.refAssemblyManagerVM.CurrProject.Name)
End If
ElseIf Not IsNothing(Map.refAssemblyManagerVM.CurrProject) AndAlso
Not String.IsNullOrWhiteSpace(Map.refAssemblyManagerVM.CurrProject.Name) Then
WriteMainPrivateProfileString(S_LAUNCHERWINDOW, K_LASTPROJECT, Map.refAssemblyManagerVM.CurrProject.Name)
End If
' Terminazione generale di EgtInterface
EgtExit()
' Libero eventuale porta bloccata
Utility.UnLockDir()
' Aggiorno istanze usate
m_MainWindowModel.ReleaseInstance()
' Chiudo la finestra principale del programma
Application.Current.MainWindow.Close()
End Sub
Private m_StopClosingApplication As Boolean = False
Public ReadOnly Property StopClosingApplication As Boolean
Get
' restituisce vero se l'operazione di chiusura del programma è stata interotta
Return m_StopClosingApplication
End Get
End Property
#End Region ' CloseApplicationCommand
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub NotifyPropertyChanged(propName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
End Sub
End Class