简体   繁体   English

如何在Eclipse插件中使用Timer和SwingWorker?

[英]How to use Timer and SwingWorker in an Eclipse Plugin?

I'm developing an Eclipse plugin that will contribute to the GUI with a view. 我正在开发一个Eclipse插件,它将通过视图为GUI做出贡献。

The view is updated with informations from a versioning system when the user selects a folder or a file in the workspace. 当用户在工作空间中选择文件夹或文件时,将使用来自版本控制系统的信息来更新视图。

In order to avoid collecting data everytime the user goes through the project subfolders and files, I need to wait for 3 seconds in order to be sure that the file or folder is the one of interest. 为了避免每次用户浏览项目子文件夹和文件时都收集数据,我需要等待3秒钟,以确保文件或文件夹是您感兴趣的文件或文件夹之一。

I'm currently doing this using a Swing Timer. 我目前正在使用Swing计时器进行此操作。

This is ok for small amount of data, but for large amount of data the GUI blocks, waiting for the timer to execute the update function. 对于少量数据,这是可以的,但是对于大量数据,GUI会阻塞,等待计时器执行更新功能。

I know for this kind of task I can use SwingWorker but I can't figure out how to delay the task and to restart the delay when needed. 我知道对于这种任务,我可以使用SwingWorker,但我不知道如何延迟任务并在需要时重新启动延迟。

Can anyone give me a solution on how to correctly solve this problem ? 谁能给我解决方法,以正确解决此问题?

Here is my current code: 这是我当前的代码:

  public void resetTimerIfNeeded()
    {
        if(timer.isRunning())
            timer.restart();
        else
            timer.start();
    }


    public void timer()
    {
        selectionTimer = new Timer(3000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub


                Display.getDefault().syncExec(new Runnable(){
                    @Override
                    public void run()
                    {               
                        updateView();
                        selectionTimer.stop();                  
                    }
                }); 
            }
        });
    }

Since Eclipse uses SWT rather than Swing it is best to avoid using Swing code. 由于Eclipse使用SWT而不是Swing,因此最好避免使用Swing代码。

You can run code in the UI thread after a delay using UIJob , something like: 延迟后,您可以使用UIJob在UI线程中运行代码,如下所示:

UIJob job = new UIJob("Job title") {
        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {

            updateView();

            return Status.OK_STATUS;
        }
    };

job.schedule(3000);

Alternatively you can use Display.timerExec : 另外,您可以使用Display.timerExec

Display.getDefault().timerExec(3000, new Runnable(){
         @Override
         public void run()
         {               
           updateView();
         }
    });

Schedule it as a Job instead: https://eclipse.org/articles/Article-Concurrency/jobs-api.html . 将其安排为作业: https : //eclipse.org/articles/Article-Concurrency/jobs-api.html Use a UIJob if the entirety of what it's doing is interacting with the UI. 如果整个操作都在与UI交互,请使用UIJob。 The cancel/schedule and sleep/wakeUp methods will be of interest , see http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/jobs/Job.html for the JavaDoc. cancel / schedule和sleep / wakeUp方法将很有趣,请参阅http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime /jobs/Job.html(对于JavaDoc)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM