1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 import java.awt.Color; 35 import java.awt.Component; 36 import java.awt.FontMetrics; 37 import java.awt.Graphics; 38 import java.awt.SystemColor; 39 import javax.swing.Icon; 40 import javax.swing.ImageIcon; 41 import javax.swing.JLabel; 42 import javax.swing.JTree; 43 import javax.swing.tree.DefaultMutableTreeNode; 44 import javax.swing.tree.DefaultTreeCellRenderer; 45 46 47 48 public class UnoTreeRenderer extends DefaultTreeCellRenderer{ 49 private Icon m_oMethodIcon; 50 private Icon m_oPropertyIcon; 51 private Icon m_oContainerIcon; 52 private Icon m_oContentIcon; 53 private Icon m_oServiceIcon; 54 private Icon m_oInterfaceIcon; 55 private Icon m_oPropertyValueIcon; 56 private boolean bSelected; 57 private int nWidth = 0; 58 59 60 /** Creates a new instance of UnoTreeRenderer */ 61 public UnoTreeRenderer(){ 62 super(); 63 try { 64 65 final ClassLoader loader = ClassLoader.getSystemClassLoader(); 66 m_oMethodIcon = new ImageIcon(loader.getResource("images/methods_16.png")); 67 m_oPropertyIcon = new ImageIcon("images/properties_16.png"); 68 m_oPropertyValueIcon = new ImageIcon("images/properties_16.png"); 69 m_oContainerIcon = new ImageIcon("images/containers_16.png"); 70 m_oServiceIcon = new ImageIcon("images/services_16.png"); 71 m_oInterfaceIcon = new ImageIcon("images/interfaces_16.png"); 72 m_oContentIcon = new ImageIcon("images/content_16.png"); 73 } catch (RuntimeException e) { 74 System.out.println("Sorry, could not locate resourecs, treecell icons will not be displayed."); 75 } 76 } 77 78 79 public synchronized Component getTreeCellRendererComponent(JTree tree,Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){ 80 try{ 81 bSelected = sel; 82 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 83 Component rc = super.getTreeCellRendererComponent( tree, value, sel,expanded, leaf, row,hasFocus); 84 String sLabelText = (String)node.getUserObject(); 85 if (sLabelText != null){ 86 if (sLabelText.equals(XUnoFacetteNode.SCONTAINERDESCRIPTION)){ 87 // setIcon(m_oContainerIcon); 88 } else if (sLabelText.equals(XUnoFacetteNode.SCONTENTDESCRIPTION)){ 89 // setIcon(m_oContentIcon); 90 } else if (sLabelText.equals(XUnoFacetteNode.SINTERFACEDESCRIPTION)){ 91 // setIcon(m_oInterfaceIcon); 92 } else if (sLabelText.equals(XUnoFacetteNode.SMETHODDESCRIPTION)){ 93 // setIcon(m_oMethodIcon); 94 } else if (sLabelText.equals(XUnoFacetteNode.SPROPERTYDESCRIPTION)){ 95 // setIcon(m_oPropertyIcon); 96 } else if (sLabelText.startsWith(XUnoFacetteNode.SPROPERTYINFODESCRIPTION)){ 97 // setIcon(m_oPropertyIcon); 98 } else if (sLabelText.equals(XUnoFacetteNode.SPROPERTYVALUEDESCRIPTION)){ 99 // setIcon(m_oPropertyValueIcon); 100 } else if (sLabelText.equals(XUnoFacetteNode.SSERVICEDESCRIPTION)){ 101 // setIcon(m_oServiceIcon); 102 } else{ 103 setText(sLabelText); 104 rc.validate(); 105 } 106 setSize(getPreferredSize()); //fm.stringWidth(sLabelText), (int) getSize().getHeight()); 107 rc.validate(); 108 // nWidth = (int) rc.getPreferredSize().getWidth(); 109 doLayout(); 110 } 111 } catch (RuntimeException e) { 112 System.out.println("Sorry, icon for treecell could not be displayed."); 113 } 114 return this; 115 } 116 117 118 119 public void paintComponent(Graphics g) { 120 FontMetrics fm = getFontMetrics(getFont()); 121 int x, y; 122 y = fm.getAscent() + 2; 123 if(getIcon() == null) { 124 x = 0; 125 } else { 126 x = getIcon().getIconWidth() + getIconTextGap(); 127 } 128 g.setColor(getForeground()); 129 // g.fillRect(x,y,x + fm.stringWidth(getText()),y); 130 // System.out.println("Text: " + getText()); 131 super.paintComponent(g); 132 } 133 } 134 135 136