KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > navigator > NavigatorTreeCellRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.text.navigator;
21 import java.awt.Component JavaDoc;
22 import java.awt.Image JavaDoc;
23 import javax.swing.Icon JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
28 import javax.swing.tree.TreeCellRenderer JavaDoc;
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 /** TreeCellRenderer implementatin for the XML Navigator.
36  *
37  * @author Marek Fukala
38  * @version 1.0
39  */

40 public class NavigatorTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
41     
42     private static final String JavaDoc TAG_16 = "org/netbeans/modules/xml/text/navigator/resources/tag.png";
43     private static final String JavaDoc PI_16 = "org/netbeans/modules/xml/text/navigator/resources/xml_declaration.png";
44     private static final String JavaDoc DOCTYPE_16 = "org/netbeans/modules/xml/text/navigator/resources/doc_type.png";
45     private static final String JavaDoc CDATA_16 = "org/netbeans/modules/xml/text/navigator/resources/cdata.png";
46     
47     private static final String JavaDoc ERROR_16 = "org/netbeans/modules/xml/text/navigator/resources/badge_error.png";
48     
49     private final Image JavaDoc ERROR_IMAGE = Utilities.loadImage(ERROR_16, true);
50    
51     private final Icon JavaDoc[] TAG_ICON = new Icon JavaDoc[]{getImageIcon(TAG_16, false), getImageIcon(TAG_16, true)};
52     private final Icon JavaDoc[] PI_ICON = new Icon JavaDoc[]{getImageIcon(PI_16, false), getImageIcon(PI_16, true)};
53     private final Icon JavaDoc[] DOCTYPE_ICON = new Icon JavaDoc[]{getImageIcon(DOCTYPE_16, false), getImageIcon(DOCTYPE_16, true)};
54     private final Icon JavaDoc[] CDATA_ICON = new Icon JavaDoc[]{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 JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc 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 JavaDoc htmlText = tna.getText(true);
71         Component JavaDoc comp = renderer.getTreeCellRendererComponent(tree, htmlText, sel, expanded, leaf, row, hasFocus);
72         comp.setEnabled(tree.isEnabled());
73         ((JLabel JavaDoc)comp).setToolTipText(tna.getToolTipText().trim().length() > 0 ? tna.getToolTipText() : null);
74         
75         boolean containsError = tna.getChildrenErrorCount() > 0;
76         //normal icons
77
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 JavaDoc[] icons, boolean containsError) {
92         renderer.setIcon(icons[containsError ? 1 : 0]);
93     }
94     
95     private ImageIcon JavaDoc getImageIcon(String JavaDoc name, boolean error){
96         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(Utilities.loadImage(name));
97         if(error)
98             return new ImageIcon JavaDoc(Utilities.mergeImages( icon.getImage(), ERROR_IMAGE, 15, 7 ));
99         else
100             return icon;
101     }
102     
103 }
104
Popular Tags