X

Geertjan's Blog

  • October 31, 2011

org.openide.util.RequestProcessor

Geertjan Wielenga
Product Manager
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);
                }
            }
        });
    }

}

Join the discussion

Comments ( 1 )
  • Jesse Glick Wednesday, November 2, 2011

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.