JavaFX 2用户空闲检测

ran*_*n88 4 java javafx idle-processing javafx-2


我正在尝试使用JavaFX作为UI创建一个简单的Java事务应用程序.
我现在要做的是从我的应用程序中检测用户空闲状态,该应用程序具有1个主要阶段和许多场景.

示例:如果用户空闲3分钟,则返回主菜单.

我已经在网上尝试了一些关于如何检测JavaFX空闲状态的例子,但我发现总是-one函数空闲状态检测,它发生了所有场景 - 对于事务应用程序来说是危险的(我认为)危险(例如:apps检测)交易过程中的空闲状态).
可以在每个场景中检测用户空闲状态吗?怎么样?
谢谢.

编辑:

我已经尝试过的例子:

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

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

Jam*_*s_D 9

我真的不明白你对交易行为的看法.事务涉及对数据的保证,您的事务行为应该在数据级别定义,不应受UI中发生的事件的影响.换句话说,即使UI由于用户空闲而重置,您的原子行为也应该完成或回滚.

也许这会有所帮助.(注意我在这些示例中使用了Java 8代码,但如果需要,您可以相当容易地使其符合JavaF 2.2.)这遵循Tomas Mikula的一般方法,因为它使用a Timeline来实现空闲检查.我没有使用Tomas的FX Timer包装器,但如果你愿意,你当然可以这样做.此类封装监视器以确定用户是否处于空闲状态.可以注册任何节点(或场景)和事件的类型:如果在该节点(或场景)发生这种类型的事件时,用户被确定为不是空闲的.如果指定的时间过去而没有发生任何已注册的事件,则执行提供的runnable(在FX Application Thread上).这使您可以根据需要灵活地创建多个监视器,并为每个监视器注册一个或多个节点.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个测试."开始"按钮可能是用于登录的替身.主UI具有带有两个选项卡的选项卡窗格:每个单独的选项卡以其自己的"开始"按钮开始,然后主要内容具有标签,文本字段和按钮.

每个选项卡内容都有一个(短的,用于测试)空闲监视器与它们相关联.选项卡内容上的任何事件都将重置空闲监视器,但选项卡内容之外的事件不会重置它.整个窗口还有一个"全局"空闲监视器,可在30秒后重置整个UI.

请注意,数据将被保留:即如果由于空闲而超时,则您在文本字段中键入的任何文本都会被正确保留.这就是为什么我认为"交易"的问题根本不重要.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)