001: /*
002: * XMLDocumentTreeCellRenderer.java
003: *
004: * Created on May 15, 2007, 12:41:05 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package it.businesslogic.ireport.data.xml;
011:
012: import java.awt.Component;
013: import java.awt.Font;
014: import javax.swing.ImageIcon;
015: import javax.swing.JTree;
016: import javax.swing.tree.DefaultMutableTreeNode;
017: import javax.swing.tree.DefaultTreeCellRenderer;
018: import org.w3c.dom.Node;
019:
020: /**
021: *
022: * @author gtoffoli
023: */
024: public class XMLDocumentTreeCellRenderer extends
025: DefaultTreeCellRenderer {
026:
027: static ImageIcon tagIcon;
028: static ImageIcon attributeIcon;
029: static ImageIcon errorIcon;
030:
031: XMLFieldMappingEditor mappingEditor = null;
032:
033: public XMLFieldMappingEditor getMappingEditor() {
034: return mappingEditor;
035: }
036:
037: public void setMappingEditor(XMLFieldMappingEditor mappingEditor) {
038: this .mappingEditor = mappingEditor;
039: }
040:
041: public XMLDocumentTreeCellRenderer(
042: XMLFieldMappingEditor mappingEditor) {
043: super ();
044:
045: this .mappingEditor = mappingEditor;
046: if (tagIcon == null)
047: tagIcon = new javax.swing.ImageIcon(getClass().getResource(
048: "/it/businesslogic/ireport/icons/xml/tag.png"));
049: if (attributeIcon == null)
050: attributeIcon = new javax.swing.ImageIcon(
051: getClass()
052: .getResource(
053: "/it/businesslogic/ireport/icons/xml/attribute.png"));
054: if (errorIcon == null)
055: errorIcon = new javax.swing.ImageIcon(
056: getClass()
057: .getResource(
058: "/it/businesslogic/ireport/icons/problems/error.png"));
059:
060: }
061:
062: public Component getTreeCellRendererComponent(JTree tree,
063: Object value, boolean sel, boolean expanded, boolean leaf,
064: int row, boolean hasFocus) {
065:
066: super .getTreeCellRendererComponent(tree, value, sel, expanded,
067: leaf, row, hasFocus);
068:
069: try {
070: if (value != null
071: && value instanceof DefaultMutableTreeNode) {
072: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) value;
073: if (dmtn.getUserObject() != null
074: && dmtn.getUserObject() instanceof Node) {
075: Node node = (Node) dmtn.getUserObject();
076: String s = node.getNodeName();
077: if (node.getNodeValue() != null) {
078: s += " (" + node.getNodeValue() + ")";
079: }
080:
081: if (node.getNodeType() == Node.ELEMENT_NODE) {
082: setIcon(tagIcon);
083: }
084: if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
085: setIcon(attributeIcon);
086: }
087:
088: boolean needBold = false;
089: if (getMappingEditor() != null
090: && getMappingEditor().getRecordNodes()
091: .contains(node)) {
092: needBold = true;
093: }
094:
095: java.awt.Font f = getFont();
096:
097: if (f.isBold() && !needBold) {
098: setFont(f.deriveFont(Font.PLAIN));
099: } else if (!f.isBold() && needBold) {
100: setFont(f.deriveFont(Font.BOLD));
101: }
102:
103: setText(s);
104: } else {
105: setIcon(errorIcon);
106: }
107: }
108: } catch (Exception ex) {
109: //ex.printStackTrace();
110: }
111:
112: return this;
113: }
114:
115: }
|