org.openide.util.RequestProcessor

Andrzej Olszak contributed some code to the Sticky Note Sidebar for detecting when the user has stopped typing for 10 seconds. Here his code is extrapolated into a poller action registered in the Java editor popupmenus:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.StyledDocument;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.cookies.EditorCookie;
import org.openide.util.NbBundle.Messages;
import org.openide.util.RequestProcessor;

@ActionID(category = "Edit",
id = "org.general.poller.GeneralPollerAction")
@ActionRegistration(displayName = "#CTL_GeneralPollerAction")
@ActionReferences({
    @ActionReference(path = "Editors/text/x-java/Popup", position = 400)
})
@Messages("CTL_GeneralPollerAction=Start Reminder")
public final class GeneralPollerAction implements ActionListener {

    private StyledDocument target;
    private final RequestProcessor rp;
    public static final int REFRESH_DELAY = 10000;

    public GeneralPollerAction(EditorCookie context) {
        target = context.getDocument();
        rp = new RequestProcessor(target.getClass());
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        setupAutoRefresh(target);
    }

    private void sendMessage() {
        JOptionPane.showMessageDialog(null, "Last time you typed"
                + " was 10 seconds ago!");
    }

    private void setupAutoRefresh(final StyledDocument target) {
        target.addDocumentListener(new DocumentListener() {
            private RequestProcessor.Task lastRefreshTask;
            @Override
            public void insertUpdate(DocumentEvent de) {
                doRefresh();
            }
            @Override
            public void removeUpdate(DocumentEvent de) {
                doRefresh();
            }
            @Override
            public void changedUpdate(DocumentEvent de) {
                doRefresh();
            }
            private void doRefresh() {
                if (lastRefreshTask != null && !lastRefreshTask.isFinished()) {
                    lastRefreshTask.schedule(REFRESH_DELAY);
                } else {
                    lastRefreshTask = rp.post(new Runnable() {
                        @Override
                        public void run() {
                            sendMessage();
                        }
                    }, REFRESH_DELAY);
                }
            }
        });
    }

}

Comments:

The RequestProcessor ctor should take GeneralPollerAction.class.

doRefresh looks too complicated. The usual pattern is

if (lastRefreshTask == null) {
lastRefreshTask = rp.create(new Runnable() {...});
}
lastRefreshTask.schedule(REFRESH_DELAY);

since schedule(...) automatically defers a pending task.

Posted by Jesse Glick on November 02, 2011 at 01:17 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today