< prev index next >

src/java.desktop/share/classes/javax/swing/text/html/HTMLDocument.java

Print this page




 166  * after the <code>DIV</code> element.  The newly inserted elements
 167  * become siblings of the <code>DIV</code> element.</p>
 168  *
 169  * <h3>Replacing elements</h3>
 170  *
 171  * <p>Elements and all their descendants can be replaced by using the
 172  * methods <code>setInnerHTML</code> and <code>setOuterHTML</code>.
 173  * For example, if <code>e</code> is the <code>DIV</code> element,
 174  * <code>d.setInnerHTML(e, "&lt;ul&gt;&lt;li&gt;List
 175  * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces all children paragraphs with
 176  * the list, and <code>d.setOuterHTML(e, "&lt;ul&gt;&lt;li&gt;List
 177  * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces the <code>DIV</code> element
 178  * itself.  In latter case the parent of the list is the
 179  * <code>BODY</code> element.
 180  *
 181  * <h3>Summary</h3>
 182  *
 183  * <p>The following table shows the example document and the results
 184  * of various methods described above.</p>
 185  *
 186  * <table border=1 cellspacing=0 summary="HTML Content of example above">

 187  *   <tr>
 188  *     <th>Example</th>
 189  *     <th><code>insertAfterStart</code></th>
 190  *     <th><code>insertBeforeEnd</code></th>
 191  *     <th><code>insertBeforeStart</code></th>
 192  *     <th><code>insertAfterEnd</code></th>
 193  *     <th><code>setInnerHTML</code></th>
 194  *     <th><code>setOuterHTML</code></th>
 195  *   </tr>
 196  *   <tr valign="top">
 197  *     <td style="white-space:nowrap">
 198  *       <div style="background-color: silver;">
 199  *         <p>Paragraph 1</p>
 200  *         <p>Paragraph 2</p>
 201  *       </div>
 202  *     </td>
 203  * <!--insertAfterStart-->
 204  *     <td style="white-space:nowrap">
 205  *       <div style="background-color: silver;">
 206  *         <ul style="color: red;">


2112      * <dd>Produce an embedded graphical element.
2113      * <dt>Form
2114      * <dd>Produce an element that is like the embedded graphical
2115      * element, except that it also has a component model associated
2116      * with it.
2117      * <dt>Hidden
2118      * <dd>Create an element that is hidden from view when the
2119      * document is being viewed read-only, and visible when the
2120      * document is being edited.  This is useful to keep the
2121      * model from losing information, and used to store things
2122      * like comments and unrecognized tags.
2123      *
2124      * </dl>
2125      * <p>
2126      * Currently, &lt;APPLET&gt;, &lt;PARAM&gt;, &lt;MAP&gt;, &lt;AREA&gt;, &lt;LINK&gt;,
2127      * &lt;SCRIPT&gt; and &lt;STYLE&gt; are unsupported.
2128      *
2129      * <p>
2130      * The assignment of the actions described is shown in the
2131      * following table for the tags defined in <code>HTML.Tag</code>.
2132      * <table border=1 summary="HTML tags and assigned actions">



2133      * <tr><th>Tag</th><th>Action</th></tr>


2134      * <tr><td><code>HTML.Tag.A</code>         <td>CharacterAction
2135      * <tr><td><code>HTML.Tag.ADDRESS</code>   <td>CharacterAction
2136      * <tr><td><code>HTML.Tag.APPLET</code>    <td>HiddenAction
2137      * <tr><td><code>HTML.Tag.AREA</code>      <td>AreaAction
2138      * <tr><td><code>HTML.Tag.B</code>         <td>CharacterAction
2139      * <tr><td><code>HTML.Tag.BASE</code>      <td>BaseAction
2140      * <tr><td><code>HTML.Tag.BASEFONT</code>  <td>CharacterAction
2141      * <tr><td><code>HTML.Tag.BIG</code>       <td>CharacterAction
2142      * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction
2143      * <tr><td><code>HTML.Tag.BODY</code>      <td>BlockAction
2144      * <tr><td><code>HTML.Tag.BR</code>        <td>SpecialAction
2145      * <tr><td><code>HTML.Tag.CAPTION</code>   <td>BlockAction
2146      * <tr><td><code>HTML.Tag.CENTER</code>    <td>BlockAction
2147      * <tr><td><code>HTML.Tag.CITE</code>      <td>CharacterAction
2148      * <tr><td><code>HTML.Tag.CODE</code>      <td>CharacterAction
2149      * <tr><td><code>HTML.Tag.DD</code>        <td>BlockAction
2150      * <tr><td><code>HTML.Tag.DFN</code>       <td>CharacterAction
2151      * <tr><td><code>HTML.Tag.DIR</code>       <td>BlockAction
2152      * <tr><td><code>HTML.Tag.DIV</code>       <td>BlockAction
2153      * <tr><td><code>HTML.Tag.DL</code>        <td>BlockAction


2186      * <tr><td><code>HTML.Tag.SAMP</code>      <td>CharacterAction
2187      * <tr><td><code>HTML.Tag.SCRIPT</code>    <td>HiddenAction
2188      * <tr><td><code>HTML.Tag.SELECT</code>    <td>FormAction
2189      * <tr><td><code>HTML.Tag.SMALL</code>     <td>CharacterAction
2190      * <tr><td><code>HTML.Tag.STRIKE</code>    <td>CharacterAction
2191      * <tr><td><code>HTML.Tag.S</code>         <td>CharacterAction
2192      * <tr><td><code>HTML.Tag.STRONG</code>    <td>CharacterAction
2193      * <tr><td><code>HTML.Tag.STYLE</code>     <td>StyleAction
2194      * <tr><td><code>HTML.Tag.SUB</code>       <td>CharacterAction
2195      * <tr><td><code>HTML.Tag.SUP</code>       <td>CharacterAction
2196      * <tr><td><code>HTML.Tag.TABLE</code>     <td>BlockAction
2197      * <tr><td><code>HTML.Tag.TD</code>        <td>BlockAction
2198      * <tr><td><code>HTML.Tag.TEXTAREA</code>  <td>FormAction
2199      * <tr><td><code>HTML.Tag.TH</code>        <td>BlockAction
2200      * <tr><td><code>HTML.Tag.TITLE</code>     <td>TitleAction
2201      * <tr><td><code>HTML.Tag.TR</code>        <td>BlockAction
2202      * <tr><td><code>HTML.Tag.TT</code>        <td>CharacterAction
2203      * <tr><td><code>HTML.Tag.U</code>         <td>CharacterAction
2204      * <tr><td><code>HTML.Tag.UL</code>        <td>BlockAction
2205      * <tr><td><code>HTML.Tag.VAR</code>       <td>CharacterAction

2206      * </table>
2207      * <p>
2208      * Once &lt;/html&gt; is encountered, the Actions are no longer notified.
2209      */
2210     public class HTMLReader extends HTMLEditorKit.ParserCallback {
2211 
2212         /**
2213          * Constructs an HTMLReader using default pop and push depth and no tag to insert.
2214          *
2215          * @param offset the starting offset
2216          */
2217         public HTMLReader(int offset) {
2218             this(offset, 0, 0, null);
2219         }
2220 
2221         /**
2222          * Constructs an HTMLReader.
2223          *
2224          * @param offset the starting offset
2225          * @param popDepth how many parents to ascend before insert new element


3368                 if ((name != null) && (value != null)) {
3369                     ElementSpec objSpec = parseBuffer.lastElement();
3370                     MutableAttributeSet objAttr = (MutableAttributeSet) objSpec.getAttributes();
3371                     objAttr.addAttribute(name, value);
3372                 }
3373             }
3374         }
3375 
3376         /**
3377          * Action to support forms by building all of the elements
3378          * used to represent form controls.  This will process
3379          * the &lt;INPUT&gt;, &lt;TEXTAREA&gt;, &lt;SELECT&gt;,
3380          * and &lt;OPTION&gt; tags.  The element created by
3381          * this action is expected to have the attribute
3382          * <code>StyleConstants.ModelAttribute</code> set to
3383          * the model that holds the state for the form control.
3384          * This enables multiple views, and allows document to
3385          * be iterated over picking up the data of the form.
3386          * The following are the model assignments for the
3387          * various type of form elements.
3388          * <table summary="model assignments for the various types of form elements">




3389          * <tr>
3390          *   <th>Element Type
3391          *   <th>Model Type



3392          * <tr>
3393          *   <td>input, type button
3394          *   <td>{@link DefaultButtonModel}
3395          * <tr>
3396          *   <td>input, type checkbox
3397          *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
3398          * <tr>
3399          *   <td>input, type image
3400          *   <td>{@link DefaultButtonModel}
3401          * <tr>
3402          *   <td>input, type password
3403          *   <td>{@link PlainDocument}
3404          * <tr>
3405          *   <td>input, type radio
3406          *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
3407          * <tr>
3408          *   <td>input, type reset
3409          *   <td>{@link DefaultButtonModel}
3410          * <tr>
3411          *   <td>input, type submit
3412          *   <td>{@link DefaultButtonModel}
3413          * <tr>
3414          *   <td>input, type text or type is null.
3415          *   <td>{@link PlainDocument}
3416          * <tr>
3417          *   <td>select
3418          *   <td>{@link DefaultComboBoxModel} or an {@link DefaultListModel}, with an item type of Option
3419          * <tr>
3420          *   <td>textarea
3421          *   <td>{@link PlainDocument}

3422          * </table>
3423          *
3424          */
3425         public class FormAction extends SpecialAction {
3426 
3427             public void start(HTML.Tag t, MutableAttributeSet attr) {
3428                 if (t == HTML.Tag.INPUT) {
3429                     String type = (String)
3430                         attr.getAttribute(HTML.Attribute.TYPE);
3431                     /*
3432                      * if type is not defined the default is
3433                      * assumed to be text.
3434                      */
3435                     if (type == null) {
3436                         type = "text";
3437                         attr.addAttribute(HTML.Attribute.TYPE, "text");
3438                     }
3439                     setModel(type, attr);
3440                 } else if (t == HTML.Tag.TEXTAREA) {
3441                     inTextArea = true;




 166  * after the <code>DIV</code> element.  The newly inserted elements
 167  * become siblings of the <code>DIV</code> element.</p>
 168  *
 169  * <h3>Replacing elements</h3>
 170  *
 171  * <p>Elements and all their descendants can be replaced by using the
 172  * methods <code>setInnerHTML</code> and <code>setOuterHTML</code>.
 173  * For example, if <code>e</code> is the <code>DIV</code> element,
 174  * <code>d.setInnerHTML(e, "&lt;ul&gt;&lt;li&gt;List
 175  * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces all children paragraphs with
 176  * the list, and <code>d.setOuterHTML(e, "&lt;ul&gt;&lt;li&gt;List
 177  * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces the <code>DIV</code> element
 178  * itself.  In latter case the parent of the list is the
 179  * <code>BODY</code> element.
 180  *
 181  * <h3>Summary</h3>
 182  *
 183  * <p>The following table shows the example document and the results
 184  * of various methods described above.</p>
 185  *
 186  * <table class="plain">
 187  * <caption style="display:none">HTML Content of example above</caption>
 188  *   <tr>
 189  *     <th>Example</th>
 190  *     <th><code>insertAfterStart</code></th>
 191  *     <th><code>insertBeforeEnd</code></th>
 192  *     <th><code>insertBeforeStart</code></th>
 193  *     <th><code>insertAfterEnd</code></th>
 194  *     <th><code>setInnerHTML</code></th>
 195  *     <th><code>setOuterHTML</code></th>
 196  *   </tr>
 197  *   <tr valign="top">
 198  *     <td style="white-space:nowrap">
 199  *       <div style="background-color: silver;">
 200  *         <p>Paragraph 1</p>
 201  *         <p>Paragraph 2</p>
 202  *       </div>
 203  *     </td>
 204  * <!--insertAfterStart-->
 205  *     <td style="white-space:nowrap">
 206  *       <div style="background-color: silver;">
 207  *         <ul style="color: red;">


2113      * <dd>Produce an embedded graphical element.
2114      * <dt>Form
2115      * <dd>Produce an element that is like the embedded graphical
2116      * element, except that it also has a component model associated
2117      * with it.
2118      * <dt>Hidden
2119      * <dd>Create an element that is hidden from view when the
2120      * document is being viewed read-only, and visible when the
2121      * document is being edited.  This is useful to keep the
2122      * model from losing information, and used to store things
2123      * like comments and unrecognized tags.
2124      *
2125      * </dl>
2126      * <p>
2127      * Currently, &lt;APPLET&gt;, &lt;PARAM&gt;, &lt;MAP&gt;, &lt;AREA&gt;, &lt;LINK&gt;,
2128      * &lt;SCRIPT&gt; and &lt;STYLE&gt; are unsupported.
2129      *
2130      * <p>
2131      * The assignment of the actions described is shown in the
2132      * following table for the tags defined in <code>HTML.Tag</code>.
2133      *
2134      * <table class="striped">
2135      * <caption style="display:none">HTML tags and assigned actions</caption>
2136      * <thead>
2137      * <tr><th>Tag</th><th>Action</th></tr>
2138      * </thead>
2139      * <tbody>
2140      * <tr><td><code>HTML.Tag.A</code>         <td>CharacterAction
2141      * <tr><td><code>HTML.Tag.ADDRESS</code>   <td>CharacterAction
2142      * <tr><td><code>HTML.Tag.APPLET</code>    <td>HiddenAction
2143      * <tr><td><code>HTML.Tag.AREA</code>      <td>AreaAction
2144      * <tr><td><code>HTML.Tag.B</code>         <td>CharacterAction
2145      * <tr><td><code>HTML.Tag.BASE</code>      <td>BaseAction
2146      * <tr><td><code>HTML.Tag.BASEFONT</code>  <td>CharacterAction
2147      * <tr><td><code>HTML.Tag.BIG</code>       <td>CharacterAction
2148      * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction
2149      * <tr><td><code>HTML.Tag.BODY</code>      <td>BlockAction
2150      * <tr><td><code>HTML.Tag.BR</code>        <td>SpecialAction
2151      * <tr><td><code>HTML.Tag.CAPTION</code>   <td>BlockAction
2152      * <tr><td><code>HTML.Tag.CENTER</code>    <td>BlockAction
2153      * <tr><td><code>HTML.Tag.CITE</code>      <td>CharacterAction
2154      * <tr><td><code>HTML.Tag.CODE</code>      <td>CharacterAction
2155      * <tr><td><code>HTML.Tag.DD</code>        <td>BlockAction
2156      * <tr><td><code>HTML.Tag.DFN</code>       <td>CharacterAction
2157      * <tr><td><code>HTML.Tag.DIR</code>       <td>BlockAction
2158      * <tr><td><code>HTML.Tag.DIV</code>       <td>BlockAction
2159      * <tr><td><code>HTML.Tag.DL</code>        <td>BlockAction


2192      * <tr><td><code>HTML.Tag.SAMP</code>      <td>CharacterAction
2193      * <tr><td><code>HTML.Tag.SCRIPT</code>    <td>HiddenAction
2194      * <tr><td><code>HTML.Tag.SELECT</code>    <td>FormAction
2195      * <tr><td><code>HTML.Tag.SMALL</code>     <td>CharacterAction
2196      * <tr><td><code>HTML.Tag.STRIKE</code>    <td>CharacterAction
2197      * <tr><td><code>HTML.Tag.S</code>         <td>CharacterAction
2198      * <tr><td><code>HTML.Tag.STRONG</code>    <td>CharacterAction
2199      * <tr><td><code>HTML.Tag.STYLE</code>     <td>StyleAction
2200      * <tr><td><code>HTML.Tag.SUB</code>       <td>CharacterAction
2201      * <tr><td><code>HTML.Tag.SUP</code>       <td>CharacterAction
2202      * <tr><td><code>HTML.Tag.TABLE</code>     <td>BlockAction
2203      * <tr><td><code>HTML.Tag.TD</code>        <td>BlockAction
2204      * <tr><td><code>HTML.Tag.TEXTAREA</code>  <td>FormAction
2205      * <tr><td><code>HTML.Tag.TH</code>        <td>BlockAction
2206      * <tr><td><code>HTML.Tag.TITLE</code>     <td>TitleAction
2207      * <tr><td><code>HTML.Tag.TR</code>        <td>BlockAction
2208      * <tr><td><code>HTML.Tag.TT</code>        <td>CharacterAction
2209      * <tr><td><code>HTML.Tag.U</code>         <td>CharacterAction
2210      * <tr><td><code>HTML.Tag.UL</code>        <td>BlockAction
2211      * <tr><td><code>HTML.Tag.VAR</code>       <td>CharacterAction
2212      * </tbody>
2213      * </table>
2214      * <p>
2215      * Once &lt;/html&gt; is encountered, the Actions are no longer notified.
2216      */
2217     public class HTMLReader extends HTMLEditorKit.ParserCallback {
2218 
2219         /**
2220          * Constructs an HTMLReader using default pop and push depth and no tag to insert.
2221          *
2222          * @param offset the starting offset
2223          */
2224         public HTMLReader(int offset) {
2225             this(offset, 0, 0, null);
2226         }
2227 
2228         /**
2229          * Constructs an HTMLReader.
2230          *
2231          * @param offset the starting offset
2232          * @param popDepth how many parents to ascend before insert new element


3375                 if ((name != null) && (value != null)) {
3376                     ElementSpec objSpec = parseBuffer.lastElement();
3377                     MutableAttributeSet objAttr = (MutableAttributeSet) objSpec.getAttributes();
3378                     objAttr.addAttribute(name, value);
3379                 }
3380             }
3381         }
3382 
3383         /**
3384          * Action to support forms by building all of the elements
3385          * used to represent form controls.  This will process
3386          * the &lt;INPUT&gt;, &lt;TEXTAREA&gt;, &lt;SELECT&gt;,
3387          * and &lt;OPTION&gt; tags.  The element created by
3388          * this action is expected to have the attribute
3389          * <code>StyleConstants.ModelAttribute</code> set to
3390          * the model that holds the state for the form control.
3391          * This enables multiple views, and allows document to
3392          * be iterated over picking up the data of the form.
3393          * The following are the model assignments for the
3394          * various type of form elements.
3395          *
3396          <table class="striped">
3397          * <caption style="display:none">Model assignments for the various types
3398          * of form elements</caption>
3399          * <thead>
3400          * <tr>
3401          *   <th>Element Type
3402          *   <th>Model Type
3403          * </tr>
3404          * </thead>
3405          * <tbody>
3406          * <tr>
3407          *   <td>input, type button
3408          *   <td>{@link DefaultButtonModel}
3409          * <tr>
3410          *   <td>input, type checkbox
3411          *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
3412          * <tr>
3413          *   <td>input, type image
3414          *   <td>{@link DefaultButtonModel}
3415          * <tr>
3416          *   <td>input, type password
3417          *   <td>{@link PlainDocument}
3418          * <tr>
3419          *   <td>input, type radio
3420          *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
3421          * <tr>
3422          *   <td>input, type reset
3423          *   <td>{@link DefaultButtonModel}
3424          * <tr>
3425          *   <td>input, type submit
3426          *   <td>{@link DefaultButtonModel}
3427          * <tr>
3428          *   <td>input, type text or type is null.
3429          *   <td>{@link PlainDocument}
3430          * <tr>
3431          *   <td>select
3432          *   <td>{@link DefaultComboBoxModel} or an {@link DefaultListModel}, with an item type of Option
3433          * <tr>
3434          *   <td>textarea
3435          *   <td>{@link PlainDocument}
3436          * </tbody>
3437          * </table>
3438          *
3439          */
3440         public class FormAction extends SpecialAction {
3441 
3442             public void start(HTML.Tag t, MutableAttributeSet attr) {
3443                 if (t == HTML.Tag.INPUT) {
3444                     String type = (String)
3445                         attr.getAttribute(HTML.Attribute.TYPE);
3446                     /*
3447                      * if type is not defined the default is
3448                      * assumed to be text.
3449                      */
3450                     if (type == null) {
3451                         type = "text";
3452                         attr.addAttribute(HTML.Attribute.TYPE, "text");
3453                     }
3454                     setModel(type, attr);
3455                 } else if (t == HTML.Tag.TEXTAREA) {
3456                     inTextArea = true;


< prev index next >