[ Pobierz całość w formacie PDF ]
import javax.swing.event.*;
import java.util.*;
public class DomEcho04 extends JPanel
{
// Global value so it can be ref'd by the tree-adapter
static Document document;
boolean compress = true;
static final int windowHeight = 460;
static final int leftWidth = 300;
static final int rightWidth = 340;
static final int windowWidth = leftWidth + rightWidth;
public DomEcho04()
{
// Make a nice border
EmptyBorder eb = new EmptyBorder(5,5,5,5);
BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
CompoundBorder cb = new CompoundBorder(eb,bb);
this.setBorder(new CompoundBorder(cb,eb));
// Set up the tree
JTree tree = new JTree(new DomToTreeModelAdapter());
// Iterate over the tree and make nodes visible
// (Otherwise, the tree shows up fully collapsed)
//TreePath nodePath = ???;
// tree.expandPath(nodePath);
// Build left-side view
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(
new Dimension( leftWidth, windowHeight ));
// Build right-side view
// (must be final to be referenced in inner class)
final
JEditorPane htmlPane = new JEditorPane("text/html","");
htmlPane.setEditable(false);
JScrollPane htmlView = new JScrollPane(htmlPane);
htmlView.setPreferredSize(
new Dimension( rightWidth, windowHeight ));
// Wire the two views together. Use a selection listener
// created with an anonymous inner-class adapter.
tree.addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath p = e.getNewLeadSelectionPath();
if (p != null) {
AdapterNode adpNode =
(AdapterNode) p.getLastPathComponent();
htmlPane.setText(adpNode.content());
}
}
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java (2 of 9) [8/22/2001 12:54:18 PM]
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java
}
);
// Build split-pane view
JSplitPane splitPane =
new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
treeView,
htmlView );
splitPane.setContinuousLayout( true );
splitPane.setDividerLocation( leftWidth );
splitPane.setPreferredSize(
new Dimension( windowWidth + 10, windowHeight+10 ));
// Add GUI components
this.setLayout(new BorderLayout());
this.add("Center", splitPane );
} // constructor
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java DomEcho filename");
System.exit(1);
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse( new File(argv[0]) );
makeFrame();
} catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
} // main
public static void makeFrame() {
// Set up a GUI framework
JFrame frame = new JFrame("DOM Echo");
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
}
);
// Set up the tree, the views, and display it all
final DomEcho04 echoPanel =
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java (3 of 9) [8/22/2001 12:54:18 PM]
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java
new DomEcho04();
frame.getContentPane().add("Center", echoPanel );
frame.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int w = windowWidth + 10;
int h = windowHeight + 10;
frame.setLocation(screenSize.width/3 - w/2,
screenSize.height/2 - h/2);
frame.setSize(w, h);
frame.setVisible(true);
} // makeFrame
// An array of names for DOM node-types
// (Array indexes = nodeType() values.)
static final String[] typeName = {
"none",
"Element",
"Attr",
"Text",
"CDATA",
"EntityRef",
"Entity",
"ProcInstr",
"Comment",
"Document",
"DocType",
"DocFragment",
"Notation",
};
static final int ELEMENT_TYPE = 1;
static final int ATTR_TYPE = 2;
static final int TEXT_TYPE = 3;
static final int CDATA_TYPE = 4;
static final int ENTITYREF_TYPE = 5;
static final int ENTITY_TYPE = 6;
static final int PROCINSTR_TYPE = 7;
static final int COMMENT_TYPE = 8;
static final int DOCUMENT_TYPE = 9;
static final int DOCTYPE_TYPE = 10;
static final int DOCFRAG_TYPE = 11;
static final int NOTATION_TYPE = 12;
// The list of elements to display in the tree
// Could set this with a command-line argument, but
// not much point -- the list of tree elements still
// has to be defined internally.
// Extra credit: Read the list from a file
// Super-extra credit: Process a DTD and build the list.
static String[] treeElementNames = {
"slideshow",
"slide",
"title", // For slideshow #1
"slide-title", // For slideshow #10
"item",
};
boolean treeElement(String elementName) {
for (int i=0; i
if ( elementName.equals(treeElementNames[i]) ) return true;
}
return false;
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java (4 of 9) [8/22/2001 12:54:18 PM]
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/work/DomEcho04.java
}
// This class wraps a DOM node and returns the text we want to
// display in the tree. It also returns children, index values,
// and child counts.
public class AdapterNode
{
org.w3c.dom.Node domNode;
// Construct an Adapter node from a DOM node
public AdapterNode(org.w3c.dom.Node node) {
domNode = node;
}
// Return a string that identifies this node in the tree
// *** Refer to table at top of org.w3c.dom.Node ***
public String toString() {
String s = typeName[domNode.getNodeType()];
String nodeName = domNode.getNodeName();
[ Pobierz całość w formacie PDF ]