1 /* 2 * Copyright (c) 2005, The JUNG Authors 3 * All rights reserved. 4 * 5 * This software is open-source under the BSD license; see either "license.txt" 6 * or https://github.com/jrtom/jung/blob/master/LICENSE 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 * 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 * Returns the default label renderer. 84 * 85 * @param vv the <code>VisualizationViewer</code> to render on 86 * @param value the value to assign to the label 87 * @return the default label renderer 88 */ 89 public Component getAnnotationRendererComponent(JComponent vv, Object value) { 90 91 super.setForeground(vv.getForeground()); 92 super.setBackground(vv.getBackground()); 93 94 setFont(vv.getFont()); 95 setIcon(null); 96 setBorder(noFocusBorder); 97 setValue(value); 98 return this; 99 } 100 101 /* 102 * The following methods are overridden as a performance measure to 103 * to prune code-paths are often called in the case of renders 104 * but which we know are unnecessary. Great care should be taken 105 * when writing your own renderer to weigh the benefits and 106 * drawbacks of overriding methods like these. 107 */ 108 109 /** 110 * Overridden for performance reasons. 111 * See the <a href="#override">Implementation Note</a> 112 * for more information. 113 */ 114 @Override 115 public boolean isOpaque() { 116 Color back = getBackground(); 117 Component p = getParent(); 118 if (p != null) { 119 p = p.getParent(); 120 } 121 boolean colorMatch = (back != null) && (p != null) && 122 back.equals(p.getBackground()) && 123 p.isOpaque(); 124 return !colorMatch && super.isOpaque(); 125 } 126 127 /** 128 * Overridden for performance reasons. 129 * See the <a href="#override">Implementation Note</a> 130 * for more information. 131 */ 132 @Override 133 public void validate() {} 134 135 /** 136 * Overridden for performance reasons. 137 * See the <a href="#override">Implementation Note</a> 138 * for more information. 139 */ 140 @Override 141 public void revalidate() {} 142 143 /** 144 * Overridden for performance reasons. 145 * See the <a href="#override">Implementation Note</a> 146 * for more information. 147 */ 148 @Override 149 public void repaint(long tm, int x, int y, int width, int height) {} 150 151 /** 152 * Overridden for performance reasons. 153 * See the <a href="#override">Implementation Note</a> 154 * for more information. 155 */ 156 @Override 157 public void repaint(Rectangle r) { } 158 159 /** 160 * Overridden for performance reasons. 161 * See the <a href="#override">Implementation Note</a> 162 * for more information. 163 */ 164 @Override 165 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 166 // Strings get interned... 167 if (propertyName=="text") { 168 super.firePropertyChange(propertyName, oldValue, newValue); 169 } 170 } 171 172 /** 173 * Overridden for performance reasons. 174 * See the <a href="#override">Implementation Note</a> 175 * for more information. 176 */ 177 @Override 178 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 179 180 /** 181 * Sets the <code>String</code> object for the cell being rendered to 182 * <code>value</code>. 183 * 184 * @param value the string value for this cell; if value is 185 * <code>null</code> it sets the text value to an empty string 186 * @see JLabel#setText 187 * 188 */ 189 protected void setValue(Object value) { 190 setText((value == null) ? "" : value.toString()); 191 } 192 }