Skip to content

Commit

Permalink
Better mouse position caching impl
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jan 17, 2019
1 parent 7551bd7 commit c87f152
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.protege.editor.core.ui.list;

import org.protege.editor.core.ui.util.UIUtil;
import org.protege.editor.owl.ui.util.MousePositionCache;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -79,9 +80,7 @@ public String getName() {

public int lastMousePositionCellIndex = -1;

private Point cachedMousePosition = null;

private boolean cachedMousePositionStale = true;
private final MousePositionCache mousePositionCache;

public MList() {
ListCellRenderer renderer = this.getCellRenderer();
Expand All @@ -95,6 +94,7 @@ public void mouseMoved(MouseEvent e) {
handleMouseMoved();
}
};
mousePositionCache = MousePositionCache.createAndInstall(this);
this.addMouseMotionListener(mouseMovementListener);
MouseListener mouseButtonListener = new MouseAdapter() {
@Override
Expand Down Expand Up @@ -151,7 +151,6 @@ protected void clearCellHeightCache() {


private void handleMouseMoved() {
cachedMousePositionStale = true;
if (MList.this.getModel().getSize() > 0) {
// only repaint all the cells the mouse has moved over
Rectangle dirty = getCellBounds(lastMousePositionCellIndex, lastMousePositionCellIndex);
Expand Down Expand Up @@ -512,11 +511,7 @@ private Color getButtonColor(MListButton button) {

@Nullable
private Point getCachedMousePosition() {
if(cachedMousePositionStale) {
cachedMousePosition = getMousePosition();
cachedMousePositionStale = false;
}
return cachedMousePosition;
return mousePositionCache.getMousePosition();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.protege.editor.owl.ui.util;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2019-01-17
*/
public class MousePositionCache {

@Nonnull
private final Component component;

private boolean stale = true;

private Point cachedPosition = null;

private MousePositionCache(@Nonnull Component component) {
this.component = checkNotNull(component);
}

public static MousePositionCache createAndInstall(@Nonnull Component component) {
MousePositionCache cache = new MousePositionCache(component);
cache.attacheListeners();
return cache;
}

private void attacheListeners() {
component.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
markAsStale();
}
});
}

private void markAsStale() {
stale = true;
}

@Nullable
public Point getMousePosition() {
if(stale) {
cachedPosition = component.getMousePosition();
stale = false;
}
return cachedPosition;
}
}

0 comments on commit c87f152

Please sign in to comment.