1 19 20 package org.netbeans.modules.xml.text.navigator; 21 import java.awt.Component ; 22 import java.awt.Image ; 23 import javax.swing.Icon ; 24 import javax.swing.ImageIcon ; 25 import javax.swing.JLabel ; 26 import javax.swing.JTree ; 27 import javax.swing.tree.DefaultTreeCellRenderer ; 28 import javax.swing.tree.TreeCellRenderer ; 29 import org.netbeans.modules.editor.structure.api.DocumentElement; 30 import org.netbeans.modules.xml.text.structure.XMLDocumentModelProvider; 31 import org.openide.awt.HtmlRenderer; 32 import org.openide.util.Utilities; 33 34 35 40 public class NavigatorTreeCellRenderer extends DefaultTreeCellRenderer { 41 42 private static final String TAG_16 = "org/netbeans/modules/xml/text/navigator/resources/tag.png"; 43 private static final String PI_16 = "org/netbeans/modules/xml/text/navigator/resources/xml_declaration.png"; 44 private static final String DOCTYPE_16 = "org/netbeans/modules/xml/text/navigator/resources/doc_type.png"; 45 private static final String CDATA_16 = "org/netbeans/modules/xml/text/navigator/resources/cdata.png"; 46 47 private static final String ERROR_16 = "org/netbeans/modules/xml/text/navigator/resources/badge_error.png"; 48 49 private final Image ERROR_IMAGE = Utilities.loadImage(ERROR_16, true); 50 51 private final Icon [] TAG_ICON = new Icon []{getImageIcon(TAG_16, false), getImageIcon(TAG_16, true)}; 52 private final Icon [] PI_ICON = new Icon []{getImageIcon(PI_16, false), getImageIcon(PI_16, true)}; 53 private final Icon [] DOCTYPE_ICON = new Icon []{getImageIcon(DOCTYPE_16, false), getImageIcon(DOCTYPE_16, true)}; 54 private final Icon [] CDATA_ICON = new Icon []{getImageIcon(CDATA_16, false), getImageIcon(CDATA_16, true)}; 55 56 private HtmlRenderer.Renderer renderer; 57 58 public NavigatorTreeCellRenderer() { 59 super(); 60 renderer = HtmlRenderer.createRenderer(); 61 renderer.setHtml(true); 62 } 63 64 public Component getTreeCellRendererComponent(JTree tree, Object value, 65 boolean sel, boolean expanded, boolean leaf, int row, 66 boolean hasFocus) { 67 TreeNodeAdapter tna = (TreeNodeAdapter)value; 68 DocumentElement de = (DocumentElement)tna.getDocumentElement(); 69 70 String htmlText = tna.getText(true); 71 Component comp = renderer.getTreeCellRendererComponent(tree, htmlText, sel, expanded, leaf, row, hasFocus); 72 comp.setEnabled(tree.isEnabled()); 73 ((JLabel )comp).setToolTipText(tna.getToolTipText().trim().length() > 0 ? tna.getToolTipText() : null); 74 75 boolean containsError = tna.getChildrenErrorCount() > 0; 76 if(de.getType().equals(XMLDocumentModelProvider.XML_TAG) 78 || de.getType().equals(XMLDocumentModelProvider.XML_EMPTY_TAG)) { 79 setIcon(TAG_ICON, containsError); 80 } else if(de.getType().equals(XMLDocumentModelProvider.XML_PI)) { 81 setIcon(PI_ICON, containsError); 82 } else if(de.getType().equals(XMLDocumentModelProvider.XML_DOCTYPE)) { 83 setIcon(DOCTYPE_ICON, containsError); 84 } else if(de.getType().equals(XMLDocumentModelProvider.XML_CDATA)) { 85 setIcon(CDATA_ICON, containsError); 86 } 87 88 return comp; 89 } 90 91 public void setIcon(Icon [] icons, boolean containsError) { 92 renderer.setIcon(icons[containsError ? 1 : 0]); 93 } 94 95 private ImageIcon getImageIcon(String name, boolean error){ 96 ImageIcon icon = new ImageIcon (Utilities.loadImage(name)); 97 if(error) 98 return new ImageIcon (Utilities.mergeImages( icon.getImage(), ERROR_IMAGE, 15, 7 )); 99 else 100 return icon; 101 } 102 103 } 104 | Popular Tags |