+ Map<ModelComponent, Map<String, Consumer<Object>>> sysoutListenersPerHLSPerTarget = new HashMap<>();
+ addListener.addListener(SWT.Selection, e ->
+ {
+ try
+ {
+ int componentIndex = componentSelector.getSelectionIndex();
+ if (componentIndex < 0 || componentIndex >= componentsByItemIndex.size())
+ throw new RuntimeException("No component selected");
+ ModelComponent target = componentsByItemIndex.get(componentIndex);
+ Map<String, Consumer<Object>> sysoutListenersPerHLS = sysoutListenersPerHLSPerTarget.computeIfAbsent(target,
+ k -> new HashMap<>());
+ String stateIDString = stateIDText.getText();
+ if (sysoutListenersPerHLS.containsKey(stateIDString))
+ throw new RuntimeException("Listener already registered");
+ Consumer<Object> sysoutListener = v -> System.out.println(stateIDString + ": " + v);
+ target.addHighLevelStateListener(stateIDString, sysoutListener);
+ sysoutListenersPerHLS.put(stateIDString, sysoutListener);
+ output.setText("Success!");
+ }
+ catch (Exception x)
+ {
+ output.setText(x.getClass().getSimpleName() + (x.getMessage() == null ? "" : ": " + x.getMessage()));
+ }
+ });
+ removeListener.addListener(SWT.Selection, e ->
+ {
+ try
+ {
+ int componentIndex = componentSelector.getSelectionIndex();
+ if (componentIndex < 0 || componentIndex >= componentsByItemIndex.size())
+ throw new RuntimeException("No component selected");
+ ModelComponent target = componentsByItemIndex.get(componentIndex);
+ Map<String, Consumer<Object>> sysoutListenersPerHLS = sysoutListenersPerHLSPerTarget.get(target);
+ if (sysoutListenersPerHLS == null)
+ throw new RuntimeException("Listener not registered");
+ String stateIDString = stateIDText.getText();
+ Consumer<Object> sysoutListener = sysoutListenersPerHLS.remove(stateIDString);
+ if (sysoutListener == null)
+ throw new RuntimeException("Listener not registered");
+ target.removeHighLevelStateListener(stateIDString, sysoutListener);
+ output.setText("Success!");
+ }
+ catch (Exception x)
+ {
+ output.setText(x.getClass().getSimpleName() + (x.getMessage() == null ? "" : ": " + x.getMessage()));
+ }
+ });