1 /* 2 * Copyright (c) 2005, the JUNG Project and the Regents of the University of 3 * California All rights reserved. 4 * 5 * This software is open-source under the BSD license; see either "license.txt" 6 * or http://jung.sourceforge.net/license.txt for a description. 7 * 8 * 9 */ 10 11 package edu.uci.ics.jung.visualization.annotations; 12 13 import java.awt.Color; 14 import java.awt.Component; 15 import java.awt.Rectangle; 16 import java.io.Serializable; 17 18 import javax.swing.JComponent; 19 import javax.swing.JLabel; 20 import javax.swing.border.Border; 21 import javax.swing.border.EmptyBorder; 22 23 /** 24 * AnnotationRenderer is similar to the cell renderers 25 * used by the JTable and JTree jfc classes. 26 * 27 * @author Tom Nelson 28 * 29 * 30 */ 31 @SuppressWarnings("serial") 32 public class AnnotationRenderer extends JLabel implements 33 Serializable { 34 35 protected static Border noFocusBorder = new EmptyBorder(0,0,0,0); 36 37 /** 38 * Creates a default table cell renderer. 39 */ 40 public AnnotationRenderer() { 41 setOpaque(true); 42 setBorder(noFocusBorder); 43 } 44 45 /** 46 * Overrides <code>JComponent.setForeground</code> to assign 47 * the unselected-foreground color to the specified color. 48 * 49 * @param c set the foreground color to this value 50 */ 51 @Override 52 public void setForeground(Color c) { 53 super.setForeground(c); 54 } 55 56 /** 57 * Overrides <code>JComponent.setBackground</code> to assign 58 * the unselected-background color to the specified color. 59 * 60 * @param c set the background color to this value 61 */ 62 @Override 63 public void setBackground(Color c) { 64 super.setBackground(c); 65 } 66 67 /** 68 * Notification from the <code>UIManager</code> that the look and feel 69 * [L&F] has changed. 70 * Replaces the current UI object with the latest version from the 71 * <code>UIManager</code>. 72 * 73 * @see JComponent#updateUI 74 */ 75 @Override 76 public void updateUI() { 77 super.updateUI(); 78 setForeground(null); 79 setBackground(null); 80 } 81 82 /** 83 * 84 * Returns the default label renderer for an Edge 85 * 86 * @param vv the <code>VisualizationViewer</code> to render on 87 * @param value the value to assign to the label for 88 * <code>Edge</code> 89 * @param edge the <code>Edge</code> 90 * @return the default label renderer 91 */ 92 public Component getAnnotationRendererComponent(JComponent vv, Object value) { 93 94 super.setForeground(vv.getForeground()); 95 super.setBackground(vv.getBackground()); 96 97 // if(font != null) { 98 // setFont(font); 99 // } else { 100 setFont(vv.getFont()); 101 // } 102 setIcon(null); 103 setBorder(noFocusBorder); 104 setValue(value); 105 return this; 106 } 107 108 /* 109 * The following methods are overridden as a performance measure to 110 * to prune code-paths are often called in the case of renders 111 * but which we know are unnecessary. Great care should be taken 112 * when writing your own renderer to weigh the benefits and 113 * drawbacks of overriding methods like these. 114 */ 115 116 /** 117 * Overridden for performance reasons. 118 * See the <a href="#override">Implementation Note</a> 119 * for more information. 120 */ 121 @Override 122 public boolean isOpaque() { 123 Color back = getBackground(); 124 Component p = getParent(); 125 if (p != null) { 126 p = p.getParent(); 127 } 128 boolean colorMatch = (back != null) && (p != null) && 129 back.equals(p.getBackground()) && 130 p.isOpaque(); 131 return !colorMatch && super.isOpaque(); 132 } 133 134 /** 135 * Overridden for performance reasons. 136 * See the <a href="#override">Implementation Note</a> 137 * for more information. 138 */ 139 @Override 140 public void validate() {} 141 142 /** 143 * Overridden for performance reasons. 144 * See the <a href="#override">Implementation Note</a> 145 * for more information. 146 */ 147 @Override 148 public void revalidate() {} 149 150 /** 151 * Overridden for performance reasons. 152 * See the <a href="#override">Implementation Note</a> 153 * for more information. 154 */ 155 @Override 156 public void repaint(long tm, int x, int y, int width, int height) {} 157 158 /** 159 * Overridden for performance reasons. 160 * See the <a href="#override">Implementation Note</a> 161 * for more information. 162 */ 163 @Override 164 public void repaint(Rectangle r) { } 165 166 /** 167 * Overridden for performance reasons. 168 * See the <a href="#override">Implementation Note</a> 169 * for more information. 170 */ 171 @Override 172 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 173 // Strings get interned... 174 if (propertyName=="text") { 175 super.firePropertyChange(propertyName, oldValue, newValue); 176 } 177 } 178 179 /** 180 * Overridden for performance reasons. 181 * See the <a href="#override">Implementation Note</a> 182 * for more information. 183 */ 184 @Override 185 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 186 187 /** 188 * Sets the <code>String</code> object for the cell being rendered to 189 * <code>value</code>. 190 * 191 * @param value the string value for this cell; if value is 192 * <code>null</code> it sets the text value to an empty string 193 * @see JLabel#setText 194 * 195 */ 196 protected void setValue(Object value) { 197 setText((value == null) ? "" : value.toString()); 198 } 199 }