www.pudn.com > DomView.zip > DomTreeCellRenderer.java


/******************************************************************** 
Domview 1.0 displays contents of an XML DOM object 
Copyright (C) 1999  The Bean Factory, LLC. 
 
This program is free software; you can redistribute it and/or 
modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation; either version 2 
of the License, or (at your option) any later version. 
 
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
GNU General Public License for more details. 
 
You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 
Author: Sun Koh, The Bean Factory, LLC. 
*******************************************************************/ 
import java.awt.*;                  //AWT classes 
import java.awt.event.*;            //AWT event classes 
import java.util.*;                 //Vectors, etc 
import java.io.*;                   //Serializable, etc 
import java.net.*;                  //Network classes  
import javax.swing.*;               //Swing classes 
import javax.swing.event.*;         //Swing events 
import javax.swing.table.*;         //JTable models 
import javax.swing.tree.*;          //JTree models 
import javax.swing.border.*;        //JComponent Borders 
 
 
import org.w3c.dom.*; 
 
/******************************************************************** 
 DomTreeCellRenderer nodes in appropriate format. 
 Element node is displayed as a branach and Text and CDATASection  
 node are displayed as a branch as a black font color. 
  
 @version      : 1.0 
 @author       : Sun Koh 
 
********************************************************************/ 
public class DomTreeCellRenderer 
extends java.lang.Object 
implements TreeCellRenderer { 
// 
// Data Members 
// 
 
 
// 
// Methods 
// 
/** 
 Default Constructor  
 
 @param 
 */ 
    public DomTreeCellRenderer(){ 
 
    } 
    /** 
     * Sets the value of the current tree cell to value. 
     * If selected is true, the cell will be drawn as if 
     * selected. If expanded is true the node is currently 
     * expanded and if leaf is true the node represets a 
     * leaf anf if hasFocus is true the node currently has 
     * focus. tree is the JTree the receiver is being 
     * configured for. 
     * Returns the Component that the renderer uses to draw the value. 
     * 
     * @return	Component that the renderer uses to draw the value. 
     */ 
 
    public Component getTreeCellRendererComponent(JTree tree,  
                                                  Object value,  
                                                  boolean selected,  
                                                  boolean expanded,  
                                                  boolean leaf,  
                                                  int row,  
                                                  boolean hasFocus) { 
 
 
        //create JLabel  
        JLabel l = new JLabel(); 
        l.setOpaque(true); 
 
        //find and see if the node is element node 
        if ( !leaf ) { 
            try { 
                if ( value.getClass() != DefaultMutableTreeNode.class ) { 
 
                    if ( selected ) { 
                        l.setBackground(new Color(195, 195, 250) ); 
                        l.setForeground(Color.black); 
                        l.setText( ((Node)value).getNodeName() ); 
                    } 
                    else { 
                        l.setBackground(Color.white); 
                        l.setText( ((Node)value).getNodeName()); 
                    } 
                } 
            } 
            catch ( Exception e ) { 
                System.out.println(":: "+e+"\n"+value); 
            } 
 
        } 
        else { 
            if ( selected ) { 
                l.setBackground(new Color(195, 195, 250) ); 
                l.setForeground(Color.black); 
                l.setText( ((Node)value).getNodeValue() ); 
            } 
            else { 
                l.setBackground(Color.white); 
                l.setForeground(Color.black); 
                l.setText( ((Node)value).getNodeValue() ); 
            } 
        } 
 
 
        return l; 
    } 
 
 
 
}//end of DomTreeCellRenderer class