-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7551bd7
commit c87f152
Showing
2 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
protege-editor-core/src/main/java/org/protege/editor/owl/ui/util/MousePositionCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |