Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports System.Text.RegularExpressions Namespace EgtCAM5 Public Class MTableDbViewModel Inherits TabViewModel Private m_TablesList As New ObservableCollection(Of MTableListBoxItem) Public Property TablesList As ObservableCollection(Of MTableListBoxItem) Get Return m_TablesList End Get Set(value As ObservableCollection(Of MTableListBoxItem)) m_TablesList = value End Set End Property Private m_SelectedTable As MTableListBoxItem Public Property SelectedTable As MTableListBoxItem Get Return m_SelectedTable End Get Set(value As MTableListBoxItem) If value IsNot m_SelectedTable Then m_SelectedTable = value m_SelectedTable.SelectedAssociation = If(m_SelectedTable.AssociationList.Count > 0, m_SelectedTable.AssociationList(0), Nothing) OnPropertyChanged("SelectedTable") End If End Set End Property Private m_Title As String Public ReadOnly Property Title As String Get Return EgtMsg(MSG_MAINWINDOW + 20) End Get End Property ' Definizione comandi Private m_cmdNewTable As ICommand Private m_cmdSaveTable As ICommand Private m_cmdSaveTableAs As ICommand Private m_cmdRemoveTable As ICommand Private m_cmdAddMach As ICommand Private m_cmdRemoveMach As ICommand Private m_cmdAddRow As ICommand Private m_cmdRemoveRow As ICommand Private m_cmdMoveRowUp As ICommand Private m_cmdMoveRowDown As ICommand #Region "CONSTRUCTOR" Sub New() SearchTables() SelectedTable = If(m_TablesList.Count > 0, m_TablesList(0), Nothing) End Sub #End Region #Region "METHODS" Private Sub SearchTables() ' Leggo dal file ini il direttorio per le Table If GetPrivateProfileString(S_TABLE, K_TABLESDIR, "", IniFile.m_sTablesRoot) = 0 _ Or Not My.Computer.FileSystem.DirectoryExists(IniFile.m_sTablesRoot) Then ' Se non lo trovo mando messaggio di errore e chiudo la finestra MessageBox.Show("ERROR IN LOADING TABLES", "Tables dir not found.") End If ' se trovo la cartella carico la lista di tabelle For Each Table In My.Computer.FileSystem.GetFiles(IniFile.m_sTablesRoot) If Path.GetExtension(Table).ToLower = ".lua" Then m_TablesList.Add(New MTableListBoxItem(Path.GetFileNameWithoutExtension(Table), Table)) Dim sNameIndex As String = Regex.Match(Path.GetFileNameWithoutExtension(Table), "MTable_(\d+)").Groups(1).Value Dim nNameIndex As Integer = 0 If String.IsNullOrEmpty(sNameIndex) Then ' il suo valore di default è 1 quindi lo imposto a questo valore nNameIndex = 0 Else Integer.TryParse(sNameIndex, nNameIndex) End If If nNameIndex > MTableListBoxItem.NewMTableIndex Then MTableListBoxItem.NewMTableIndex = nNameIndex End If End If Next End Sub #End Region #Region "COMMANDS" #Region "NewTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property NewTableCommand As ICommand Get If m_cmdNewTable Is Nothing Then m_cmdNewTable = New RelayCommand(AddressOf NewTable) End If Return m_cmdNewTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub NewTable(ByVal param As Object) Dim NewMTable As MTableListBoxItem = New MTableListBoxItem("MTable_" & MTableListBoxItem.NewMTableIndex, String.Empty) m_TablesList.Add(NewMTable) MTableListBoxItem.NewMTableIndex += 1 SelectedTable = NewMTable End Sub #End Region ' NewTableCommand #Region "SaveTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property SaveTableCommand As ICommand Get If m_cmdSaveTable Is Nothing Then m_cmdSaveTable = New RelayCommand(AddressOf SaveTable) End If Return m_cmdSaveTable End Get End Property Private Const MMACHINEDATA As String = ".MMachineData" Private Const MTABLE As String = ".MTable" Private Const DATETIME As String = "%DATE_TIME%" Private Const TABLENAME As String = "%TABLE_NAME%" ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub SaveTable() If String.IsNullOrEmpty(SelectedTable.TableNamePath) Then SaveTableAs() Return End If TableUtility.WriteDoorsTable(SelectedTable, SelectedTable.TableNamePath) End Sub #End Region ' SaveTableCommand #Region "SaveTableAsCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property SaveTableAsCommand As ICommand Get If m_cmdSaveTableAs Is Nothing Then m_cmdSaveTableAs = New RelayCommand(AddressOf SaveTableAs) End If Return m_cmdSaveTableAs End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub SaveTableAs() Dim sFile As String = String.Empty ' Direttorio corrente per MTable Dim sDir As String = IniFile.m_sTablesRoot ' Apertura dialogo di scelta file DDF Dim SaveFileDialog As New System.Windows.Forms.SaveFileDialog SaveFileDialog.Title = "Save MTable file" SaveFileDialog.Filter = "MTable Lua script(*.lua)|*.lua" SaveFileDialog.FilterIndex = 1 SaveFileDialog.InitialDirectory = sDir SaveFileDialog.FileName = m_SelectedTable.TableName If SaveFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Sub End If sFile = SaveFileDialog.FileName TableUtility.WriteDoorsTable(SelectedTable, sFile) End Sub #End Region ' SaveTableAsCommand #Region "RemoveTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveTableCommand As ICommand Get If m_cmdRemoveTable Is Nothing Then m_cmdRemoveTable = New RelayCommand(AddressOf RemoveTable) End If Return m_cmdRemoveTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveTable(ByVal param As Object) If MessageBox.Show("Are you sure you want to delete this MTable?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then File.Delete(SelectedTable.TableNamePath) Dim SelectedTableIndex As Integer = m_TablesList.IndexOf(m_SelectedTable) m_TablesList.RemoveAt(SelectedTableIndex) If SelectedTableIndex > 0 Then SelectedTable = TablesList(SelectedTableIndex - 1) End If End If End Sub #End Region ' RemoveTableCommand #Region "AddMachCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddMachCommand As ICommand Get If m_cmdAddMach Is Nothing Then m_cmdAddMach = New RelayCommand(AddressOf AddMach) End If Return m_cmdAddMach End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddMach(ByVal param As Object) If Not IsNothing(m_SelectedTable) Then m_SelectedTable.SharedMachIndex += 1 m_SelectedTable.ActiveMachinesList.Add(New MTableMachineListBoxItem(String.Empty, False, False, m_SelectedTable.SharedMachIndex)) m_SelectedTable.MachIdList.Add(m_SelectedTable.SharedMachIndex) End If End Sub #End Region ' AddMachCommand #Region "RemoveMachCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveMachCommand As ICommand Get If m_cmdRemoveMach Is Nothing Then m_cmdRemoveMach = New RelayCommand(AddressOf RemoveMach) End If Return m_cmdRemoveMach End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveMach(ByVal param As Object) If Not IsNothing(m_SelectedTable) And m_SelectedTable.ActiveMachinesList.Count > 1 Then m_SelectedTable.ActiveMachinesList.RemoveAt(m_SelectedTable.SharedMachIndex - 1) m_SelectedTable.MachIdList.RemoveAt(m_SelectedTable.SharedMachIndex - 1) m_SelectedTable.SharedMachIndex -= 1 End If End Sub #End Region ' RemoveMachCommand #Region "AddRowCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddRowCommand As ICommand Get If m_cmdAddRow Is Nothing Then m_cmdAddRow = New RelayCommand(AddressOf AddRow) End If Return m_cmdAddRow End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddRow(ByVal param As Object) If Not IsNothing(m_SelectedTable) And Not IsNothing(m_SelectedTable.SelectedAssociation) Then Dim SelectedIndex As Integer = m_SelectedTable.AssociationList.IndexOf(m_SelectedTable.SelectedAssociation) Dim NewEmptyRow As MTableAssociationGridBoxItem = New MTableAssociationGridBoxItem(False, String.Empty, 1, 0, String.Empty, String.Empty, String.Empty, String.Empty) m_SelectedTable.AssociationList.Insert(SelectedIndex, NewEmptyRow) m_SelectedTable.SelectedAssociation = NewEmptyRow m_SelectedTable.NotifyPropertyChanged("SelectedAssociation") End If End Sub #End Region ' AddRowCommand #Region "RemoveRowCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveRowCommand As ICommand Get If m_cmdRemoveRow Is Nothing Then m_cmdRemoveRow = New RelayCommand(AddressOf RemoveRow) End If Return m_cmdRemoveRow End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveRow(ByVal param As Object) If Not IsNothing(m_SelectedTable) And m_SelectedTable.AssociationList.Count > 1 Then Dim SelectedIndex As Integer = m_SelectedTable.AssociationList.IndexOf(m_SelectedTable.SelectedAssociation) m_SelectedTable.AssociationList.RemoveAt(SelectedIndex) If Not SelectedIndex < m_SelectedTable.AssociationList.Count Then SelectedIndex = m_SelectedTable.AssociationList.Count - 1 End If m_SelectedTable.SelectedAssociation = m_SelectedTable.AssociationList(SelectedIndex) m_SelectedTable.NotifyPropertyChanged("SelectedAssociation") End If End Sub #End Region ' RemoveRowCommand #Region "MoveRowUpCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveRowUpCommand As ICommand Get If m_cmdMoveRowUp Is Nothing Then m_cmdMoveRowUp = New RelayCommand(AddressOf MoveRowUp) End If Return m_cmdMoveRowUp End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveRowUp(ByVal param As Object) If Not IsNothing(m_SelectedTable) Then Dim SelectedIndex As Integer = m_SelectedTable.AssociationList.IndexOf(m_SelectedTable.SelectedAssociation) If SelectedIndex >= 1 Then m_SelectedTable.AssociationList.Move(SelectedIndex, SelectedIndex - 1) End If End If End Sub #End Region ' MoveRowUpCommand #Region "MoveRowDownCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveRowDownCommand As ICommand Get If m_cmdMoveRowDown Is Nothing Then m_cmdMoveRowDown = New RelayCommand(AddressOf MoveRowDown) End If Return m_cmdMoveRowDown End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveRowDown(ByVal param As Object) If Not IsNothing(m_SelectedTable) Then Dim SelectedIndex As Integer = m_SelectedTable.AssociationList.IndexOf(m_SelectedTable.SelectedAssociation) If SelectedIndex < m_SelectedTable.AssociationList.Count - 1 Then m_SelectedTable.AssociationList.Move(SelectedIndex, SelectedIndex + 1) End If End If End Sub #End Region ' MoveRowDownCommand #End Region End Class End Namespace