Menu

#13 SwingBox: Opening a SwingBox BrowserPane window affects behavior of other components

accepted
nobody
None
1
2014-07-15
2013-03-02
Anonymous
No

When SwingBox BrowserPane component is opened in a complex application, other components embedding JEditorPane render content using SwingBoxEditorKit, which is not desired and leads to unpredictable results.

The source of the problem is that BrowserPane in the registerEditorKit method calls registerEditorKitForContentType for types which are by default rendered in JEditorPane. This affects each component embedding JEditorPane and not only BrowserPane.

Proposed fix is as follows: Instead of registering Editor Kit for types supported by Swingbox, override the getEditorKitForContentType method serving SwingBoxEditorKit directly where content type is supported by BrowserPane. This approach leaves MIME types registrations intact for other components.

--- "a/BrowserPane-0b5abf8-left.java"
+++ "b/org\\fit\\cssbox\\swingbox\\BrowserPane.java"
@@ -72,6 +72,7 @@ public class BrowserPane extends JEditorPane
     private InputStream loadingStream;
     private Hashtable<String, Object> pageProperties;
     private Document document;
+    private static EditorKit swingBoxEditorKit = null;

     // "org.fit.cssbox.swingbox.SwingBoxEditorKit"
     protected String HtmlEditorKitClass = "org.fit.cssbox.swingbox.SwingBoxEditorKit";
@@ -96,8 +97,10 @@ public class BrowserPane extends JEditorPane
         java.security.Security
                 .addProvider(new com.sun.net.ssl.internal.ssl.Provider());

-        // Register custom EditorKit
-        registerEditorKit();
+        // Create custom EditorKit if needed
+        if (swingBoxEditorKit == null) {
+            swingBoxEditorKit = new SwingBoxEditorKit();
+        }

         setEditable(false);
         setContentType("text/html");
@@ -361,20 +364,13 @@ public class BrowserPane extends JEditorPane
         return null;
     }

-    private void registerEditorKit()
-    {
-        ClassLoader loader = getClass().getClassLoader();
-        if (loader == null)
-        {
-            loader = Thread.currentThread().getContextClassLoader();
+    @Override
+    public EditorKit getEditorKitForContentType(String type) {
+        if (type.equalsIgnoreCase("text/html") || type.equalsIgnoreCase("application/xhtml+xml")
+                || type.equalsIgnoreCase("text/xhtml")) {
+            return swingBoxEditorKit;
         }
-
-        registerEditorKitForContentType("text/html", HtmlEditorKitClass, loader);
-        registerEditorKitForContentType("application/xhtml+xml",
-                HtmlEditorKitClass, loader);
-        // this is not official MIME, but may be used
-        registerEditorKitForContentType("text/xhtml", HtmlEditorKitClass,
-                loader);
+        return super.getEditorKitForContentType(type);
     }

     @Override

Discussion

  • Radek Burget

    Radek Burget - 2013-03-06

    Many thanks for providing the solution. I am not a Swing expert so I welcome any comments on this. I will do some more tests and I will include your patch in trunk.

     
  • Radek Burget

    Radek Burget - 2013-03-06
    • status: open --> accepted
     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.