Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.ComponentModel
Imports System.Diagnostics
Imports DevExpress.Xpf.Docking
Imports System.Windows.Input
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports Microsoft.Win32
Imports System.IO
Imports System.Windows.Controls
Imports DevExpress.Xpf.Bars
Namespace DockingDemo.MVVM
Public Class RelayCommand
Implements ICommand
Private ReadOnly _execute As Action(Of Object)
Private ReadOnly _canExecute As Predicate(Of Object)
Public Sub New(ByVal execute As Action(Of Object))
Me.New(execute, Nothing)
End Sub
Public Sub New(ByVal execute As Action(Of Object), ByVal canExecute As Predicate(Of Object))
If execute Is Nothing Then
Throw New ArgumentNullException("execute")
End If
_execute = execute
_canExecute = canExecute
End Sub
#Region "ICommand Members"
<DebuggerStepThrough> _
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Return If(_canExecute Is Nothing, True, _canExecute(parameter))
End Function
Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
Protected Overridable Sub OnCanExecuteChanged(ByVal e As EventArgs)
Dim canExecuteChanged = CanExecuteChangedEvent
If CanExecuteChangedEvent IsNot Nothing Then
CanExecuteChangedEvent(Me, e)
End If
End Sub
Public Sub RaiseCanExecuteChanged()
OnCanExecuteChanged(EventArgs.Empty)
End Sub
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
_execute(parameter)
End Sub
#End Region ' ICommand Members
End Class
Public MustInherit Class ViewModelBase
Inherits DependencyObject
Implements IDisposable
Protected Sub New()
End Sub
Private privateDisplayName As String
Public Overridable Property DisplayName() As String
Get
Return privateDisplayName
End Get
Protected Set(ByVal value As String)
privateDisplayName = value
End Set
End Property
Private privateGlyph As ImageSource
Public Overridable Property Glyph() As ImageSource
Get
Return privateGlyph
End Get
Set(ByVal value As ImageSource)
privateGlyph = value
End Set
End Property
#Region "IDisposable Members"
Public Sub Dispose() Implements IDisposable.Dispose
Me.OnDispose()
End Sub
Protected Overridable Sub OnDispose()
End Sub
#If DEBUG Then
''' <summary>
''' Useful for ensuring that ViewModel objects are properly garbage collected.
''' </summary>
Protected Overrides Sub Finalize()
Dim msg As String = String.Format("{0} ({1}) ({2}) Finalized", Me.GetType().Name, Me.DisplayName, Me.GetHashCode())
System.Diagnostics.Debug.WriteLine(msg)
End Sub
#End If
#End Region '_ IDisposable Members
End Class
Public MustInherit Class WorkspaceViewModel
Inherits ViewModelBase
#Region "static"
Public Shared ReadOnly IsClosedProperty As DependencyProperty
Public Shared ReadOnly IsOpenedProperty As DependencyProperty
Public Shared ReadOnly IsActiveProperty As DependencyProperty
Shared Sub New()
IsClosedProperty = DependencyProperty.Register("IsClosed", GetType(Boolean), GetType(WorkspaceViewModel), New PropertyMetadata(True, AddressOf OnIsClosedChanged))
IsOpenedProperty = DependencyProperty.Register("IsOpened", GetType(Boolean?), GetType(WorkspaceViewModel), New PropertyMetadata(False))
IsActiveProperty = DependencyProperty.Register("IsActive", GetType(Boolean), GetType(WorkspaceViewModel), Nothing)
End Sub
Private Shared Sub OnIsClosedChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim model As WorkspaceViewModel = CType(d, WorkspaceViewModel)
model.OnIsClosedChanged(CBool(e.NewValue))
End Sub
#End Region
Protected Sub New()
End Sub
Public Property IsClosed() As Boolean
Get
Return CBool(GetValue(IsClosedProperty))
End Get
Set(ByVal value As Boolean)
SetValue(IsClosedProperty, value)
End Set
End Property
Public Property IsOpened() As Boolean?
Get
Return CType(GetValue(IsOpenedProperty), Boolean?)
End Get
Set(ByVal value? As Boolean)
SetValue(IsOpenedProperty, value)
End Set
End Property
Public Property IsActive() As Boolean
Get
Return CBool(GetValue(IsActiveProperty))
End Get
Set(ByVal value As Boolean)
SetValue(IsActiveProperty, value)
End Set
End Property
Private _closeCommand As RelayCommand
Public ReadOnly Property CloseCommand() As ICommand
Get
If _closeCommand Is Nothing Then
_closeCommand = New RelayCommand(New Action(Of Object)(AddressOf OnRequestClose))
End If
Return _closeCommand
End Get
End Property
Public Event RequestClose As EventHandler
Private Sub OnRequestClose(ByVal param As Object)
Dim handler As EventHandler = Me.RequestCloseEvent
If handler IsNot Nothing Then
handler(Me, EventArgs.Empty)
End If
End Sub
Protected Overridable Sub OnIsClosedChanged(ByVal newValue As Boolean)
IsOpened = Not newValue
End Sub
End Class
Public Class CommandViewModel
Inherits ViewModelBase
Public Sub New(ByVal displayName As String, ByVal command As ICommand, Optional ByVal commands As List(Of CommandViewModel) = Nothing)
DisplayName = displayName
Command = command
If commands IsNot Nothing Then
Commands = commands
End If
IsEnabled = True
End Sub
Private privateCommand As ICommand
Public Property Command() As ICommand
Get
Return privateCommand
End Get
Private Set(ByVal value As ICommand)
privateCommand = value
End Set
End Property
Private privateCommands As List(Of CommandViewModel)
Public Property Commands() As List(Of CommandViewModel)
Get
Return privateCommands
End Get
Set(ByVal value As List(Of CommandViewModel))
privateCommands = value
End Set
End Property
Private privateOwner As WorkspaceViewModel
Public Property Owner() As WorkspaceViewModel
Get
Return privateOwner
End Get
Set(ByVal value As WorkspaceViewModel)
privateOwner = value
End Set
End Property
Private privateIsEnabled As Boolean
Public Property IsEnabled() As Boolean
Get
Return privateIsEnabled
End Get
Set(ByVal value As Boolean)
privateIsEnabled = value
End Set
End Property
Private privateIsSubItem As Boolean
Public Property IsSubItem() As Boolean
Get
Return privateIsSubItem
End Get
Set(ByVal value As Boolean)
privateIsSubItem = value
End Set
End Property
Private privateIsSeparator As Boolean
Public Property IsSeparator() As Boolean
Get
Return privateIsSeparator
End Get
Set(ByVal value As Boolean)
privateIsSeparator = value
End Set
End Property
Private privateKeyGesture As KeyGesture
Public Property KeyGesture() As KeyGesture
Get
Return privateKeyGesture
End Get
Set(ByVal value As KeyGesture)
privateKeyGesture = value
End Set
End Property
End Class
Public MustInherit Class PanelWorkspaceViewModel
Inherits WorkspaceViewModel
Protected MustOverride ReadOnly Property TargetName() As String
Public Sub New()
MVVMHelper.SetTargetName(Me, TargetName)
End Sub
End Class
Public Class ToolboxViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "Toolbox"
End Get
End Property
Public Sub New()
DisplayName = "Toolbox"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/Toolbox_16x16.png", UriKind.Relative))
End Sub
End Class
Public Class SolutionExplorerViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "RightHost"
End Get
End Property
Public Sub New()
DisplayName = "Solution Explorer"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/Solution Explorer.png", UriKind.Relative))
End Sub
End Class
Public Class PropertiesViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "RightHost"
End Get
End Property
Public Sub New()
DisplayName = "Properties"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/PropertiesWindow_16x16.png", UriKind.Relative))
End Sub
End Class
Public Class ErrorListViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "BottomHost"
End Get
End Property
Public Sub New()
DisplayName = "Error List"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/TaskList_16x16.png", UriKind.Relative))
End Sub
End Class
Public Class SearchResultsViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "BottomHost"
End Get
End Property
Public Sub New()
DisplayName = "Search Results"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/FindInFiles_16x16.png", UriKind.Relative))
End Sub
End Class
Public Class OutputViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "BottomHost"
End Get
End Property
Public Sub New()
DisplayName = "Output"
Glyph = New BitmapImage(New Uri("/DockingDemo;component/Images/VS2010Docking/Output_16x16.png", UriKind.Relative))
End Sub
End Class
Public Class DocumentViewModel
Inherits PanelWorkspaceViewModel
Protected Overrides ReadOnly Property TargetName() As String
Get
Return "DocumentHost"
End Get
End Property
Public Sub New(ByVal displayName As String)
DisplayName = displayName
End Sub
Public Sub New()
End Sub
Public Overridable Function Open() As Boolean
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
Dim userClickedOK? As Boolean = openFileDialog1.ShowDialog()
If userClickedOK.GetValueOrDefault() = True Then
DisplayName = openFileDialog1.File.Name
Dim fileStream As System.IO.Stream = openFileDialog1.File.OpenRead()
Using reader As New System.IO.StreamReader(fileStream)
Text = reader.ReadToEnd()
End Using
fileStream.Close()
End If
Return userClickedOK.GetValueOrDefault() = True
End Function
Private privateText As String
Public Property Text() As String
Get
Return privateText
End Get
Protected Set(ByVal value As String)
privateText = value
End Set
End Property
Private privateFooter As String
Public Property Footer() As String
Get
Return privateFooter
End Get
Protected Set(ByVal value As String)
privateFooter = value
End Set
End Property
Private privateDescription As String
Public Property Description() As String
Get
Return privateDescription
End Get
Protected Set(ByVal value As String)
privateDescription = value
End Set
End Property
End Class
Public Class BarModel
Inherits ViewModelBase
Public Shared ReadOnly CommandsProperty As DependencyProperty
Shared Sub New()
CommandsProperty = DependencyProperty.Register("Commands", GetType(ObservableCollection(Of Object)), GetType(BarModel), New PropertyMetadata(Nothing))
End Sub
Public Sub New(ByVal displayName As String)
Me.New()
DisplayName = displayName
End Sub
Public Sub New()
Commands = New ObservableCollection(Of Object)()
End Sub
Public Property Commands() As ObservableCollection(Of Object)
Get
Return (CType(GetValue(CommandsProperty), ObservableCollection(Of Object)))
End Get
Set(ByVal value As ObservableCollection(Of Object))
SetValue(CommandsProperty, value)
End Set
End Property
Private privateIsMainMenu As Boolean
Public Property IsMainMenu() As Boolean
Get
Return privateIsMainMenu
End Get
Set(ByVal value As Boolean)
privateIsMainMenu = value
End Set
End Property
End Class
Public Class MainWindowViewModel
Inherits WorkspaceViewModel
Private _commands As ReadOnlyCollection(Of CommandViewModel)
Private _workspaces As ObservableCollection(Of WorkspaceViewModel)
Public Sub New()
DisplayName = "MainWindowViewModel"
InitDefaultLayout()
End Sub
Protected Overridable Sub InitDefaultLayout()
OpenOrCloseWorkspace(_ToolboxViewModel)
OpenOrCloseWorkspace(_SolutionExplorerViewModel)
OpenOrCloseWorkspace(_PropertiesViewModel)
OpenOrCloseWorkspace(_ErrorListViewModel)
OpenOrCloseWorkspace(New DocumentViewModel("Document1"), True)
End Sub
Public ReadOnly Property Commands() As ReadOnlyCollection(Of CommandViewModel)
Get
If _commands Is Nothing Then
Dim cmds As List(Of CommandViewModel) = Me.CreateCommands()
_commands = New ReadOnlyCollection(Of CommandViewModel)(cmds)
End If
Return _commands
End Get
End Property
Protected Overridable Function CreateViewCommands() As List(Of CommandViewModel)
Dim toolbox As New CommandViewModel("Toolbox", New RelayCommand(New Action(Of Object)(AddressOf OnShowToolboxPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Toolbox_16x16.png"), .Owner = _ToolboxViewModel}
Dim solutionExplorer As New CommandViewModel("Solution Explorer", New RelayCommand(New Action(Of Object)(AddressOf OnShowSolutionExplorerPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Solution Explorer.png"), .Owner = _SolutionExplorerViewModel}
Dim properties As New CommandViewModel("Properties", New RelayCommand(New Action(Of Object)(AddressOf OnShowPropertiesPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/PropertiesWindow_16x16.png"), .Owner = _PropertiesViewModel}
Dim errorList As New CommandViewModel("Error List", New RelayCommand(New Action(Of Object)(AddressOf OnShowErrorListPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/TaskList_16x16.png"), .Owner = _ErrorListViewModel}
Dim output As New CommandViewModel("Output", New RelayCommand(New Action(Of Object)(AddressOf OnShowOutputPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/Output_16x16.png"), .Owner = _OutputViewModel}
Dim searchResults As New CommandViewModel("Search Results", New RelayCommand(New Action(Of Object)(AddressOf OnShowSearchResultsPanel))) With {.Glyph = GlyphHelper.GetGlyph("/Images/VS2010Docking/FindInFiles_16x16.png"), .Owner = _SearchResultsViewModel}
Return New List(Of CommandViewModel) (New CommandViewModel() {toolbox, solutionExplorer, properties, errorList, output, searchResults})
End Function
Protected Overridable Function CreateFileCommands() As List(Of CommandViewModel)
Dim openDocument As New CommandViewModel("Open...", New RelayCommand(New Action(Of Object)(AddressOf OnOpenDocument))) With {.Glyph = GlyphHelper.GetGlyph("/Images/Open_16x16.png"), .IsEnabled = True}
Return New List(Of CommandViewModel) (New CommandViewModel() {openDocument})
End Function
Private Function CreateCommands() As List(Of CommandViewModel)
Dim fileCommands As List(Of CommandViewModel) = CreateFileCommands()
Dim viewCommands As List(Of CommandViewModel) = CreateViewCommands()
Dim file = New CommandViewModel("File", Nothing, fileCommands)
Dim view = New CommandViewModel("View", Nothing, viewCommands)
Return New List(Of CommandViewModel) (New CommandViewModel() {file, view})
End Function
Private _bars As ReadOnlyCollection(Of BarModel)
Public ReadOnly Property Bars() As ReadOnlyCollection(Of BarModel)
Get
If _bars Is Nothing Then
Dim cmds As List(Of BarModel) = CreateBars()
_bars = New ReadOnlyCollection(Of BarModel)(cmds)
End If
Return _bars
End Get
End Property
Protected Overridable Function CreateBars() As List(Of BarModel)
Dim model As New BarModel("Main") With {.IsMainMenu = True}
Dim commands = CreateCommands()
For Each cmd In commands
model.Commands.Add(cmd)
Next cmd
Return New List(Of BarModel) (New BarModel() {model})
End Function
Public ReadOnly Property Workspaces() As ObservableCollection(Of WorkspaceViewModel)
Get
If _workspaces Is Nothing Then
_workspaces = New ObservableCollection(Of WorkspaceViewModel)()
AddHandler _workspaces.CollectionChanged, AddressOf OnWorkspacesChanged
End If
Return _workspaces
End Get
End Property
Private Sub OnWorkspacesChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
If e.NewItems IsNot Nothing AndAlso e.NewItems.Count <> 0 Then
For Each workspace As WorkspaceViewModel In e.NewItems
AddHandler workspace.RequestClose, AddressOf OnWorkspaceRequestClose
Next workspace
End If
If e.OldItems IsNot Nothing AndAlso e.OldItems.Count <> 0 Then
For Each workspace As WorkspaceViewModel In e.OldItems
RemoveHandler workspace.RequestClose, AddressOf OnWorkspaceRequestClose
Next workspace
End If
End Sub
Private Sub OnWorkspaceRequestClose(ByVal sender As Object, ByVal e As EventArgs)
Dim workspace As WorkspaceViewModel = TryCast(sender, WorkspaceViewModel)
If TypeOf workspace Is PanelWorkspaceViewModel Then
CType(workspace, PanelWorkspaceViewModel).IsClosed = True
If TypeOf workspace Is DocumentViewModel Then
workspace.Dispose()
Me.Workspaces.Remove(workspace)
End If
End If
End Sub
Private toolBoxViewModel As ToolboxViewModel
Public Property _ToolboxViewModel() As ToolboxViewModel
Get
If toolBoxViewModel Is Nothing Then
toolBoxViewModel = New ToolboxViewModel()
End If
Return toolBoxViewModel
End Get
Set(ByVal value As ToolboxViewModel)
If toolBoxViewModel Is value Then
Return
End If
toolBoxViewModel = value
End Set
End Property
Private solutionExplorerViewModel As SolutionExplorerViewModel
Public Property _SolutionExplorerViewModel() As SolutionExplorerViewModel
Get
If solutionExplorerViewModel Is Nothing Then
solutionExplorerViewModel = New SolutionExplorerViewModel()
End If
Return solutionExplorerViewModel
End Get
Set(ByVal value As SolutionExplorerViewModel)
If solutionExplorerViewModel Is value Then
Return
End If
solutionExplorerViewModel = value
End Set
End Property
Private propertiesViewModel As PropertiesViewModel
Public Property _PropertiesViewModel() As PropertiesViewModel
Get
If propertiesViewModel Is Nothing Then
propertiesViewModel = New PropertiesViewModel()
End If
Return propertiesViewModel
End Get
Set(ByVal value As PropertiesViewModel)
If propertiesViewModel Is value Then
Return
End If
propertiesViewModel = value
End Set
End Property
Private errorListViewModel As ErrorListViewModel
Public Property _ErrorListViewModel() As ErrorListViewModel
Get
If errorListViewModel Is Nothing Then
errorListViewModel = New ErrorListViewModel()
End If
Return errorListViewModel
End Get
Set(ByVal value As ErrorListViewModel)
If errorListViewModel Is value Then
Return
End If
errorListViewModel = value
End Set
End Property
Private outputViewModel As OutputViewModel
Public Property _OutputViewModel() As OutputViewModel
Get
If outputViewModel Is Nothing Then
outputViewModel = New OutputViewModel()
End If
Return outputViewModel
End Get
Set(ByVal value As OutputViewModel)
If outputViewModel Is value Then
Return
End If
outputViewModel = value
End Set
End Property
Private searchResultsViewModel As SearchResultsViewModel
Public Property _SearchResultsViewModel() As SearchResultsViewModel
Get
If searchResultsViewModel Is Nothing Then
searchResultsViewModel = New SearchResultsViewModel()
End If
Return searchResultsViewModel
End Get
Set(ByVal value As SearchResultsViewModel)
If searchResultsViewModel Is value Then
Return
End If
searchResultsViewModel = value
End Set
End Property
Protected Sub OpenOrCloseWorkspace(ByVal workspace As PanelWorkspaceViewModel, ByVal activateOnOpen As Boolean)
If Workspaces.Contains(workspace) Then
workspace.IsClosed = Not workspace.IsClosed
Else
Me.Workspaces.Add(workspace)
workspace.IsClosed = False
If activateOnOpen Then
Me.SetActiveWorkspace(workspace)
End If
End If
End Sub
Protected Sub OpenOrCloseWorkspace(ByVal workspace As PanelWorkspaceViewModel)
OpenOrCloseWorkspace(workspace, False)
End Sub
Private Sub OnShowToolboxPanel(ByVal param As Object)
OpenOrCloseWorkspace(_ToolboxViewModel)
End Sub
Private Sub OnShowSolutionExplorerPanel(ByVal param As Object)
OpenOrCloseWorkspace(_SolutionExplorerViewModel)
End Sub
Private Sub OnShowPropertiesPanel(ByVal param As Object)
OpenOrCloseWorkspace(_PropertiesViewModel)
End Sub
Private Sub OnShowErrorListPanel(ByVal param As Object)
OpenOrCloseWorkspace(_ErrorListViewModel)
End Sub
Private Sub OnShowOutputPanel(ByVal param As Object)
OpenOrCloseWorkspace(_OutputViewModel)
End Sub
Private Sub OnShowSearchResultsPanel(ByVal param As Object)
OpenOrCloseWorkspace(_SearchResultsViewModel)
End Sub
Protected Overridable Function GetDocumentViewModel() As DocumentViewModel
Return New DocumentViewModel()
End Function
Private Sub OnOpenDocument(ByVal param As Object)
Dim workspace = GetDocumentViewModel()
If (Not workspace.Open()) Then
Return
End If
OpenOrCloseWorkspace(workspace, True)
End Sub
Private Sub SetActiveWorkspace(ByVal workspace As WorkspaceViewModel)
Debug.Assert(Me.Workspaces.Contains(workspace))
workspace.IsActive = True
End Sub
Protected NotInheritable Class GlyphHelper
Private Sub New()
End Sub
Public Shared Function GetGlyph(ByVal path As String) As BitmapImage
Return New BitmapImage(DevExpress.Utils.AssemblyHelper.GetResourceUri(GetType(GlyphHelper).Assembly, path))
End Function
End Class
End Class
End Namespace