Ticket #7503: timer_test.patch

File timer_test.patch, 3.6 KB (added by xeen, 12 years ago)

Another idea which might improve the situation: Reduce the amount of repaints required when moving the mouse. This avoid "in the middle" repaints at the cost of higher response times. I don’t notice the additional delay but feel the improvement when covering long distances. The delay might be more noticable on low end systems. Can you try?

  • src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    ### Eclipse Workspace Patch 1.0
    #P JOSM
     
    2121import java.util.Iterator;
    2222import java.util.LinkedList;
    2323import java.util.Set;
     24import java.util.Timer;
     25import java.util.TimerTask;
    2426
    2527import javax.swing.JOptionPane;
     28import javax.swing.SwingUtilities;
    2629
    2730import org.openstreetmap.josm.Main;
    2831import org.openstreetmap.josm.actions.MergeNodesAction;
     
    193196        removeHighlighting();
    194197    }
    195198
     199    private int saved = 0;
     200    private final Timer requestRepaintTimer = new Timer();
     201    private final Runnable repaintRunnable = new Runnable() {
     202        @Override
     203        public void run() {
     204            mv.repaint();
     205            saved--;
     206            System.out.println("repaint!" + saved);
     207        }
     208    };
     209
     210    class RequestRepaintTimerTask extends TimerTask {
     211        @Override
     212        public void run() {
     213
     214            SwingUtilities.invokeLater(repaintRunnable);
     215
     216        }
     217    }
     218    private TimerTask requestRepaintTimerTask = null;
     219    private void requestRepaint() {
     220        if(requestRepaintTimerTask != null) {
     221            requestRepaintTimerTask.cancel();
     222        }
     223        saved++;
     224        requestRepaintTimer.purge();
     225        requestRepaintTimerTask = new RequestRepaintTimerTask();
     226        requestRepaintTimer.schedule(requestRepaintTimerTask, 25);
     227    }
     228
     229    private synchronized void requestRepaintNow() {
     230        if(requestRepaintTimerTask != null) {
     231            requestRepaintTimerTask.cancel();
     232        }
     233        requestRepaintTimer.purge();
     234        System.out.println("repaint now!");
     235        mv.repaint();
     236    }
     237
    196238    /**
    197239     * works out which cursor should be displayed for most of SelectAction's
    198240     * features. The only exception is the "move" cursor when actually dragging
     
    354396        // We don't have a mouse event, so we pass the old mouse event but the
    355397        // new modifiers.
    356398        if(giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers())) {
    357             mv.repaint();
     399            requestRepaintNow();
    358400        }
    359401    }
    360402
     
    403445            // when dragging a node onto another one and then press CTRL to merge
    404446            oldEvent = e;
    405447            if(needsRepaint) {
    406                 mv.repaint();
     448                requestRepaintNow();
    407449            }
    408450        }
    409451
     
    461503                return;
    462504
    463505            Command c = !Main.main.undoRedo.commands.isEmpty()
    464                     ? Main.main.undoRedo.commands.getLast() : null;
     506            ? Main.main.undoRedo.commands.getLast() : null;
    465507            if (c instanceof SequenceCommand) {
    466508                c = ((SequenceCommand) c).getLastCommand();
    467509            }
     
    503545            }
    504546        }
    505547
    506         mv.repaint();
     548        requestRepaintNow();
    507549        if (mode != Mode.scale) {
    508550            lastMousePos = e.getPoint();
    509551        }
     
    520562        }
    521563        oldEvent = e;
    522564        if(giveUserFeedback(e)) {
    523             mv.repaint();
     565            requestRepaint();
    524566        }
    525567    }
    526568
    527569    @Override
    528570    public void mouseExited(MouseEvent e) {
    529571        if(removeHighlighting()) {
    530             mv.repaint();
     572            requestRepaint();
    531573        }
    532574    }
    533575
     
    731773            break;
    732774        }
    733775        giveUserFeedback(e);
    734         mv.repaint();
     776        requestRepaintNow();
    735777        updateStatusLine();
    736778    }
    737779