From 65a8ff63bae4f6f2e025e3dbf0b7b8eb64193039 Mon Sep 17 00:00:00 2001 From: Erlend Ã…mdal Date: Sun, 12 May 2019 09:47:41 +0200 Subject: Add forward and backward reference history with mouse navigation (#132) * Add History * Add forward and backward reference history * Update PopupMenuBar text for history * Fix indentation * Fix more indentation --- src/main/java/cuchaz/enigma/gui/util/History.java | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/cuchaz/enigma/gui/util/History.java (limited to 'src/main/java/cuchaz/enigma/gui/util') diff --git a/src/main/java/cuchaz/enigma/gui/util/History.java b/src/main/java/cuchaz/enigma/gui/util/History.java new file mode 100644 index 0000000..94f3105 --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/util/History.java @@ -0,0 +1,49 @@ +package cuchaz.enigma.gui.util; + +import com.google.common.collect.Queues; + +import java.util.Deque; + +public class History { + private final Deque previous = Queues.newArrayDeque(); + private final Deque next = Queues.newArrayDeque(); + private T current; + + public History(T initial) { + current = initial; + } + + public T getCurrent() { + return current; + } + + public void push(T value) { + previous.addLast(current); + current = value; + next.clear(); + } + + public void replace(T value) { + current = value; + } + + public boolean canGoBack() { + return !previous.isEmpty(); + } + + public T goBack() { + next.addFirst(current); + current = previous.removeLast(); + return current; + } + + public boolean canGoForward() { + return !next.isEmpty(); + } + + public T goForward() { + previous.addLast(current); + current = next.removeFirst(); + return current; + } +} -- cgit v1.2.3