Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow installation of a temporary input map without needing to hold a reference to previous one (stackable) #18

Merged
merged 2 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 95 additions & 19 deletions src/main/java/org/fxmisc/wellbehaved/event/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;

import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
Expand All @@ -15,24 +17,40 @@
import org.fxmisc.wellbehaved.event.InputMap.HandlerConsumer;

/**
* Helper class for "installing/uninstalling" an {@link InputMap} into a {@link Node}. To add an {@link InputMap}
* as a default behavior that can be overridden later, use {@link #addFallbackInputMap(Node, InputMap)}.
* To add an {@code InputMap} that might override default behaviors, use {@link #addInputMap(Node, InputMap)}. To
* remove an {@code InputMap}, use {@link #removeInputMap(Node, InputMap)}.
* Helper class for "installing/uninstalling" an {@link InputMap} into a {@link Node}.
*
* <h3>Method Summary</h3>
* <ul>
* <li>
* To add an {@link InputMap} as a default behavior that can be overridden later,
* use {@link #addFallbackInputMap(Node, InputMap)}.
* </li>
* <li>
* To add an {@code InputMap} that might override default behaviors, use {@link #addInputMap(Node, InputMap)}.
* </li>
* <li>
* To remove an {@code InputMap}, use {@link #removeInputMap(Node, InputMap)}.
* </li>
* <li>
* See also {@link #pushInputMap(Node, InputMap)} and {@link #popInputMap(Node)} for temporary behavior
* modification.
* </li>
* </ul>
*/
public class Nodes {

private static final String P_INPUTMAP = "org.fxmisc.wellbehaved.event.inputmap";
private static final String P_HANDLERS = "org.fxmisc.wellbehaved.event.handlers";
private static final String P_STACK = "org.fxmisc.wellbehaved.event.stack";

/**
* Adds the given input map to the start of the node's list of input maps, so that an event will be pattern-matched
* against the given input map before being pattern-matched against any other input maps currently
* "installed" in the node.
*/
public static void addInputMap(Node node, InputMap<?> im) {
init(node);
setInputMap(node, InputMap.sequence(im, getInputMap(node)));
// getInputMap calls init, so can use unsafe setter
setInputMapUnsafe(node, InputMap.sequence(im, getInputMap(node)));
}

/**
Expand All @@ -41,43 +59,76 @@ public static void addInputMap(Node node, InputMap<?> im) {
* input map.
*/
public static void addFallbackInputMap(Node node, InputMap<?> im) {
init(node);
setInputMap(node, InputMap.sequence(getInputMap(node), im));
// getInputMap calls init, so can use unsafe setter
setInputMapUnsafe(node, InputMap.sequence(getInputMap(node), im));
}

/**
* Removes (or uninstalls) the given input map from the node.
*/
public static void removeInputMap(Node node, InputMap<?> im) {
setInputMap(node, getInputMap(node).without(im));
// getInputMap calls init, so can use unsafe setter
setInputMapUnsafe(node, getInputMap(node).without(im));
}

static InputMap<?> getInputMap(Node node) {
/**
* Gets the {@link InputMap} for the given node or {@link InputMap#empty()} if there is none.
*/
public static InputMap<?> getInputMap(Node node) {
init(node);
return (InputMap<?>) node.getProperties().get(P_INPUTMAP);
return getInputMapUnsafe(node);
}

private static void setInputMap(Node node, InputMap<?> im) {
node.getProperties().put(P_INPUTMAP, im);
/**
* Removes the currently installed {@link InputMap} (InputMap1) on the given node and installs the {@code im}
* (InputMap2) in its place. When finished, InputMap2 can be uninstalled and InputMap1 reinstalled via
* {@link #popInputMap(Node)}. Multiple InputMaps can be installed so that InputMap(n) will be installed over
* InputMap(n-1)
*/
public static void pushInputMap(Node node, InputMap<?> im) {
// store currently installed im; getInputMap calls init
InputMap<?> previousInputMap = getInputMap(node);
getStack(node).push(previousInputMap);

// completely override the previous one with the given one
setInputMapUnsafe(node, im);
}

/**
* If the internal stack has an {@link InputMap}, removes the current {@link InputMap} that was installed
* on the give node via {@link #pushInputMap(Node, InputMap)}, reinstalls the previous {@code InputMap},
* and then returns true. If the stack is empty, returns false.
*/
public static boolean popInputMap(Node node) {
Stack<InputMap<?>> stackedInputMaps = getStack(node);
if (!stackedInputMaps.isEmpty()) {
// If stack is not empty, node has already been initialized, so can use unsafe methods.
// Now, completely override current input map with previous one on stack
setInputMapUnsafe(node, stackedInputMaps.pop());
return true;
} else {
return false;
}
}

/**
*
* @param node
*/
private static void init(Node node) {
if(node.getProperties().get(P_INPUTMAP) == null) {
ObservableMap<Object, Object> nodeProperties = getProperties(node);
if(nodeProperties.get(P_INPUTMAP) == null) {

node.getProperties().put(P_INPUTMAP, InputMap.empty());
node.getProperties().put(P_HANDLERS, new ArrayList<Map.Entry<?, ?>>());
nodeProperties.put(P_INPUTMAP, InputMap.empty());
nodeProperties.put(P_HANDLERS, new ArrayList<Map.Entry<?, ?>>());

MapChangeListener<Object, Object> listener = ch -> {
if(!P_INPUTMAP.equals(ch.getKey())) {
return;
}

getHandlers(node).forEach(entry -> {
node.removeEventHandler((EventType<Event>) entry.getKey(), (EventHandler<Event>) entry.getValue());
node.removeEventHandler(entry.getKey(), (EventHandler<Event>) entry.getValue());
});

getHandlers(node).clear();
Expand All @@ -92,11 +143,36 @@ public <E extends Event> void accept(
getHandlers(node).add(new SimpleEntry<>(t, h));
}});
};
node.getProperties().addListener(listener);
nodeProperties.addListener(listener);
}
}

/** Expects a {@link #init(Node)} call with the given node before this one is called */
private static void setInputMapUnsafe(Node node, InputMap<?> im) {
getProperties(node).put(P_INPUTMAP, im);
}

/** Expects a {@link #init(Node)} call with the given node before this one is called */
private static InputMap<?> getInputMapUnsafe(Node node) {
return (InputMap<?>) getProperties(node).get(P_INPUTMAP);
}

private static List<Map.Entry<EventType<?>, EventHandler<?>>> getHandlers(Node node) {
return (List<Entry<EventType<?>, EventHandler<?>>>) node.getProperties().get(P_HANDLERS);
return (List<Entry<EventType<?>, EventHandler<?>>>) getProperties(node).get(P_HANDLERS);
}

private static Stack<InputMap<?>> getStack(Node node) {
ObservableMap<Object, Object> nodeProperties = getProperties(node);
if (nodeProperties.get(P_STACK) == null) {
Stack<InputMap<?>> stackedInputMaps = new Stack<>();
nodeProperties.put(P_STACK, stackedInputMaps);
return stackedInputMaps;
}

return (Stack<InputMap<?>>) nodeProperties.get(P_STACK);
}

private static ObservableMap<Object, Object> getProperties(Node node) {
return node.getProperties();
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/fxmisc/wellbehaved/event/InputMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.function.Supplier;

public class InputMapTest {

@BeforeClass
Expand Down Expand Up @@ -411,4 +413,49 @@ public void ifProceededTest() {
assertEquals(4, counter.get());
assertFalse(right.isConsumed());
}

@Test
public void pushAndPopInputMap() {
StringProperty res = new SimpleStringProperty();

Region node = new Region();
Nodes.addInputMap(node, InputMap.consume(keyPressed(UP), e -> res.set("Up")));
Supplier<KeyEvent> createUpKeyEvent = () ->
new KeyEvent(KEY_PRESSED, "", "", UP, false, false, false, false);

// regular input map works
KeyEvent up = createUpKeyEvent.get();

dispatch(up, node);
assertEquals("Up", res.get());
assertTrue(up.isConsumed());

// temporary input map works
Nodes.pushInputMap(node, InputMap.consume(keyPressed(UP), e -> res.set("Down")));
up = createUpKeyEvent.get();

dispatch(up, node);
assertEquals("Down", res.get());
assertTrue(up.isConsumed());

// popping reinstalls previous input map
Nodes.popInputMap(node);
up = createUpKeyEvent.get();

dispatch(up, node);
assertEquals("Up", res.get());
assertTrue(up.isConsumed());

// popping when no temporary input maps exist does nothing
Nodes.popInputMap(node);
up = createUpKeyEvent.get();

// set value to something else to insure test works as expected
res.set("Other value");

dispatch(up, node);
assertEquals("Up", res.get());
assertTrue(up.isConsumed());
}

}