< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicHTML.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.basic;
  26 
  27 import java.io.*;
  28 import java.awt.*;
  29 import java.net.URL;
  30 

  31 import javax.swing.*;
  32 import javax.swing.text.*;
  33 import javax.swing.text.html.*;
  34 
  35 import sun.swing.SwingUtilities2;
  36 
  37 /**
  38  * Support for providing html views for the swing components.
  39  * This translates a simple html string to a javax.swing.text.View
  40  * implementation that can render the html and provide the necessary
  41  * layout semantics.
  42  *
  43  * @author  Timothy Prinzing
  44  * @since 1.3
  45  */
  46 public class BasicHTML {
  47 
  48     /**
  49      * Create an html renderer for the given component and
  50      * string of html.


 206      * This method is useful for ComponentUI implementations
 207      * that are static (i.e. shared) and get their state
 208      * entirely from the JComponent.
 209      *
 210      * @param c a component
 211      * @param text a text
 212      */
 213     public static void updateRenderer(JComponent c, String text) {
 214         View value = null;
 215         View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
 216         Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
 217         if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
 218             value = BasicHTML.createHTMLView(c, text);
 219         }
 220         if (value != oldValue && oldValue != null) {
 221             for (int i = 0; i < oldValue.getViewCount(); i++) {
 222                 oldValue.getView(i).setParent(null);
 223             }
 224         }
 225         c.putClientProperty(BasicHTML.propertyKey, value);








 226     }
 227 
 228     /**
 229      * If this client property of a JComponent is set to Boolean.TRUE
 230      * the component's 'text' property is never treated as HTML.
 231      */
 232     private static final String htmlDisable = "html.disable";
 233 
 234     /**
 235      * Key to use for the html renderer when stored as a
 236      * client property of a JComponent.
 237      */
 238     public static final String propertyKey = "html";
 239 
 240     /**
 241      * Key stored as a client property to indicate the base that relative
 242      * references are resolved against. For example, lets say you keep
 243      * your images in the directory resources relative to the code path,
 244      * you would use the following the set the base:
 245      * <pre>


   1 /*
   2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.basic;
  26 
  27 import java.io.*;
  28 import java.awt.*;
  29 import java.net.URL;
  30 
  31 import javax.accessibility.AccessibleContext;
  32 import javax.swing.*;
  33 import javax.swing.text.*;
  34 import javax.swing.text.html.*;
  35 
  36 import sun.swing.SwingUtilities2;
  37 
  38 /**
  39  * Support for providing html views for the swing components.
  40  * This translates a simple html string to a javax.swing.text.View
  41  * implementation that can render the html and provide the necessary
  42  * layout semantics.
  43  *
  44  * @author  Timothy Prinzing
  45  * @since 1.3
  46  */
  47 public class BasicHTML {
  48 
  49     /**
  50      * Create an html renderer for the given component and
  51      * string of html.


 207      * This method is useful for ComponentUI implementations
 208      * that are static (i.e. shared) and get their state
 209      * entirely from the JComponent.
 210      *
 211      * @param c a component
 212      * @param text a text
 213      */
 214     public static void updateRenderer(JComponent c, String text) {
 215         View value = null;
 216         View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
 217         Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
 218         if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
 219             value = BasicHTML.createHTMLView(c, text);
 220         }
 221         if (value != oldValue && oldValue != null) {
 222             for (int i = 0; i < oldValue.getViewCount(); i++) {
 223                 oldValue.getView(i).setParent(null);
 224             }
 225         }
 226         c.putClientProperty(BasicHTML.propertyKey, value);
 227         if (value != null) {
 228             try {
 229                 c.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,
 230                     value.getDocument().getText(1, value.getDocument().getLength() - 1));
 231             } catch (BadLocationException e) {
 232                 e.printStackTrace();
 233             }
 234         }
 235     }
 236 
 237     /**
 238      * If this client property of a JComponent is set to Boolean.TRUE
 239      * the component's 'text' property is never treated as HTML.
 240      */
 241     private static final String htmlDisable = "html.disable";
 242 
 243     /**
 244      * Key to use for the html renderer when stored as a
 245      * client property of a JComponent.
 246      */
 247     public static final String propertyKey = "html";
 248 
 249     /**
 250      * Key stored as a client property to indicate the base that relative
 251      * references are resolved against. For example, lets say you keep
 252      * your images in the directory resources relative to the code path,
 253      * you would use the following the set the base:
 254      * <pre>


< prev index next >