⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxpdom6.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
</p><a name="wp64817"> </a><p class="pDefinition">We're just flat out ignoring this one. These nodes are used to include binary data in the DOM. As discussed earlier in <a  href="JAXPSAX8.html#wp90267">Choosing your Parser Implementation</a> and <a  href="JAXPSAX12.html#wp65656">Using the DTDHandler and EntityResolver</a>, the MIME types (in conjunction with namespaces) make a better mechanism for that. </p><a name="wp64825"> </a><h4 class="pHeading3">Display the Content in the JTree</h4><a name="wp64826"> </a><p class="pBody">With the content-concatenation out of the way, only a few small programming steps remain. The first is to modify <code class="cCode">toString</code> so that it uses the first line of the node's content for identifying information. Add the code highlighted below to do that: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho extends JPanel{&nbsp;&nbsp;...&nbsp;&nbsp;public class AdapterNode &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;public String toString() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (! nodeName.startsWith(&quot;#&quot;)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += &quot;: &quot; + nodeName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (compress) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String t = content().trim();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x = t.indexOf(&quot;\n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (x &gt;= 0) t = t.substring(0, x);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += &quot; &quot; + t;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (domNode.getNodeValue() != null) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s;&nbsp;&nbsp;&nbsp;&nbsp;} <a name="wp64827"> </a></pre></div><a name="wp64829"> </a><h4 class="pHeading3">Wire the JTree to the JEditorPane</h4><a name="wp64830"> </a><p class="pBody">Returning now to the app's constructor, create a tree selection listener and use to wire the <code class="cCode">JTree</code> to the <code class="cCode">JEditorPane</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho extends JPanel{&nbsp;&nbsp;...&nbsp;&nbsp;public DomEcho()&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;// Build right-side view&nbsp;&nbsp;&nbsp;&nbsp;JEditorPane htmlPane = new JEditorPane(&quot;text/html&quot;,&quot;&quot;);&nbsp;&nbsp;&nbsp;&nbsp;htmlPane.setEditable(false);&nbsp;&nbsp;&nbsp;&nbsp;JScrollPane htmlView = new JScrollPane(htmlPane);&nbsp;&nbsp;&nbsp;&nbsp;htmlView.setPreferredSize( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Dimension( rightWidth, windowHeight ));<code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree.addTreeSelectionListener(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new TreeSelectionListener() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void valueChanged(TreeSelectionEvent e)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreePath p = e.getNewLeadSelectionPath();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (p != null) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AdapterNode adpNode = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(AdapterNode)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.getLastPathComponent();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;htmlPane.setText(adpNode.content());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); </code><a name="wp64831"> </a></pre></div><a name="wp64832"> </a><p class="pBody">Now, when a <code class="cCode">JTree</code> node is selected, it's contents are delivered to the <code class="cCode">htmlPane</code>.    </p><hr><a name="wp64833"> </a><p class="pNote">Note: The <code class="cCode">TreeSelectionListener</code> in this example is created using an anonymous inner-class adapter. If you are programming for the 1.1 version of the platform, you'll need to define an external class for this purpose.</p><hr><a name="wp64834"> </a><p class="pBody">If you compile this version of the app, you'll discover immediately that the <code class="cCode">htmlPane</code> needs to be specified as <code class="cCode">final</code> to be referenced in an inner class, so add the keyword highlighted below:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public DomEcho04(){&nbsp;&nbsp;...&nbsp;&nbsp;// Build right-side view&nbsp;&nbsp;<code class="cCodeBold">final </code>JEditorPane htmlPane = new&nbsp;&nbsp;&nbsp;&nbsp;JEditorPane(&quot;text/html&quot;,&quot;&quot;);&nbsp;&nbsp;htmlPane.setEditable(false);&nbsp;&nbsp;JScrollPane htmlView = new JScrollPane(htmlPane);&nbsp;&nbsp;htmlView.setPreferredSize( &nbsp;&nbsp;&nbsp;&nbsp;new Dimension( rightWidth, windowHeight ));<a name="wp64835"> </a></pre></div><a name="wp64837"> </a><h4 class="pHeading3">Run the App</h4><a name="wp64838"> </a><p class="pBody">When you compile the application and run it on <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample10.xml" target="_blank">slideSample10.xml</a></code> (the browsable version is <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample10-xml.html" target="_blank">slideSample10-xml.html</a></code>), you get a display like that shown in <a  href="JAXPDOM6.html#wp64847">Figure 6-9</a>. Expanding the hierarchy shows that the <code class="cCode">JTree</code> now includes identifying text for a node whenever possible.</p><a name="wp64845"> </a><p class="pBody"></p><div align="left"><img src="images/pd-410b8.gif" height="261" width="403" alt="Collapsed Hierarchy Showing Text in Nodes" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64847"> </a><strong><font >Figure 6-9    Collapsed Hierarchy Showing Text in Nodes</font></strong></p><a name="wp64848"> </a><p class="pBody">Selecting an item that includes XHTML subelements produces a display like that shown in <a  href="JAXPDOM6.html#wp64857">Figure 6-10</a>:</p><a name="wp64855"> </a><p class="pBody"></p><div align="left"><img src="images/pd-410c9.gif" height="229" width="361" alt="Node with &lt;em&gt; Tag Selected" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64857"> </a><strong><font >Figure 6-10    Node with <code class="cCode">&lt;em&gt;</code> Tag Selected</font></strong></p><a name="wp64858"> </a><p class="pBody">Selecting a node that contains an entity reference causes the entity text to be included, as shown in <a  href="JAXPDOM6.html#wp64867">Figure 6-11</a>: </p><a name="wp64865"> </a><p class="pBody"></p><div align="left"><img src="images/pd-410d10.gif" height="238" width="373" alt="Node with Entity Reference Selected" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64867"> </a><strong><font >Figure 6-11    Node with Entity Reference Selected</font></strong></p><a name="wp64868"> </a><p class="pBody">Finally, selecting a node that includes a <code class="cCode">CDATA</code> section produces results like those shown in <a  href="JAXPDOM6.html#wp64877">Figure 6-12</a>:</p><a name="wp64875"> </a><p class="pBody"></p><div align="left"><img src="images/pd-410e11.gif" height="288" width="447" alt="Node with CDATA Component Selected" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64877"> </a><strong><font >Figure 6-12    Node with <code class="cCode">CDATA</code> Component Selected</font></strong></p><a name="wp64879"> </a><h4 class="pHeading3">Extra Credit</h4><a name="wp64880"> </a><p class="pBody">Now that you have the application working, here are some ways you might think about extending it in the future:</p><a name="wp64881"> </a><p class="pDefinitionTerm">Use Title Text to Identify Slides</p><a name="wp64882"> </a><p class="pDefinition">Special case the <code class="cCode">slide</code> element so that the contents of the <code class="cCode">title</code> node is used as the identifying text. When selected, convert the title node's contents to a centered <code class="cCode">H1</code> tag, and ignore the <code class="cCode">title</code> element when constructing the tree.</p><a name="wp64883"> </a><p class="pDefinitionTerm">Convert Item Elements to Lists</p><a name="wp64884"> </a><p class="pDefinition">Remove <code class="cCode">item</code> elements from the <code class="cCode">JTree</code> and convert them to HTML lists using <code class="cCode">&lt;ul&gt;</code>, <code class="cCode">&lt;li&gt;</code>, <code class="cCode">&lt;/ul&gt;</code> tags, including them in the slide's content when the slide is selected.</p><a name="wp64886"> </a><h3 class="pHeading2">Handling Modifications</h3><a name="wp64887"> </a><p class="pBody">A full discussion of the mechanisms for modifying the <code class="cCode">JTree's</code> underlying data model is beyond the scope of this tutorial. However, a few words on the subject are in order.</p><a name="wp75094"> </a><p class="pBody">Most importantly, note that if you allow the user to modifying the structure by manipulating the <code class="cCode">JTree</code>, you have take the compression into account when you figure out where to apply the change. For example, if you are displaying text in the tree and the user modifies that, the changes would have to be applied to text subelements, and perhaps require a rearrangement of the XHTML subtree.</p><a name="wp75095"> </a><p class="pBody">When you make those changes, you'll need to understand more about the interactions between a <code class="cCode">JTree</code>, it's <code class="cCode">TreeModel</code>, and an underlying data model. That subject is covered in depth in the Swing Connection article, <em class="cEmphasis"><a  href="http://java.sun.com/products/jfc/tsc/articles/jtree/" target="_blank">Understanding the TreeModel</a></em> at <code class="cCode">http://java.sun.com/products/jfc/tsc/articles/jtree/index.html</code>.</p><a name="wp64891"> </a><h3 class="pHeading2">Finishing Up</h3><a name="wp64892"> </a><p class="pBody">You now understand pretty much what there is know about the structure of a DOM, and you know how to adapt a DOM to create a user-friendly display in a <code class="cCode">JTree</code>. It has taken quite a bit of coding, but in return you have obtained valuable tools for exposing a DOM's structure and a template for GUI apps. In the next section, you'll make a couple of minor modifications to the code that turn the application into a vehicle for experimentation, and then experiment with building and manipulating a DOM. </p>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPDOM5.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPDOM7.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -