View Javadoc

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