-
Notifications
You must be signed in to change notification settings - Fork 1
/
UndoManager.go
529 lines (513 loc) · 19.9 KB
/
UndoManager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright 2017 Paul C. Brown. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"errors"
"log"
"sync"
)
type undoManager struct {
sync.Mutex
debugUndo bool
recordingUndo bool
redoStack undoStack
undoStack undoStack
uOfD *UniverseOfDiscourse
}
// NewUndoManager creates and initializes the manager for the undo/redo functionality
func newUndoManager(uOfD *UniverseOfDiscourse) *undoManager {
var undoMgr undoManager
undoMgr.debugUndo = false
undoMgr.recordingUndo = false
undoMgr.uOfD = uOfD
return &undoMgr
}
// markChangedElement() If undo is enabled, updates the undo stack.
func (undoMgr *undoManager) markChangedElement(changedElement Concept, trans *Transaction) error {
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
trans.ReadLockElement(changedElement)
priorState := clone(changedElement, trans)
priorOwnedElements := undoMgr.uOfD.ownedIDsMap.GetMappedValues(changedElement.GetConceptID(trans)).Clone()
priorListeners := undoMgr.uOfD.listenersMap.GetMappedValues(changedElement.GetConceptID(trans)).Clone()
priorUofD := ""
if changedElement.getUniverseOfDiscourseNoLock() != nil {
priorUofD = changedElement.getUniverseOfDiscourseNoLock().id
}
if undoMgr.recordingUndo {
stackEntry := newUndoRedoStackEntry(Change, priorState, priorOwnedElements, priorListeners, priorUofD, changedElement)
if undoMgr.debugUndo {
PrintStackEntry(stackEntry, trans)
}
undoMgr.undoStack.Push(stackEntry)
}
return nil
}
// markNewElement() If undo is enabled, updates the undo stack.
func (undoMgr *undoManager) markNewElement(el Concept, trans *Transaction) error {
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
if trans == nil {
return errors.New("UndoManager.markNewElement called with nil HeldLocks")
}
trans.ReadLockElement(el)
if undoMgr.recordingUndo {
clone := clone(el, trans)
priorOwnedElements := undoMgr.uOfD.ownedIDsMap.GetMappedValues(el.GetConceptID(trans)).Clone()
priorListeners := undoMgr.uOfD.listenersMap.GetMappedValues(el.GetConceptID(trans)).Clone()
priorUofD := ""
if el.getUniverseOfDiscourseNoLock() != nil {
priorUofD = el.getUniverseOfDiscourseNoLock().id
}
stackEntry := newUndoRedoStackEntry(Creation, clone, priorOwnedElements, priorListeners, priorUofD, el)
if undoMgr.debugUndo {
PrintStackEntry(stackEntry, trans)
}
undoMgr.undoStack.Push(stackEntry)
}
return nil
}
// markRemoveElement() If undo is enabled, updates the undo stack.
func (undoMgr *undoManager) markRemovedElement(el Concept, trans *Transaction) error {
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
if trans == nil {
return errors.New("UndoManager.markRemovedElement called with nil HeldLocks")
}
trans.ReadLockElement(el)
if undoMgr.recordingUndo {
clone := clone(el, trans)
priorOwnedElements := undoMgr.uOfD.ownedIDsMap.GetMappedValues(el.GetConceptID(trans)).Clone()
priorListeners := undoMgr.uOfD.listenersMap.GetMappedValues(el.GetConceptID(trans)).Clone()
priorUofD := ""
if el.getUniverseOfDiscourseNoLock() != nil {
priorUofD = el.getUniverseOfDiscourseNoLock().id
}
stackEntry := newUndoRedoStackEntry(Deletion, clone, priorOwnedElements, priorListeners, priorUofD, el)
if undoMgr.debugUndo {
PrintStackEntry(stackEntry, trans)
}
undoMgr.undoStack.Push(stackEntry)
}
return nil
}
// MarkUndoPoint() If undo is enabled, puts a marker on the undo stack.
func (undoMgr *undoManager) MarkUndoPoint() {
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
if undoMgr.recordingUndo {
if undoMgr.debugUndo {
log.Print("***** MARK UNDO POINT *****")
}
undoMgr.undoStack.Push(newUndoRedoStackEntry(Marker, nil, nil, nil, "", nil))
}
}
// PrintUndoStack prints the undo stack. It is intended only for debugging.
func PrintUndoStack(s undoStack, stackName string, uOfD *UniverseOfDiscourse) {
trans := uOfD.NewTransaction()
defer trans.ReleaseLocks()
log.Printf("%s:", stackName)
for _, entry := range s {
var changeType string
switch entry.changeType {
case Creation:
{
changeType = "Creation"
}
case Deletion:
{
changeType = "Deletion"
}
case Change:
{
changeType = "Change"
}
case Marker:
{
changeType = "Marker"
}
}
log.Printf("Change type: %s", changeType)
log.Printf(" Prior state:")
Print(entry.priorState, " ", trans)
log.Printf(" Changed element:")
log.Printf(" Prior UofD: %s", entry.priorUofD)
Print(entry.changedElement, " ", trans)
}
}
// PrintStackEntry prints the entry on the stack. It is intended for use only in debugging
func PrintStackEntry(entry *undoRedoStackEntry, trans *Transaction) {
var changeType string
switch entry.changeType {
case Creation:
{
changeType = "Creation"
}
case Deletion:
{
changeType = "Deletion"
}
case Change:
{
changeType = "Change"
}
case Marker:
{
changeType = "Marker"
}
}
log.Printf("Change type: %s", changeType)
log.Printf(" Prior state:")
Print(entry.priorState, " ", trans)
log.Printf(" Changed element:")
Print(entry.changedElement, " ", trans)
log.Printf(" Prior UofD: %s", entry.priorUofD)
log.Printf(" Undo/Redo Stack Entry priorOwnedElements: %v", trans.uOfD.ownedIDsMap.GetMappedValues(entry.changedElement.getConceptIDNoLock()))
log.Printf(" Prior Listeners: %v", entry.priorListeners.ToSlice())
}
func (undoMgr *undoManager) redo(trans *Transaction) {
if undoMgr.debugUndo {
log.Print("***** BEGIN REDO ****")
}
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
uOfD := undoMgr.uOfD
for len(undoMgr.redoStack) > 0 {
currentEntry := undoMgr.redoStack.Pop()
var currentID string
if currentEntry.changedElement != nil {
currentID = currentEntry.changedElement.GetConceptID(trans)
}
if currentEntry.changeType == Marker {
undoMgr.undoStack.Push(currentEntry)
return
}
if currentEntry.changeType == Creation {
// Update listeners. If this is a reference or refinement pointing to another element, add this element to the other element's listener's set
currentOwnerID := currentEntry.changedElement.(*concept).OwningConceptID
if currentOwnerID != "" {
uOfD.addMappedValueToOwnedIDsMap(currentOwnerID, currentID)
}
switch currentEntry.changedElement.GetConceptType() {
case Reference:
referencedElementID := currentEntry.changedElement.(*concept).ReferencedConceptID
if referencedElementID != "" {
uOfD.addMappedValueToListenersMap(referencedElementID, currentID)
}
case Refinement:
abstractID := currentEntry.changedElement.(*concept).AbstractConceptID
if abstractID != "" {
uOfD.addMappedValueToListenersMap(abstractID, currentID)
}
refinedID := currentEntry.changedElement.(*concept).RefinedConceptID
if refinedID != "" {
uOfD.addMappedValueToListenersMap(refinedID, currentID)
}
}
// Update the uriUUIDMap
uri := currentEntry.changedElement.GetURI(trans)
if uri != "" {
uOfD.uriUUIDMap.SetEntry(uri, currentID)
}
undoMgr.undoStack.Push(currentEntry)
undoMgr.restoreState(currentEntry.priorState, currentEntry.changedElement, trans)
// this was a new element
uOfD.addElementForUndo(currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
} else if currentEntry.changeType == Deletion {
// Update listeners. If this is a reference or refinement pointing to another element, remove this element from the other element's listener's set
priorOwnerID := currentEntry.priorState.(*concept).OwningConceptID
if priorOwnerID != "" {
uOfD.removeMappedValueFromOwnedIDsMap(priorOwnerID, currentEntry.priorState.(*concept).ConceptID)
}
switch currentEntry.priorState.GetConceptType() {
case Reference:
referencedElementID := currentEntry.priorState.(*concept).ReferencedConceptID
if referencedElementID != "" {
uOfD.removeMappedValueFromListenersMap(referencedElementID, currentEntry.priorState.GetConceptID(trans))
}
case Refinement:
abstractID := currentEntry.priorState.(*concept).AbstractConceptID
if abstractID != "" {
uOfD.removeMappedValueFromListenersMap(abstractID, currentEntry.priorState.GetConceptID(trans))
}
refinedID := currentEntry.priorState.(*concept).RefinedConceptID
if refinedID != "" {
uOfD.removeMappedValueFromListenersMap(refinedID, currentEntry.priorState.GetConceptID(trans))
}
}
// Update the uriUUIDMap
uri := currentEntry.priorState.GetURI(trans)
if uri != "" {
uOfD.uriUUIDMap.DeleteEntry(uri)
}
undoMgr.undoStack.Push(currentEntry)
undoMgr.restoreState(currentEntry.priorState, currentEntry.changedElement, trans)
// this was an deleted element
uOfD.removeElementForUndo(currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
} else if currentEntry.changeType == Change {
// Update listeners. If this is a reference or refinement pointing to another element, remove this element from the other element's listener's set
currentOwnerID := currentEntry.changedElement.(*concept).OwningConceptID
if currentOwnerID != "" {
uOfD.removeMappedValueFromOwnedIDsMap(currentOwnerID, currentID)
}
priorOwnerID := currentEntry.priorState.(*concept).OwningConceptID
if priorOwnerID != "" {
uOfD.addMappedValueToOwnedIDsMap(priorOwnerID, currentEntry.priorState.(*concept).ConceptID)
}
switch currentEntry.changedElement.GetConceptType() {
case Reference:
currentReferencedElementID := currentEntry.changedElement.(*concept).ReferencedConceptID
priorReferencedElementID := currentEntry.priorState.(*concept).ReferencedConceptID
if currentReferencedElementID != priorReferencedElementID {
if currentReferencedElementID != "" {
uOfD.removeMappedValueFromListenersMap(currentReferencedElementID, currentID)
}
if priorReferencedElementID != "" {
uOfD.addMappedValueToListenersMap(priorReferencedElementID, currentID)
}
}
case Refinement:
currentAbstractID := currentEntry.changedElement.(*concept).AbstractConceptID
priorAbstractID := currentEntry.priorState.(*concept).AbstractConceptID
if currentAbstractID != priorAbstractID {
if currentAbstractID != "" {
uOfD.removeMappedValueFromListenersMap(currentAbstractID, currentID)
}
if priorAbstractID != "" {
uOfD.addMappedValueToListenersMap(priorAbstractID, currentID)
}
}
currentRefinedID := currentEntry.changedElement.(*concept).RefinedConceptID
priorRefinedID := currentEntry.priorState.(*concept).RefinedConceptID
if currentRefinedID != priorRefinedID {
if currentRefinedID != "" {
uOfD.removeMappedValueFromListenersMap(currentRefinedID, currentID)
}
if priorRefinedID != "" {
uOfD.addMappedValueToListenersMap(priorRefinedID, currentID)
}
}
}
// Update the uriUUIDMap
currentURI := currentEntry.changedElement.GetURI(trans)
priorURI := currentEntry.priorState.GetURI(trans)
if currentURI != priorURI {
if currentURI != "" {
uOfD.uriUUIDMap.DeleteEntry(currentURI)
}
if priorURI != "" {
uOfD.uriUUIDMap.SetEntry(priorURI, currentID)
}
}
clone := clone(currentEntry.changedElement, trans)
priorOwnedElements := undoMgr.uOfD.ownedIDsMap.GetMappedValues(currentID).Clone()
priorListeners := undoMgr.uOfD.listenersMap.GetMappedValues(currentID).Clone()
undoEntry := newUndoRedoStackEntry(Change, clone, priorOwnedElements, priorListeners, currentEntry.priorUofD, currentEntry.changedElement)
undoMgr.restoreState(currentEntry.priorState, currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
if currentEntry.priorUofD != uOfD.id {
uOfD.deleteUUIDElementMapEntry(currentID)
}
undoMgr.undoStack.Push(undoEntry)
}
}
}
// restoreState is used as part of the undo process. It changes the currentState object
// to have the priorState.
func (undoMgr *undoManager) restoreState(priorState Concept, currentState Concept, trans *Transaction) {
if undoMgr.debugUndo {
log.Printf("Restoring State")
log.Printf(" Current state:")
Print(currentState, " ", trans)
log.Printf(" Prior state")
Print(priorState, " ", trans)
}
switch currentState.(type) {
case *concept:
currentState.(*concept).cloneAttributes(priorState.(*concept), trans)
default:
log.Printf("restoreState called with unhandled type %T\n", currentState)
}
}
func (undoMgr *undoManager) setRecordingUndo(newSetting bool) {
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
undoMgr.recordingUndo = newSetting
}
func (undoMgr *undoManager) TraceableLock() {
// if TraceLocks {
// log.Printf("About to lock Undo Manager %p\n", undoMgr)
// }
undoMgr.Lock()
}
func (undoMgr *undoManager) TraceableUnlock() {
// if TraceLocks {
// log.Printf("About to unlock Undo Manager %p\n", undoMgr)
// }
undoMgr.Unlock()
}
func (undoMgr *undoManager) undo(trans *Transaction) {
if undoMgr.debugUndo {
log.Print("***** BEGIN UNDO ****")
}
undoMgr.TraceableLock()
defer undoMgr.TraceableUnlock()
uOfD := undoMgr.uOfD
firstEntry := true
for len(undoMgr.undoStack) > 0 {
currentEntry := undoMgr.undoStack.Pop()
var currentID string
if currentEntry.changedElement != nil {
currentID = currentEntry.changedElement.GetConceptID(trans)
}
if currentEntry.changeType == Marker {
if firstEntry {
undoMgr.redoStack.Push(currentEntry)
} else {
// Put it back on the undo stack
undoMgr.undoStack.Push(currentEntry)
return
}
} else {
if currentEntry.changeType == Creation {
// Update listeners. If this is a reference or refinement pointing to another element, remove this element from the other element's listener's set
currentOwnerID := currentEntry.changedElement.(*concept).OwningConceptID
if currentOwnerID != "" {
uOfD.removeMappedValueFromOwnedIDsMap(currentOwnerID, currentID)
}
switch currentEntry.changedElement.GetConceptType() {
case Reference:
referencedElementID := currentEntry.changedElement.(*concept).ReferencedConceptID
if referencedElementID != "" {
uOfD.removeMappedValueFromListenersMap(referencedElementID, currentID)
}
case Refinement:
abstractID := currentEntry.changedElement.(*concept).AbstractConceptID
if abstractID != "" {
uOfD.removeMappedValueFromListenersMap(abstractID, currentID)
}
refinedID := currentEntry.changedElement.(*concept).RefinedConceptID
if refinedID != "" {
uOfD.removeMappedValueFromListenersMap(refinedID, currentID)
}
}
// Update the uriUUIDMap
uri := currentEntry.changedElement.GetURI(trans)
if uri != "" {
uOfD.uriUUIDMap.DeleteEntry(uri)
}
undoMgr.redoStack.Push(currentEntry)
uOfD.removeElementForUndo(currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
} else if currentEntry.changeType == Deletion {
// Update listeners. If this is a reference or refinement pointing to another element, add this element to the other element's listener's set
priorOwnerID := currentEntry.priorState.(*concept).OwningConceptID
if priorOwnerID != "" {
uOfD.addMappedValueToOwnedIDsMap(priorOwnerID, currentEntry.priorState.(*concept).ConceptID)
}
switch currentEntry.priorState.GetConceptType() {
case Reference:
referencedElementID := currentEntry.priorState.(*concept).ReferencedConceptID
if referencedElementID != "" {
uOfD.addMappedValueToListenersMap(referencedElementID, currentEntry.priorState.GetConceptID(trans))
}
case Refinement:
abstractID := currentEntry.priorState.(*concept).AbstractConceptID
if abstractID != "" {
uOfD.addMappedValueToListenersMap(abstractID, currentEntry.priorState.GetConceptID(trans))
}
refinedID := currentEntry.priorState.(*concept).RefinedConceptID
if refinedID != "" {
uOfD.addMappedValueToListenersMap(refinedID, currentEntry.priorState.GetConceptID(trans))
}
}
// Update the uriUUIDMap
uri := currentEntry.priorState.GetURI(trans)
if uri != "" {
uOfD.uriUUIDMap.SetEntry(uri, currentID)
}
undoMgr.restoreState(currentEntry.priorState, currentEntry.changedElement, trans)
undoMgr.redoStack.Push(currentEntry)
uOfD.addElementForUndo(currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
} else if currentEntry.changeType == Change {
// Update listeners. If this is a reference or refinement pointing to another element, remove this element from the other element's listener's set
currentOwnerID := currentEntry.changedElement.(*concept).OwningConceptID
if currentOwnerID != "" {
uOfD.removeMappedValueFromOwnedIDsMap(currentOwnerID, currentID)
}
priorOwnerID := currentEntry.priorState.(*concept).OwningConceptID
if priorOwnerID != "" {
uOfD.addMappedValueToOwnedIDsMap(priorOwnerID, currentEntry.priorState.(*concept).ConceptID)
}
switch currentEntry.changedElement.GetConceptType() {
case Reference:
currentReferencedElementID := currentEntry.changedElement.(*concept).ReferencedConceptID
priorReferencedElementID := currentEntry.priorState.(*concept).ReferencedConceptID
if currentReferencedElementID != priorReferencedElementID {
if currentReferencedElementID != "" {
uOfD.removeMappedValueFromListenersMap(currentReferencedElementID, currentID)
}
if priorReferencedElementID != "" {
uOfD.addMappedValueToListenersMap(priorReferencedElementID, currentID)
}
}
case Refinement:
currentAbstractID := currentEntry.changedElement.(*concept).AbstractConceptID
priorAbstractID := currentEntry.priorState.(*concept).AbstractConceptID
if currentAbstractID != priorAbstractID {
if currentAbstractID != "" {
uOfD.removeMappedValueFromListenersMap(currentAbstractID, currentID)
}
if priorAbstractID != "" {
uOfD.addMappedValueToListenersMap(priorAbstractID, currentID)
}
}
currentRefinedID := currentEntry.changedElement.(*concept).RefinedConceptID
priorRefinedID := currentEntry.priorState.(*concept).RefinedConceptID
if currentRefinedID != priorRefinedID {
if currentRefinedID != "" {
uOfD.removeMappedValueFromListenersMap(currentRefinedID, currentID)
}
if priorRefinedID != "" {
uOfD.addMappedValueToListenersMap(priorRefinedID, currentID)
}
}
}
// Update the uriUUIDMap
currentURI := currentEntry.changedElement.GetURI(trans)
priorURI := currentEntry.priorState.GetURI(trans)
if currentURI != priorURI {
if currentURI != "" {
uOfD.uriUUIDMap.DeleteEntry(currentURI)
}
if priorURI != "" {
uOfD.uriUUIDMap.SetEntry(priorURI, currentID)
}
}
clone := clone(currentEntry.changedElement, trans)
priorOwnedElements := uOfD.ownedIDsMap.GetMappedValues(currentID).Clone()
priorListeners := uOfD.listenersMap.GetMappedValues(currentID).Clone()
redoEntry := newUndoRedoStackEntry(Change, clone, priorOwnedElements, priorListeners, currentEntry.priorUofD, currentEntry.changedElement)
undoMgr.restoreState(currentEntry.priorState, currentEntry.changedElement, trans)
uOfD.setOwnedIDsMapValues(currentID, currentEntry.priorOwnedElements)
uOfD.setMappedValuesForListenersMap(currentID, currentEntry.priorListeners)
if currentEntry.priorUofD != uOfD.id {
uOfD.deleteUUIDElementMapEntry(currentID)
} else if currentEntry.priorUofD == uOfD.id {
uOfD.setUUIDElementMapEntry(currentID, currentEntry.changedElement)
}
undoMgr.redoStack.Push(redoEntry)
}
}
firstEntry = false
}
}