Imports System.Collections.ObjectModel Imports System.ComponentModel Imports System.IO Imports EgtUILib Imports Ionic.zip Public Class ProjectManagerVM Implements INotifyPropertyChanged #Region "FIELDS & PROPERTIES" 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 Private m_cmdSendFeedback As ICommand Private m_CmdCreateTemplate As ICommand Private m_cmdGuide As ICommand #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 SendFeedbackToolTip As String Get Return EgtMsg(50046) End Get End Property Public ReadOnly Property OptionsToolTip As String Get Return EgtMsg(MSG_MAINWINDOW + 209) End Get End Property Public ReadOnly Property TemplateDoorToolTip As String Get Return EgtMsg(MSG_MAINWINDOW + 223) End Get End Property Public ReadOnly Property GuideToolTip As String Get Return EgtMsg(50305) End Get End Property Public ReadOnly Property Help As String Get Return EgtMsg(50305) End Get End Property #End Region ' ToolTip #End Region ' Fields & Properties #Region "CONSTRUCTOR" Sub New() Map.SetRefProjectManagerVM(Me) 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() If Not Map.refAssemblyManagerVM.SaveCurrentAssembly() Then Return Map.refMainWindowVM.SelectedPage = MainWindowVM.ListPageEnum.nNothingSelected ' visualizzo la text per inserire il nome del progetto (cioè la directory da creare) Dim SaveFileDialog As New EgtWPFLib5.EgtSaveFileDialog SaveFileDialog.Title = EgtMsg(50304) SaveFileDialog.Directory = IniFile.m_MyProjectDir & "\" SaveFileDialog.Extension = "\" If Not SaveFileDialog.EgtShowDialog Then Return End If Directory.CreateDirectory(SaveFileDialog.FileName) Dim CurrProject As Project = Map.refAssemblyManagerVM.CurrProject ' salvo il riferimento al progetto corrente m_CurrProject = CurrProject ' salvo path progetto CurrProject.Name = SaveFileDialog.FileName ' Ripulisco lista porte CurrProject.AssemblyList.Clear() '' se una porta è già aperta 'Map.refDoorParametersVM.CurrPart = Nothing Map.refAssemblyPageVM.CurrAssembly = Nothing Map.refPartPageVM.CurrPart = Nothing EgtNewFile() EgtZoom(ZM.ALL) ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("PartList") Map.refMainWindowVM.ProjectNameMsg = Map.refMainWindowVM.SetTitle(m_CurrProject.Name) Map.refMainWindowVM.ModifyTitle(True) 'm_rfMainWindowVM.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() If Not Map.refAssemblyManagerVM.SaveCurrentAssembly() Then Return Dim FolderBrowserDialog As New System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog.SelectedPath = IniFile.m_MyProjectDir ' 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 CurrProject As Project = Map.refAssemblyManagerVM.CurrProject ' salvo il riferimento al progetto corrente m_CurrProject = CurrProject ' salvo path progetto CurrProject.Name = FolderBrowserDialog.SelectedPath ' Ripulisco lista porte CurrProject.AssemblyList.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 Dim Local_Assembly As New AssemblyName Local_Assembly.Name = DDFFileArray(DDFIndex) Local_Assembly.IsModify = False CurrProject.AssemblyList.Add(Local_Assembly) End If Next ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("AssemblyList") ' Se c'è almeno una porta la visualizzo If CurrProject.AssemblyList.Count > 0 Then Map.refAssemblyManagerVM.OpenSelectedAssembly(CurrProject.AssemblyList(0)) 'DoorManagerVM.SelectedDoor = CurrProject.DoorList(0) Else Map.refPartPageVM.CurrPart = Nothing EgtNewFile() EgtZoom(ZM.ALL) End If Map.refMainWindowVM.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() Dim SetTitle As String Dim CurrAssembly As Assembly = Map.refAssemblyPageVM.CurrAssembly If Not String.IsNullOrWhiteSpace(Map.refAssemblyManagerVM.SelectedAssembly.Name) Then 'Map.refDoorManagerVM.DeleteNewPart = 1 If Not IsNothing(CurrAssembly) Then SetTitle = Map.refAssemblyManagerVM.CurrProject.Name 'DdfFile.WriteDDFAssembly(CurrAssembly, Map.refDoorManagerVM.SelectedAssembly, True) Map.refAssemblyManagerVM.SelectedAssembly.IsModify = False Part.FirstReadingEdge = False Map.refAssemblyManagerVM.SelectedAssembly.NotifyPropertyChanged("IsModify") If Map.refAssemblyPageVM.CurrAssembly.GetArrayPartDoor(0).Door.IsNotAssembly Then DdfFile.WriteDDFPart(Map.refAssemblyPageVM.CurrAssembly.GetArrayPartDoor(0).Door, Map.refAssemblyManagerVM.SelectedAssembly.Name, True, False) DdfFile.WriteDDFAssembly(Map.refAssemblyPageVM.CurrAssembly, IniFile.m_sTempDir & "\" & TEMP_FILE, True) ExecDoors(Map.refSceneManagerVM.ProjectScene, IniFile.m_sTempDir & "\" & TEMP_FILE) Else DdfFile.WriteDDFAssembly(Map.refAssemblyPageVM.CurrAssembly, Map.refAssemblyManagerVM.SelectedAssembly.Name, True) DdfFile.WriteDDFAssembly(Map.refAssemblyPageVM.CurrAssembly, IniFile.m_sTempDir & "\" & TEMP_ASSEMBLY, True) ExecDoors(Map.refSceneManagerVM.ProjectScene, IniFile.m_sTempDir & "\" & TEMP_ASSEMBLY) Map.refAssemblyManagerVM.CurrProject.NotifyPropertyChanged("AssemblyList") End If Else MessageBox.Show(EgtMsg(50128), EgtMsg(50101), MessageBoxButton.OK, MessageBoxImage.Exclamation) End If Else MessageBox.Show(EgtMsg(50128), EgtMsg(50101), 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 OptionsV OptionsWindow.Height = 550 OptionsWindow.Width = 650 OptionsWindow.DataContext = New OptionsVM(Map.refMainWindowVM) 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 CurrProject As Project = Map.refAssemblyManagerVM.CurrProject ' carico il percorso dell'ultimo progetto salvato (sul file Config.ini) CurrProject.Name = OptionModule.m_sLastProject ' ripulisco la lista di porte CurrProject.AssemblyList.Clear() ' verifico che esista la cartella If Directory.Exists(CurrProject.Name) Then ' 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 Dim Local_Assembly As New AssemblyName Local_Assembly.Name = DDFFileArray(DDFIndex) Local_Assembly.IsModify = False CurrProject.AssemblyList.Add(Local_Assembly) End If Next ' Notifico aggiornamento DoorList e Name per aggiornare la grafica CurrProject.NotifyPropertyChanged("Name") CurrProject.NotifyPropertyChanged("AssemblyList") Map.refMainWindowVM.ProjectNameMsg = CurrProject.Name Map.refMainWindowVM.ModifyTitle(False) ' Se c'è almeno una porta la visualizzo If CurrProject.AssemblyList.Count > 0 Then 'Map.refAssemblyManagerVM.SelectedAssembly = CurrProject.AssemblyList(0) Map.refAssemblyManagerVM.OpenSelectedAssembly(CurrProject.AssemblyList(0)) End If End If End Sub #End Region ' LastProject #Region "SendFeedbackCommand" ''' ''' Returns a command that do SendFeedback. ''' Public ReadOnly Property SendFeedbackCommand As ICommand Get If m_cmdSendFeedback Is Nothing Then m_cmdSendFeedback = New Command(AddressOf SendFeedback) End If Return m_cmdSendFeedback End Get End Property ''' ''' Execute the SendFeedback. This method is invoked by the SendFeedbackCommand. ''' Public Sub SendFeedback() ' Recupero indirizzo a cui spedire la mail Dim sSupportAddress As String = String.Empty GetPrivateProfileString(S_GENERAL, K_SUPPORT, "support@egaltech.com", sSupportAddress) ' se vuoto do messaggio di errore ed esco If String.IsNullOrWhiteSpace(sSupportAddress) Then MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 10), EgtMsg(50101), MessageBoxButton.OK, MessageBoxImage.Error) Return End If ' Recupero numero chiave Dim sKey As String = String.Empty EgtGetKeyInfo(sKey) ' Recupero file del progetto corrente (tipo file .nge) Dim sCurrProject As String = String.Empty EgtGetCurrFilePath(sCurrProject) ' se nome file vuoto, chiedo se si vuole salvare (file .nge) If String.IsNullOrWhiteSpace(sCurrProject) Then If MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 11), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then Save() End If EgtGetCurrFilePath(sCurrProject) ' se modificato, chiedo se si vuole salvare Else If EgtGetModified() Then If MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 11), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then Save() End If End If End If ' recuro il file DDF associato al file .nge Dim sCurrProjectDDF As String = String.Empty sCurrProjectDDF = IniFile.m_sTempDir & "\CurrDoor.ddf" ' recupero il file di log Dim sFileLog As String = String.Empty sFileLog = IniFile.m_sTempDir & "\EgtDOORCreatorLog1.txt" ' Creo zip file da allegare Dim sZipToCreate As String = IniFile.m_sTempDir & "\Feedback.zip" If File.Exists(sZipToCreate) Then File.Delete(sZipToCreate) End If Try Using zip As New Ionic.Zip.ZipFile(sZipToCreate, Console.Out) ' aggiungo progetto corrente .nge If Not String.IsNullOrWhiteSpace(sCurrProject) Then zip.AddItem(sCurrProject, "") End If ' aggiungo progetto corrente .ddf If Not String.IsNullOrWhiteSpace(sCurrProjectDDF) Then zip.AddItem(sCurrProjectDDF, "") End If ' aggiungo file log If Not String.IsNullOrWhiteSpace(sFileLog) Then zip.AddItem(sFileLog, "") End If ' aggiungo la cartella Doors If Directory.Exists(IniFile.m_sDoorsDirPath) Then zip.AddItem(IniFile.m_sDoorsDirPath, "Doors") End If zip.Save() End Using Catch ex1 As Exception EgtOutLog("Exception in zip: " & ex1.ToString()) End Try ' preparo la mail per il supporto Dim bEx As Boolean = False Try Dim SendFeedbackWindow As New EgtWPFLib5.MapiMailMessage("EgtDOORCreator Feedback - " & sKey) SendFeedbackWindow.Recipients.Add(sSupportAddress) If Not String.IsNullOrWhiteSpace(sZipToCreate) AndAlso File.Exists(sZipToCreate) Then SendFeedbackWindow.Files.Add(IniFile.m_sTempDir & "\Feedback.zip") End If SendFeedbackWindow.ShowDialog() Catch ex As Exception EgtOutLog("Feedback exception: " & ex.ToString) bEx = True End Try If bEx OrElse EgtWPFLib5.MapiMailMessage.m_ErrorCode <> 0 Then MessageBox.Show(String.Format(EgtMsg(MSG_TOPCOMMANDBAR + 12), sSupportAddress, sZipToCreate), EgtMsg(MSG_EGTDOORCREATOR + 118), MessageBoxButton.OK, MessageBoxImage.Information) ' stampo il messaggio nella StatusBar Map.refStatusBarVM.StatusOutput = EgtMsg(MSG_TOPCOMMANDBAR + 14) Else ' stampo il messaggio nella StatusBar Map.refStatusBarVM.StatusOutput = EgtMsg(MSG_TOPCOMMANDBAR + 14) End If End Sub #End Region ' SendFeedbackCommand #Region "CreateTemplate" Public ReadOnly Property CreateTemplateCommand As ICommand Get If m_CmdCreateTemplate Is Nothing Then m_CmdCreateTemplate = New Command(AddressOf CreateTemplate) End If Return m_CmdCreateTemplate End Get End Property Public Sub CreateTemplate() ' se esite una porta, allora rendo possibile la creazione di un template della porta corrente If Not IsNothing(Map.refPartPageVM.CurrPart) Then Dim rfCurrDoor As Part = Map.refPartPageVM.CurrPart ' Stampo il file correte nella cartella Template Dim SaveFileDialog As New EgtWPFLib5.EgtSaveFileDialog SaveFileDialog.Title = K_NEWTEMPLATE SaveFileDialog.Directory = IniFile.m_TemplateDir & "\" SaveFileDialog.Extension = K_DDTEXTENSION 'SaveFileDialog.FileName = m_CurrProject.Name & "\" ' se decido di chiuedere la finestra If Not SaveFileDialog.EgtShowDialog Then Return End If ' Controllo se ha estensione il nome della porta If Path.HasExtension(SaveFileDialog.FileName) Then Dim sExtension As String = Path.GetExtension(SaveFileDialog.FileName) If Not sExtension.ToLower = K_DDTEXTENSION Then SaveFileDialog.FileName = SaveFileDialog.FileName.Replace(sExtension, K_DDTEXTENSION) End If Else SaveFileDialog.FileName &= K_DDTEXTENSION End If DdfFile.WriteDDFPart(rfCurrDoor, SaveFileDialog.FileName, False, True) Else MessageBox.Show(EgtMsg(50155), EgtMsg(50101), MessageBoxButton.OK, MessageBoxImage.Exclamation) End If End Sub #End Region ' CreateTemplate #Region "Guide" Public ReadOnly Property GuideCommand As ICommand Get If m_CmdGuide Is Nothing Then m_cmdGuide = New Command(AddressOf Guide) End If Return m_cmdGuide End Get End Property Public Sub Guide() Dim GuideWindow As New GuideV(Application.Current.MainWindow, New GuideVM) GuideWindow.Show() End Sub #End Region ' Guide #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