diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/ui/list/MList.java b/protege-editor-core/src/main/java/org/protege/editor/core/ui/list/MList.java index 802be98f1..e0c672a66 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/ui/list/MList.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/ui/list/MList.java @@ -94,7 +94,7 @@ public void mouseMoved(MouseEvent e) { handleMouseMoved(); } }; - mousePositionCache = MousePositionCache.createAndInstall(this); + mousePositionCache = MousePositionCache.createAndInstall(this, super::getMousePosition); this.addMouseMotionListener(mouseMovementListener); MouseListener mouseButtonListener = new MouseAdapter() { @Override @@ -157,7 +157,7 @@ private void handleMouseMoved() { if (dirty != null) { repaint(dirty); } - Point pt = getCachedMousePosition(); + Point pt = getMousePosition(); if (pt != null) { lastMousePositionCellIndex = MList.this.locationToIndex(pt); } @@ -390,7 +390,7 @@ else if (obj instanceof MListItem) { @Override public String getToolTipText(MouseEvent event) { - Point mousePos = getCachedMousePosition(); + Point mousePos = getMousePosition(); if (mousePos == null) { return null; } @@ -494,7 +494,7 @@ private int paintButton(Graphics2D g2, Rectangle clipBound, int endOfButtonRun, } private Color getButtonColor(MListButton button) { - Point pt = getCachedMousePosition(); + Point pt = getMousePosition(); if (pt == null) { return button.getBackground(); } @@ -509,9 +509,8 @@ private Color getButtonColor(MListButton button) { return button.getBackground(); } - @Nullable - private Point getCachedMousePosition() { + @Override + public Point getMousePosition() throws HeadlessException { return mousePositionCache.getMousePosition(); } - } diff --git a/protege-editor-core/src/main/java/org/protege/editor/owl/ui/util/MousePositionCache.java b/protege-editor-core/src/main/java/org/protege/editor/owl/ui/util/MousePositionCache.java index 9eeb5f20e..3619557e2 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/owl/ui/util/MousePositionCache.java +++ b/protege-editor-core/src/main/java/org/protege/editor/owl/ui/util/MousePositionCache.java @@ -5,6 +5,7 @@ import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; +import java.util.function.Supplier; import static com.google.common.base.Preconditions.checkNotNull; @@ -18,21 +19,27 @@ public class MousePositionCache { @Nonnull private final Component component; + @Nonnull + private final Supplier mousePositionSupplier; + private boolean stale = true; private Point cachedPosition = null; - private MousePositionCache(@Nonnull Component component) { + private MousePositionCache(@Nonnull Component component, + @Nonnull Supplier mousePositionSupplier) { this.component = checkNotNull(component); + this.mousePositionSupplier = checkNotNull(mousePositionSupplier); } - public static MousePositionCache createAndInstall(@Nonnull Component component) { - MousePositionCache cache = new MousePositionCache(component); - cache.attacheListeners(); + public static MousePositionCache createAndInstall(@Nonnull Component component, + @Nonnull Supplier mousePositionSupplier) { + MousePositionCache cache = new MousePositionCache(component, mousePositionSupplier); + cache.attachListeners(); return cache; } - private void attacheListeners() { + private void attachListeners() { component.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { @@ -48,7 +55,7 @@ private void markAsStale() { @Nullable public Point getMousePosition() { if(stale) { - cachedPosition = component.getMousePosition(); + cachedPosition = mousePositionSupplier.get(); stale = false; } return cachedPosition;