Advertisement
PiToLoKo

Undo/Redo framework (c) Copyright 2009 Etienne Nijboer

Oct 13th, 2013
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.81 KB | None | 0 0
  1.  
  2. #Region " Undo-Redo "
  3.  
  4. ' [ Undo-Redo ]
  5. '
  6. ' Supported controls:
  7. '
  8. ' CheckBox
  9. ' ComboBox
  10. ' DateTimePicker
  11. ' ListBox (single and multi-select)
  12. ' MaskedTextBox
  13. ' MonthCalendar
  14. ' NumericUpDown
  15. ' RadioButton
  16. ' TextBox
  17. '
  18. ' Examples:
  19. '
  20. ' Private WithEvents UndoRedoAction As UndoRedoManager
  21. ' UndoRedoAction = New UndoRedoManager(Me)
  22. ' UndoRedoAction.Undo()
  23. ' UndoRedoAction.Redo()
  24.  
  25. #Region " UndoRedoManager "
  26.  
  27. '******************************************************************************************************************
  28. ' Undo/Redo framework (c) Copyright 2009 Etienne Nijboer
  29. '******************************************************************************************************************
  30.  
  31. Public Enum UndoRedoCommandType
  32. ctNone
  33. ctUndo
  34. ctRedo
  35. End Enum
  36.  
  37. Public Class UndoRedoManager
  38.  
  39. #Region "UndoRedoMonitor auto register types"
  40.  
  41. Private Shared RegisteredUndoRedoMonitorTypes As List(Of Type) = Nothing
  42.  
  43. ' ScanAssembly
  44. ' The first created UndoRedoMonitor will scan the assembly for BaseUndoRedoMonitors and
  45. ' store these types in the monitor type list.
  46. '
  47. Private Shared Sub ScanAssembly()
  48. If RegisteredUndoRedoMonitorTypes Is Nothing Then
  49. RegisteredUndoRedoMonitorTypes = New List(Of Type)
  50. Dim AssemblyTypes() As Type = Reflection.Assembly.GetExecutingAssembly().GetTypes()
  51. Dim BaseUndoRedoMonitorType As Type = GetType(BaseUndoRedoMonitor)
  52. For Each typeItem As Type In AssemblyTypes
  53. If typeItem.BaseType Is BaseUndoRedoMonitorType Then
  54. RegisteredUndoRedoMonitorTypes.Add(typeItem)
  55. End If
  56. Next
  57. End If
  58. End Sub
  59.  
  60. #End Region
  61.  
  62. Private Control As Control = Nothing
  63. Private UndoRedoMonitors As List(Of BaseUndoRedoMonitor)
  64. Private ExcludeControls As List(Of Control)
  65.  
  66. ' InitializeUndoRedoMonitors
  67. ' When a new UndoRedoManager instance is created, a new instance of each registered monitor
  68. ' is created and used only within the scope of this UndoRedoManager, preventing temporary data
  69. ' moved to another UndoRedoManager. This is because Each form, or group control like a panel
  70. ' to make seperate undo/redo groups on a single form, can have it's own UndoRedoManager. It is
  71. ' of course also possible to use one global UndoRedoManager for multiple forms. This lets you
  72. ' control how data is seperated or combined, depending on the relation between te undo/redo commands.
  73. Private Sub InitializeUndoRedoMonitors()
  74. ScanAssembly()
  75. UndoRedoMonitors = New List(Of BaseUndoRedoMonitor)
  76. For Each typeItem In RegisteredUndoRedoMonitorTypes
  77. UndoRedoMonitors.Add(Activator.CreateInstance(typeItem, Me))
  78. Next
  79. End Sub
  80.  
  81. Public Sub New()
  82. InitializeUndoRedoMonitors()
  83. End Sub
  84.  
  85. Public Sub New(ByVal AControl As Control)
  86. Me.New(AControl, New List(Of Control))
  87. End Sub
  88.  
  89. Public Sub New(ByVal AControl As Control, ByVal AExcludeControls As List(Of Control))
  90. Me.New()
  91. ExcludeControls = AExcludeControls
  92. MonitorControl(AControl)
  93. End Sub
  94.  
  95. Public Sub New(ByVal AControl As Control, ByVal ParamArray AExcludeControls() As Control)
  96. Me.New(AControl, AExcludeControls.ToList)
  97. End Sub
  98.  
  99. ' MonitorControl
  100. ' If a given control is not in the list of controls to exclude from undo/redo actions,
  101. ' an attempt is made to attach it to a matching UndoRedoMonitor. If no direct match is
  102. ' found, a same attempt is made for each control contained within the control recursively.
  103. Private Sub MonitorControl(ByVal AControl As Control)
  104. If Not ExcludeControls.Contains(AControl) Then
  105. If Not BindMonitor(AControl) Then
  106. For Each ctl As Control In AControl.Controls
  107. MonitorControl(ctl)
  108. Next
  109. End If
  110. End If
  111. End Sub
  112.  
  113. ' BindMonitor
  114. ' An attempt is made to bind the control to a each registered monitor. When a match is
  115. ' found the search ends and the function will return true, false otherwise meaning there
  116. ' is no specific UndoRedoMonitor for this control.
  117. Private Function BindMonitor(ByVal AControl As Control) As Boolean
  118. Dim index As Integer = UndoRedoMonitors.Count - 1, result As Boolean = False
  119. While index >= 0 And Not result
  120. result = UndoRedoMonitors(index).Monitor(AControl)
  121. index -= 1
  122. End While
  123. Return result
  124. End Function
  125.  
  126. Public Sub Monitor(ByVal AControl As Control)
  127. MonitorControl(AControl)
  128. End Sub
  129.  
  130. Private undoStack As Stack(Of BaseUndoRedoCommand) = New Stack(Of BaseUndoRedoCommand)
  131. Private redoStack As Stack(Of BaseUndoRedoCommand) = New Stack(Of BaseUndoRedoCommand)
  132. Private _undoRedoCommand As UndoRedoCommandType = UndoRedoCommandType.ctNone
  133. Private _canUndo As Boolean = False
  134. Private _canRedo As Boolean = False
  135.  
  136. Public Event CanUndoChanged(ByVal Sender As Object, ByVal CanUndo As Boolean)
  137. Public Event CanRedoChanged(ByVal Sender As Object, ByVal CanRedo As Boolean)
  138. Public Event UndoRedoStacksChanged(ByVal Sender As Object)
  139.  
  140. Private Sub UpdateCanUndoRedo()
  141. Dim isCanUndoChanged As Boolean = Not (undoStack.Count > 0) = _canUndo, _
  142. isCanRedoChanged As Boolean = Not (redoStack.Count > 0) = _canRedo
  143. _canUndo = undoStack.Count > 0
  144. _canRedo = redoStack.Count > 0
  145. If isCanUndoChanged Then
  146. RaiseEvent CanUndoChanged(Me, _canUndo)
  147. End If
  148. If isCanRedoChanged Then
  149. RaiseEvent CanRedoChanged(Me, _canRedo)
  150. End If
  151. RaiseEvent UndoRedoStacksChanged(Me)
  152. End Sub
  153.  
  154. Public ReadOnly Property isUndoing() As Boolean
  155. Get
  156. Return _undoRedoCommand = UndoRedoCommandType.ctUndo
  157. End Get
  158. End Property
  159. Public ReadOnly Property isRedoing() As Boolean
  160. Get
  161. Return _undoRedoCommand = UndoRedoCommandType.ctRedo
  162. End Get
  163. End Property
  164. Public ReadOnly Property isPerformingUndoRedo() As Boolean
  165. Get
  166. Return _undoRedoCommand <> UndoRedoCommandType.ctNone
  167. End Get
  168. End Property
  169.  
  170. Public ReadOnly Property CanUndo() As Boolean
  171. Get
  172. Return _canUndo
  173. End Get
  174. End Property
  175.  
  176. Public ReadOnly Property CanRedo() As Boolean
  177. Get
  178. Return _canRedo
  179. End Get
  180. End Property
  181.  
  182. Public Sub AddUndoCommand(ByVal UndoRedoCommand As BaseUndoRedoCommand)
  183. If Not isUndoing Then
  184. undoStack.Push(UndoRedoCommand)
  185. If Not isRedoing Then
  186. redoStack.Clear()
  187. UpdateCanUndoRedo()
  188. End If
  189. End If
  190. End Sub
  191.  
  192. Public Sub AddRedoCommand(ByVal UndoRedoCommand As BaseUndoRedoCommand)
  193. If Not isRedoing Then
  194. redoStack.Push(UndoRedoCommand)
  195. If Not isUndoing Then
  196. UpdateCanUndoRedo()
  197. End If
  198. End If
  199. End Sub
  200.  
  201. Public Sub AddCommand(ByVal UndoRedoCommandType As UndoRedoCommandType, ByVal UndoRedoCommand As BaseUndoRedoCommand)
  202. Select Case UndoRedoCommandType
  203. Case UndoRedoCommandType.ctUndo
  204. AddUndoCommand(UndoRedoCommand)
  205. Case UndoRedoCommandType.ctRedo
  206. AddRedoCommand(UndoRedoCommand)
  207. Case Else
  208. Throw New Exception("An undo or redo command could not be accepted.")
  209. End Select
  210. End Sub
  211.  
  212. Public Sub Undo()
  213. If CanUndo Then
  214. 'Try
  215. _undoRedoCommand = UndoRedoCommandType.ctUndo
  216. undoStack.Pop.Undo()
  217. 'Catch e As Exception
  218. 'Finally
  219. UpdateCanUndoRedo()
  220. _undoRedoCommand = UndoRedoCommandType.ctNone
  221. 'End Try
  222. End If
  223. End Sub
  224.  
  225. Public Sub Redo()
  226. If CanRedo Then
  227. _undoRedoCommand = UndoRedoCommandType.ctRedo
  228. redoStack.Pop.Redo()
  229. UpdateCanUndoRedo()
  230. _undoRedoCommand = UndoRedoCommandType.ctNone
  231. End If
  232. End Sub
  233.  
  234. Protected Overrides Sub Finalize()
  235. MyBase.Finalize()
  236. End Sub
  237.  
  238.  
  239. #Region "debug info"
  240.  
  241. Public Shared Function ArrayToString(ByVal ObjectArray() As Object) As String
  242. Dim sb As New System.Text.StringBuilder
  243. For Each item As Object In ObjectArray
  244. sb.AppendLine(item.ToString)
  245. Next
  246. Return sb.ToString
  247. End Function
  248.  
  249.  
  250. Public Function GetUndoStack() As String
  251. Return ArrayToString(undoStack.ToArray)
  252. End Function
  253.  
  254. Public Function GetRedoStack() As String
  255. Return ArrayToString(redoStack.ToArray)
  256. End Function
  257.  
  258. Public Function GetRegisteredUndoRedoMonitorTypes() As String
  259. Return ArrayToString(RegisteredUndoRedoMonitorTypes.ToArray)
  260. End Function
  261.  
  262. #End Region
  263.  
  264. End Class
  265.  
  266. #End Region
  267.  
  268. #Region " BaseUndoRedoMonitor "
  269.  
  270. Public MustInherit Class BaseUndoRedoMonitor
  271.  
  272. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  273. _UndoRedoManager = AUndoRedoManager
  274. End Sub
  275.  
  276. Private _UndoRedoManager As UndoRedoManager
  277. Public Property UndoRedoManager() As UndoRedoManager
  278. Get
  279. Return _UndoRedoManager
  280. End Get
  281. Set(ByVal value As UndoRedoManager)
  282. _UndoRedoManager = value
  283. End Set
  284. End Property
  285.  
  286. Public ReadOnly Property isUndoing() As Boolean
  287. Get
  288. Return UndoRedoManager.isUndoing
  289. End Get
  290. End Property
  291. Public ReadOnly Property isRedoing() As Boolean
  292. Get
  293. Return UndoRedoManager.isRedoing
  294. End Get
  295. End Property
  296.  
  297. Public ReadOnly Property isPerformingUndoRedo() As Boolean
  298. Get
  299. Return UndoRedoManager.isPerformingUndoRedo
  300. End Get
  301. End Property
  302.  
  303. Public Sub AddCommand(ByVal UndoRedoCommandType As UndoRedoCommandType, ByVal UndoRedoCommand As BaseUndoRedoCommand)
  304. UndoRedoManager.AddCommand(UndoRedoCommandType, UndoRedoCommand)
  305. End Sub
  306.  
  307. Public MustOverride Function Monitor(ByVal AControl As Control) As Boolean
  308.  
  309. End Class
  310.  
  311. '**************************************************************************
  312. ' SimpleControl
  313. ' Controls: TextBox, ComboBox, DateTimePicker, NumericUpDown, MaskedTextBox
  314. '**************************************************************************
  315. Public Class SimpleControlMonitor : Inherits BaseUndoRedoMonitor
  316.  
  317. Private Data As String
  318.  
  319. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  320. MyBase.New(AUndoRedoManager)
  321. End Sub
  322.  
  323. Public Overrides Function Monitor(ByVal AControl As System.Windows.Forms.Control) As Boolean
  324. If TypeOf AControl Is TextBox Or _
  325. TypeOf AControl Is ComboBox Or _
  326. TypeOf AControl Is DateTimePicker Or _
  327. TypeOf AControl Is NumericUpDown Or _
  328. TypeOf AControl Is ListView Or _
  329. TypeOf AControl Is MaskedTextBox Then
  330. AddHandler AControl.Enter, AddressOf Control_Enter
  331. AddHandler AControl.Leave, AddressOf Control_Leave
  332. Return True
  333. End If
  334. Return False
  335. End Function
  336.  
  337. Private Sub Control_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  338. Data = CType(sender, Control).Text
  339. End Sub
  340.  
  341. Private Sub Control_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
  342. Dim CurrentData As String = CType(sender, Control).Text
  343. If Not String.Equals(CurrentData, Data) Then
  344. AddCommand(UndoRedoCommandType.ctUndo, New SimpleControlUndoRedoCommand(Me, sender, Data))
  345. End If
  346. End Sub
  347. End Class
  348.  
  349. '********
  350. ' ListBox
  351. '********
  352. Public Class ListBoxMonitor : Inherits BaseUndoRedoMonitor
  353.  
  354. Private Data As Object
  355.  
  356. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  357. MyBase.New(AUndoRedoManager)
  358. End Sub
  359.  
  360. Public Overrides Function Monitor(ByVal AControl As System.Windows.Forms.Control) As Boolean
  361. If TypeOf AControl Is ListBox Then
  362. AddHandler AControl.Enter, AddressOf Control_Enter
  363. AddHandler CType(AControl, ListBox).SelectedIndexChanged, AddressOf Control_Changed
  364. Return True
  365. End If
  366. Return False
  367. End Function
  368.  
  369. Public Function GetSelected(ByVal AListBox As Object) As String
  370. Dim Indices As List(Of String) = New List(Of String)
  371. For Each itemIndex As Integer In CType(AListBox, ListBox).SelectedIndices
  372. Indices.Add(CStr(itemIndex + 1))
  373. Next
  374. Return String.Join(",", Indices.ToArray)
  375. End Function
  376.  
  377. Public Sub RestoreSelected(ByVal AListBox As Object, ByVal ASelection As String)
  378. If Not String.IsNullOrEmpty(ASelection) Then
  379. Dim Indices As List(Of Integer) = New List(Of Integer)(Array.ConvertAll(ASelection.Split(","), New Converter(Of String, Integer)(AddressOf Integer.Parse)))
  380. Dim Control As ListBox = CType(AListBox, ListBox)
  381. Select Case Control.SelectionMode
  382. Case SelectionMode.None
  383. Case SelectionMode.One
  384. Control.SetSelected(Indices(0) - 1, True)
  385. Case SelectionMode.MultiSimple, SelectionMode.MultiExtended
  386. For index As Integer = 0 To Control.Items.Count - 1
  387. Control.SetSelected(index, Indices.IndexOf(index + 1) >= 0)
  388. Next
  389. End Select
  390. Else
  391. CType(AListBox, ListBox).ClearSelected()
  392. End If
  393. End Sub
  394.  
  395. Private Sub Control_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
  396. ' Events that are also fired when the undo/redo value is changed by code, like change events,
  397. ' it is important to make sure that no undo/redo command is added when performing a undo/redo action.
  398. If Not isPerformingUndoRedo Then
  399. Dim CurrentData As String = GetSelected(sender)
  400. If Not String.Equals(Data, CurrentData) Then
  401. AddCommand(UndoRedoCommandType.ctUndo, New ListBoxUndoRedoCommand(Me, sender, Data))
  402. Data = CurrentData
  403. End If
  404. End If
  405. End Sub
  406.  
  407. Private Sub Control_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  408. Data = GetSelected(sender)
  409. End Sub
  410.  
  411. End Class
  412.  
  413.  
  414. '*********
  415. ' CheckBox
  416. '*********
  417. Public Class CheckBoxMonitor : Inherits BaseUndoRedoMonitor
  418. Private Data As CheckState
  419.  
  420. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  421. MyBase.New(AUndoRedoManager)
  422. End Sub
  423.  
  424. Public Overrides Function Monitor(ByVal AControl As System.Windows.Forms.Control) As Boolean
  425. If TypeOf AControl Is CheckBox Then
  426. AddHandler AControl.Enter, AddressOf Control_Enter
  427. AddHandler AControl.Leave, AddressOf Control_Leave
  428. Return True
  429. End If
  430. Return False
  431. End Function
  432.  
  433. Private Sub Control_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  434. Data = CType(sender, CheckBox).CheckState
  435. End Sub
  436.  
  437. Private Sub Control_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
  438. Dim CurrentData As CheckState = CType(sender, CheckBox).CheckState
  439. If Data <> CurrentData Then
  440. AddCommand(UndoRedoCommandType.ctUndo, New CheckBoxUndoRedoCommand(Me, sender, Data))
  441. End If
  442. End Sub
  443. End Class
  444.  
  445. '************
  446. ' RadioButton
  447. '************
  448. Public Class RadioButtonMonitor : Inherits BaseUndoRedoMonitor
  449. Private Data As RadioButton
  450.  
  451. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  452. MyBase.New(AUndoRedoManager)
  453. End Sub
  454.  
  455. Public Overrides Function Monitor(ByVal AControl As System.Windows.Forms.Control) As Boolean
  456. If TypeOf AControl Is RadioButton Then
  457. AddHandler CType(AControl, RadioButton).CheckedChanged, AddressOf Control_CheckedChanged
  458. Return True
  459. End If
  460. Return False
  461. End Function
  462.  
  463. Private Sub Control_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  464. ' Events that are also fired when the undo/redo value is changed by code, like change events,
  465. ' it is important to make sure that no undo/redo command is added when performing a undo/redo action.
  466. If Not isPerformingUndoRedo Then
  467. If CType(sender, RadioButton).Checked Then
  468. AddCommand(UndoRedoCommandType.ctUndo, New RadioButtonUndoRedoCommand(Me, sender, Data))
  469. Else
  470. Data = sender
  471. End If
  472. End If
  473. End Sub
  474. End Class
  475.  
  476. '**************
  477. ' MonthCalendar
  478. '**************
  479. Public Class MonthCalendarMonitor : Inherits BaseUndoRedoMonitor
  480. Private Data As SelectionRange
  481.  
  482. Public Sub New(ByVal AUndoRedoManager As UndoRedoManager)
  483. MyBase.New(AUndoRedoManager)
  484. End Sub
  485.  
  486. Public Overrides Function Monitor(ByVal AControl As System.Windows.Forms.Control) As Boolean
  487. If TypeOf AControl Is MonthCalendar Then
  488. AddHandler AControl.Enter, AddressOf Control_Enter
  489. AddHandler CType(AControl, MonthCalendar).DateSelected, AddressOf Control_DateSelected
  490. Return True
  491. End If
  492. Return False
  493. End Function
  494.  
  495. Private Sub Control_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  496. Data = CType(sender, MonthCalendar).SelectionRange
  497. End Sub
  498.  
  499. Private Sub Control_DateSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
  500. ' Events that are also fired when the undo/redo value is changed by code, like selected events,
  501. ' it is important to make sure that no undo/redo command is added when performing a undo/redo action.
  502. If Not isPerformingUndoRedo Then
  503. Dim CurrentData As SelectionRange = CType(sender, MonthCalendar).SelectionRange
  504. If Not SelectionRange.Equals(Data, CurrentData) Then
  505. AddCommand(UndoRedoCommandType.ctUndo, New MonthCalendarUndoRedoCommand(Me, sender, Data))
  506. Data = CurrentData
  507. End If
  508. End If
  509. End Sub
  510.  
  511. End Class
  512.  
  513. #End Region
  514.  
  515. #Region " BaseUndoRedoCommand "
  516.  
  517. Public MustInherit Class BaseUndoRedoCommand
  518.  
  519. Private _UndoRedoMonitor As BaseUndoRedoMonitor
  520. Private _UndoRedoControl As Control
  521. Private _UndoRedoData As Object
  522.  
  523. Public ReadOnly Property UndoRedoMonitor() As BaseUndoRedoMonitor
  524. Get
  525. Return _UndoRedoMonitor
  526. End Get
  527. End Property
  528.  
  529. Public ReadOnly Property UndoRedoControl() As Control
  530. Get
  531. Return _UndoRedoControl
  532. End Get
  533. End Property
  534.  
  535. Protected Property UndoRedoData() As Object
  536. Get
  537. Return _UndoRedoData
  538. End Get
  539. Set(ByVal value As Object)
  540. _UndoRedoData = value
  541. End Set
  542. End Property
  543.  
  544. Protected Sub New()
  545. Throw New Exception("Cannot create instance with the default constructor.")
  546. End Sub
  547.  
  548. Public Sub New(ByVal AUndoRedoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  549. Me.New(AUndoRedoMonitor, AMonitorControl, Nothing)
  550. End Sub
  551.  
  552. Public Sub New(ByVal AUndoRedoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As Object)
  553. _UndoRedoMonitor = AUndoRedoMonitor
  554. _UndoRedoControl = AMonitorControl
  555. _UndoRedoData = AUndoRedoData
  556. End Sub
  557.  
  558. Protected Sub AddCommand(ByVal UndoRedoCommandType As UndoRedoCommandType, ByVal UndoRedoCommand As BaseUndoRedoCommand)
  559. UndoRedoMonitor.AddCommand(UndoRedoCommandType, UndoRedoCommand)
  560. End Sub
  561.  
  562. Public Overridable Sub Undo()
  563. AddCommand(UndoRedoCommandType.ctRedo, Activator.CreateInstance(Me.GetType, UndoRedoMonitor, UndoRedoControl))
  564. End Sub
  565.  
  566. Public Overridable Sub Redo()
  567. AddCommand(UndoRedoCommandType.ctUndo, Activator.CreateInstance(Me.GetType, UndoRedoMonitor, UndoRedoControl))
  568. End Sub
  569.  
  570. Public Overridable Sub Undo(ByVal RedoData As Object)
  571. AddCommand(UndoRedoCommandType.ctRedo, Activator.CreateInstance(Me.GetType, UndoRedoMonitor, UndoRedoControl, RedoData))
  572. End Sub
  573.  
  574. Public Overridable Sub Redo(ByVal UndoData As Object)
  575. AddCommand(UndoRedoCommandType.ctUndo, Activator.CreateInstance(Me.GetType, UndoRedoMonitor, UndoRedoControl, UndoData))
  576. End Sub
  577.  
  578. Public MustOverride Function CommandAsText() As String
  579.  
  580. Public Overrides Function ToString() As String
  581. Return CommandAsText()
  582. End Function
  583.  
  584. End Class
  585.  
  586. '**************************************************************************
  587. ' SimpleControl
  588. ' Controls: TextBox, ComboBox, DateTimePicker, NumericUpDown, MaskedTextBox
  589. '**************************************************************************
  590. Public Class SimpleControlUndoRedoCommand : Inherits BaseUndoRedoCommand
  591.  
  592. Protected ReadOnly Property UndoRedoText() As String
  593. Get
  594. Return CStr(UndoRedoData)
  595. End Get
  596. End Property
  597.  
  598. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  599. MyBase.New(AUndoMonitor, AMonitorControl)
  600. UndoRedoData = UndoRedoControl.Text
  601. End Sub
  602.  
  603. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As String)
  604. MyBase.New(AUndoMonitor, AMonitorControl, AUndoRedoData)
  605. End Sub
  606.  
  607. Public Overrides Sub Undo()
  608. MyBase.Undo()
  609. UndoRedoControl.Text = UndoRedoText
  610. End Sub
  611.  
  612. Public Overrides Sub Redo()
  613. MyBase.Redo()
  614. UndoRedoControl.Text = UndoRedoText
  615. End Sub
  616.  
  617. Public Overrides Function CommandAsText() As String
  618. Return String.Format("Change to '{0}'", UndoRedoText)
  619. End Function
  620.  
  621. End Class
  622.  
  623. '********
  624. ' ListBox
  625. '********
  626. Public Class ListBoxUndoRedoCommand : Inherits BaseUndoRedoCommand
  627.  
  628. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  629. MyBase.New(AUndoMonitor, AMonitorControl)
  630. UndoRedoData = GetSelection()
  631. End Sub
  632.  
  633. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As Object)
  634. MyBase.New(AUndoMonitor, AMonitorControl, AUndoRedoData)
  635. End Sub
  636.  
  637. Public ReadOnly Property Control() As ListBox
  638. Get
  639. Return CType(UndoRedoControl, ListBox)
  640. End Get
  641. End Property
  642.  
  643. Private Sub RestoreSelection()
  644. CType(UndoRedoMonitor, ListBoxMonitor).RestoreSelected(UndoRedoControl, CStr(UndoRedoData))
  645. End Sub
  646.  
  647. Private Function GetSelection() As Object
  648. Return CType(UndoRedoMonitor, ListBoxMonitor).GetSelected(UndoRedoControl)
  649. End Function
  650.  
  651. Public Overrides Sub Undo()
  652. MyBase.Undo()
  653. RestoreSelection()
  654. End Sub
  655.  
  656. Public Overrides Sub Redo()
  657. MyBase.Redo()
  658. RestoreSelection()
  659. End Sub
  660.  
  661. Public Overrides Function CommandAsText() As String
  662. Return String.Format("Select {0}", CStr(UndoRedoData))
  663. End Function
  664. End Class
  665.  
  666.  
  667. '*********
  668. ' CheckBox
  669. '*********
  670. Public Class CheckBoxUndoRedoCommand : Inherits BaseUndoRedoCommand
  671.  
  672. Protected ReadOnly Property UndoRedoCheckState() As CheckState
  673. Get
  674. Return CType(UndoRedoData, CheckState)
  675. End Get
  676. End Property
  677.  
  678. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  679. MyBase.New(AUndoMonitor, AMonitorControl)
  680. UndoRedoData = Control.CheckState
  681. End Sub
  682.  
  683. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As String)
  684. MyBase.New(AUndoMonitor, AMonitorControl, AUndoRedoData)
  685. End Sub
  686.  
  687. Public ReadOnly Property Control() As CheckBox
  688. Get
  689. Return CType(UndoRedoControl, CheckBox)
  690. End Get
  691. End Property
  692.  
  693. Public Overrides Sub Undo()
  694. MyBase.Undo()
  695. Control.CheckState = UndoRedoCheckState
  696. End Sub
  697.  
  698. Public Overrides Sub Redo()
  699. MyBase.Redo()
  700. Control.CheckState = UndoRedoCheckState
  701. End Sub
  702.  
  703. Public Overrides Function CommandAsText() As String
  704. Return String.Format("Change to '{0}'", UndoRedoCheckState.ToString)
  705. End Function
  706.  
  707. End Class
  708.  
  709. '************
  710. ' RadioButton
  711. '************
  712. Public Class RadioButtonUndoRedoCommand : Inherits BaseUndoRedoCommand
  713.  
  714. Protected ReadOnly Property UndoRedoRadioButton() As RadioButton
  715. Get
  716. Return CType(UndoRedoData, RadioButton)
  717. End Get
  718. End Property
  719.  
  720. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  721. MyBase.New(AUndoMonitor, AMonitorControl)
  722. UndoRedoData = Control.Checked
  723. End Sub
  724.  
  725. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As Control)
  726. MyBase.New(AUndoMonitor, AMonitorControl, AUndoRedoData)
  727. End Sub
  728.  
  729. Public ReadOnly Property Control() As RadioButton
  730. Get
  731. Return CType(UndoRedoControl, RadioButton)
  732. End Get
  733. End Property
  734.  
  735. Public Overrides Sub Undo()
  736. MyBase.Undo(UndoRedoRadioButton)
  737. Control.Checked = False
  738. If UndoRedoRadioButton IsNot Nothing Then
  739. UndoRedoRadioButton.Checked = True
  740. End If
  741. End Sub
  742.  
  743. Public Overrides Sub Redo()
  744. MyBase.Redo(UndoRedoRadioButton)
  745. If UndoRedoRadioButton IsNot Nothing Then
  746. UndoRedoRadioButton.Checked = False
  747. End If
  748. Control.Checked = True
  749. End Sub
  750.  
  751. Public Overrides Function CommandAsText() As String
  752. If UndoRedoRadioButton IsNot Nothing Then
  753. Return String.Format("Invert '{0}'/'{1}'", Control.Text, UndoRedoRadioButton.Text)
  754. Else
  755. Return String.Format("Change '{0}'", Control.Text)
  756. End If
  757. End Function
  758.  
  759. End Class
  760.  
  761.  
  762. '**************
  763. ' MonthCalendar
  764. '**************
  765. Public Class MonthCalendarUndoRedoCommand : Inherits BaseUndoRedoCommand
  766.  
  767. Protected ReadOnly Property UndoRedoSelectionRange() As SelectionRange
  768. Get
  769. Return CType(UndoRedoData, SelectionRange)
  770. End Get
  771. End Property
  772.  
  773. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control)
  774. MyBase.New(AUndoMonitor, AMonitorControl)
  775. UndoRedoData = Control.SelectionRange
  776. End Sub
  777.  
  778. Public Sub New(ByVal AUndoMonitor As BaseUndoRedoMonitor, ByVal AMonitorControl As Control, ByVal AUndoRedoData As SelectionRange)
  779. MyBase.New(AUndoMonitor, AMonitorControl, AUndoRedoData)
  780. End Sub
  781.  
  782. Public ReadOnly Property Control() As MonthCalendar
  783. Get
  784. Return CType(UndoRedoControl, MonthCalendar)
  785. End Get
  786. End Property
  787.  
  788. Public Overrides Sub Undo()
  789. MyBase.Undo()
  790. Control.SelectionRange = UndoRedoSelectionRange
  791. End Sub
  792.  
  793. Public Overrides Sub Redo()
  794. MyBase.Redo()
  795. Control.SelectionRange = UndoRedoSelectionRange
  796. End Sub
  797.  
  798. Public Overrides Function CommandAsText() As String
  799. If Date.Equals(UndoRedoSelectionRange.Start, UndoRedoSelectionRange.End) Then
  800. Return String.Format("Select date {0}", FormatDateTime(UndoRedoSelectionRange.Start, DateFormat.ShortDate))
  801. Else
  802. End If
  803. Return String.Format("Change to '{0}'", String.Format("{0} until {1}", FormatDateTime(UndoRedoSelectionRange.Start, DateFormat.ShortDate), _
  804. FormatDateTime(UndoRedoSelectionRange.End, DateFormat.ShortDate)))
  805. End Function
  806.  
  807. End Class
  808.  
  809. #End Region
  810.  
  811. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement