javajavafxjavafx-2idle-processing

JavaFX 2 User Idle Detection



I'm trying to make a simple Java transaction app with JavaFX as UI.
What I want to do now is to detect user idle state from my application which has 1 primary stage and many scenes.

Example : if user idle for 3 minutes then go back to main menu.

I already try some examples on the web about how to detect JavaFX idle state, but what I found is always -one function idle state detection which is occuring all scenes- method which is (I think) dangerous for transaction app (ex : apps detect idle state in the middle of transaction process).
It's possible to detect user idle state on every single scene? how?
Thanks.

EDIT :

Examples that I already try :

http://tomasmikula.github.io/blog/2014/06/04/timers-in-javafx-and-reactfx.html

and

http://ochafik.com/blog/?p=98


Solution

  • I don't really understand the point you are making about transactional behavior. Transactions concern guarantees about the data, and your transactional behavior should be defined at the data level and should not be impacted by what is happening in the UI. In other words, your atomic behavior should complete or rollback even if the UI resets due to the user being idle.

    Maybe this will help, though. (Note I used Java 8 code in these examples, but you can fairly easily make it JavaF 2.2 compliant if you need.) This follows Tomas Mikula's general approach in that it uses a Timeline to implement the idle check. I didn't use Tomas' FX Timer wrapper but you could certainly do so if you like. This class encapsulates a monitor for whether the user is idle. You can register any node (or scene) and type of event: if an event of that type occurs on that node (or scene), the user is determined not to be idle. If the specified time elapses without any registered events occurring, the provided runnable is executed (on the FX Application Thread). This gives you the flexibility to create multiple monitors, if needed, and to register one or more nodes with each.

    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.event.EventType;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    
    import javafx.util.Duration;
    
    public class IdleMonitor {
    
        private final Timeline idleTimeline ;
    
        private final EventHandler<Event> userEventHandler ;
    
        public IdleMonitor(Duration idleTime, Runnable notifier, boolean startMonitoring) {
            idleTimeline = new Timeline(new KeyFrame(idleTime, e -> notifier.run()));
            idleTimeline.setCycleCount(Animation.INDEFINITE);
    
            userEventHandler = e -> notIdle() ; 
    
            if (startMonitoring) {
                startMonitoring();
            }
        }
    
        public IdleMonitor(Duration idleTime, Runnable notifier) {
            this(idleTime, notifier, false);
        }
    
        public void register(Scene scene, EventType<? extends Event> eventType) {
            scene.addEventFilter(eventType, userEventHandler);
        }
    
        public void register(Node node, EventType<? extends Event> eventType) {
            node.addEventFilter(eventType, userEventHandler);
        }
    
        public void unregister(Scene scene, EventType<? extends Event> eventType) {
            scene.removeEventFilter(eventType, userEventHandler);
        }
    
        public void unregister(Node node, EventType<? extends Event> eventType) {
            node.removeEventFilter(eventType, userEventHandler);
        }
    
        public void notIdle() {
            if (idleTimeline.getStatus() == Animation.Status.RUNNING) {
                idleTimeline.playFromStart();
            }
        }
    
        public void startMonitoring() {
            idleTimeline.playFromStart();
        }
    
        public void stopMonitoring() {
            idleTimeline.stop();
        }
    }
    

    Here's a test. The "Start" buttons are perhaps stand-ins for logging in. The main UI has a tab pane with two tabs: each individual tab starts with its own "Start" button and then the main content has a label, text field, and button.

    The tab contents each have a (short, for testing) idle monitor associated with them. Any event on the content of the tab will reset the idle monitor, but events outside of the tab content will not reset it. There's also a "global" idle monitor for the entire window which resets the whole UI after 30 seconds.

    Note that the data is preserved: i.e. if you timeout due to the idle, any text you type in the text field is preserved properly. This is why I think the issue with "transactions" should not matter at all.

    import javafx.application.Application;
    import javafx.event.Event;
    import javafx.geometry.Pos;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class IdleTest extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            StackPane root = new StackPane();
    
            Parent mainUI = buildMainUI();
            Scene scene = new Scene(root, 350, 150);
            Parent startUI = buildStartUI(() -> root.getChildren().setAll(mainUI));
            root.getChildren().add(startUI);
    
            IdleMonitor idleMonitor = new IdleMonitor(Duration.seconds(30),
                    () -> root.getChildren().setAll(startUI), true);
            idleMonitor.register(scene, Event.ANY);
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        private Parent buildStartUI(Runnable start) {
            Button button = new Button("Start");
            button.setOnAction(e -> start.run());
            StackPane root = new StackPane(button);
            return root ;
        }
    
        private Parent buildMainUI() {
            TabPane tabPane = new TabPane();
            Tab tab1 = new Tab("One");
            Parent tab1Content = buildTabUI("Tab 1");
            Parent tab1StartContent = buildStartUI(() -> tab1.setContent(tab1Content));
            tab1.setContent(tab1StartContent);
            IdleMonitor tab1IdleMonitor = new IdleMonitor(Duration.seconds(5), 
                    () -> tab1.setContent(tab1StartContent), true);
            tab1IdleMonitor.register(tab1Content, Event.ANY);
    
            Tab tab2 = new Tab("Two");
            Parent tab2Content = buildTabUI("Tab 2") ;
            Parent tab2StartContent = buildStartUI(() -> tab2.setContent(tab2Content));
            tab2.setContent(tab2StartContent);
            IdleMonitor tab2IdleMonitor = new IdleMonitor(Duration.seconds(10),
                    () -> tab2.setContent(tab2StartContent), true);
            tab2IdleMonitor.register(tab2Content, Event.ANY);
    
            tabPane.getTabs().addAll(tab1, tab2);
            return tabPane ;
        }
    
        private Parent buildTabUI(String text) {
            Button button = new Button("Click here");
            button.setOnAction(e -> System.out.println("Click in "+text));
            VBox content = new VBox(10, new Label(text), new TextField(), button);
            content.setAlignment(Pos.CENTER);
            return content ;
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }