Author:水如烟
Namespace LzmTW.uSystem.uWindows.AddIns
Friend Delegate Sub ManagerActionHandler(ByVal assemblyFilesOrAddInsNames As String())
End Namespace
Friend Delegate Sub ManagerActionHandler(ByVal assemblyFilesOrAddInsNames As String())
End Namespace
Namespace LzmTW.uSystem.uWindows.AddIns
''' 本类实例须在主窗体调用 InitializeComponent后生成。为的是利用主窗体的Load事件安装有关插件。
''' </summary>
Public Class AddInsManager
'菜单控制
Private gMenuManager As New uForms.uMainForm.Menu.MenuManager
'配置文件
Private gConfigManager As New AddInsConfig
'当前已加载的插件
Private gPackages As New Dictionary(Of String, IPackage)
Me.gMainForm = mainForm
gMenuManager.SetMainFormMenuStrip(gMainForm)
End Sub
If item.IsLoad Then Return
Dim mPackage As LzmTW.uSystem.uWindows.AddIns.IPackage = Nothing
gConfigManager.Add(item)
gConfigManager.Save()
End If
End Sub
If gPackages.ContainsKey(item.Name) Then
gPackages(item.Name).Unload()
gPackages.Remove(item.Name)
gConfigManager.FindItem(item.Name).IsLoad = False
gConfigManager.Save()
End If
End Sub
ItemUnLoad(item)
ItemLoad(item)
End Sub
If gPackages.ContainsKey(item.Name) Then
gPackages(item.Name).Unload()
gPackages.Remove(item.Name)
End If
With gMenuManager
.AddParentToolStripMenuItem(uForms.uMainForm.Menu.MenuManager.GetDefaultMenuItem("Tools"))
End If
.AddToolStripMenuItem("Tools", uForms.uMainForm.Menu.MenuManager.GetDefaultMenuItem("AddIns"))
End If
Me.gConfigManager.Save()
Else
Me.gConfigManager.Read()
Dim mItem As AddInsItem = Nothing
mItem = Me.gConfigManager.Item(i)
mItem.IsLoad = False
Me.ItemLoad(mItem)
Catch ex As Exception
Me.gConfigManager.Remove(mItem)
Me.gConfigManager.Save()
End Try
Next
End If
gAddInsForm = New AddInsForm
Using gAddInsForm
gAddInsForm.ShowDialog(gMainForm, AddressOf OnWaitingForAdd, AddressOf OnWaitingForRemove, AddressOf OnWaitingForReLoad)
End Using
End Sub
Dim mHasValidItems As Boolean = False
Dim mNotValidFiles(-1) As String
Dim mInformation As AddInsAttribute() = AddInsAssemblyReader.Read(file)
If mInformation.Length = 0 Then '不是插件
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, file)
Else
If Me.gPackages.ContainsKey(a.Name) Then '已安装
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, String.Format("[{0}]", a.Name) & file)
Else
Try
Me.ItemLoad(mItem)
Me.gAddInsForm.Add(mItem)
Catch ex As Exception
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, String.Format("[{0}]", a.Name) & file)
End Try
End If
Next
Next
gAddInsForm.ShowInValidForm(mNotValidFiles)
End If
For Each itemName As String In names
Me.ItemRemove(Me.gConfigManager.FindItem(itemName))
Next
End Sub
For Each itemName As String In names
Me.ItemReLoad(Me.gConfigManager.FindItem(itemName))
Next
End Sub
End Namespace
''' <summary>
''' 本类实例须在主窗体调用 InitializeComponent后生成。为的是利用主窗体的Load事件安装有关插件。
''' </summary>
Public Class AddInsManager
Private WithEvents gMainForm As Form
'菜单控制
Private gMenuManager As New uForms.uMainForm.Menu.MenuManager
'配置文件
Private gConfigManager As New AddInsConfig
'当前已加载的插件
Private gPackages As New Dictionary(Of String, IPackage)
Private gAddInsForm As AddInsForm
Sub New(ByVal mainForm As Form)
Me.gMainForm = mainForm
gMenuManager.SetMainFormMenuStrip(gMainForm)
End Sub
Private Sub ItemLoad(ByVal item As AddInsItem)
If item.IsLoad Then Return
Dim mAttribute As AddInsAttribute = AddInsAssemblyReader.Read(item.Location, item.Name)
Dim mAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile(item.Location)
Dim mPackage As LzmTW.uSystem.uWindows.AddIns.IPackage = Nothing
mPackage
= CType(mAssembly.CreateInstance(mAttribute.FullTypeName), LzmTW.uSystem.uWindows.AddIns.IPackage)mPackage.Load(gMainForm)
gPackages.Add(item.Name, mPackage)
item.IsLoad
= TrueIf gConfigManager.FindItem(item.Name) Is Nothing Then
gConfigManager.Add(item)
gConfigManager.Save()
End If
End Sub
Private Sub ItemUnLoad(ByVal item As AddInsItem)
If gPackages.ContainsKey(item.Name) Then
gPackages(item.Name).Unload()
gPackages.Remove(item.Name)
gConfigManager.FindItem(item.Name).IsLoad = False
gConfigManager.Save()
End If
End Sub
Private Sub ItemReLoad(ByVal item As AddInsItem)
ItemUnLoad(item)
ItemLoad(item)
End Sub
Private Sub ItemRemove(ByVal item As AddInsItem)
If gPackages.ContainsKey(item.Name) Then
gPackages(item.Name).Unload()
gPackages.Remove(item.Name)
End If
gConfigManager.Remove(item)
gConfigManager.Save()
Private Sub gMainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gMainForm.Load
With gMenuManager
If .FindToolStripItem("Tools") Is Nothing Then
.AddParentToolStripMenuItem(uForms.uMainForm.Menu.MenuManager.GetDefaultMenuItem("Tools"))
End If
If .FindToolStripItem("AddIns") Is Nothing Then
.AddToolStripMenuItem("Tools", uForms.uMainForm.Menu.MenuManager.GetDefaultMenuItem("AddIns"))
End If
gMenuManager.AddClickHandler(
"AddIns", AddressOf OnAddInsToolStripMenuItemClick)End With
If Not IO.File.Exists(Me.gConfigManager.FileName) Then
Me.gConfigManager.Save()
Else
Me.gConfigManager.Read()
Dim mItem As AddInsItem = Nothing
For i As Integer = Me.gConfigManager.Count - 1 To 0 Step -1
mItem = Me.gConfigManager.Item(i)
mItem.IsLoad = False
Try
Me.ItemLoad(mItem)
Catch ex As Exception
Me.gConfigManager.Remove(mItem)
Me.gConfigManager.Save()
End Try
Next
End If
End Sub
Private Sub OnAddInsToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
gAddInsForm = New AddInsForm
Using gAddInsForm
gAddInsForm.ShowDialog(gMainForm, AddressOf OnWaitingForAdd, AddressOf OnWaitingForRemove, AddressOf OnWaitingForReLoad)
End Using
End Sub
Private Sub OnWaitingForAdd(ByVal files As String())
Dim mHasValidItems As Boolean = False
Dim mNotValidFiles(-1) As String
For Each file As String In files
Dim mInformation As AddInsAttribute() = AddInsAssemblyReader.Read(file)
If mInformation.Length = 0 Then '不是插件
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, file)
Else
For Each a As AddInsAttribute In mInformation
If Me.gPackages.ContainsKey(a.Name) Then '已安装
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, String.Format("[{0}]", a.Name) & file)
Else
Dim mItem As New AddInsItem(a.Name, file)
Try
Me.ItemLoad(mItem)
Me.gAddInsForm.Add(mItem)
Catch ex As Exception
uCollection.CommonFunction.Append(Of String)(mNotValidFiles, String.Format("[{0}]", a.Name) & file)
End Try
End If
Next
End If
Next
If mNotValidFiles.Length > 0 Then
gAddInsForm.ShowInValidForm(mNotValidFiles)
End If
End Sub
Private Sub OnWaitingForRemove(ByVal names As String())
For Each itemName As String In names
Me.ItemRemove(Me.gConfigManager.FindItem(itemName))
Next
End Sub
Private Sub OnWaitingForReLoad(ByVal names As String())
For Each itemName As String In names
Me.ItemReLoad(Me.gConfigManager.FindItem(itemName))
Next
End Sub
End Class
End Namespace
Namespace LzmTW.uSystem.uWindows.AddIns
Friend Class InValidAddInsForm
End Namespace
Friend Class InValidAddInsForm
End Class
End Namespace
Namespace LzmTW.uSystem.uWindows.AddIns
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class InValidAddInsForm
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ListView = New System.Windows.Forms.ListView
Me.LocationColumnHeader = New System.Windows.Forms.ColumnHeader
Me.CloseButton = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ListView
'
Me.ListView.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ListView.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.LocationColumnHeader})
Me.ListView.Location = New System.Drawing.Point(16, 29)
Me.ListView.Name = "ListView"
Me.ListView.Size = New System.Drawing.Size(411, 191)
Me.ListView.TabIndex = 0
Me.ListView.UseCompatibleStateImageBehavior = False
Me.ListView.View = System.Windows.Forms.View.Details
'
'LocationColumnHeader
'
Me.LocationColumnHeader.Text = "来源"
Me.LocationColumnHeader.Width = 700
'
'CloseButton
'
Me.CloseButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CloseButton.DialogResult = System.Windows.Forms.DialogResult.OK
Me.CloseButton.Location = New System.Drawing.Point(351, 234)
Me.CloseButton.Name = "CloseButton"
Me.CloseButton.Size = New System.Drawing.Size(76, 27)
Me.CloseButton.TabIndex = 1
Me.CloseButton.Text = "返回(&C)"
Me.CloseButton.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(19, 5)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(107, 12)
Me.Label1.TabIndex = 4
Me.Label1.Text = "无法安装下列插件:"
'
'InValidAddInsForm
'
Me.AcceptButton = Me.CloseButton
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.CancelButton = Me.CloseButton
Me.ClientSize = New System.Drawing.Size(439, 273)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.CloseButton)
Me.Controls.Add(Me.ListView)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Name = "InValidAddInsForm"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "插件管理"
Me.ResumeLayout(False)
Me.PerformLayout()
Friend WithEvents ListView As System.Windows.Forms.ListView
Friend WithEvents LocationColumnHeader As System.Windows.Forms.ColumnHeader
Friend WithEvents CloseButton As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
End Namespace
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class InValidAddInsForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ListView = New System.Windows.Forms.ListView
Me.LocationColumnHeader = New System.Windows.Forms.ColumnHeader
Me.CloseButton = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ListView
'
Me.ListView.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ListView.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.LocationColumnHeader})
Me.ListView.Location = New System.Drawing.Point(16, 29)
Me.ListView.Name = "ListView"
Me.ListView.Size = New System.Drawing.Size(411, 191)
Me.ListView.TabIndex = 0
Me.ListView.UseCompatibleStateImageBehavior = False
Me.ListView.View = System.Windows.Forms.View.Details
'
'LocationColumnHeader
'
Me.LocationColumnHeader.Text = "来源"
Me.LocationColumnHeader.Width = 700
'
'CloseButton
'
Me.CloseButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CloseButton.DialogResult = System.Windows.Forms.DialogResult.OK
Me.CloseButton.Location = New System.Drawing.Point(351, 234)
Me.CloseButton.Name = "CloseButton"
Me.CloseButton.Size = New System.Drawing.Size(76, 27)
Me.CloseButton.TabIndex = 1
Me.CloseButton.Text = "返回(&C)"
Me.CloseButton.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(19, 5)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(107, 12)
Me.Label1.TabIndex = 4
Me.Label1.Text = "无法安装下列插件:"
'
'InValidAddInsForm
'
Me.AcceptButton = Me.CloseButton
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.CancelButton = Me.CloseButton
Me.ClientSize = New System.Drawing.Size(439, 273)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.CloseButton)
Me.Controls.Add(Me.ListView)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Name = "InValidAddInsForm"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "插件管理"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents ListView As System.Windows.Forms.ListView
Friend WithEvents LocationColumnHeader As System.Windows.Forms.ColumnHeader
Friend WithEvents CloseButton As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
End Namespace
Imports System.Windows.Forms
Namespace LzmTW.uSystem.uWindows.AddIns
Friend Class AddInsForm
Public Event WaitingForRemove As ManagerActionHandler
Public Event WaitingForReLoad As ManagerActionHandler
With Me
AddHandler .WaitingForAdd, forAddAction
AddHandler .WaitingForRemove, forRemoveAction
AddHandler .WaitingForReLoad, forReloadAction
MyBase.ShowDialog(parent)
RemoveHandler .WaitingForAdd, forAddAction
RemoveHandler .WaitingForRemove, forRemoveAction
RemoveHandler .WaitingForReLoad, forReloadAction
End With
End Sub
With item
Me.ListView.Items.Add(New ListViewItem(New String() {.Name, .IsLoad.ToString, .Location}))
End With
End Sub
Using mInValidAddinsForm As New InValidAddInsForm
With mInValidAddinsForm
For Each file As String In inValidfiles
.ListView.Items.Add(file)
Next
.ShowDialog(Me)
End With
End Using
End Sub
If isOneSelected Then
Dim mName As String = Me.ListView.SelectedItems(0).Text
Dim mLocation As String = Me.ListView.SelectedItems(0).SubItems(2).Text
Me.InfoTextBox.Text = AddInsAssemblyReader.Read(mLocation, mName).ToString
Else
Me.InfoTextBox.Text = ""
End If
Dim Items As New AddInsConfig
Items.Read()
Items.ForEach(AddressOf AddItem)
End Sub
Add(item)
End Sub
Namespace LzmTW.uSystem.uWindows.AddIns
Friend Class AddInsForm
Public Event WaitingForAdd As ManagerActionHandler
Public Event WaitingForRemove As ManagerActionHandler
Public Event WaitingForReLoad As ManagerActionHandler
Public Shadows Sub ShowDialog(ByVal parent As Form, ByVal forAddAction As ManagerActionHandler, ByVal forRemoveAction As ManagerActionHandler, ByVal forReloadAction As ManagerActionHandler)
With Me
AddHandler .WaitingForAdd, forAddAction
AddHandler .WaitingForRemove, forRemoveAction
AddHandler .WaitingForReLoad, forReloadAction
MyBase.ShowDialog(parent)
RemoveHandler .WaitingForAdd, forAddAction
RemoveHandler .WaitingForRemove, forRemoveAction
RemoveHandler .WaitingForReLoad, forReloadAction
End With
End Sub
Public Sub Add(ByVal item As AddInsItem)
With item
Me.ListView.Items.Add(New ListViewItem(New String() {.Name, .IsLoad.ToString, .Location}))
End With
End Sub
Public Sub ShowInValidForm(ByVal inValidfiles As String())
Using mInValidAddinsForm As New InValidAddInsForm
With mInValidAddinsForm
For Each file As String In inValidfiles
.ListView.Items.Add(file)
Next
.ShowDialog(Me)
End With
End Using
End Sub
Private Sub ShowAddinsDeclareInformation(ByVal isOneSelected As Boolean)
If isOneSelected Then
Dim mName As String = Me.ListView.SelectedItems(0).Text
Dim mLocation As String = Me.ListView.SelectedItems(0).SubItems(2).Text
Me.InfoTextBox.Text = AddInsAssemblyReader.Read(mLocation, mName).ToString
Else
Me.InfoTextBox.Text = ""
End If
End Sub
Private Sub AddInsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Items As New AddInsConfig
Items.Read()
Items.ForEach(AddressOf AddItem)
End Sub
Private Sub AddItem(ByVal item As AddInsItem)
Add(item)
End Sub
End Class