in taverna-renderers-impl/src/main/java/org/apache/taverna/renderers/impl/XMLTree.java [127:252]
private void init(Content content) {
rootElement = (Element) content;
/*
* Fix for platforms other than metal which can't otherwise cope with
* arbitrary size rows
*/
setRowHeight(0);
getSelectionModel().setSelectionMode(SINGLE_TREE_SELECTION);
setShowsRootHandles(true);
setEditable(false);
setModel(new DefaultTreeModel(createTreeNode(content)));
setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Color getBackgroundNonSelectionColor() {
return null;
}
@Override
public Color getBackground() {
return null;
}
@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean sel, boolean expanded, boolean leaf,
int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
setOpaque(false);
if (value instanceof XMLNode) {
XMLNode node = (XMLNode) value;
if (node.getUserObject() instanceof Element)
renderElementNode((Element) node.getUserObject());
else if (node.getUserObject() instanceof Text)
renderTextNode((Text) node.getUserObject());
// TODO what about other node types?
}
setBackground(new Color(0, 0, 0, 0));
return this;
}
private void renderElementNode(Element element) {
// setIcon(TavernaIcons.xmlNodeIcon);
StringBuilder nameBuffer = new StringBuilder("<html>")
.append(element.getQualifiedName());
/*
* Bit of a quick and dirty hack here to try to ensure that the
* element namespace is shown. There appears no way to get the
* actual xmlns declarations that are part of an element through
* jdom. Also, please note, there's no namespace handling at all
* for attributes...
*/
if (element.getParent() instanceof Element) {
Element parent = (Element) element.getParent();
if (parent.getNamespace(element.getNamespacePrefix()) == null)
nameBuffer
.append(" <font color=\"purple\">xmlns:")
.append(element.getNamespacePrefix())
.append("</font>=\"<font color=\"green\">")
.append(element.getNamespaceURI() + "</font>\"");
} else
nameBuffer.append(" <font color=\"purple\">xmlns:")
.append(element.getNamespacePrefix())
.append("</font>=\"<font color=\"green\">")
.append(element.getNamespaceURI() + "</font>\"");
String sep = "";
for (Object a : element.getAttributes()) {
Attribute attribute = (Attribute) a;
String name = attribute.getName().trim();
String value = attribute.getValue().trim();
if (value != null && value.length() > 0) {
// TODO xml-quote name and value
nameBuffer.append(sep)
.append(" <font color=\"purple\">")
.append(name)
.append("</font>=\"<font color=\"green\">")
.append(value).append("</font>\"");
sep = ",";
}
}
nameBuffer.append("</html>");
setText(nameBuffer.toString());
}
private void renderTextNode(Text text) {
// setIcon(TavernaIcons.leafIcon);
String name = text.getText();
if (textSizeLimit > -1 && name.length() > textSizeLimit)
name = name.substring(0, textSizeLimit) + "...";
setText("<html><pre><font color=\"blue\">"
+ name.replaceAll("<br>", "\n").replaceAll("<", "<")
+ "</font></pre></html>");
}
});
setAllNodesExpanded();
// Add a listener to present the 'save as text' option
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
doEvent(e);
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
doEvent(e);
}
public void doEvent(MouseEvent e) {
JPopupMenu menu = new JPopupMenu();
JMenuItem item = new JMenuItem("Save as XML text");
menu.add(item);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
saveTreeXML();
}
});
menu.show(XMLTree.this, e.getX(), e.getY());
}
});
}