Skip to content

Commit

Permalink
8296832: Improve Swing platform support
Browse files Browse the repository at this point in the history
Reviewed-by: mbalao
Backport-of: a81c810a76d91b79917417ed22e5e5aa530690ca
  • Loading branch information
Yuri Nesterenko authored and RealCLanger committed Apr 12, 2023
1 parent 36871ab commit 91328b3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.swing.text.*;
import javax.swing.text.html.*;

import sun.swing.SwingAccessor;
import sun.swing.SwingUtilities2;

/**
Expand Down Expand Up @@ -215,7 +216,7 @@ public static void updateRenderer(JComponent c, String text) {
View value = null;
View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
if (!(Boolean.TRUE.equals(htmlDisabled)) && BasicHTML.isHTMLString(text)) {
value = BasicHTML.createHTMLView(c, text);
}
if (value != oldValue && oldValue != null) {
Expand Down Expand Up @@ -371,15 +372,36 @@ public ViewFactory getViewFactory() {
*/
static class BasicHTMLViewFactory extends HTMLEditorKit.HTMLFactory {
public View create(Element elem) {
View view = super.create(elem);

View view = null;
try {
setAllowHTMLObject();
view = super.create(elem);
} finally {
clearAllowHTMLObject();
}
if (view instanceof ImageView) {
((ImageView)view).setLoadsSynchronously(true);
}
return view;
}
}

private static Boolean useOV = null;

@SuppressWarnings("removal")
private static void setAllowHTMLObject() {
if (useOV == null) {
useOV = java.security.AccessController.doPrivileged(
new sun.security.action.GetBooleanAction(
"swing.html.object"));
};
SwingAccessor.setAllowHTMLObject(useOV);
}

private static void clearAllowHTMLObject() {
SwingAccessor.setAllowHTMLObject(null);
}
}

/**
* The subclass of HTMLDocument that is used as the model. getForeground
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.text.html.parser.ParserDelegator;
import sun.swing.SwingAccessor;

/**
* The Swing JEditorPane text component supports different kinds
Expand Down Expand Up @@ -1306,7 +1307,11 @@ public View create(Element elem) {
(kind == HTML.Tag.TEXTAREA)) {
return new FormView(elem);
} else if (kind == HTML.Tag.OBJECT) {
return new ObjectView(elem);
if (SwingAccessor.getAllowHTMLObject()) {
return new ObjectView(elem);
} else {
return new ObjectView(elem, false);
}
} else if (kind == HTML.Tag.FRAMESET) {
if (elem.getAttributes().isDefined(HTML.Attribute.ROWS)) {
return new FrameSetView(elem, View.Y_AXIS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
*/
public class ObjectView extends ComponentView {

private boolean createComp = true; // default

/**
* Creates a new ObjectView object.
*
Expand All @@ -80,13 +82,21 @@ public ObjectView(Element elem) {
super(elem);
}

ObjectView(Element elem, boolean createComp) {
super(elem);
this.createComp = createComp;
}

/**
* Create the component. The classid is used
* as a specification of the classname, which
* we try to load.
*/
@SuppressWarnings("deprecation")
protected Component createComponent() {
if (!createComp) {
return getUnloadableRepresentation();
}
AttributeSet attr = getElement().getAttributes();
String classname = (String) attr.getAttribute(HTML.Attribute.CLASSID);
try {
Expand Down
15 changes: 15 additions & 0 deletions src/java.desktop/share/classes/sun/swing/SwingAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,19 @@ public static KeyStrokeAccessor getKeyStrokeAccessor() {
public static void setKeyStrokeAccessor(KeyStrokeAccessor accessor) {
SwingAccessor.keyStrokeAccessor = accessor;
}

private static ThreadLocal<Boolean> tlObj = new ThreadLocal<Boolean>();

public static Boolean getAllowHTMLObject() {
Boolean b = tlObj.get();
if (b == null) {
return Boolean.TRUE;
} else {
return b;
}
}

public static void setAllowHTMLObject(Boolean val) {
tlObj.set(val);
}
}

0 comments on commit 91328b3

Please sign in to comment.