Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Xml.Serialization
Imports DevExpress.Utils
Imports DevExpress.Utils.Zip
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.DemoBase
Imports DevExpress.Xpf.DemoBase.Helpers
Imports DevExpress.Xpf.DemoBase.Helpers.TextColorizer
Imports DevExpress.Xpf.RichEdit
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraSpellChecker
Namespace SpellCheckerDemo
Public Class DemoUtils
Public Shared ReadOnly PathToDemoData As String = "SpellCheckerDemo.Data"
Public Shared ReadOnly PathToDictionaries As String = PathToDemoData & ".Dictionaries"
Public Shared Function GetPathToResource(ByVal path As String, ByVal name As String) As String
If DemoHelper.GetDemoLanguage(GetType(DemoUtils).Assembly) = CodeLanguage.CS Then
Return String.Format("{0}.{1}", path, name)
Else
Return name
End If
End Function
Public Shared Function GetDataStream(ByVal path As String, ByVal name As String) As Stream
Dim fullPath As String = DemoUtils.GetPathToResource(path, name)
If (Not String.IsNullOrEmpty(fullPath)) Then
Return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(fullPath)
End If
Return Nothing
End Function
Public Shared Sub ShowDialog(ByVal title As String, ByVal text As String, ByVal owner As FrameworkElement)
Dim textBox As New TextBlock() With {.Text = text}
textBox.TextWrapping = TextWrapping.Wrap
textBox.VerticalAlignment = VerticalAlignment.Center
textBox.HorizontalAlignment = HorizontalAlignment.Center
Dim dialog As DXDialog
dialog = New DXDialog(title, DialogButtons.Ok)
dialog.Content = textBox
dialog.UseContentIndents = True
dialog.IsSizable = False
dialog.ShowDialog()
End Sub
Public Shared Function GetBitmapImage(ByVal fileName As String) As BitmapImage
Dim bmp As New BitmapImage()
bmp.SetSource(DemoUtils.GetDataStream(DemoUtils.PathToDemoData, fileName))
Return bmp
End Function
End Class
Public NotInheritable Class SpellCheckerHelper
Private Sub New()
End Sub
Public Shared Sub RegisterDefaultDictionaries(ByVal spellChecker As SpellChecker)
spellChecker.Dictionaries.Add(GetDefaultDictionary())
spellChecker.Dictionaries.Add(GetCustomDictionary())
End Sub
Public Shared Sub RegisterHunspellDictionaries(ByVal spellChecker As SpellChecker)
spellChecker.Dictionaries.Add(CreateHunspellDictionaries(New CultureInfo("en-US")))
spellChecker.Dictionaries.Add(CreateHunspellDictionaries(New CultureInfo("de-DE")))
spellChecker.Dictionaries.Add(CreateHunspellDictionaries(New CultureInfo("ru-RU")))
End Sub
Private Shared Function CreateHunspellDictionaries(ByVal culture As CultureInfo) As HunspellDictionary
Dim parts() As String = culture.Name.Split("-"c)
Dim result As New HunspellDictionary()
Using stream As Stream = DemoUtils.GetDataStream(DemoUtils.PathToDictionaries, String.Format("{0}_{1}.zip", parts(0), parts(1)))
Dim files As InternalZipFileCollection = InternalZipArchive.Open(stream)
Dim dictionaryStream As Stream = GetFileStream(files, ".dic")
Dim grammarStream As Stream = GetFileStream(files, ".aff")
Try
result.LoadFromStream(dictionaryStream, grammarStream)
Catch
Finally
dictionaryStream.Close()
grammarStream.Close()
End Try
End Using
result.Culture = culture
Return result
End Function
Private Shared Function GetFileStream(ByVal files As InternalZipFileCollection, ByVal name As String) As Stream
Dim stream As Stream = files.Find(Function(file As InternalZipFile) file.FileName.IndexOf(name) >= 0).FileDataStream
Try
Return CreateMemoryStream(stream)
Finally
stream.Close()
End Try
End Function
Private Shared Function CreateMemoryStream(ByVal stream As Stream) As Stream
Dim result As New MemoryStream()
Do
Dim readedByte As Integer = stream.ReadByte()
If readedByte < 0 Then
Exit Do
End If
result.WriteByte(CByte(readedByte))
Loop
result.Flush()
result.Seek(0, SeekOrigin.Begin)
Return result
End Function
Private Shared Function GetDefaultDictionary() As ISpellCheckerDictionary
Dim dic As New SpellCheckerISpellDictionary()
Using stream As Stream = DemoUtils.GetDataStream(DemoUtils.PathToDictionaries, "default.zip")
Dim files As InternalZipFileCollection = InternalZipArchive.Open(stream)
Dim dictionaryStream As Stream = GetFileStream(files, "american.xlg")
Dim grammarStream As Stream = GetFileStream(files, "english.aff")
Dim alphabetStream As Stream = DemoUtils.GetDataStream(DemoUtils.PathToDictionaries, "EnglishAlphabet.txt")
Try
dic.LoadFromStream(dictionaryStream, grammarStream, alphabetStream)
Catch
Finally
dictionaryStream.Close()
grammarStream.Close()
alphabetStream.Close()
End Try
End Using
dic.Culture = New CultureInfo("en-US")
Return dic
End Function
Private Shared Function GetCustomDictionary() As ISpellCheckerDictionary
Dim result As New SpellCheckerCustomDictionary()
Dim dictionaryStream As Stream = DemoUtils.GetDataStream(DemoUtils.PathToDictionaries, "CustomEnglish.dic")
Dim alphabetStream As Stream = DemoUtils.GetDataStream(DemoUtils.PathToDictionaries, "EnglishAlphabet.txt")
Try
result.Load(dictionaryStream, alphabetStream)
Catch
Finally
dictionaryStream.Close()
alphabetStream.Close()
End Try
result.Culture = New CultureInfo("en-US")
Return result
End Function
End Class
Public Class DocumentLoadHelper
Public Shared Sub Load(ByVal fileName As String, ByVal format As DocumentFormat, ByVal richEditControl As RichEditControl)
Dim path As String = DemoUtils.GetPathToResource(DemoUtils.PathToDemoData, fileName)
If (Not String.IsNullOrEmpty(path)) Then
Using stream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
richEditControl.LoadDocument(stream, format)
End Using
End If
End Sub
End Class
Public Class Employees
Implements System.ComponentModel.INotifyPropertyChanged
Private privateEmployeeID As Integer
Public Property EmployeeID() As Integer
Get
Return privateEmployeeID
End Get
Set(ByVal value As Integer)
privateEmployeeID = value
End Set
End Property
Private privateLastName As String
Public Property LastName() As String
Get
Return privateLastName
End Get
Set(ByVal value As String)
privateLastName = value
End Set
End Property
Private privateFirstName As String
Public Property FirstName() As String
Get
Return privateFirstName
End Get
Set(ByVal value As String)
privateFirstName = value
End Set
End Property
Private privateTitle As String
Public Property Title() As String
Get
Return privateTitle
End Get
Set(ByVal value As String)
privateTitle = value
End Set
End Property
Private privateTitleOfCourtesy As String
Public Property TitleOfCourtesy() As String
Get
Return privateTitleOfCourtesy
End Get
Set(ByVal value As String)
privateTitleOfCourtesy = value
End Set
End Property
Private privateBirthDate As DateTime
Public Property BirthDate() As DateTime
Get
Return privateBirthDate
End Get
Set(ByVal value As DateTime)
privateBirthDate = value
End Set
End Property
Private privateHireDate As DateTime
Public Property HireDate() As DateTime
Get
Return privateHireDate
End Get
Set(ByVal value As DateTime)
privateHireDate = value
End Set
End Property
Private privateAddress As String
Public Property Address() As String
Get
Return privateAddress
End Get
Set(ByVal value As String)
privateAddress = value
End Set
End Property
Private privateCity As String
Public Property City() As String
Get
Return privateCity
End Get
Set(ByVal value As String)
privateCity = value
End Set
End Property
Private privateRegion As String
Public Property Region() As String
Get
Return privateRegion
End Get
Set(ByVal value As String)
privateRegion = value
End Set
End Property
Private privatePostalCode As String
Public Property PostalCode() As String
Get
Return privatePostalCode
End Get
Set(ByVal value As String)
privatePostalCode = value
End Set
End Property
Private privateCountry As String
Public Property Country() As String
Get
Return privateCountry
End Get
Set(ByVal value As String)
privateCountry = value
End Set
End Property
Private privateHomePhone As String
Public Property HomePhone() As String
Get
Return privateHomePhone
End Get
Set(ByVal value As String)
privateHomePhone = value
End Set
End Property
Private privateExtension As String
Public Property Extension() As String
Get
Return privateExtension
End Get
Set(ByVal value As String)
privateExtension = value
End Set
End Property
Private privateSalary As Double
Public Property Salary() As Double
Get
Return privateSalary
End Get
Set(ByVal value As Double)
privateSalary = value
End Set
End Property
Private privateOnVacation As Boolean
Public Property OnVacation() As Boolean
Get
Return privateOnVacation
End Get
Set(ByVal value As Boolean)
privateOnVacation = value
End Set
End Property
Private privatePhoto As Byte()
Public Property Photo() As Byte()
Get
Return privatePhoto
End Get
Set(ByVal value As Byte())
privatePhoto = value
End Set
End Property
Private privateNotes As String
Public Property Notes() As String
Get
Return privateNotes
End Get
Set(ByVal value As String)
privateNotes = value
End Set
End Property
Private privateReportsTo As Integer
Public Property ReportsTo() As Integer
Get
Return privateReportsTo
End Get
Set(ByVal value As Integer)
privateReportsTo = value
End Set
End Property
#Region "INotifyPropertyChanged Members"
Private Event onPropertyChanged As System.ComponentModel.PropertyChangedEventHandler
Public Custom Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
AddHandler(ByVal value As System.ComponentModel.PropertyChangedEventHandler)
AddHandler onPropertyChanged, value
End AddHandler
RemoveHandler(ByVal value As System.ComponentModel.PropertyChangedEventHandler)
RemoveHandler onPropertyChanged, value
End RemoveHandler
RaiseEvent(ByVal sender As System.Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
End RaiseEvent
End Event
Private Sub RaisePropertyChanged(ByVal propertyName As String)
Dim handler As System.ComponentModel.PropertyChangedEventHandler = onPropertyChangedEvent
If handler IsNot Nothing Then
handler(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub
#End Region
End Class
Public NotInheritable Class EmployeesData
Private Shared dataSource_Renamed As IList
Private Sub New()
End Sub
Public Shared ReadOnly Property DataSource() As IList
Get
If dataSource_Renamed Is Nothing Then
dataSource_Renamed = GetDataSource()
DoMistakes(dataSource_Renamed)
End If
Return dataSource_Renamed
End Get
End Property
Private Shared Function GetDataSource() As IList
Dim s As New XmlSerializer(GetType(List(Of Employees)), New XmlRootAttribute("NewDataSet"))
Dim stream As Stream = GetType(EmployeesData).Assembly.GetManifestResourceStream(DemoUtils.GetPathToResource(DemoUtils.PathToDemoData, "nwind.xml"))
Return CType(s.Deserialize(stream), IList)
End Function
Private Shared Sub DoMistakes(ByVal dataSet As IList)
For Each employee As Employees In dataSet
Dim text As New StringBuilder(employee.Notes)
Dim charSet As List(Of Char) = CreateCharSet(text)
Dim random As New Random(Environment.TickCount)
For i As Integer = text.Length - 1 To 0 Step -30
If (Not Char.IsLetter(text(i))) Then
Continue For
End If
Dim ch As Char = GetRandomChar(charSet)
If Char.IsUpper(text(i)) Then
ch = Char.ToUpper(ch)
End If
If text(i) = ch Then
text.Remove(i, 1)
Else
text(i) = ch
End If
Next i
employee.Notes = text.ToString()
Next employee
End Sub
Private Shared Function CreateCharSet(ByVal text As StringBuilder) As List(Of Char)
Dim result As New List(Of Char)()
Dim length As Integer = text.Length
For i As Integer = 0 To length - 1
Dim ch As Char = text(i)
If (Not Char.IsLetter(ch)) Then
Continue For
End If
ch = Char.ToLower(ch)
Dim index As Integer = result.BinarySearch(ch)
If index < 0 Then
result.Insert((Not index), ch)
End If
Next i
Return result
End Function
Private Shared Function GetRandomChar(ByVal charSet As List(Of Char)) As Char
Dim random As New Random(Environment.TickCount)
Dim index As Integer = random.Next(0, charSet.Count - 1)
Return charSet(index)
End Function
End Class
Public Class BitmapToBitmapSourceConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
Return GetImageSource(CType(value, Byte()))
End Function
Public Shared Function GetImageSource(ByVal bytes() As Byte) As ImageSource
Dim bi As New BitmapImage()
bi.SetSource(New MemoryStream(bytes))
Return bi
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
Public Class EmployeeToAddressStringConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim employee As Employees = TryCast(value, Employees)
If employee Is Nothing OrElse GetType(String) IsNot targetType Then
Return Nothing
End If
Return String.Format("{0}, {1}, {2}", employee.Country, employee.City, employee.Address)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
End Namespace