ToolCollection/ToolCollection/ClassNIVerknüpfungen.vb

297 lines
12 KiB
VB.net

'Imports System.Windows.Forms
Public Class ClassNIVerknüpfungen
Private _links() As ClassNIVerknüpfung
Private _parentProfile As ClassNIProfil
Public Shared xmlNIConfigFile As String
Public selectedLink As ClassNIVerknüpfung
Private _linksSaved As Boolean = True
Public Sub New(ByVal profil As ClassNIProfil)
Me._parentProfile = profil
xmlNIConfigFile = My.Application.Info.DirectoryPath & "\" & My.Settings.vNIConfigDatei
Me.LoadFromXmlFile()
End Sub
Public Sub LoadFromXmlFile()
Try
If Me._parentProfile.Profilname IsNot Nothing Then
Dim i As Integer = 0
Dim xml As New Xml.XmlDocument()
' XML-Datei mit Profilen öffnen und laden
xml.Load(xmlNIConfigFile)
' Anzahl der Profilelemente in der XML-Datei zählen
Dim anzProfile = xml.SelectNodes("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']/Link").Count
' wenn mindestens ein Profilelement eingetragen ist
If anzProfile > 0 Then
' alle Profile laden
Dim xmlNodes As Xml.XmlNodeList = xml.SelectNodes("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']/Link")
' alle Profile der XML-Datei durchlaufen
For Each xmlLink As Xml.XmlNode In xmlNodes
' prüfen ob ein Profilname angegeben wurde
If xmlLink.Attributes("index") IsNot Nothing And xmlLink.Attributes("spalte") IsNot Nothing And xmlLink.Attributes("from") IsNot Nothing And _
xmlLink.Attributes("select") IsNot Nothing Then
' ein Element an Items anhängen
ReDim Preserve Me._links(i)
' das Profilobjekt erzeugen
'MsgBox(xmlLink.Attributes("index").Value)
'MsgBox(xmlLink.Attributes("spalte").Value)
'MsgBox(xmlLink.Attributes("from").Value)
'MsgBox(xmlLink.Attributes("select").Value)
'MsgBox(xmlLink.Attributes("vktstate").Value)
Me._links(i) = New ClassNIVerknüpfung(xmlLink.Attributes("index").Value, xmlLink.Attributes("spalte").Value, xmlLink.Attributes("from").Value, _
xmlLink.Attributes("select").Value, xmlLink.Attributes("vktstate").Value)
' Zähler für Profile erhöhen
i += 1
End If
Next
End If
End If
Catch ex As Exception
MsgBox("Fehler beim Laden der XML-Datei!" & vbNewLine & vbNewLine & "Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Public Sub AddLink(ByVal index As String, ByVal spalte As String, ByVal from As String, ByVal selectanweisung As String, Optional vektorinsertState As Integer = 1)
Try
Dim xml As New Xml.XmlDocument
Dim xmlProfil As Xml.XmlElement
' XML-Datei laden
xml.Load(xmlNIConfigFile)
Try
' versuchen die XML-Beschreibung zu schreiben
xml.InsertBefore(xml.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xml.ChildNodes(0))
Catch ex As Exception
' wenn dies nicht funktioniert, ist die Beschreibung schon vorhanden
End Try
' wenn die Hauptknoten noch nicht eingetragen wurden, sollen diese angelegt werden
If xml.SelectNodes("Settings") Is Nothing Or _
xml.SelectNodes("Settings/Profile") Is Nothing Or _
xml.SelectNodes("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']") Is Nothing Then
MsgBox("Profil ist nicht vorhanden. Möglicherweise ist die Konfigurationsdatei ungültig.", MsgBoxStyle.Exclamation, "Fehler beim Anlegen einer Verknüpfung")
Exit Sub
End If
' dann ein neues Profil anlegen
xmlProfil = xml.CreateElement("Link")
' dem Profil die Attribute zuweisen
xmlProfil.SetAttribute("index", index)
xmlProfil.SetAttribute("spalte", spalte)
xmlProfil.SetAttribute("from", from)
xmlProfil.SetAttribute("select", selectanweisung)
xmlProfil.SetAttribute("vktstate", vektorinsertState)
' und in den Hauptknoten 'Profile' einhängen
xml.SelectSingleNode("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']").AppendChild(xmlProfil)
' XML-Datei speichern
xml.Save(xmlNIConfigFile)
' die Liste der Profile im Objekt vergrößern
ReDim Preserve Me._links(Me.CountLinks)
' und das neue Profil dem Profilarray anhängen und die Eigenschaften zuweisen
Me._links(Me.CountLinks - 1) = New ClassNIVerknüpfung(index, spalte, from, selectanweisung, vektorinsertState)
Catch ex As Exception
MsgBox("Die Verknüpfung konnte nicht gespeichert werden." & vbNewLine & vbNewLine & "Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Fehler beim Anlegen einer Verknüpfung")
End Try
End Sub
Public Sub RemoveLink(ByVal index As String, ByVal spalte As String, ByVal from As String)
Try
' *** Element aus Konfigurationsdatei löschen ***
' XML-Objekt initialisieren
Dim xml As New Xml.XmlDocument
' XML-Datei laden
xml.Load(xmlNIConfigFile)
' XML-Element auswählen
Dim node As Xml.XmlElement = xml.SelectSingleNode("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']/Link[@index='" & index & "'][@spalte='" & spalte & "'][@from='" & from & "']")
If node IsNot Nothing Then
' Element löschen
node.ParentNode.RemoveChild(node)
' XML-Datei speichern
xml.Save(xmlNIConfigFile)
End If
' *** Array umschreiben (Element aus Array entfernen) ***
' temporäres Array erzeugen
Dim tempItems() As ClassNIVerknüpfung = Me._links
Dim i As Integer = 0
' Größe anpassen
ReDim Preserve Me._links(tempItems.Length - 2)
' alle Verknüpfungen durchlaufen
For Each item As ClassNIVerknüpfung In tempItems
' wenn es sich nicht um das gelöschte handelt
If Not (item.Index = index And item.Spalte = spalte And item.From = from) Then
' die Verknüpfung in das Items-Array übertragen
Me._links(i) = item
' Counter hochzählen
i += 1
End If
Next
Catch ex As Exception
MsgBox("Die Verknüpfung konnte nicht gelöscht werden (RemoveLink):" & vbNewLine & vbNewLine & "Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Public Sub RenewLinks(ByVal _listview As ListView, vktinsstate As Integer)
Try
' XML-Objekt initialisieren
Dim xml As New Xml.XmlDocument
' XML-Datei laden
xml.Load(xmlNIConfigFile)
Dim xmlnode As Xml.XmlElement
Dim strSelect(_listview.Items.Count) As String
For Each _item As ListViewItem In _listview.Items
' *** Element aus Konfigurationsdatei löschen ***
' XML-Element auswählen
xmlnode = xml.SelectSingleNode("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']/Link[@index='" & _listview.Items(_item.Index).SubItems(0).Text & "'][@spalte='" & _listview.Items(_item.Index).SubItems(1).Text & "'][@from='" & _listview.Items(_item.Index).SubItems(2).Text & "']")
strSelect(_item.Index) = xmlnode.Attributes("select").Value
'MsgBox(strSelect(_item.Index).ToString)
If xmlnode IsNot Nothing Then
' Element löschen
xmlnode.ParentNode.RemoveChild(xmlnode)
End If
Next
For Each _item As ListViewItem In _listview.Items
' dann ein neues Profil anlegen
xmlnode = xml.CreateElement("Link")
' dem Profil die Attribute zuweisen
xmlnode.SetAttribute("index", _listview.Items(_item.Index).SubItems(0).Text)
xmlnode.SetAttribute("spalte", _listview.Items(_item.Index).SubItems(1).Text)
xmlnode.SetAttribute("from", _listview.Items(_item.Index).SubItems(2).Text)
xmlnode.SetAttribute("select", strSelect(_item.Index).ToString)
xmlnode.SetAttribute("vktstate", vktinsstate.ToString)
' und in den Hauptknoten 'Profile' einhängen
xml.SelectSingleNode("Settings/Profile/Profil[@profilname='" & Me._parentProfile.OriginalProfilname & "']").AppendChild(xmlnode)
Next
' XML-Datei speichern
xml.Save(xmlNIConfigFile)
Catch ex As Exception
MsgBox("Fehler in RenewLinks:" & vbNewLine & vbNewLine & "Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Public ReadOnly Property GetSaved() As Boolean
Get
Return Me._linksSaved
End Get
End Property
Public ReadOnly Property IsSaved() As Boolean
Get
Return Me._linksSaved = True
End Get
End Property
Public Function CountLinks()
' Anzahl der Verknüpfungen zurückgeben
If Me._links IsNot Nothing Then
Return Me._links.Length
Else
Return 0
End If
End Function
Public Function getLinkById(ByVal id As Integer)
If Not id < 0 And Not id > Me.CountLinks - 1 Then
Return Me._links(id)
Else
Return Nothing
End If
End Function
Public Function getLinkByValues(ByVal index As String, ByVal spalte As String, ByVal from As String) As ClassNIVerknüpfung
For Each link As ClassNIVerknüpfung In Me._links
If link.Index = index And link.Spalte = spalte And link.From = from Then
Return link
End If
Next
Return Nothing
End Function
Public Function getLinkByIndex(ByVal indexname As String) As ClassNIVerknüpfung
For Each link As ClassNIVerknüpfung In Me._links
If link.Index = indexname Then
Return link
End If
Next
Return Nothing
End Function
''' <summary>
''' Gibt zurück, ob die Verknüpfungen gespeichert wurden oder nicht.
''' </summary>
''' <returns>Liefert True wenn die Verknüpfungen gespeichert wurden und false wenn nicht</returns>
''' <remarks></remarks>
Public Function IsLinksSaved()
Return Me._linksSaved
End Function
''' <summary>
''' setzt das Profil auf geändert
''' </summary>
''' <remarks></remarks>
Public Sub setLinksChanged()
' Profil auf geändert setzen
Me._linksSaved = False
End Sub
''' <summary>
''' setzt das Profil auf gespeichert
''' </summary>
''' <remarks></remarks>
Public Sub setLinksSaved()
' Profil auf gespeichert setzen
Me._linksSaved = True
End Sub
Public ReadOnly Property Links() As ClassNIVerknüpfung()
Get
If Me._links IsNot Nothing Then
Return Me._links
Else
Return Nothing
End If
End Get
End Property
End Class