Skip to content

Commit

Permalink
fix: only add one event handler to the undo history panel list
Browse files Browse the repository at this point in the history
  • Loading branch information
sillydan1 committed Mar 22, 2024
1 parent 58b4aaf commit 92696c5
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import atlantafx.base.theme.Styles;
import atlantafx.base.theme.Tweaks;
import dk.gtz.graphedit.util.IObservableUndoSystem;
Expand All @@ -18,6 +21,7 @@
import javafx.scene.text.Font;

public class UndoTreePanelController extends VBox {
private static final Logger logger = LoggerFactory.getLogger(UndoTreePanelController.class);
private ListView<ObservableUndoable> list;
private ChangeListener<Undoable> undoListener;

Expand All @@ -42,11 +46,22 @@ private void initialize(IBufferContainer bufferContainer) {
VBox.setVgrow(list, Priority.ALWAYS);
getChildren().add(list);
setList(bufferContainer.getCurrentlyFocusedBuffer().get());
list.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
var index = list.getSelectionModel().getSelectedItem();
if(index == null)
return;
var buffer = DI.get(IBufferContainer.class).getCurrentlyFocusedBuffer().get();
if(buffer == null)
return;
buffer.getUndoSystem().gotoAction(index.undoable());
});
}

private void setList(ViewModelProjectResource currentBuffer) {
if(currentBuffer == null)
if(currentBuffer == null) {
list.getItems().clear();
return;
}
setList(currentBuffer.getUndoSystem());
undoListener = (e,o,n) -> setList(currentBuffer.getUndoSystem());
currentBuffer.getUndoSystem().getCurrentUndoableProperty().addListener(undoListener);
Expand All @@ -57,12 +72,5 @@ private void setList(IObservableUndoSystem undosystem) {
var history = undosystem.getStringRepresentation();
Collections.reverse(history);
list.getItems().setAll(history);
// TODO: This fires way too often. Basically 4-5 times per click.
list.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
var index = list.getSelectionModel().getSelectedItem();
if(index == null)
return;
undosystem.gotoAction(index.undoable());
});
}
}

0 comments on commit 92696c5

Please sign in to comment.