Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NbClipboard adjustments: Ease retries, remove dead code #7668

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions platform/o.n.bootstrap/src/org/netbeans/NbClipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public final class NbClipboard extends ExClipboard
private Lookup.Result<ExClipboard.Convertor> result;
final boolean slowSystemClipboard;
private Transferable last;
private long lastWindowActivated;
private long lastWindowDeactivated;
private Reference<Object> lastWindowDeactivatedSource = new WeakReference<>(null);
private volatile Task setContentsTask = Task.EMPTY;
private volatile Task getContentsTask = Task.EMPTY;
private boolean anyWindowIsActivated = true;
Expand Down Expand Up @@ -279,14 +276,12 @@ final void waitFinished () {
getContentsTask.waitFinished ();
}

final void activateWindowHack (boolean reschedule) {
/** Used by tests only. */
final void activateWindowHack () {
// if WINDOW_DEACTIVATED is followed immediatelly with
// WINDOW_ACTIVATED then it is JDK bug described in
// issue 41098.
lastWindowActivated = System.currentTimeMillis();
if (reschedule) {
scheduleGetFromSystemClipboard(true);
}
scheduleGetFromSystemClipboard(true);
}

private void logFlavors (Transferable trans, Level level, boolean content) {
Expand Down Expand Up @@ -340,8 +335,6 @@ public void eventDispatched(AWTEvent ev) {
return;

if (ev.getID() == WindowEvent.WINDOW_DEACTIVATED) {
lastWindowDeactivated = System.currentTimeMillis();
lastWindowDeactivatedSource = new WeakReference<>(ev.getSource());
anyWindowIsActivated = false;
if( Utilities.isWindows() ) {
//#247585 - even listening to clipboard changes when the window isn't active
Expand All @@ -356,10 +349,6 @@ public void eventDispatched(AWTEvent ev) {
fireChange();
}
anyWindowIsActivated = true;
if (System.currentTimeMillis() - lastWindowDeactivated < 100 &&
ev.getSource() == lastWindowDeactivatedSource.get()) {
activateWindowHack (false);
}
if (log.isLoggable (Level.FINE)) {
log.log (Level.FINE, "window activated scheduling update"); // NOI18N
}
Expand Down Expand Up @@ -435,20 +424,25 @@ public void run() {
// that is used because accessing the clipboard can block
// indefinitely. Running the access loop here is deemed similar
// in nature.
final int MAX_TRIES = 50;

/* The loop will actually stop before getting to 10 iterations, per the delay
formula and conditional throw. But keep the MAX_TRIES just as a fail-safe. */
final int MAX_TRIES = 10;
final long start = System.currentTimeMillis();
int delay = 20;
for (int i = 0; i < MAX_TRIES; i++) {
try {
transferable = systemClipboard.getContents(this);
break;
} catch (IllegalStateException ex) {
// Throw exception if retries failed
if (i == (MAX_TRIES - 1) || (System.currentTimeMillis() - start) > 980L) {
if (i == (MAX_TRIES - 1) || (System.currentTimeMillis() + delay - start) > 1000L) {
throw ex;
} else {
log.log(Level.INFO, "systemClipboard#getContents threw IllegalStateException (try: {0})", i + 1); // NOI18N
log.log(Level.INFO, "systemClipboard#getContents ISE, attempt {0}", i + 1); // NOI18N
}
Thread.sleep(20); // Give system time to settle
Thread.sleep(delay); // Give system time to settle
delay *= 2;
eirikbakke marked this conversation as resolved.
Show resolved Hide resolved
}
}
superSetContents(transferable, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testWhenCallingGetContentsItChecksSystemClipboardFirstTimeAfterActiv


// just simulate initial switch to NetBeans main window
ec.activateWindowHack (true);
ec.activateWindowHack ();
waitFinished (ec);

if (listenerCalls == 0) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public void testWhenCallingGetContentsItChecksSystemClipboardFirstTimeAfterActiv
if (slowClipboardHack ()) {
assertEquals ("The getContents rechecked the clipboard just for the first time, not now, so the content is the same", "data2", s);

ec.activateWindowHack (true);
ec.activateWindowHack ();
Thread.sleep (200);

t = this.ec.getContents(this);
Expand Down
Loading