Imports System.Collections.ObjectModel Imports EgtUILib Public Class MachGroupPanelVM Inherits VMBase #Region "FIELDS & PROPERTIES" ' Lista delle macchine disponibili Private m_MachineList As List(Of Machine) ' Macchina di default Private m_DefaultMachine As String = String.Empty Public ReadOnly Property DefaultMachine As String Get Return m_DefaultMachine End Get End Property ' Variabile che definisce se il nome è automatico Private m_IsGroupNameAutomatic As Boolean = True ' Variabile che definisce se ci sono più gruppi di lavorazione Private m_IsMultiMachGroup As Boolean ' Nome base dei gruppi Private m_BaseName As String Private m_MachGroupList As New ObservableCollection(Of MachGroup) Public Property MachGroupList As ObservableCollection(Of MachGroup) Get Return m_MachGroupList End Get Set(value As ObservableCollection(Of MachGroup)) If value IsNot m_MachGroupList Then m_MachGroupList = value End If End Set End Property Private m_SelectedMachGroup As MachGroup Public Property SelectedMachGroup As MachGroup Get Return m_SelectedMachGroup End Get Set(value As MachGroup) If Not IsNothing(value) AndAlso value IsNot m_SelectedMachGroup Then OnPreSetCurrMachGroup() If EgtSetCurrMachGroup(value.Id) Then m_SelectedMachGroup = value Else value.IsEnabled = False EgtSetCurrMachGroup(m_SelectedMachGroup.Id) End If OnPostSetCurrMachGroup() End If NotifyPropertyChanged(NameOf(SelectedMachGroup)) End Set End Property Private m_MachGroupPanel_Visibility As Visibility Public ReadOnly Property MachGroupPanel_Visibility As Visibility Get Return m_MachGroupPanel_Visibility End Get End Property Public Sub SetMachGroupPanelVisibility(IsVisible As Boolean) If IsVisible Then m_MachGroupPanel_Visibility = Visibility.Visible Else m_MachGroupPanel_Visibility = Visibility.Collapsed End If NotifyPropertyChanged(NameOf(MachGroupPanel_Visibility)) End Sub ' Variabile che permette di abilitare/disabilitare i bottoni aggiungi e togli MachGroup Private m_IsEnabledAddRemove As Boolean Public Property IsEnabledAddRemove As Boolean Get Return m_IsEnabledAddRemove End Get Set(value As Boolean) m_IsEnabledAddRemove = value NotifyPropertyChanged(NameOf(IsEnabledAddRemove)) End Set End Property ' Variabile che definisce lo stato (attivi/disattivi) di tutti i gruppi tranne quello selezionato Private m_IsEnabledMachGroups As Boolean Public ReadOnly Property IsEnabledMachGroups As Boolean Get Return m_IsEnabledMachGroups End Get End Property ' Definizione comandi Private m_cmdSetCurrMachGroup As ICommand Private m_cmdAddMachGroup As ICommand Private m_cmdRemoveMachGroup As ICommand #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() ' Creo riferimento a questa classe in LibMap LibMap.SetRefMachGroupPanelVM(Me) End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Public Function InitMachGroupPanel(IsMultiMachGroup As Boolean, MachineList As List(Of Machine), DefaultMachine As String, Optional sBaseName As String = "MachGroup_1") As Boolean m_IsMultiMachGroup = IsMultiMachGroup ' se è a gruppo singolo, nascondo il panel If Not m_IsMultiMachGroup Then SetMachGroupPanelVisibility(False) ' Assegno nome base m_BaseName = sBaseName ' Recupero lista macchine m_MachineList = MachineList ' Assegno macchina di default m_DefaultMachine = DefaultMachine Return MachineList.Count > 0 End Function Public Overridable Function InitMachGroupList(Optional bUseDefaults As Boolean = False) As Boolean ' Svuoto precedente lista di MachGroup m_MachGroupList.Clear() ' verifico quale di queste è attivabile Dim bOk As Boolean = False Dim nId = GetFirstMachGroupValid(m_MachGroupList) If nId = GDB_ID.NULL Then If bUseDefaults Then bOk = NewMachGroupWithDefaults() Else bOk = NewMachGroup() End If nId = EgtGetLastMachGroup() Else bOk = True End If ' Carico la lista delle macchine LoadMachGroupList() If nId = GDB_ID.NULL OrElse Not bOk Then EgtOutLog("Impossible creating MachGroup!!") Return False ElseIf Not IsNothing(MachGroupList) AndAlso MachGroupList.Count > 0 Then SelectedMachGroup = MachGroupList.FirstOrDefault(Function(x) x.Id = nId) End If Return True End Function ' restituisco l'inidce della prima macchina valida e la imposto Public Function GetFirstMachGroupValid(My_MachGroupList As ObservableCollection(Of MachGroup)) As Integer Dim bOk As Boolean = False Dim nId = EgtGetFirstMachGroup() While nId <> GDB_ID.NULL bOk = EgtSetCurrMachGroup(nId) If bOk Then Exit While nId = EgtGetNextMachGroup(nId) End While Return nId End Function ' Verifico che la macchina indicata sia presente in elenco Public Function VerifyMachExists(sNameMach As String) As Boolean For Each Item As Machine In m_MachineList If String.Compare(sNameMach, Item.Name, True) = 0 Then Return True End If Next Return False End Function Public Overridable Sub LoadMachGroupList() ' Carico i gruppi di lavorazione nella lista Dim nId = EgtGetFirstMachGroup() While nId <> GDB_ID.NULL Dim sName As String = String.Empty Dim sMachine As String = String.Empty EgtGetMachGroupName(nId, sName) EgtGetMachGroupMachineName(nId, sMachine) Dim NewMachGoup As MachGroup = New MachGroup(nId, sName, sMachine) MachGroupList.Add(NewMachGoup) nId = EgtGetNextMachGroup(nId) End While End Sub Public Function NewMachGroup() As Boolean ' Sistemazioni preliminari OnPreNewMachGroup() ' Se non ci sono macchine disponibili esco con errore If m_MachineList.Count <= 0 Then EgtOutLog("There is no one machine!!") Return False End If ' Creo il VM per chiedere nome e macchina a seconda del caso in cui mi trovo Dim NewMachGroupWndVM As NewMachGroupWndVM = Nothing Dim sNewMGrpName As String = m_BaseName Dim sNewMachName As String = m_MachineList(0).Name If m_IsMultiMachGroup Then EgtGetMachGroupNewName(sNewMGrpName) If m_MachineList.Count = 1 Then If Not m_IsGroupNameAutomatic Then NewMachGroupWndVM = New NewMachGroupWndVM(sNewMGrpName) End If Else If m_IsGroupNameAutomatic Then NewMachGroupWndVM = New NewMachGroupWndVM(m_DefaultMachine, m_MachineList) Else NewMachGroupWndVM = New NewMachGroupWndVM(sNewMGrpName, m_DefaultMachine, m_MachineList) End If End If ElseIf EgtGetMachGroupCount() = 0 Then If m_MachineList.Count > 1 Then NewMachGroupWndVM = New NewMachGroupWndVM(m_DefaultMachine, m_MachineList) End If End If ' Se necessario, lancio finestra di acquisizione dati If Not IsNothing(NewMachGroupWndVM) Then ' Se non esistono gruppi nella lista, rendo la finestra non chiudibile senza scelta If m_MachGroupList.Count = 0 Then NewMachGroupWndVM.IsClosable = False ' Lancio finestra Dim NewMachGroupWndV As New NewMachGroupWndV(Application.Current.MainWindow, NewMachGroupWndVM) If NewMachGroupWndV.ShowDialog() = False Then Return False ' Recupero dati If Not IsNothing(NewMachGroupWndVM.Name) Then sNewMGrpName = NewMachGroupWndVM.Name If Not IsNothing(NewMachGroupWndVM.SelectedMachine) Then sNewMachName = NewMachGroupWndVM.SelectedMachine.Name End If ' Creo il nuovo gruppo di lavorazione con i dati ottenuti a seconda del caso in cui mi trovo If EgtAddMachGroup(sNewMGrpName, sNewMachName) = GDB_ID.NULL Then Return False ' Sistemazioni finali m_DefaultMachine = sNewMachName OnPostNewMachGroup() Return True End Function Private Function NewMachGroupWithDefaults() As Boolean ' Sistemazioni preliminari OnPreNewMachGroup() ' Se non ci sono macchine disponibili esco con errore If m_MachineList.Count <= 0 Then Return False ' Assegno valori di default Dim sNewMGrpName As String = m_BaseName Dim sNewMachName As String = If(Machine.ExistsMachine(m_DefaultMachine, m_MachineList), m_DefaultMachine, m_MachineList(0).Name) ' Creo il nuovo gruppo di lavorazione con i dati ottenuti a seconda del caso in cui mi trovo If EgtAddMachGroup(sNewMGrpName, sNewMachName) = GDB_ID.NULL Then Return False ' Sistemazioni finali m_DefaultMachine = sNewMachName OnPostNewMachGroup() Return True End Function ' Funzione che permette di abilitare o disabilitare tutti i gruppi tranne quello selezionato più i bottoni Add/Remove Public Function SetMachGroupState(bState As Boolean) As Boolean If bState Then For Each Group In m_MachGroupList If Group.IsValid Then Group.IsEnabled = True End If Next m_IsEnabledAddRemove = True NotifyPropertyChanged(NameOf(IsEnabledAddRemove)) m_IsEnabledMachGroups = True Return True Else For Each Group In m_MachGroupList If Group Is m_SelectedMachGroup Then Group.IsEnabled = True Else Group.IsEnabled = False End If Next m_IsEnabledAddRemove = False NotifyPropertyChanged(NameOf(IsEnabledAddRemove)) m_IsEnabledMachGroups = False Return True End If Return False End Function Public Overridable Function OnPreNewMachGroup() As Boolean Return True End Function Public Overridable Function OnPostNewMachGroup() As Boolean Return True End Function Public Overridable Function OnPreSetCurrMachGroup() As Boolean Return True End Function Public Overridable Function OnPostSetCurrMachGroup() As Boolean Return True End Function Public Overridable Function OnPreRemoveCurrMachGroup() As Boolean Return True End Function #End Region ' METHODS #Region "COMMANDS" #Region "AddMachGroupCommand" ''' ''' Returns a command that set the selected MachGroup as the Current one. ''' Public ReadOnly Property AddMachGroupCommand As ICommand Get If m_cmdAddMachGroup Is Nothing Then m_cmdAddMachGroup = New Command(AddressOf AddMachGroup) End If Return m_cmdAddMachGroup End Get End Property Public Overridable Sub AddMachGroup() If NewMachGroup() Then ' creo oggetto gruppo creato Dim nNewMachGroupID As Integer = EgtGetCurrMachGroup() Dim sNewMachGroupName As String = String.Empty Dim sNewMachGroupMachineName As String = String.Empty EgtGetMachGroupName(EgtGetCurrMachGroup(), sNewMachGroupName) EgtGetMachGroupMachineName(nNewMachGroupID, sNewMachGroupMachineName) Dim machGroup As New MachGroup(EgtGetCurrMachGroup(), sNewMachGroupName, sNewMachGroupMachineName) ' lo aggiungo alla lista MachGroupList.Add(machGroup) ' e lo seleziono SelectedMachGroup = machGroup EgtZoom(ZM.ALL) End If End Sub #End Region ' AddMachGroupCommand #Region "RemoveMachGroupCommand" ''' ''' Returns a command that set the selected MachGroup as the Current one. ''' Public ReadOnly Property RemoveMachGroupCommand As ICommand Get If m_cmdRemoveMachGroup Is Nothing Then m_cmdRemoveMachGroup = New Command(AddressOf RemoveMachGroup) End If Return m_cmdRemoveMachGroup End Get End Property Public Overridable Sub RemoveMachGroup() ' Calcolo indice del gruppo da cancellare Dim nSelectedMachGroupIndex As Integer = MachGroupList.IndexOf(SelectedMachGroup) If MachGroupList.Count = 0 Or nSelectedMachGroupIndex = -1 Then ' non c'è alcunchè da cancellare ElseIf MachGroupList.Count = 1 Then 'chiedo conferma prima di resettare il gruppo di lavorazione Select Case MessageBox.Show("Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) 'Select Case EgtMessageBoxV.Show(Application.Current.MainWindow, "Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) Case MessageBoxResult.Yes ' cancello il gruppo corrente e ne creo uno nuovo con lo stesso nome OnPreRemoveCurrMachGroup() EgtRemoveMachGroup(MachGroupList(0).Id) MachGroupList.Clear() AddMachGroup() EgtZoom(ZM.ALL) Case MessageBoxResult.No Return End Select Else 'chiedo conferma prima di cancellare il gruppo di lavorazione Select Case MessageBox.Show("Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) 'Select Case EgtMessageBoxV.Show(Application.Current.MainWindow, "Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) Case MessageBoxResult.Yes ' cancello quello selezionato (ovvero il corrente) OnPreRemoveCurrMachGroup() EgtRemoveMachGroup(MachGroupList(nSelectedMachGroupIndex).Id) ' rendo corrente il gruppo di lavorazione successivo a quello da cancellare If nSelectedMachGroupIndex = 0 And MachGroupList.Count > 1 Then EgtSetCurrMachGroup(MachGroupList(nSelectedMachGroupIndex + 1).Id) SelectedMachGroup = MachGroupList(nSelectedMachGroupIndex + 1) ' rendo corrente il gruppo di lavorazione precedente a quello da cancellare ElseIf nSelectedMachGroupIndex > 0 Then EgtSetCurrMachGroup(MachGroupList(nSelectedMachGroupIndex - 1).Id) SelectedMachGroup = MachGroupList(nSelectedMachGroupIndex - 1) End If EgtZoom(ZM.ALL) ' aggiorno la lista dei gruppi MachGroupList.RemoveAt(nSelectedMachGroupIndex) Case MessageBoxResult.No Return End Select End If End Sub #End Region ' RemoveMachGroupCommand #End Region ' COMMANDS End Class Public Class MachGroup Inherits VMBase Private m_Id As Integer Public ReadOnly Property Id As Integer Get Return m_Id End Get End Property Private m_Name As String Public ReadOnly Property Name As String Get Return m_Name End Get End Property Public Sub SetName(NewName As String) m_Name = NewName NotifyPropertyChanged("Name") End Sub Private m_Machine As String Public ReadOnly Property Machine As String Get Return m_Machine End Get End Property Private m_IsEnabled As Boolean = False Public Property IsEnabled As Boolean Get Return m_IsEnabled End Get Set(value As Boolean) m_IsEnabled = value NotifyPropertyChanged("IsEnabled") End Set End Property Private m_IsValid As Boolean = True Public Property IsValid As Boolean Get Return m_IsValid End Get Set(value As Boolean) m_IsValid = value NotifyPropertyChanged("m_IsValid") End Set End Property #Region "ToolTip" Public Overridable ReadOnly Property MachGroupToolTip As String Get Return "Name: " & m_Name & Environment.NewLine & "Machine: " & m_Machine End Get End Property #End Region ' ToolTip Sub New(nId As Integer, sName As String, sMachine As String) m_Id = nId m_Name = sName m_Machine = sMachine End Sub End Class