TextualDisplay is a utility for displaying data as styled text. Most commonly, it can be used to display scientific data as HTML tables.
Requirements
Developer's Guide
Abstract
This guide describes in detail how to use TextualDisplay in a Swing application. In general, you serialize your object to XML, transform XML to HTML via an XSLT style sheet, and display HTML in a Swing widget.
Overview
We recommend the following steps:
- #Check out TextualDisplay from CVS.
- #Build TextualDisplay and run an example application.
- #Serialize your exemplary object to XML.
- #Write an XSLT style sheet that transforms your XML to HTML.
- Display your HTML in a web browser.
- Display your object in the TextDispViewer that comes with TextualDisplay.
- Integrate TextualDisplay into your application.
Check out TextualDisplay from CVS
Make sure you have access to LCLS CVS!
Check out safely from the CVS head:
bash-3.00$ cvs co physics/textdisp bash-3.00$ cd physics/textdisp
Build TextualDisplay and run an example application
Make sure you have ANT version 1.6 (or newer)!
In the top directory of TextualDisplay, type
ant
if BUILD is successful, run the example application:
java -cp textdisp.jar:lib/core-renderer-R7final.jar:lib/xstream-1.2.2.jar edu.stanford.slac.util.textdisp.example.ExampleApplication
Some tips on how to use the example application:
- The main window consists of two panels, separated by a drag-able bar (if you see only one panel, just drag the bar).
- The left panel contains the serialized object (XML), the right panel the styled data from the same object (HTML). All tags in the XML file are self-explanatory.
- The command line output of the application contains some useful info, e.g. when you resize the main window.
If you want to look at the source code of the example, check out these classes:
- edu.stanford.slac.util.textdisp.example.ExampleApplication
(the main method contains a good example of how to use TextualDisplay) - edu.stanford.slac.util.textdisp.example.ExampleData
(an instance of this class is serialized to XML that is displayed in the left panel of the main application window)
Serialize your exemplary object to XML
Add this code to your test class:
Object myData = ...; ObjectSerializer objSerializer = new ObjectSerializer(); String xml = objSerializer.toXML(myData); System.out.println(xml);
You can tweak the XML output via the XStream API, which you get from ObjectSerializer:
XStream xstream = objSerializer.getXStream();
Some of the most useful methods are:
xstream.alias(...); xstream.omitField(...);
More info:
When you are pleased with your exemplary XML, proceed to the next step.
Write an XSLT style sheet that transforms your XML to HTML
This step is the most specialized of all. Basically, I can only recommend to look at:
- The example file edu/stanford/slac/util/textdisp/example/example.xsl
- The tutorial at http://w3schools.com/xsl/default.asp
- A good article on embedding CSS into XSLT at http://www.ibm.com/developerworks/xml/library/x-xslt5.html
Of course, you can always ask an XSLT expert (i.e. me) to write you a style sheet...
Once you have your style sheet, copy in to the directory of your test class and add this code to your test class:
String xml = ...; URL url = TestClass.class.getResource("myxslt.xsl"); TextDispModel textDispModel = null; try { textDispModel = TextDispModel.newDefault(xml, url); } catch (IOException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } TextDispViewer textDispViewer = new TextDispViewer(); textDispViewer.setModel(textDispModel);
Tweak your XSLT, until you're satisfied with your HTML.