1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 import java.awt.Color; 24 import java.awt.Component; 25 import java.awt.FontMetrics; 26 import java.awt.Graphics; 27 import java.awt.SystemColor; 28 import javax.swing.Icon; 29 import javax.swing.ImageIcon; 30 import javax.swing.JLabel; 31 import javax.swing.JTree; 32 import javax.swing.tree.DefaultMutableTreeNode; 33 import javax.swing.tree.DefaultTreeCellRenderer; 34 35 36 37 public class UnoTreeRenderer extends DefaultTreeCellRenderer{ 38 private Icon m_oMethodIcon; 39 private Icon m_oPropertyIcon; 40 private Icon m_oContainerIcon; 41 private Icon m_oContentIcon; 42 private Icon m_oServiceIcon; 43 private Icon m_oInterfaceIcon; 44 private Icon m_oPropertyValueIcon; 45 private boolean bSelected; 46 private int nWidth = 0; 47 48 49 /** Creates a new instance of UnoTreeRenderer */ 50 public UnoTreeRenderer(){ 51 super(); 52 try { 53 54 final ClassLoader loader = ClassLoader.getSystemClassLoader(); 55 m_oMethodIcon = new ImageIcon(loader.getResource("images/methods_16.png")); 56 m_oPropertyIcon = new ImageIcon("images/properties_16.png"); 57 m_oPropertyValueIcon = new ImageIcon("images/properties_16.png"); 58 m_oContainerIcon = new ImageIcon("images/containers_16.png"); 59 m_oServiceIcon = new ImageIcon("images/services_16.png"); 60 m_oInterfaceIcon = new ImageIcon("images/interfaces_16.png"); 61 m_oContentIcon = new ImageIcon("images/content_16.png"); 62 } catch (RuntimeException e) { 63 System.out.println("Sorry, could not locate resourecs, treecell icons will not be displayed."); 64 } 65 } 66 67 68 public synchronized Component getTreeCellRendererComponent(JTree tree,Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){ 69 try{ 70 bSelected = sel; 71 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 72 Component rc = super.getTreeCellRendererComponent( tree, value, sel,expanded, leaf, row,hasFocus); 73 String sLabelText = (String)node.getUserObject(); 74 if (sLabelText != null){ 75 if (sLabelText.equals(XUnoFacetteNode.SCONTAINERDESCRIPTION)){ 76 // setIcon(m_oContainerIcon); 77 } else if (sLabelText.equals(XUnoFacetteNode.SCONTENTDESCRIPTION)){ 78 // setIcon(m_oContentIcon); 79 } else if (sLabelText.equals(XUnoFacetteNode.SINTERFACEDESCRIPTION)){ 80 // setIcon(m_oInterfaceIcon); 81 } else if (sLabelText.equals(XUnoFacetteNode.SMETHODDESCRIPTION)){ 82 // setIcon(m_oMethodIcon); 83 } else if (sLabelText.equals(XUnoFacetteNode.SPROPERTYDESCRIPTION)){ 84 // setIcon(m_oPropertyIcon); 85 } else if (sLabelText.startsWith(XUnoFacetteNode.SPROPERTYINFODESCRIPTION)){ 86 // setIcon(m_oPropertyIcon); 87 } else if (sLabelText.equals(XUnoFacetteNode.SPROPERTYVALUEDESCRIPTION)){ 88 // setIcon(m_oPropertyValueIcon); 89 } else if (sLabelText.equals(XUnoFacetteNode.SSERVICEDESCRIPTION)){ 90 // setIcon(m_oServiceIcon); 91 } else{ 92 setText(sLabelText); 93 rc.validate(); 94 } 95 setSize(getPreferredSize()); //fm.stringWidth(sLabelText), (int) getSize().getHeight()); 96 rc.validate(); 97 // nWidth = (int) rc.getPreferredSize().getWidth(); 98 doLayout(); 99 } 100 } catch (RuntimeException e) { 101 System.out.println("Sorry, icon for treecell could not be displayed."); 102 } 103 return this; 104 } 105 106 107 108 public void paintComponent(Graphics g) { 109 FontMetrics fm = getFontMetrics(getFont()); 110 int x, y; 111 y = fm.getAscent() + 2; 112 if(getIcon() == null) { 113 x = 0; 114 } else { 115 x = getIcon().getIconWidth() + getIconTextGap(); 116 } 117 g.setColor(getForeground()); 118 // g.fillRect(x,y,x + fm.stringWidth(getText()),y); 119 // System.out.println("Text: " + getText()); 120 super.paintComponent(g); 121 } 122 } 123 124 125