Skip to content

Commit

Permalink
InputMethodRequests for JFXPanel should be processed on JavaFX thread
Browse files Browse the repository at this point in the history
  • Loading branch information
beldenfox committed Dec 30, 2023
1 parent 2493a23 commit 00ec7b1
Showing 1 changed file with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package javafx.embed.swing;

import com.sun.javafx.application.PlatformImpl;
import com.sun.javafx.collections.ObservableListWrapper;
import com.sun.javafx.scene.input.ExtendedInputMethodRequests;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -56,42 +57,63 @@ public InputMethodRequestsAdapter(javafx.scene.input.InputMethodRequests fxReque
this.fxRequests = fxRequests;
}

// InputMethodRequests should all come in on the same thread so
// it's safe to re-use these.
private Point2D pointValue = null;
private int intValue = 0;
private String stringValue = null;

@Override
public Rectangle getTextLocation(TextHitInfo offset) {
Point2D result = fxRequests.getTextLocation(offset.getInsertionIndex());
return new Rectangle((int)result.getX(), (int)result.getY(), 0, 0);
pointValue = new Point2D(0.0, 0.0);
PlatformImpl.runAndWait(() -> {
pointValue = fxRequests.getTextLocation(offset.getInsertionIndex());
});
return new Rectangle((int)pointValue.getX(), (int)pointValue.getY(), 0, 0);
}

@Override
public TextHitInfo getLocationOffset(int x, int y) {
int result = fxRequests.getLocationOffset(x, y);
return TextHitInfo.afterOffset(result);
intValue = 0;
PlatformImpl.runAndWait(() -> {
intValue = fxRequests.getLocationOffset(x, y);
});
return TextHitInfo.afterOffset(intValue);
}

@Override
public int getInsertPositionOffset() {
intValue = 0;
if (fxRequests instanceof ExtendedInputMethodRequests) {
return ((ExtendedInputMethodRequests)fxRequests).getInsertPositionOffset();
PlatformImpl.runAndWait(() -> {
intValue = ((ExtendedInputMethodRequests)fxRequests).getInsertPositionOffset();
});
}
return 0;
return intValue;
}

@Override
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex, AttributedCharacterIterator.Attribute[] attributes) {
String result = null;
if (fxRequests instanceof ExtendedInputMethodRequests) {
result = ((ExtendedInputMethodRequests)fxRequests).getCommittedText(beginIndex, endIndex);
PlatformImpl.runAndWait(() -> {
stringValue = ((ExtendedInputMethodRequests)fxRequests).getCommittedText(beginIndex, endIndex);
});
}
if (result == null) result = "";
return new AttributedString(result).getIterator();
String text = stringValue;
stringValue = null;
if (text == null) text = "";
return new AttributedString(text).getIterator();
}

@Override
public int getCommittedTextLength() {
intValue = 0;
if (fxRequests instanceof ExtendedInputMethodRequests) {
return ((ExtendedInputMethodRequests)fxRequests).getCommittedTextLength();
PlatformImpl.runAndWait(() -> {
intValue = ((ExtendedInputMethodRequests)fxRequests).getCommittedTextLength();
});
}
return 0;
return intValue;
}

@Override
Expand All @@ -102,7 +124,11 @@ public AttributedCharacterIterator cancelLatestCommittedText(AttributedCharacter

@Override
public AttributedCharacterIterator getSelectedText(AttributedCharacterIterator.Attribute[] attributes) {
String text = fxRequests.getSelectedText();
PlatformImpl.runAndWait(() -> {
stringValue = fxRequests.getSelectedText();
});
String text = stringValue;
stringValue = null;
if (text == null) text = "";
return new AttributedString(text).getIterator();
}
Expand Down

1 comment on commit 00ec7b1

@prsadhuk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that JDK-8221261 is integrated, I guess you can raise this PR...

Please sign in to comment.