【问题标题】:MVC: Best way of watching a directory for changesMVC:查看目录更改的最佳方式
【发布时间】:2015-08-04 15:06:08
【问题描述】:

背景:
我是 JavaFX 的新手,但我正在尝试构建一个应用程序,其基本功能之一是向用户显示某个目录中的所有文件夹,并在该目录中删除新文件夹或文件夹时自动更新视图。这些文件夹可以被视为对象(例如在 ListView 中),用户应该能够与它们进行交互。 我想使用 MVC 架构构建该应用程序。

到目前为止我做了什么:
所以我有一个视图(fxml)、一个控制器类和我的模型来处理我的应用程序逻辑。我在我的模型中使用WatchDir example from Oracle 作为辅助类,并在控制器中启动 WatchService,如下所示:

@Override
public void initialize(URL location, ResourceBundle resources) {
    this.config = loadConfig(configFile);
    this.facade = new facade(config);
    Path rootPath = Paths.get(config.getDlRootPath());
    try {
        // register WatchService
        new WatchDir(rootPath, false).processEvents();
        statusText(rootPath + "is now being watched for changes");
    } catch (IOException e) {
        statusError("Directory " + e.getLocalizedMessage() + " does not exist.");
    }
}

在 WatchDir 方法 processEvents() 中,我可以执行以下操作:

if (!recursive && (kind == ENTRY_CREATE)) {
  // new folder was created
}

我的问题:
告诉我的控制器发生了变化并更新 ListView 的最佳/最优雅的方式是什么?我想保持 MVC 风格。

也欢迎不同的方法:)

【问题讨论】:

    标签: java model-view-controller javafx filesystems watchservice


    【解决方案1】:

    我将使用的方法是在WatchDir 中提供用于注册回调的方法。最简单的方法是使用Consumer 属性进行回调:

    public class WatchDir {
    
        private Consumer<Path> onCreate ;
    
        public void setOnCreate(Consumer<Path> onCreate) {
            this.onCreate = onCreate ;
        }
    
        // other callbacks as needed...
    
        // ...
    
        void processEvents() {
    
            // ...
    
            Path child = ... ; // file/folder that was created
    
            if (! recursive && kind == ENTRY_CREATE) {
                if (onCreate != null) {
                    onCreate.accept(child);
                }
            }
    
            // ...
        }
    
        // ...
    }
    

    请注意,WatchDir.processEvents() 是一个(非终止)阻塞调用,因此您需要在后台线程中运行它。所以从你的控制器你做:

    WatchDir watchDir = new WatchDir(rootPath, false) ;
    watchDir.setOnCreate(path -> 
        Platform.runLater(() -> listView.getItems().add(path)));
    Thread watchThread = new Thread(watchDir::processEvents);
    watchThread.setDaemon(true);
    watchThread.start();
    

    请注意,由于回调将在后台线程上调用,因此对 UI 的更新应包含在 Platform.runLater(...) 中。如果你愿意,你可以为WatchDir 配备一个Executor 来执行回调,这将允许你告诉它一次总是通过Platform.runLater(...) 执行它们:

    public class WatchDir {
    
        // Executor for notifications: by default just run on the current thread
        private Executor notificationExecutor = Runnable::run ;
        public void setNotificationExecutor(Executor notificationExecutor) {
            this.notificationExecutor = notificationExecutor ;
        }
    
        private Consumer<Path> onCreate ;
        // etc...
    
        void processEvents() {
    
            // ...
    
            if (! recursive && kind == ENTRY_CREATE) {
                if (onCreate != null) {
                    notificationExecutor.execute(() -> onCreate.accept(child));
                }
            }
    
            // ...
        }
    
    }
    

    然后

    WatchDir watchDir = new WatchDir(rootPath, false) ;
    watchDir.setNotificationExecutor(Platform::runLater);
    watchDir.setOnCreate(listView.getItems()::add); /* or path -> listView.getItems().add(path) */
    Thread watchThread = new Thread(watchDir::processEvents);
    watchThread.setDaemon(true);
    watchThread.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2014-12-23
      相关资源
      最近更新 更多