Imports System.Collections.ObjectModel Imports System.ComponentModel Imports System.IO Imports EgtUILib Public Class ProjectManagerViewModel Implements INotifyPropertyChanged #Region "FIELDS & PROPERTIES" ' Riferimento al MainWindowViewModel Private m_rfMainWindowViewModel As MainWindowViewModel Friend m_CurrProject As Project 'Public ReadOnly Property MruFileNames As ObservableCollection(Of String) ' Get ' Return IniFile.m_MruFiles.m_FileNames ' End Get 'End Property ' Definizione comandi Private m_cmdNew As ICommand Private m_cmdOpen As ICommand 'Private Shared m_cmdOpenMruFile As ICommand Private m_cmdSave As ICommand Private m_cmdSaveAs As ICommand Private m_cmdInsert As ICommand Private m_cmdImport As ICommand Private m_cmdExport As ICommand Private m_cmdOptions As ICommand Private m_CmdLastProject As ICommand #Region "Messages" #End Region ' Messages #Region "ToolTip" 'Proprietà ToolTip Public ReadOnly Property NewToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 1) End Get End Property Public ReadOnly Property OpenToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 2) End Get End Property Public ReadOnly Property SaveToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 3) End Get End Property Public ReadOnly Property SaveAsToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 4) End Get End Property Public ReadOnly Property InsertToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 5) End Get End Property Public ReadOnly Property ImportToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 6) End Get End Property Public ReadOnly Property ExportToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 7) End Get End Property Public ReadOnly Property OptionsToolTip As String Get Return EgtMsg(MSG_TOPCOMMANDBAR + 4) End Get End Property #End Region ' ToolTip #End Region ' Fields & Properties #Region "CONSTRUCTOR" Sub New(ByRef MainWindowViewModel As MainWindowViewModel) m_rfMainWindowViewModel = MainWindowViewModel ' controllo che la selezione di avvio sia settata sull'apertura dell'ultimo progetto 'If OptionModule.m_SelectedOptionLauncher = "Last Project" Then ' ' richiamo la funzione che già esiste per l'apertura dell'ultimo proegetto ' Dim LauncherViewModel As LauncherViewModel = DirectCast(m_rfMainWindowViewModel.Launcher.DataContext, LauncherViewModel) ' ' LauncherViewModel.OpenLastProject() 'End If ''Dim DoorManagerViewModel As DoorManagerViewModel = DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel) ''Dim CurrProject As Project = DoorManagerViewModel.CurrProject ' '' salvo il riferimento al progetto corrente ''m_CurrProject = CurrProject ' '' '' salvo path progetto '' ''CurrProject.Name = LastProject ' '' '' Ripulisco lista porte '' ''CurrProject.DoorList.Clear() ' '' '' Aggiungo porte trovate nella cartella ad un vettore '' ''Dim DDFFileArray() As String = Directory.GetFiles(LastProject) ' '' '' ripulisco il nome della porta (cancello tutto il percorso del file lasciando solo il nome del file) '' ''For DDFIndex = 0 To DDFFileArray.Count - 1 '' '' If Path.GetExtension(DDFFileArray(DDFIndex)).ToLower = ".ddf" Then '' '' ' lo aggiungo alla lista delle porte '' '' CurrProject.DoorList.Add(DDFFileArray(DDFIndex)) '' '' End If '' ''Next ' '' Notifico aggiornamento DoorList e Name per aggiornare la grafica ''CurrProject.NotifyPropertyChanged("Name") ''CurrProject.NotifyPropertyChanged("DoorList") ' '' Se c'è almeno una porta la visualizzo ''If CurrProject.DoorList.Count > 0 Then '' DoorManagerViewModel.SelectedDoor = CurrProject.DoorList(0) ''End If End Sub #End Region ' Constructor #Region "COMMANDS" #Region "NewCommand" ''' ''' Returns a command that do New. ''' Public ReadOnly Property NewCommand As ICommand Get If m_cmdNew Is Nothing Then m_cmdNew = New Command(AddressOf NewCmd) End If Return m_cmdNew End Get End Property ''' ''' Execute the New. This method is invoked by the NewCommand. ''' Public Sub NewCmd() Dim SaveFileDialog As New EgtWPFLib5.EgtSaveFileDialog SaveFileDialog.Title = EgtMsg(50304) SaveFileDialog.FileName = "c:\EgtData\EgtDOORCreator\MyProjects\" If Not SaveFileDialog.ShowDialog Then Return End If Directory.CreateDirectory(SaveFileDialog.FileName) Dim DoorManagerViewModel As DoorManagerViewModel = DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel) Dim CurrProject As Project = DoorManagerViewModel.CurrProject ' salvo il riferimento al progetto corrente m_CurrProject = CurrProject ' salvo path progetto CurrProject.Name = SaveFileDialog.FileName ' Ripulisco lista porte CurrProject.DoorList.Clear() ' se una porta è già aperta If Not String.IsNullOrWhiteSpace(DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel).SelectedDoor) Then DirectCast(m_rfMainWindowViewModel.DoorParameters.DataContext, DoorParametersViewModel).CurrDoor = Nothing ' assegno all'inidirizzo della porta uno spazio vuoto DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel).SelectedDoor = Nothing End If EgtNewFile() EgtZoom(ZM.ALL) ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("DoorList") m_rfMainWindowViewModel.ProjectNameMsg = m_CurrProject.Name End Sub #End Region ' NewCommand #Region "OpenCommand" ''' ''' Returns a command that do Open. ''' Public ReadOnly Property OpenCommand As ICommand Get If m_cmdOpen Is Nothing Then m_cmdOpen = New Command(AddressOf Open) End If Return m_cmdOpen End Get End Property ''' ''' Execute the Open. This method is invoked by the OpenCommand. ''' Public Sub Open() Dim FolderBrowserDialog As New System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog.SelectedPath = "c:\EgtData\EgtDOORCreator\MyProjects" ' mostriamo la finestra di dialogo aperta fino alla directory MyProjects If FolderBrowserDialog.ShowDialog <> Forms.DialogResult.OK Then ' se la risposta è diversa da OK esce Return End If Dim DoorManagerViewModel As DoorManagerViewModel = DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel) Dim CurrProject As Project = DoorManagerViewModel.CurrProject ' salvo il riferimento al progetto corrente m_CurrProject = CurrProject ' salvo path progetto CurrProject.Name = FolderBrowserDialog.SelectedPath ' Ripulisco lista porte CurrProject.DoorList.Clear() ' Aggiungo porte trovate nella cartella ad un vettore Dim DDFFileArray() As String = Directory.GetFiles(FolderBrowserDialog.SelectedPath) ' ripulisco il nome della porta (cancello tutto il percorso del file lasciando solo il nome del file) For DDFIndex = 0 To DDFFileArray.Count - 1 If Path.GetExtension(DDFFileArray(DDFIndex)).ToLower = ".ddf" Then ' lo aggiungo alla lista delle porte CurrProject.DoorList.Add(DDFFileArray(DDFIndex)) End If Next ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("DoorList") ' Se c'è almeno una porta la visualizzo If CurrProject.DoorList.Count > 0 Then DoorManagerViewModel.SelectedDoor = CurrProject.DoorList(0) Else DirectCast(m_rfMainWindowViewModel.DoorParameters.DataContext, DoorParametersViewModel).CurrDoor = Nothing EgtNewFile() EgtZoom(ZM.ALL) End If m_rfMainWindowViewModel.ProjectNameMsg = CurrProject.Name End Sub #End Region ' OpenCommand '#Region "OpenMruFileCommand" ' ''' ' ''' Returns a command that do Open. ' ''' ' Public Shared ReadOnly Property OpenMruFileCommand As ICommand ' Get ' If m_cmdOpenMruFile Is Nothing Then ' m_cmdOpenMruFile = New RelayCommand(AddressOf OpenMruFile) ' End If ' Return m_cmdOpenMruFile ' End Get ' End Property ' ''' ' ''' Execute the Open. This method is invoked by the OpenCommand. ' ''' ' Public Shared Sub OpenMruFile(ByVal param As Object) ' Application.Msn.NotifyColleagues(Application.OPENPROJECT, DirectCast(param, String).Replace("__", "_")) ' End Sub '#End Region ' OpenMruFileCommand #Region "SaveCommand" ''' ''' Returns a command that do Save. ''' Public ReadOnly Property SaveCommand As ICommand Get If m_cmdSave Is Nothing Then m_cmdSave = New Command(AddressOf Save) End If Return m_cmdSave End Get End Property ''' ''' Execute the Save. This method is invoked by the SaveCommand. ''' Public Sub Save(ByVal param As Object) Dim SetTitle As String Dim CurrDoor As Door = DirectCast(m_rfMainWindowViewModel.DoorParameters.DataContext, DoorParametersViewModel).CurrDoor Dim DoorManagerViewModel As DoorManagerViewModel = DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel) If Not String.IsNullOrWhiteSpace(DoorManagerViewModel.SelectedDoor) Then DoorManagerViewModel.DeleteNewDoor = 1 If Not IsNothing(CurrDoor) Then SetTitle = DoorManagerViewModel.CurrProject.Name DdfFile.WriteDDF(CurrDoor, DoorManagerViewModel.SelectedDoor) CurrDoor.m_IsModifyDoor = False DirectCast(m_rfMainWindowViewModel.DoorParameters.DataContext, DoorParametersViewModel).bSetChange = False m_rfMainWindowViewModel.ProjectNameMsg = SetTitle Else MessageBox.Show(EgtMsg(50128), "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation) End If Else MessageBox.Show(EgtMsg(50128), "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation) End If End Sub #End Region ' SaveCommand '#Region "SaveAsCommand" ' ''' ' ''' Returns a command that do SaveAs. ' ''' ' Public ReadOnly Property SaveAsCommand As ICommand ' Get ' If m_cmdSaveAs Is Nothing Then ' m_cmdSaveAs = New RelayCommand(AddressOf SaveAs) ' End If ' Return m_cmdSaveAs ' End Get ' End Property ' ''' ' ''' Execute the SaveAs. This method is invoked by the SaveAsCommand. ' ''' ' Public Sub SaveAs(ByVal param As Object) ' Application.Msn.NotifyColleagues(Application.SAVEASPROJECT) ' End Sub '#End Region ' SaveAsCommand #Region "OptionsCommand" ''' ''' Returns a command that do Export. ''' Public ReadOnly Property OptionsCommand As ICommand Get If m_cmdOptions Is Nothing Then m_cmdOptions = New Command(AddressOf Options) End If Return m_cmdOptions End Get End Property ''' ''' Execute the Export. This method is invoked by the ExportCommand. ''' Public Sub Options(ByVal param As Object) Dim OptionsWindow As New OptionsView OptionsWindow.Height = 614 OptionsWindow.Width = 1024 OptionsWindow.DataContext = New OptionsViewModel OptionsWindow.Owner = Application.Current.MainWindow OptionsWindow.ShowDialog() End Sub #End Region ' OptionsCommand #Region "LastProject" Public ReadOnly Property LastProjectCommand As ICommand Get If m_CmdLastProject Is Nothing Then m_CmdLastProject = New Command(AddressOf OpenLastProject) End If Return m_CmdLastProject End Get End Property Public Sub OpenLastProject() ' Apro l'ultimo progetto Dim DoorManagerViewModel As DoorManagerViewModel = DirectCast(m_rfMainWindowViewModel.DoorManager.DataContext, DoorManagerViewModel) Dim CurrProject As Project = DoorManagerViewModel.CurrProject ' carico il percorso dell'ultimo progetto salvato (sul file Config.ini) CurrProject.Name = OptionModule.m_sLastProject ' ripulisco la lista di porte CurrProject.DoorList.Clear() ' Aggiungo porte trovate nella cartella ad un vettore Dim DDFFileArray() As String = Directory.GetFiles(CurrProject.Name) ' ripulisco il nome della porta (cancello tutto il percorso del file lasciando solo il nome del file) For DDFIndex = 0 To DDFFileArray.Count - 1 If Path.GetExtension(DDFFileArray(DDFIndex)).ToLower = ".ddf" Then ' lo aggiungo alla lista delle porte CurrProject.DoorList.Add(DDFFileArray(DDFIndex)) End If Next ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("DoorList") ' Se c'è almeno una porta la visualizzo If CurrProject.DoorList.Count > 0 Then DoorManagerViewModel.SelectedDoor = CurrProject.DoorList(0) End If m_rfMainWindowViewModel.ProjectNameMsg = CurrProject.Name End Sub #End Region ' LastProject #End Region ' Commands Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Sub NotifyPropertyChanged(propName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName)) End Sub End Class