Click here to Skip to main content
15,891,905 members
Articles / Programming Languages / Visual Basic

Structured Print Document Utility

Rate me:
Please Sign up or sign in to vote.
4.79/5 (19 votes)
14 Jun 2014CPOL6 min read 152.7K   3.5K   95  
A set of classes for creating structured documents
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing.Design
Imports System.Windows.Forms.ComponentModel
Imports System.Windows.Forms.Design
Imports System.Reflection
Imports System.Globalization
Imports System.IO
Imports System.Xml.Serialization

Namespace StructuredDocuments

#Region "StructuredDocumentDataDefinition"
    ' Stores a single data source definition for the structured print document
    <TypeConverterAttribute(GetType(Design.DocumentDataDefinitionTypeConverter)), _
     DesignTimeVisibleAttribute(False), ToolboxItemAttribute(False)> _
    Public Class StructuredDocumentDataDefinition
        Inherits Component

#Region "Private members"
        Private _Name As String 'Must be unique
        Private _Description As String
        Private _DataObjectType As Type
        Private _IsCollection As Boolean

        Private Shared _ReferencedDataTypes As TypeCollection
#End Region

#Region "Public interface"
#Region "Name"
        <Description("The unique name identifying this data definition item")> _
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal Value As String)
                _Name = Value
            End Set
        End Property
#End Region

#Region "Description"
        <Description("The description of the data definition")> _
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal Value As String)
                _Description = Value
            End Set
        End Property
#End Region

#Region "DataObjectType"
        <Description("The type of the data object"), _
         Editor(GetType(Design.DocumentDataDefinitionTypeSelector), GetType(System.Drawing.Design.UITypeEditor))> _
        Public Property DataObjectType() As Type
            Get
                Return _DataObjectType
            End Get
            Set(ByVal Value As Type)
                _DataObjectType = Value
            End Set
        End Property
#End Region

#Region "IsCollection"
        <Description("Whether or not the data object is a collection which can be iterated through")> _
        Public Property IsCollection() As Boolean
            Get
                Return _IsCollection
            End Get
            Set(ByVal Value As Boolean)
                _IsCollection = Value
            End Set
        End Property
#End Region
#End Region

#Region "Public shared interface"
        Public Shared ReadOnly Property ReferencedDataTypes() As TypeCollection
            Get
                If _ReferencedDataTypes Is Nothing Then
                    Call InitialiseReferencedDataTypes()
                End If
                Return _ReferencedDataTypes
            End Get
        End Property

        Public Shared Sub AddReferencedType(ByVal NewType As Type)
            If _ReferencedDataTypes Is Nothing Then
                Call InitialiseReferencedDataTypes()
            End If
            If _ReferencedDataTypes.Contains(NewType.FullName) Then
                If StructuredPrintDocumentProvider.TraceSwitch.TraceWarning Then
                    Trace.WriteLine("Type added that was already referenced: " & NewType.ToString, "AddReferencedType")
                End If
            Else
                _ReferencedDataTypes.Add(NewType.FullName)
            End If
            Call SaveReferencedDataTypes()
        End Sub

        Private Shared Sub InitialiseReferencedDataTypes()
            _ReferencedDataTypes = New TypeCollection
            'Load them from the background store?
            If File.Exists("StructuredDocumentDataDefinition.xml") Then

            End If
        End Sub

        Private Shared Sub SaveReferencedDataTypes()
            If File.Exists("StructuredDocumentDataDefinition.xml") Then
                File.Delete("StructuredDocumentDataDefinition.xml")
            End If
            Dim serializer As New XmlSerializer(_ReferencedDataTypes.GetType)
            Dim writer As New StreamWriter("StructuredDocumentDataDefinition.xml", False)
            serializer.Serialize(writer, _ReferencedDataTypes)
            writer.Close()
        End Sub
#End Region

#Region "Public constructors"
        Public Sub New(ByVal Name As String, _
                        ByVal Description As String, _
                        ByVal DataObjectType As Type, _
                        ByVal IsCollection As Boolean)

            _Name = Name
            _Description = Description
            _DataObjectType = DataObjectType
            _IsCollection = IsCollection

        End Sub

        Public Sub New()

        End Sub
#End Region

    End Class
#End Region

#Region "StructuredDocumentDataDefinitionCollection"
    ' Stores a collection of data source definitions for the current print document
        Public Class StructuredDocumentDataDefinitionCollection
        Inherits ArrayList

#Region "Shadowed interface"
        Public Shadows Function Add(ByVal Item As StructuredDocumentDataDefinition) As Integer
            Return MyBase.Add(Item)
        End Function

        Default Public Shadows Property Item(ByVal Index As Integer) As StructuredDocumentDataDefinition
            Get
                Return DirectCast(MyBase.Item(Index), StructuredDocumentDataDefinition)
            End Get
            Set(ByVal Value As StructuredDocumentDataDefinition)
                MyBase.Item(Index) = Value
            End Set
        End Property
#End Region

#Region "Public constructor"
        Public Sub New()

        End Sub
#End Region

    End Class
#End Region

#Region "TypeCollection"
    <Serializable()> _
    Public Class TypeCollection
        Inherits ArrayList

#Region "Shadowed interface"
        Public Shadows Function Add(ByVal Item As String) As Integer
            Return MyBase.Add(Item)
        End Function

        Default Public Shadows Property Item(ByVal Index As Integer) As String
            Get
                Return DirectCast(MyBase.Item(Index), String)
            End Get
            Set(ByVal Value As String)
                MyBase.Item(Index) = Value
            End Set
        End Property
#End Region

#Region "Public constructor"
        Public Sub New()

        End Sub
#End Region
    End Class

#End Region

    Namespace Design

#Region "DocumentDataDefinitionTypeSelector"
        Public Class DocumentDataDefinitionTypeSelector
            Inherits UITypeEditor

#Region "Public interface"
#Region "Overriden members"
#Region "GetEditStyle"
            <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
            Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
                'This editor needs a window to allow more data types to be added....
                Return UITypeEditorEditStyle.Modal
            End Function
#End Region
#Region "EditValue"
            <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
            Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

                ' Use the IWindowsFormsEditorService to display drop down 
                Dim edSvc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
                If Not (edSvc Is Nothing) Then
                    Dim DataTypesForm As New DesignerForm_DocumentDataDefinitionType
                    edSvc.ShowDialog(DataTypesForm)
                    value = DataTypesForm.SelectedType
                End If
                If value Is Nothing Then
                    Return GetType(System.String)
                Else
                    Return value
                End If
            End Function
#End Region
#End Region
#End Region

        End Class
#End Region

#Region "DocumentDataDefinitionTypeConverter"
        Public Class DocumentDataDefinitionTypeConverter
            Inherits TypeConverter

#Region "CanConvertTo"
            Public Overloads Overrides Function CanConvertTo( _
               ByVal context As ITypeDescriptorContext, _
               ByVal destinationType As Type _
            ) As Boolean

                If destinationType Is GetType(InstanceDescriptor) Then
                    Return True
                End If
                Return MyBase.CanConvertTo(destinationType)

            End Function
#End Region

#Region "ConvertTo"
            Public Overloads Overrides Function ConvertTo( _
                   ByVal context As ITypeDescriptorContext, _
                   ByVal culture As CultureInfo, _
                   ByVal value As Object, _
                   ByVal destinationType As Type _
                   ) As Object

                If (destinationType Is GetType(InstanceDescriptor) AndAlso value.GetType Is GetType(StructuredDocumentDataDefinition)) Then
                    Dim spda As StructuredDocumentDataDefinition = DirectCast(value, StructuredDocumentDataDefinition)

                    'Get the full constructor:-
                    '    ByVal Name As String
                    '    ByVal Description As String
                    '    ByVal DataObjectType As Type
                    '    ByVal IsCollection As Boolean

                    Dim ctor As ConstructorInfo = spda.GetType.GetConstructor(New [Type]() {GetType(String), GetType(String), GetType(System.Type), GetType(Boolean)})

                    If Not ctor Is Nothing Then
                        Return New InstanceDescriptor(ctor, New [Object]() {spda.Name, spda.Description, spda.DataObjectType, spda.IsCollection})
                    End If
                End If

            End Function
#End Region

        End Class
#End Region

    End Namespace

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions